Which Button was pressed first? - android

I need a way to know which of my buttons was pressed first. The app layout is more or less like this.:
Button1 Button2
Button3 Button4
Button5 Button6
Button7 Button8
Button9 Button10
Button11 Button12
And if one of the buttons of it's "line" is pressed, the other disappears. The thing is, I have no idea how to know which one of all these 12 buttons was pressed first, then pressed second, then pressed third and so on...
The code I have for hiding buttons works well, but that's pretty much the easy part.
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
button1.setVisibility(View.GONE);
button2.setVisibility(View.GONE);
}
});
I searched but maybe I don't know what exactly to search for, and I didn't find a good answer.

You can do it like this:
1) Have a HashMap where you link both of your buttons on the same line to each other. 2) Have an ArrayList of button id's where you can hold the order of presses. 3) Implement a method which will perform the mapping and call it in your Activity's #onCreate method. 4) Set your global listener instance to all of your buttons.
private HashMap<Integer, Integer> buttonMap = new HashMap<>();
private ArrayList<Integer> buttonPressedOrder = new ArrayList<>();
// A global listener instance to be set to all of your buttons
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View selectedButton) {
int selectedButtonId = selectedButton.getId();
//Add pressed button to pressed buttons list
buttonPressedOrder.add(selectedButton.getId());
//Find button to hide and hide it
int hidingButtonId = buttonMap.get(selectedButtonId);
Button hidingButton = findViewById(hidingButtonId);
hidingButton.setVisibility(View.GONE);
}
}
//Put these inside your activity#onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mapButtons();
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
...
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
}
// A method for mapping your buttons in the same line to each other
private void mapButtons(){
buttonMap.put(R.id.button1, R.id.button2)
buttonMap.put(R.id.button2, R.id.button1)
buttonMap.put(R.id.button3, R.id.button4)
buttonMap.put(R.id.button4, R.id.button3)
...
}
Whenever you need to see in which order the buttons are pressed, use this method
public void getButtonPressedOrder(){
Resources res = getResources();
int numberOfPressedButtons = buttonPressedOrder.size();
for(int i=0; i<numberOfPressedButtons; i++){
Log.i("PressOrder", res.getResourceEntryName(buttonPressedOrder.get(i))
+ " is pressed at " + (i+1) + " order");
}
}
which will log something like:
I/PressOrder: button1 is pressed at 1 order
I/PressOrder: button5 is pressed at 2 order
I/PressOrder: button10 is pressed at 3 order
Hope this helps!

Related

Android: How to set onClickListener to different button when clicked

I have ten buttons and only one of them has a Red background at any point of time and the red button has an onClicklistener.
When this red button is clicked one of these ten buttons in random will get red background and this new red button should use the previous onCLickListener and when this new red button is clicked again one of these ten buttons in random gets red background and the onClickListener should be assigned to it and there is a counter which counts every time the RedButton is clicked.
Example:
There are four buttons
[WhiteButton WhiteButton RedButton WhiteButton] and only RedButton has onClickListener
when RedButton is clicked one of the buttons color changes to red
[WhiteButton RedButton WhiteButton WhiteButton]
when this new RedButton is clicked the buttons become
[WhiteButton WhiteButton WhiteButton RedButton]
I am able to change the color of one of the ten buttons to red when the RedButton is Clicked for the first time but not able to set the OnClickListener to the new RedButton.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ourButton = (Button) findViewById(R.id.button);
ourTextView = (TextView) findViewById(R.id.textView);
buttons[0] = (Button) findViewById(R.id.button2);
buttons[1] = (Button) findViewById(R.id.button3);
buttons[2] = (Button) findViewById(R.id.button4);
buttons[3] = (Button) findViewById(R.id.button5);
Random r = new Random();
oddValue = r.nextInt(4 - 0);
buttons[oddValue].setBackgroundColor(Color.RED);
buttons[oddValue].setOnClickListener(new ourOnClickListener(this));
ourOnClickListener
public class ourOnClickListener implements OnClickListener{
MainActivity caller;
private int count;
public ourOnClickListener(MainActivity activity) {
this.caller = activity;
this.count = 0;
}
public void onClick(View view) {
int i;
count = count + 1;
Random r = new Random();
int oddValue_new = r.nextInt(4 - 0);
caller.buttons[oddValue_new].setBackgroundColor(Color.RED);
caller.ourTextView.setText("Count : " + count);
}
}
You can do it the same way as you set the background color:
caller.buttons[oddValue_new].setOnClickListener(this);
Since you are the onClickListener you can refer to this as the new onClickListener.
If you want only Red button is possible to click. You can set Enabled to false for all (exclude Red button). So, you will setOnClickListener to all button.
If all button possible to click, only run action when Red is clicked. You should check clicked button and compare if it is Red button in event Click
Remove OnClickListener from all button 'setOnClickListener(null)', and SetOnClickListener to Red
I would solve this a little bit differently.
Save the reference to button with red background redButton.
All buttons have the same onClick. In onClick just verify that clicked button is the one with red background (just check by reference). If not - do nothing. If yes: select new button, change background and save reference to new button.
Your onClick method should looks like this:
public void onClick(View view) {
if (view == redButton) {
redButton.setBackgroundColor(Color.WHITE);
Random r = new Random();
int oddValue_new = r.nextInt(4 - 0);
redButton = buttons[oddValue_new];
redButton.setBackgroundColor(Color.RED);
}
}
UPDATE
As #Mr Phuc 87 suggested you can use enabled. I think it is very good approach. In addition to enabled you can use state-list. Create state-list which have white background for android:state_enabled=false and red background for android:state_enabled=true. Now all you need is just change enabled property of your buttons.
OnClick should looks like:
public void onClick(View view) {
redButton.setEnabled(false);
Random r = new Random();
int oddValue_new = r.nextInt(4 - 0);
redButton = buttons[oddValue_new];
redButton.setEnabled(true);
}
state-list should looks like(put it in drawable):
<selector xmlns:android="http://schemas.android.com/apk/res/android"
<item android:state_selected="false" android:drawable=#color/white />
<item android:state_selected="true" android:drawable=red_here />
</selector>

Android 1 function for 3 buttons

I have 1 function to print the text of the button, and i use that function for the 3 button, how do i know in what button the user click? (sorry but my english is low)
Example:
public void function(View view) {
TextView text = (TextView) findViewById(R.id.textView);
Button button1= (Button) findViewById(R.id.button3);
Button button1= (Button) findViewById(R.id.button4);
Button button1= (Button) findViewById(R.id.button5);
text.setText(button <?> .getText()); }
is the number of the button the user click
Its any way to know in which button the user click?
Please help, Thanks.
you can define one click listener and set this listener for all buttons. For example:
private OnClickListener genericListerner = new OnClickListener() {
public void Click(View v) {
switch(v.getId())
{
case R.id.button3:
text.setText(button3.getText().toString());
break;
case R.id.button4:
text.setText(button4.getText().toString());
break;
case R.id.button5:
text.setText(button5.getText().toString());
break;
default:
//do something
}
};
and you set the listeners like the following:
button3.setOnClickListener(genericListener);
button4.setOnClickListener(genericListener);
button5.setOnClickListener(genericListener);
I am not sure whether this is what you are asking, but what this code basically does is that, if you click button 3 it sets the textview's text to button 3's text, if you click button 4 it sets the textview's text to button 4's text and if you click button 5, it sets the textview's text to button 5's text.
Hope this helps.
The view that is passed in is the one that was clicked, therefore it has the id.
view.getId() == R.id.button3
If you still need help, share more of your code.
Well you will require some onclick listeners for the button, you can then set an id or name in the onclick e.g
final Button button1 = (Button) findViewById(R.id.button_id);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text.settext("Button1 Clicked")
}
});

If one button press does something, other buttons pressed should do something else

I have 5 Buttons in an activity. My code should work as follows :
1 (Correct) button pressed it should do something.
other 4 pressed, something else should be done...
I don't want to used 5 onclicklistener
if(Button1 press) {
do something
}
else if (button2 or button3 or button4 or button5 press)
{
something else to do
}
Why don't you do it this way:
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final Button button4 = (Button) findViewById(R.id.button4);
final Button button5 = (Button) findViewById(R.id.button5);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
if (v.equals(button1)) {
// do something
} else {
// do something else
}
}
};
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
button4.setOnClickListener(listener);
button5.setOnClickListener(listener);
There are several ways to do it but if the same buttons will always do the same thing then you can set the onClick() in your xml.
First, define the same function for each Button
<Button
android:id="#+id/button1"
....
android:onClick="functionName"/>
<Button
android:id="#+id/button2"
....
android:onClick="functionName"/>
then in your code
public void functionName(View v)
{
switch (v.getId()) // v is the button that was clicked
{
case (R.id.button1): // this is the oddball
...do stuff
break;
default: // this will run the same code for any button clicked that doesn't have id of button1 defined in xml
...do other stuff
break;
}
}
now your Buttons or onClickListeners don't have to be defined in your code unless you need to do something else with a Button
Edit
#prosperK has pointed out that with the newer ADT passing int to the switch causes errors so you may need an if/else if this is the case. link to SO post about this
You can define two different click listeners. Button 1 gets first listener and others get the second one. Hope this helps.

