This example is a simple messenger activity.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it MessengerActivity.
2.) Write following into main.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/mainLayout" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <LinearLayout android:layout_width= "match_parent" android:layout_height= "wrap_content" > <TextView android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "@string/message_prompt" android:textAppearance= "?android:attr/textAppearanceMedium" /> <EditText android:id= "@+id/message_field" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:ems= "10" android:hint= "@string/message_hint" > <requestFocus /> </EditText> </LinearLayout> <Button android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "@string/add_message_button_label" android:onClick= "addMessageButtonHandler" /> </LinearLayout> |
3.) Write following into values/strings.xml:
1 2 3 4 5 6 7 8 9 10 11 12 | <?xml version= "1.0" encoding= "utf-8" ?> <resources> <string name= "app_name" >MessangerActivity</string> <string name= "hello_world" >Hello world!</string> <string name= "menu_settings" >Settings</string> <string name= "title_activity_rotations_exercises" >Rotations Exercises</string> <string name= "message_prompt" >Message:</string> <string name= "font_size_prompt" >Font size:</string> <string name= "add_message_button_label" >Add Message</string> <string name= "message_hint" >Your Message</string> </resources> |
4.) Run for output.
Steps:
1.) Create a project named MessangerActivity and set the information as stated in the image.
Build Target: Android 4.0
Application Name: MessangerActivity
Package Name: com. example. MessangerActivity
Activity Name: MessangerActivity
Min SDK Version: 2.3
Application Name: MessangerActivity
Package Name: com. example. MessangerActivity
Activity Name: MessangerActivity
Min SDK Version: 2.3
2.) Open MessengerActivity.java file and write following code there:
package com.example.messangeractivity;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; public class MessangerActivity extends Activity { private LinearLayout mMainLayout; private EditText mMessageField; private ArrayList<String> mMessages = new ArrayList<String>(); @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); mMainLayout = (LinearLayout)findViewById(R.id.mainLayout); mMessageField = (EditText)findViewById(R.id.message_field); } @Override protected void onSaveInstanceState(Bundle outState) { super .onSaveInstanceState(outState); outState.putStringArrayList( "messages" , mMessages); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super .onRestoreInstanceState(savedInstanceState); mMessages = savedInstanceState.getStringArrayList( "messages" ); for (String message: mMessages) { addMessage(message); } } public void addMessageButtonHandler(View clickedButton) { String message = mMessageField.getText().toString(); if (!message.isEmpty()) { mMessages.add(message); addMessage(message); } } private void addMessage(String message) { TextView textV = new TextView( this ); textV.setText(message); mMainLayout.addView(textV); } } |
3.) Compile and build the project.
Output
0 comments:
Post a Comment