Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How get user input on same xml. Layout in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 277
    Comment on it

     In the below example I have created a demo page for user input. Here when user will enter name and phone number then user name and number  will show on same page. Here I have created first Relative layout then in relative layout added Edit text, Text View, Button after that I had declared a class and initialized id of class then I have used OnClickListener()method. You can see below program it will clearly describe you How get user input on same xml.Layout in android

    
    Step(1)activity_main.xml-
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <TextView
            android:id="@+id/nameText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="18dp"
            android:layout_marginTop="24dp"
            android:text="Name" />
    
        <EditText
            android:id="@+id/etName"
            android:maxLength="12"
    
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/nameText"
            android:ems="10" android:inputType="text"/>
    
        <TextView
            android:id="@+id/phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/nameText"
            android:layout_below="@+id/etName"
            android:layout_marginTop="26dp"
            android:text="PhoneNumber" />
        <EditText
            android:id="@+id/etPhone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:ems="10"
            android:maxLength="12"
         android:layout_marginLeft="100dp"
            android:layout_marginTop="70dp"
            android:layout_toEndOf="@+id/nameText" />
    
    
        <Button
            android:id="@+id/send"
            android:layout_width="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="150dp"
            android:text="submit" />
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="250dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Name"
                android:layout_marginTop="25dp"
                android:layout_marginLeft="10dp"
                android:id="@+id/textView6"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true" />
    
            <TextView
                android:layout_toRightOf="@+id/textView6"
                android:layout_marginTop="28dp"
                android:layout_marginLeft="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/tvName"
                android:layout_centerHorizontal="true" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:id="@+id/textView12"
                android:layout_below="@+id/textView6"
                android:text="PhoneNum"
                android:layout_margin="10dp"
                android:layout_alignParentStart="true" />
            <TextView
                android:layout_toRightOf="@+id/textView12"
                android:layout_marginTop="67dp"
                android:layout_marginLeft="20dp"
                android:text="New Text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/tvPhone"/>
            
        </RelativeLayout>
        
    
    </RelativeLayout>
    
    Step(2)MainActivity-
    public class MainActivity extends AppCompatActivity {
        EditText etName;
        EditText etPhone;
        TextView tvName;
        TextView tvPhone;
        Button button;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button = (Button) findViewById(R.id.send);
            etName=(EditText)findViewById(R.id.etName);
            etPhone=(EditText)findViewById(R.id.etPhone);
            tvName=(TextView)findViewById(R.id.tvName);
            tvPhone=(TextView)findViewById(R.id.tvPhone);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (etName.getText().toString().trim().isEmpty()) {
                        etName.setError("Please enter your name");
                        etName.requestFocus();
                    } else if (etPhone.getText().toString().trim().isEmpty()) {
                        etPhone.setError("Please enter your");
                        etPhone.requestFocus();
                    } else {
                        tvName.setText(etName.getText().toString() + "!");
                        tvPhone.setText(etPhone.getText().toString()+"!");
    
                    }
                }
            });
    
    
        }
    }

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: