How to handle with multi buttons on Setclick? [duplicate] - android

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
How to handle with multi buttons?
I´m using this code to change SoundOff to soundOff ON click
I dont now how to fix the error
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameover);
//setting the orientation to landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
ImageView medaille = (ImageView) findViewById(R.id.medaille);
//adding a click listener
//Define Logout
ImageButton LogOut = (ImageButton) findViewById(R.id.imageButton8);
//Define SoundOn
ImageButton SoundOn = (ImageButton) findViewById(R.id.imageButton9);
//Define SoundOff
ImageButton SoundOff = (ImageButton) findViewById(R.id.imageButton10);
//getting the button
ImageButton ButtonOk = (ImageButton) findViewById(R.id.imageButton11);
LogOut.setOnClickListener(this);
SoundOff.setOnClickListener(this);
SoundOn.setOnClickListener(this);
ButtonOk.setOnClickListener(this);
// Version
//define
TextView txtView = (TextView) findViewById(R.id.tv_current_score);
TextView txtsView = (TextView) findViewById(R.id.tv_current_score_value);
txtsView.setText(String.valueOf(GameView.score));
//Value
TextView myText = (TextView) findViewById(R.id.tv_best_score_value);
myText.setText(String.valueOf(GameView.Highscorer) );
TextView txtViews = (TextView) findViewById(R.id.tv_best_score);
}
#Override
public void onClick (View v){
//Define Logout
ImageButton LogOut = (ImageButton) findViewById(R.id.imageButton8);
//Define SoundOn
ImageButton SoundOn = (ImageButton) findViewById(R.id.imageButton9);
//Define SoundOff
ImageButton SoundOff = (ImageButton) findViewById(R.id.imageButton10);
//getting the button
ImageButton ButtonOk = (ImageButton) findViewById(R.id.imageButton11);
switch (v.getId()) {
case R.id.imageButton11:
//starting game activity
startActivity(new Intent(this, GameView.class));
break;
case R.id.imageButton4:
//Logout
startActivity(new Intent(this, GameLogout.class));
// do your code
break;
case R.id.imageButton8:
startActivity(new Intent(this, GameLogout.class));
break;
case R.id.imageButton9:
play = false;
SoundOn.setVisibility(View.GONE);//set visibility to false on create
SoundOff.setVisibility(View.VISIBLE);
break;
case R.id.imageButton10:
play = true;
SoundOn.setVisibility(View.VISIBLE);//set visibility to false on create
SoundOff.setVisibility(View.GONE);
}
}
I´m getting an error in NullPointerException in the "SoundOn.setVisibility(View.GONE);//set visibility to false on create"

SoundOn is null; you need to make sure you initialize it.
If you are using Activities a good place is to do it in onCreate after you inflate the view. With something like this:
SoundOn = findViewById(R.id.YourViewsId);

Find which view was updated is only half the work.
You need to inflate (get runtime object of the view) and modify (change the visibility).
Add this before you perform any action off changing the button's view (the top of your switch statments):
Button SoundOn = (Button) findViewById(R.id.soundOffBtnId) //Use ImageButton instead of Button depends on your code.
Button SoundOff = (Button) findViewById(R.id.soundOffBtnId)

Related

How to create a history of pressing ImageButton

