🔒 Closed Bot Help Number 1

Status
Not open for further replies.
Sure, here's an example program that changes the text of 3 TextViews to "Hello", "Love", "Goodbye" respectively, when a button is clicked 3 times.

First, open up Android Studio and create a new project. Then, in the activity_main.xml file, add the following code:

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
   tools:context=".MainActivity">

   <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="TextView 1"/>

   <TextView
       android:id="@+id/textView2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="TextView 2"/>

   <TextView
       android:id="@+id/textView3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="TextView 3"/>

   <Button
       android:id="@+id/button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Click me"/>

</LinearLayout>

This creates a simple layout with 3 TextViews and a Button.

Next, in the MainActivity.java file, add the following code:

Code:
public class MainActivity extends AppCompatActivity {

   private TextView textView1, textView2, textView3;
   private Button button;
   private int clickCount = 0;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       textView1 = findViewById(R.id.textView1);
       textView2 = findViewById(R.id.textView2);
       textView3 = findViewById(R.id.textView3);
       button = findViewById(R.id.button);

       button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               clickCount++;
               if (clickCount == 1) {
                   textView1.setText("Hello");
               } else if (clickCount == 2) {
                   textView2.setText("Love");
               } else if (clickCount == 3) {
                   textView3.setText("Goodbye");
                   clickCount = 0; //reset click count to 0
               }
           }
       });
   }
}

This code sets up the TextViews and Button in the layout, and sets an OnClickListener on the Button. When the Button is clicked, the clickCount variable is incremented. If the clickCount is 1, the first TextView's text is set to "Hello". If the clickCount is 2, the second TextView's text is set to "Love". If the clickCount is 3, the third TextView's text is set to "Goodbye" and the clickCount is reset to 0.

Now you can run the app and click the button 3 times to change the text of the 3 TextViews to "Hello", "Love", "Goodbye" respectively.
 
Status
Not open for further replies.

About this Thread

  • 1
    Replies
  • 584
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
1,159
Guests online
1,288
Total visitors
2,447

Forum statistics

Threads
2,282,026
Posts
29,005,686
Members
1,223,729
Latest member
Bethbeth9
Back
Top