Is it possible to click on two buttons at the same time, if one covers the other one?

Sorry for the weird question, but is it possible to click on two buttons at the same time in android(having two logs, "clicked on b1" and "clicked on b2"), if one totally covers the other one?
This is not ordinarily possible; the top button will absorb the button click event and not pass it on to the one behind it. It is not clear whether or not you want to obtain this behaviour or avoid it, nonetheless, you can force it by propagating the event manually across the click listeners.
Here is one way (there are a few); assume buttonOne is on top of buttonTwo:
final Button buttonOne = (Button) findViewById(...);
final Button buttonTwo = (Button) findViewById(...);
buttonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonOne clicked");
buttonTwo.performClick();
}
});
buttonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonTwo clicked");
}
});
The click event enters the listener on button one, which then causes a click event on button two.
Here is another way which would work (and could be changed to support long click events easily):
final Button buttonOne = (Button) findViewById(...);
final Button buttonTwo = (Button) findViewById(...);
final OnClickListener listenerTwo = new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonTwo clicked");
}
};
final OnClickListener listenerOne = new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonOne clicked");
listenerTwo.onClick(buttonTwo);
}
};
buttonOne.setOnClickListener(listenerOne);
buttonTwo.setOnClickListener(listenerTwo);
Yes, it is possible. You will need to pass the click event that occurs on the foreground view to the background view. You can do this by checking where the click occurs and if it occurs within the view's bounds.

android get Colour

Hello i have lots of button with the same OnClickListener, the buttons have different colours, how can i get the colour(or the colour resource) of the pressed button ?
Here is the code i use
// declare a OnClickListener that will execute different actions
// depending on the view that was clicked
View.OnClickListener colorButtonListener = new View.OnClickListener(){
public void onClick (View v){
textarea_note.setBackgroundDrawable(v.getBackground());//set edit background the same of the button
dialog.dismiss();
}
};
Button button1 = (Button) dialog.findViewById(R.id.button1);
Button button2 = (Button) dialog.findViewById(R.id.button2);
Button button3 = (Button) dialog.findViewById(R.id.button3);
Button button4 = (Button) dialog.findViewById(R.id.button4);
Button button5 = (Button) dialog.findViewById(R.id.button5);
Button button6 = (Button) dialog.findViewById(R.id.button6);
Button button7 = (Button) dialog.findViewById(R.id.button7);
Button button8 = (Button) dialog.findViewById(R.id.button8);
Button button9 = (Button) dialog.findViewById(R.id.button9);
/*for changing the colour when the user clicks on a button*/
button1.setOnClickListener(colorButtonListener);
button2.setOnClickListener(colorButtonListener);
button3.setOnClickListener(colorButtonListener);
button4.setOnClickListener(colorButtonListener);
button5.setOnClickListener(colorButtonListener);
button6.setOnClickListener(colorButtonListener);
button7.setOnClickListener(colorButtonListener);
button8.setOnClickListener(colorButtonListener);
button9.setOnClickListener(colorButtonListener);
/**for the round corner*/
Resources res = this.getResources();
button1.setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x1)));
button2.setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x2)));
button3.setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x3)));
button4.setBackgroundDrawable(this.Sd(res.getColor(R.color.color2x1)));
button5.setBackgroundDrawable(this.Sd(res.getColor(R.color.color2x2)));
button6.setBackgroundDrawable(this.Sd(res.getColor(R.color.color2x3)));
button7.setBackgroundDrawable(this.Sd(res.getColor(R.color.color3x1)));
button8.setBackgroundDrawable(this.Sd(res.getColor(R.color.color3x2)));
button9.setBackgroundDrawable(this.Sd(res.getColor(R.color.color3x3)));
//now that the dialog is set up, it's time to show it
dialog.show();
As far as i know, You can get the color values(for eg: R.color.green) but you can get a drawable object of the button.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Drawable d = v.getBackground()
}
});
I've found this method, but i have never try it :
int color = v.getSolidColor();
TextView txt = (TextView)findViewById(R.id.txtColor);
txt.setText("Color Of Button ");
View.OnClickListener colorButtonListener = new View.OnClickListener(){
public void onClick (View v){
txt.setTextColor(v.getSolidColor());
textarea_note.setBackgroundDrawable(v.getBackground());//set edit background the same of the button
dialog.dismiss();
Log.i("Color of Button","Color = "+v.getSolidColor() );
}
};
Note : follow this link : getSolidColor()
in your Code , try to replace the :
button1.setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x1)));
with :
button1.setBackgroundRessource(R.color.color1x1);
download this project , i've created 4 buttons, and i get the color of the background ;) , enjoy it
Get Color of Buttons
Hope it helps

Categories

Resources