Introduction
This is the second article on Activity Preference in
Android. In the previous article, we have completed the code of the 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 the Intent class in HOME ACTIVITY. Basically the work of intent classes is that it works like a messenger between two activities; if we imagine that when the user clicks on the contact us button then the 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 saved 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 in the previous step we add SharedPreferences API; it is used for saving data on mobile. Now we add a validation method in textboxes so that any user can’t enter the empty box and it’s required that they filled overall textboxes then it moves on to Thanks activity.
Step 5
Now we make toast message so that when the user escapes any empty box then the alert appears that you fill the name, comment, etc. Now the following code is added in Contact Activity.
Now when the comment edit box is empty and the 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 shown in the 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 to 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 the load event
Step 10
Now we simply insert the images in a drawable folder and put it into layout section. Now run the app, fill all the information in textboxes, and click on the send button; now the thanks activity appears
Read more articles on Android