==================================================

activity_main.xml

==================================================

<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
      android:layout_width="wrap_content"
      android:id="@+id/fruit"
      android:layout_height="wrap_content"
      android:text="fruit"
      android:textColor="#ffff00"
      android:textSize="48sp" />
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
    >
    <Button
      android:id="@+id/apple"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:text="apple"
    />
    <Button
      android:id="@+id/orange"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="orange"
    />
  </LinearLayout>

</LinearLayout>

-----------------------------------------------------------

 

 

===========================================

MainActivity.java

===========================================

public class MainActivity extends AppCompatActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnApple=(Button)findViewById(R.id.apple);
    btnApple.setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v){
        TextView textfruit = (TextView)findViewById(R.id.fruit);
        textfruit.setText("Apple");
    }
  });

  Button btnOrange=(Button)findViewById(R.id.orange);
    btnOrange.setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v){
        TextView textfruit = (TextView)findViewById(R.id.fruit);
        textfruit.setText("Orange");
     }
    });
  }
}

 

 

==============================================================

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

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

    Button btnApple=(Button)findViewById(R.id.apple);
    btnApple.setOnClickListener(this);

    Button btnOrange=(Button)findViewById(R.id.orange);
    btnOrange.setOnClickListener(this);
  }

  public void onClick(View v){
    TextView textFruit = (TextView)findViewById(R.id.fruit);
    switch (v.getId()){
      case R.id.apple:
        textFruit.setText("Apple");
        break;
      case R.id.orange:
        textFruit.setText("Orange");
        break;
    }
  }
}

 

=======================================

 

public class MainActivity extends AppCompatActivity{

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

    findViewById(R.id.apple).setOnClickListener(mClickListener);
    findViewById(R.id.orange).setOnClickListener(mClickListener);
  }

  Button.OnClickListener mClickListener = new View.OnClickListener(){
    public void onClick(View v){
      TextView textFruit = (TextView)findViewById(R.id.fruit);
      switch (v.getId()){
        case R.id.apple:
          textFruit.setText("Apple");
          break;
        case R.id.orange:
          textFruit.setText("Orange");
          break;
      }
    }
  };
}

 

==========================================================================

<?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:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">

  <TextView
    android:layout_width="wrap_content"
    android:id="@+id/fruit"
    android:layout_height="wrap_content"
    android:text="fruit"
    android:textColor="#ffff00"
    android:textSize="48sp" />
      <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
     >
       <Button
         android:id="@+id/apple"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="apple"
         android:onClick="mOnClick"
       />
      <Button
        android:id="@+id/orange"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="orange"
        android:onClick="mOnClick"
      />
     </LinearLayout>
</LinearLayout>

 

=====================================================================

public class MainActivity extends AppCompatActivity{

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

  public void mOnClick(View v){
    TextView textFruit = (TextView)findViewById(R.id.fruit);
    switch (v.getId()){
      case R.id.apple:
        textFruit.setText("Apple");
        break;
     case R.id.orange:
       textFruit.setText("Orange");
       break;
    }
  }
}

+ Recent posts