Header Ads

ad728
  • Breaking News

    Working With EditText and TextView

    activity_main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.desktop.workingwithedittextandtextview.MainActivity">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_alignParentTop="true"
            android:layout_marginTop="91dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:hint="Enter name" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Name"
            android:id="@+id/button"
            android:layout_below="@+id/editText"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/textView_show_name"
            android:layout_below="@+id/button"
            android:layout_alignLeft="@+id/button"
            android:layout_alignStart="@+id/button"
            android:layout_marginTop="89dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>
     MainActivity.java
    package com.example.desktop.workingwithedittextandtextview;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        TextView tv;
        EditText et;
        Button b1;
       
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            tv=(TextView)findViewById(R.id.textView_show_name);
            et=(EditText)findViewById(R.id.editText);
            b1=(Button)findViewById(R.id.button);
    
            b1.setOnClickListener(new View.OnClickListener() {
             
                public void onClick(View v) {
                    String name =et.getText().toString();
                    tv.setText("Entered name is :" + name);
                }
            });
        }
    }

    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728