Home > Software design >  Multiple functions with a single button in Java - Android
Multiple functions with a single button in Java - Android

Time:08-20

I have a button called Option, which toggles the visibility of the RadioGroup. By default, RadioBtn1 is selected and the text "First Choice" appears on TextView.

In this condition, if the Option button is clicked, I want to change its text to "Close". And, I want to change its text to "Set" if RadioBtn2 is selected. Now, if we click the Set button, I want to makes the text "Second Choice" visible on TextView.

After that the Set button itself changes to previous Option button making the RadioGroup invisible again.

Here is the code I have tried:

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:orientation="vertical" >
<TextView 
    android:textSize="18sp" 
    android:textColor="#ffffff" 
    android:gravity="center" 
    android:id="@ id/textView" 
    android:paddingBottom="10px" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:gravity="center"
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" >  
<Button 
    android:textSize="18sp" 
    android:id="@ id/btn1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button1" />   
<Button 
    android:textSize="18sp" 
    android:id="@ id/btn2" 
    android:visibility="gone"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button2" /> 
</LinearLayout>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@ id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:orientation="horizontal">
 <RadioButton 
    android:id="@ id/radioBtn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="RadioBtn1" />
 <RadioButton 
    android:id="@ id/radioBtn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="RadioBtn2" />
</RadioGroup>
<Button 
    android:textSize="18sp" 
    android:id="@ id/btnOption" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:text= "Option" />
</LinearLayout>
</LinearLayout>

Java:

package com.infinitenotions.sample;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.lang.String;
import android.content.DialogInterface.OnClickListener;
import java.lang.Override;

public class HWSampleActivity extends Activity
{

private TextView textView;
private Button btn1;
private Button btn2;
private Button btnOption;
private RadioGroup radioGroup;
private RadioButton radioBtn1;
private RadioButton radioBtn2;

String text1 = "First Choice"; 
String text2 = "Second Choice";

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView)findViewById(R.id.textView);
    btn1 = (Button)findViewById(R.id.btn1);
    btn2 = (Button)findViewById(R.id.btn2);
    btnOption = (Button)findViewById(R.id.btnOption);
    radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
    radioBtn1 = (RadioButton)findViewById (R.id.radioBtn1);
    radioBtn2 = (RadioButton)findViewById (R.id.radioBtn2);
   
    textView.setText(text1);
    
    btnOption.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
     btn1.setVisibility(View.GONE);
     radioGroup.setVisibility(View.VISIBLE);
     
     // **if text1 is visible which means radioBtn1 is checked 
        
        // set text of option button to "Close" 
        // on click "Close" button, change text to "Option" and return to previous state
        
        // if radioBtn2 is checked, change text of option button from "Close" to "Set" 
        // on click "Set" button, make text2 & btn2 visible, hide text1 & btn1, and change text from "Set" to "Option" as before
      
     // **and vice-versa, if text2 is visible

    }
    });
 }
}

CodePudding user response:

I'm afraid giving multiple functionalities to a single button will not work properly but I wrote this code with if statements that in each statement there is a setOnClickListener() method. Try this:

import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class HWSampleActivity extends AppCompatActivity{
     TextView textView;
     Button btn1;
     Button btn2;
     Button btnOption;
     RadioGroup radioGroup;
     RadioButton radioBtn1;
     RadioButton radioBtn2;

    String text1 = "First Choice";
    String text2 = "Second Choice";
    String textClose = "Close";
    String textSet = "Set";
    String textOption = "Option";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = findViewById(R.id.textView);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btnOption = findViewById(R.id.btnOption);
        radioGroup = findViewById(R.id.radioGroup);
        radioBtn1 = findViewById (R.id.radioBtn1);
        radioBtn2 = findViewById (R.id.radioBtn2);

        textView.setTextColor(Color.BLACK);
        textView.setText(text1);

        if (btnOption.getText().equals(textOption)){
            btnOption.setOnClickListener(view -> {
                radioGroup.setVisibility(View.VISIBLE);
                if (radioBtn1.isChecked())
                    btnOption.setText(textClose);
                else
                    btnOption.setText(textSet);
            });
        }

        if (btnOption.getText().equals(textClose)){
            btnOption.setOnClickListener(view -> {
                radioGroup.setVisibility(View.GONE);
                btnOption.setText(textOption);
            });
        }

        if (btnOption.getText().equals(textSet)){
            btnOption.setOnClickListener(view -> {
                textView.setTextColor(Color.BLACK);
                textView.setText(text2);
            });
        }
    }
}

if this does not work, then you can use your Button1 and Button2 in order to change your text with these 2 buttons.

CodePudding user response:

Adding onCLickListener should work, Try this code

radioBtn1.setChecked(true);

btnOption.setOnClickListener(view1 -> {
    if (radioGroup.getVisibility() == View.GONE){
        radioGroup.setVisibility(View.VISIBLE);
        btnOption.setText("Close");
    }

    if (radioBtn2.isChecked()) {
        textView.setText("Second Choice");
    } else if (radioBtn1.isChecked()) {
        textView.setText("First Choice");
    }
});
        
radioBtn1.setOnClickListener(view13 -> btnOption.setText("Set"));
radioBtn2.setOnClickListener(view12 -> btnOption.setText("Set"));
  • Related