0

I'm a beginner in android and making my first app. Here, I had taken 3 textViews and a button named Add. When I click on this button, I need to show the content of 2 textViews on third textView. I used eventListener and buttonClick event but it doesn't work. Please guide me with the code of Add button.

1
  • how much yuo have try ? Please put some code and we will help you in it. Commented Jan 12, 2012 at 12:06

1 Answer 1

1

First create the xml as below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_height="fill_parent"
 android:layout_width="fill_parent"      
 android:layout_gravity="center_horizontal" 
 android:orientation="horizontal"       
 android:layout_weight="1" 
 android:id="@+id/singleEmployee"
 android:background="#ffffff">
 <TextView 
   android:text="TextView" 
   android:id="@+id/textView1"
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content"/>
 <TextView 
   android:text="TextView" 
   android:id="@+id/textView2"
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content"/>
 <TextView 
   android:text="TextView" 
   android:id="@+id/textView3"
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content"/>
 <Button 
   android:text="Button" 
   android:id="@+id/button1"
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content"/>
</LinearLayout>

And then use this Java code to done your desire action:

public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView tv1,tv2,tv3;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testing);
    tv1 = findViewById(R.id.textView1);
    tv2 = findViewById(R.id.textView2);
    tv3 = findViewById(R.id.textView3);
    tv1.setText("Hello");
    tv2.setText("World");
    Button add = findViewById(R.id.button1);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tv3.setText("");
            String tvValue1 = tv1.getText().toString();
            String tvValue2 = tv2.getText().toString();
            tv3.setText("Value of First Text is: "+tvValue1+".And the value of second TextView is: "+tvValue2);
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.