I am a novice developer. Please help to create the code to display the history of pressing ImageButton (on Android). I have three ImageButton (IB1, IB2, IB3) and I have 5 empty ImageView (IV1, IV2, IV3, IV4, IV5).
When the user first time touching one of the three ImageButtons (for example IB2), the ImageView (IV1) displays button image from IB2.
When the user clicks a second time and touching one of three buttons again (for example IB1 this time) image of the first pressed button (image from IB2) is moved from IV1 to the IV2, and IV1 gets the image from button which clicked in second time – from IB1. And so on an unlimited number of times, but history shows only the last 5 clicks. Here is the beginning of my code. Thanks in advance to all.
public class MainActivity extends Activity implements View.OnClickListener {
ImageView IV1, IV2, IV3, IV4, IV5;
ImageButton IB1, IB2, IB3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IB1 = (ImageButton) findViewById(R.id.IB_first);
IB2 = (ImageButton) findViewById(R.id.IB_second);
IB3 = (ImageButton) findViewById(R.id.IB_third);
IV1 = (ImageView)findViewById(R.id.ImageView1);
IV2 = (ImageView)findViewById(R.id.ImageView2);
IV3 = (ImageView)findViewById(R.id.ImageView3);
IV4 = (ImageView)findViewById(R.id.ImageView4);
IV5 = (ImageView)findViewById(R.id.ImageView5);
IB1.setOnClickListener(this);
IB2.setOnClickListener(this);
IB3.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.IB1:
IV1.setVisibility(View.VISIBLE);
IV1.setImageResource(R.drawable.first_image);
break;
case R.id.IB2:
IV1.setVisibility(View.VISIBLE);
IV1.setImageResource(R.drawable.second_image);
break;
case R.id.IB3:
IV1.setVisibility(View.VISIBLE);
IV1.setImageResource(R.drawable.third_image);
break;
}
}
}
Inside your clicklistner you should pass the drawables to other imageViews
for example:
IV5.setImageDrawable(IV4.getDrawable());
IV4.setImageDrawable(IV3.getDrawable());
IV3.setImageDrawable(IV2.getDrawable());
IV2.setImageDrawable(IV1.getDrawable());
// set your IV1 resource
Happy Coding ;)

ImageButton Click Action Android

I've stored the imagebutton info in an xml. It's image path,width,height and so on.
But since there are multiple images I want to store data for each imagebutton while it's clicked. for example, if there are two buttons named male and female, i want to set String value = "male"; if male image button is clicked. But it did not work. Can somebody help me?
public void AddAllImageButtons() throws IOException {
AbsoluteLayout Layout = (AbsoluteLayout) findViewById(R.id.layout1);
ImageButton btn = new ImageButton(this);
Layout.addView(btn);
AbsoluteLayout.LayoutParams absParams = (AbsoluteLayout.LayoutParams) btn
.getLayoutParams();
absParams.x = x;
absParams.y = y;
btn.setLayoutParams(absParams);
btn.setBackgroundDrawable(null);
btn.setImageBitmap(BitmapFactory.decodeStream(getAssets().open(path)));
if(type.equals("imagebutton")){
if(elementId.equals("female")){
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gender = elementId;
GenerateAlertBoxes(gender);
}
});
}
Id is the image button id which is created in xml file.
AlertBox text does not show "female" for this example.
you can setTag("value") to set the value in the ImageButton btn and on the onclick event you can use getTag() function to retrieve the value and set it to gender.
use btn.setTag(elementid); whenever you add a btn to the layout.......
inside btn.setOnClickListener you can write v.getTag().toString() to get the value and set it to gender......
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gender = v.getTag().toString();
GenerateAlertBoxes(gender);
}
});

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.

How to change specify previous clicked image button view when the 2nd click button is occur

I have doubt in when I click the 2nd time image button, I want to change the both button image view in simultaneously.
Example:
1. first time press the button 1 and change already the first time image view. (get work)
2. second time press the other button 2 and I want change image view for the both button 1 and 2 in simultaneously. But I can only get the ibutton as variable signal to change the button 2 image view and button 1 can't get.
Question:
1. How do I change the button image view for button 1 when I click the button 2?
2. How do I can keep the button variable in array?
My code like this:
public class CheckersTest extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final ImageView iv_new_game = (ImageView) findViewById(R.id.new_game);
iv_new_game.setOnClickListener(welcome_listener);
}
OnClickListener welcome_listener = new View.OnClickListener() {
public void onClick(View v) {
final ImageView iv = (ImageView) v;
if (iv.getId() == R.id.new_game) {
setContentView(R.layout.checkers_board);
final ImageButton b2 = (ImageButton) findViewById(R.id.imageButton2);
final ImageButton b4 = (ImageButton) findViewById(R.id.imageButton4);
final ImageButton b6 = (ImageButton) findViewById(R.id.imageButton6);
// set the OnClickListeners.
b2.setOnClickListener(button_listener);
b4.setOnClickListener(button_listener);
b6.setOnClickListener(button_listener);
// Re-enable the Click-able property of buttons.
b2.setClickable(true);
b4.setClickable(true);
b6.setClickable(true);
}
};
};
OnClickListener button_listener = new View.OnClickListener() {
public void onClick(View v) {
ImageButton ibutton = (ImageButton) v;
ibutton.setImageResource(R.drawable.green_bol);
}
};
In the second Button's onclick method get instance of first button and set image.

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