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:
Code:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context="com.khawarislam.myapplication.HomeActivity">
- <EditText
- android:id="@+id/contact_EditText_name"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="top"
- android:hint="Please give your Full Name" />
- <EditText
- android:id="@+id/contact_EditText_phone"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:singleLine="true"
- android:hint="Please give your Phone Number"
- android:inputType="phone" />
- <EditText
- android:id="@+id/contact_EditText_comment"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:hint="Comments"
- android:inputType="textMultiLine"
- android:layout_gravity="top" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Send"
- android:onClick="onClick_send"/>
- </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.
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.
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.
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.
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.
Step 6: Those variables which are initialized first, now we use it and make a separate method and locate the textbox variables.
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.
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.
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.
And now populate and init method declare in load event
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
Read more articles on Android: