How to press multiple buttons at the same time? - java

I am developing an android application that consists of a 2 dimensional array of buttons. when i touch the buttons, at a time only one button is pressed. i want to press multiple buttons at the same time. please give me a sample code.

You can click a button programmatically by using the button.performClick() method inside your other onclicklistener button

To press another button(button2), just call button2.performClick() from button1 listeners onClick() method.
Here is an example:
Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "button 1 clicked", Toast.LENGTH_SHORT).show();
button2.performClick();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "button 2 clicked", Toast.LENGTH_SHORT).show();
// Do something
}
});

Related

How to handle setOnClickListener for dynamically added button in android?

I have added button view to a linearlayout
and now I want to setOnClickListener on each added button here's my code
for(int i=0;i<15;i++){
Button btn = new Button(this);
btn.setText("button " + i);
myview.addView(btn);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
Toast.makeText(MainActivity.this, "you clicked button " + i, Toast.LENGTH_SHORT).show();
}
});
}
but getting result is only you clicked button 15 for all buttons, but i want to get only that position result which I have clicked on button Like:
for button1 ==> you clicked button 1
for button2 ==> you clicked button 2
How to do that??
You can setTag and getTag to identify your Buttons.
Button btn = new Button(this);
btn.setText("button " + i);
btn.setTag("button" + i);
myview.addView(btn);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
Toast.makeText(MainActivity.this, "you clicked " + v.getTag(), Toast.LENGTH_SHORT).show();
}
});

Making components invisible with button

How can I make a component on my MainUI invisible by pressing a button? I want to dismiss a warning box on my MainActivity with a button that says "Close" or with a cross image.
view.setVisibility(View.GONE);
where view is the component you want to hide
Button crossButton = (Button) findViewById(R.id.crossImage)
Button button1 = (Button) findViewById(R.id.buttonOne)
//and so on
crossButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
button1.setVisibility(GONE);
//and so on
}
});

Android Studio cant resolve error setOnClickListener

Hi I can't seem to build this.. It states that it cannot resolve the OnClickListener. The onClick action is performed the back button that goes to the main activity.
Button bnCompute = (Button) this.findViewById(R.id.bnCompute);
bnCompute.setOnClickListener(new View.OnClickListener());
{
#Override
public void onClick (View view){
Toast.makeText(MainActivity.this, "You Compute All!", Toast.LENGTH_LONG).show();
EditText etBeauty = (EditText) MainActivity.this.findViewById(R.id.etBeauty);
EditText etBody = (EditText) MainActivity.this.findViewById(R.id.etBody);
EditText etIntelligence = (EditText) MainActivity.this.findViewById(R.id.etIntelligence);
int total = Integer.parseInt(String.valueOf(etBeauty.getText())) + Integer.parseInt(String.valueOf(etBody.getText()))
+ Integer.parseInt(String.valueOf(etIntelligence.getText()));
Intent actSummary = new Intent(MainActivity.this, Score.class);
actSummary.putExtra("total", Integer.toString(total));
MainActivity.this.startActivity(actSummary);
}
}
You have implemented onClickoutside the scope of listener.
It should be something like this below :
Button button = (Button) findViewById(R.id.button1);
//Your mistake is on this line.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});

tap on button to take you another page error android studio

I was working in app with 2 interface.
every thing was good until i tried to put a button that take you
to the #2 interface the app crash when i press the button in the emulator
Can someone help explain how to do that ?
Button mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(mIntent);
}
});
so what is wrong?

Getting the selected radio button inside OnClick for a button

I am building a simple app
which allow the user to enter his name and select the radio button "with name"
and a toast shows , Hello Name
and the other radio for guest to show hello guest , so How I can get the selected radio inside the OnClick do I need to get them from FindViewByID ??
here my code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
public void onClick(View v) {
if (v.getId() == R.id.button1) {
//getting the selected radio if its radio 0
Toast.makeText(this, "Hello Guest", Toast.LENGTH_LONG).show();
}
//getting the selected radio if its radio 1
EditText txt = (EditText) findViewById(R.id.editText1);
Toast.makeText(this, "Hello " + txt.toString(), Toast.LENGTH_LONG).show();
}
Try this instead:
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//YOUR CODE HERE
}
});
Put that in onCreate() and see how that works out for you. Have you also tried seeing if the Id of View v passed into onClick is actually the correct view? To see this, use log to check the Id.
better you directly check the radio button state on button click listener:
#Override
public void onClick(View v)
{
if(v == button)
{
if(rb1.isChecked() == true)
Toast.makeText(this, "Hello"+rb1.getText(), Toast.LENGTH_LONG).show();
if(rb2.isChecked() == true)
Toast.makeText(this, "Hello"+rb2.getText(), Toast.LENGTH_LONG).show();
}
}
where
rb1=(RadioButton)findViewById(R.id.optname);
rb2=(RadioButton)findViewById(R.id.optguest);
FOR MORE

Categories