Activity Preference In Android - Day Three

This is second article on Activity Preference in Android. Inthe  previous article we have completed the code of BROWSER button. Refer to the article series below:

Now we are starting to work on the second button; make sure that you create contact activity and design the layout for contact us screen.

Step 1:

design

Code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.         android:layout_width="match_parent"  
  6.         android:layout_height="match_parent"  
  7.         android:orientation="vertical"  
  8.         tools:context="com.khawarislam.myapplication.HomeActivity">  
  9.     <EditText  
  10.         android:id="@+id/contact_EditText_name"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_gravity="top"  
  14.         android:hint="Please give your Full Name" />  
  15.     <EditText  
  16.         android:id="@+id/contact_EditText_phone"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:singleLine="true"  
  20.         android:hint="Please give your Phone Number"  
  21.         android:inputType="phone" />  
  22.     <EditText  
  23.         android:id="@+id/contact_EditText_comment"  
  24.         android:layout_width="fill_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:layout_weight="1"  
  27.         android:hint="Comments"  
  28.         android:inputType="textMultiLine"  
  29.         android:layout_gravity="top" />  
  30.     <Button  
  31.         android:layout_width="fill_parent"  
  32.         android:layout_height="wrap_content"  
  33.         android:text="Send"  
  34.         android:onClick="onClick_send"/>  
  35. </LinearLayout>  
Step 2: Make sure that you add the code of Intent class in HOME ACTVITY. Basically the work of intent classes is that it works like a messenger between two activities; if we imagine that whenthe user clicks on the contact us button thenthe new activity is open now; in this way we need an intent class.

intent class

Step 3: Now move to the contact activity and we add some functionalities in it, like user validation, and if the user gives the record the first time then this record is save in mobile, so the user can’t input the record again and again. Now first we create an instance class textbox.

class textbox

Step 4: You can see that inthe previous step we add SharedPreferences API; it is used for saving data in mobile. Now we add a validation method in textboxe so that any user can’t enter the empty box and it’s required that they filled overall textboxes then it move on to Thanks activity.

Thanks activity

Step 5: Now we make toast messag so that when the user escapes any empty box then the alert  appear that you fill the name, comment etc. Now the following code is added in Contact Activity.

Contact Activity

Now when the comment edit box is empty andthe user can’t enter anything in it, then when you click on the send button then this message appears.

button

Step 6: Those variables which are initialized  first, now we use it and make a separate method and locate the textbox variables.

method

Step 7: Now we need to add some text in textbox; the first time when the user opens the application “N/A” is following string is show in textbox. Now we add the following code, you can see that we assign the key, basically for this key's values we use save preferences method.

key values

Step 8: Now we add this code to save the information of textboxes which is entered by the user and if the user exits the app but data remains save, and if you want to delete the data you should uninstall the app.

delete the data

Step 9: Now we make anon_click send method that all information in textboxes is put correctly and no empty box  appears, then we see the thanks activity.

program

And now populate and init method declare in load event

code

Step 10: Now we simply insert the images in drawable folder and put it into layout section. Now run the app, fill all the information in textboxes, and click on send button; now the thanks activity appears

run

Read more articles on Android:

Up Next
    Ebook Download
    View all
    Learn
    View all