Creating Simple User Interface in Android

Here we are going to create a simple User Interface in which we take a message from a Edit-text field and than we will Print that on a Text-View Field

Step-1:

first of all go in your_activity_name.xml file and create a Edit text field ,a button and a text view area. You can create these as you like or simply paste this code in your xml file.

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.kaushal.myapplication.MainActivity">
    
    <EditText android:id="@+id/msg" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:hint="Enter anything" 
        android:textSize="30dp" 
        />
    <Button android:id="@+id/print" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:onClick="showmsg" 
        android:text="Print" />
    <TextView android:id="@+id/sname" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:hint="Your msg" 
        android:textColor="@android:color/holo_red_dark" 
        android:textSize="80px" />


</LinearLayout>

 

Assign the id according to your choice.

Now as you can see in button element of our xml file we have used the attributes onclick , this attribute basically used to program the event handling of button or in general way how a button will response .

Step 2:

Now lets get back to your_activity_name.java file and now we have to first initialize the all the field of reference to the field so. Now just focus on the below code

public class MainActivity extends AppCompatActivity {
    EditText e,e1;
    Button b1;
    TextView t;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e=(EditText)findViewById(R.id.msg);
        b1=(Button)findViewById(R.id.print);
        t=(TextView)findViewById(R.id.sname);

    }
    public void showmsg(View view){
        String s=(String)e.getText().toString();
        t.setText(s);
    }
}

 

 

As you can see in our main activity class first of all we have initialize the three variable as edittext,button and textview,

Now in onCreate function we have given reference to these variables or we can say we are assigning id’s to these variables. Now here you can see we have used external type casting in each line because findViewById() function return the view type (which we will discuss later).

Now after that we have to write a function which going to handle the onclick property of the button, So in showmsg function we have created a string s which will get text from the our Edittext field and convert it into the string .

And after that we have assigned that string to the textfield variable t, It means that msg will now be printed in text field t. Now lets tun this application and check .

 

 

About kaushal97

Android Enthusiast

4 Comments

  1. Its gives quick explanation

  2. Happy new year 😊

  3. This is the best website for beginners

  4. it is good tutorial 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *