Android Development 19

Android DevelopmentWelcome to part 19 of my Android Development tutorial. You’ll need to have seen part 18 to understand this tutorial.

In this part of the tutorial I will be answering a bunch of common questions I receive. I’ll be covering how to have your app automatically change into another language ( Localization ). I’ll also show how to catch multiple events. We’ll also go over how to create a layout specific to when the app is in landscape mode. All of the code follows the video below.

If you like videos like this it helps to tell Google+ with a click here

Code From the Video

Contact.java

package com.newthinktank.censusapp;

import android.util.Log;

public class Contact {
	
	private String name;
	private String phoneNumber;
	private String streetAddress;
	private String city;
	
	public Contact(){
		
		
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		
		Log.e("CENSUS", "NAME CHANGED TO " + name);
		
		this.name = name;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		
		Log.e("CENSUS", "PHONE CHANGED TO " + phoneNumber);
		
		this.phoneNumber = phoneNumber;
	}


	public String getStreetAddress() {
		return streetAddress;
	}

	public void setStreetAddress(String streetAddress) {
		
		Log.e("CENSUS", "STREET CHANGED TO " + streetAddress);
		
		this.streetAddress = streetAddress;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		
		Log.e("CENSUS", "CITY CHANGED TO " + city);
		
		this.city = city;
	}

	
}

ContactFragment.java

package com.newthinktank.censusapp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class ContactFragment extends Fragment {

	private Contact contact;
	private EditText contactNameEditText;

	// NEW

	private EditText contactStreetEditText;
	private EditText contactCityEditText;
	private EditText contactPhoneEditText;

	// END OF NEW

	// Generate this with Right Click - Source - Override/Implement methods
	// This method is called when the Fragment is called for.
	// We initialize everything here.

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		contact = new Contact();
	}

	// Used to inflate the Fragment, or show it on the screen

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		// Pass in the layout to inflate, the views parent and whether
		// to add the inflated view to the parent.
		// We mark this false because the Activity will add the view.

		View theView = inflater.inflate(R.layout.fragment_contact, container, false);



		// Get a reference to the EditText

		contactNameEditText = (EditText) theView.findViewById(R.id.contactNameEditText);

		// If text in the EditText box is edited it will change the
		// name.

		// NEW
		
		contactStreetEditText = (EditText) theView.findViewById(R.id.contactStreetEditText);
		contactCityEditText = (EditText) theView.findViewById(R.id.contactCityEditText);
		contactPhoneEditText = (EditText) theView.findViewById(R.id.contactPhoneEditText);
		
		// All the EditText components will use just one TextWatcher
		// which auto updates Contact.java

		TextWatcher editTextWatcher = new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {

				if (contactNameEditText.hasFocus() == true){

					contact.setName(arg0.toString());

				} else if (contactStreetEditText.hasFocus() == true){
					
					contact.setStreetAddress(arg0.toString());
					
				} else if (contactCityEditText.hasFocus() == true){
					
					contact.setCity(arg0.toString());
					
				} else if (contactPhoneEditText.hasFocus() == true){
					
					contact.setPhoneNumber(arg0.toString());
					
				}

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start,
					int count, int after) {
				// TODO Auto-generated method stub

			}
		};

		
		contactStreetEditText.addTextChangedListener(editTextWatcher);
		contactCityEditText.addTextChangedListener(editTextWatcher);
		contactPhoneEditText.addTextChangedListener(editTextWatcher);

		// END OF NEW

		contactNameEditText.addTextChangedListener(editTextWatcher);


		// Pass in the layout to inflate, the views parent and whether
		// to add the inflated view to the parent.
		// We mark this false because the Activity will add the view.

		return theView;

	}

}

dimens.xml

<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="app_outer_margin">16dp</dimen>
    <dimen name="component_bottom_margin">8dp</dimen>

</resources>

fragment_contact.xml (Landscape)

<?xml version="1.0" encoding="utf-8"?>

<!-- Sometimes what looks good in a vertical format
looks bad in landscape. To make landscape layout
create a new folder in res called layout-land.
Then copy the xml layout in that folder and make 
the changes you want. -->

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/app_outer_margin" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    
    <EditText
        android:id="@+id/contactNameEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/component_bottom_margin"
        android:ems="10"
        android:hint="@string/contact_name_hint"
        android:inputType="textCapWords|textPersonName" 
        android:layout_weight="1">

        <requestFocus />
    </EditText>
    
    <EditText
        android:id="@+id/contactPhoneEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/component_bottom_margin"
        android:ems="10"
        android:hint="@string/contact_phone_number_hint"
        android:inputType="phone" 
        android:layout_weight="1"/>
    
     </TableRow>
     
    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/contactStreetEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/component_bottom_margin"
        android:layout_weight="1"
        android:ems="10"
        android:hint="@string/contact_street_hint"
        android:inputType="textCapWords|textPostalAddress" />

    <EditText
        android:id="@+id/contactCityEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/component_bottom_margin"
        android:ems="10"
        android:hint="@string/contact_city_hint"
        android:inputType="textCapWords" 
        android:layout_weight="1"/>
    
    </TableRow>


</TableLayout>
<!--
A Fragment is sort of a mini Activity which you can add
or remove from another Activity.
 
You can use them to create reusable pieces you can add
to your interface.

They are often used so that depending on your screen size
you can add more or less fragments 
-->

fragment_contact.xml (Vertical)

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/app_outer_margin" >

    <EditText
        android:id="@+id/contactNameEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/component_bottom_margin"
        android:ems="10"
        android:hint="@string/contact_name_hint"
        android:inputType="textCapWords|textPersonName" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/contactStreetEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/component_bottom_margin"
        android:ems="10"
        android:hint="@string/contact_street_hint"
        android:inputType="textPostalAddress" />

    <EditText
        android:id="@+id/contactCityEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/component_bottom_margin"
        android:ems="10"
        android:hint="@string/contact_city_hint"
        android:inputType="textCapWords" />

    <EditText
        android:id="@+id/contactPhoneEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/component_bottom_margin"
        android:ems="10"
        android:hint="@string/contact_phone_number_hint"
        android:inputType="phone" />

</TableLayout>
<!--
A Fragment is sort of a mini Activity which you can add
or remove from another Activity.
 
You can use them to create reusable pieces you can add
to your interface.

They are often used so that depending on your screen size
you can add more or less fragments 
-->

strings.xml (French)

<?xml version="1.0" encoding="utf-8"?>

<!-- To automatically change text to other languages 
you need to create a new values folder in res. The
new values folder will be followed by a hyphen and 
the ISO country code at the end of the directory name.
Then copy over your strings.xml file. -->

<resources>

    <string name="app_name">App Recensement</string>
    <string name="action_settings">Réglages</string>
    <string name="contact_name_hint">Entrez le nom</string>
    <string name="contact_phone_number_hint">Entrez le numéro de téléphone</string>
    <string name="contact_street_hint">Entrez rue</string>
    <string name="contact_city_hint">Saisissez votre ville</string>

</resources>

3 Responses to “Android Development 19”

  1. Anonymous says:

    hello im having problem with the seekbar..i cant get the libraries for the seekbar

  2. jena says:

    hello im having problem with the seekbar i cant get the libraries.can u please hlp.. thnks

Leave a Reply

Your email address will not be published.

Google+