Is there a reason that .setEnabled() only seems to work once? I wish to toggle it on and off multiple times during an activity lifecycle depending on the content. i've tried wrapping it in switch statements.
GAME_STATE_INPLAY = true;
if (GAME_STATE_INPLAY = true) {
explainButton.setEnabled(false);
}
....
if (c.getString(7).toString().length() > 0) {
explainButton.setEnabled(true);
}
try to get the btn by id again:
if (GAME_STATE_INPLAY = true) {
Button explainButton =(Button) findViewById(R.id.button);
explainButton.setEnabled(false);
}
if (c.getString(7).toString().length() > 0) {
Button explainButton =(Button) findViewById(R.id.button);
explainButton.setEnabled(true);
}
Related
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!
Hi can anyone help me please I'm having a hard time making this work and been trying so many ways to make this work but everytime I run this code it only shows the beginner1Check even if I turn the boolean to true. can anyone help me please?
here are my codes
.java
public void initialize(){
SharedPreferences beginner1Prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean beginner1_pref = beginner1Prefs.getBoolean("Beginner1", false);
if (beginner1_pref == false){
beginner1Check = (Button) findViewById(R.id.btnBeginner1Check);
beginner1Check.setOnClickListener(myOnlyhandler);
} else if (beginner1_pref == true){
beginner1 = (Button) findViewById(R.id.btnBeginner1);
beginner1.setOnClickListener(myOnlyhandler);
}
btnLogo = (Button) findViewById(R.id.btnLogo);
beginner2 = (Button) findViewById(R.id.btnBeginner2);
beginner3 = (Button) findViewById(R.id.btnBeginner3);
beginner4 = (Button) findViewById(R.id.btnBeginner4);
beginner5 = (Button) findViewById(R.id.btnBeginner5);
beginner6 = (Button) findViewById(R.id.btnBeginner6);
beginner7 = (Button) findViewById(R.id.btnBeginner7);
beginner8 = (Button) findViewById(R.id.btnBeginner8);
beginner9 = (Button) findViewById(R.id.btnBeginner9);
btnLogo.setOnClickListener(myOnlyhandler);
beginner2.setOnClickListener(myOnlyhandler);
beginner3.setOnClickListener(myOnlyhandler);
beginner4.setOnClickListener(myOnlyhandler);
beginner5.setOnClickListener(myOnlyhandler);
beginner6.setOnClickListener(myOnlyhandler);
beginner7.setOnClickListener(myOnlyhandler);
beginner8.setOnClickListener(myOnlyhandler);
beginner9.setOnClickListener(myOnlyhandler);
}
xml > prefs.xml
<CheckBoxPreference
android:title="Beginner1"
android:defaultValue="false"
android:key="Beginner1"
android:summary="Beginner1" />
The problem is that you haven't made the second Button invisible. You have just initialized the first Button but your second Button obviously exists in your layout. It won't hide unless you explicitly hide it. Modify your code like this:
if (beginner1_pref == false){
beginner1Check = (Button) findViewById(R.id.btnBeginner1Check);
beginner1 = (Button) findViewById(R.id.btnBeginner1);
beginner1.setVisibility(View.INVISIBLE);//You can replace it with VIEW.GONE depending on your needs.
beginner1Check.setOnClickListener(myOnlyhandler);
} else if (beginner1_pref == true){
beginner1 = (Button) findViewById(R.id.btnBeginner1);
beginner1Check = (Button) findViewById(R.id.btnBeginner1Check);
beginner1Check.setVisibility(View.INVISIBLE);
beginner1.setOnClickListener(myOnlyhandler);
}
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.
I am currently creating an android application with different options. One of the option would be to have a button that would show "Activate" as default. When the application would be running, clicking on it would change it to "Disable" and then to "activate" if clicked again. I believe that all I have to do is to .getText with a string variable then use this variable in a if statement but it seems like it is not reacting to any of my conditions...
final Button button = (Button) findViewById(R.id.bSensor);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String buttonText = button.getText().toString();
if (buttonText == "#string/Disable") {
button.setText(R.string.Enable);
}
else if (buttonText == "#string/Enable"){
button.setText(R.string.Disable);
}
}
});
Thanks for help
Phyzikk
You shouldn't use the == operator when comparing strings in Java. Source
You should either use the .equals() method of the string, or alternatively you could keep a global boolean state flag to determine which value is set. This way you won't need to do a string compare every time you need to figure out if it's active or disabled.
Use .equals to compare strings. You wont need the #String/ prefix as this is not part of what the button displays.
final Button button = (Button) findViewById(R.id.bSensor);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String buttonText = button.getText().toString();
if (buttonText.equals(getResources().getText(R.string.Disable)) {
button.setText(R.string.Enable);
}
else if (buttonText.equals(getResources().getText(R.string.Enable)){
button.setText(R.string.Disable);
}
}
});
I am trying to change the background image of a button that is clicked. The button whose image I am trying to toggle is the same button that is clicked. I ultimately want the program to test the current background image and change it to the other picture given the result of the test.
final Button testButton = (Button) findViewById(R.id.buttonTestButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (testButton.equals(getResources().getDrawable(R.drawable.fakepicture))) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
}
}//end void onClick
});//end test button on click listener
try
testButton.getBackground().equals(getResources().getDrawable(R.drawable.fakepicture));
However ToggleButton might suit your case better.
As others have said, your equals method is comparing the button itself with the image, but you need to compare the background drawables.
I recommend loading the images drawables you want to use and then using their references later to make things more clear, something like this:
final Drawable first = getResources().getDrawable(
android.R.drawable.arrow_up_float);
final Drawable second = getResources().getDrawable(
android.R.drawable.arrow_down_float);
final Button testButton = (Button) findViewById(R.id.toggleButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (testButton.getBackground().equals(first)) {
testButton.setBackgroundDrawable(second);
} else {
testButton.setBackgroundDrawable(first);
}
}
});
as the other friends answered , it is preferable to use the ToggleButton in Android ,
and in your case, if you want to keep your code , so your method should be like this :
final Button testButton = (Button) findViewById(R.id.buttonTestButton);
int status = 0;//GLOBAL VARIABLE : the status of the Button ( 0 or 1 )
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (status == 0) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
status=1 ; // change the status to 1 so the at the second clic , the else will be executed
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
status =0;//change the status to 0 so the at the second clic , the if will be executed
}
}//end void onClick
});//end test button on click listener
You can simply use ToggleButton: Android ToggleButton and use StateList for the changing of the background: StateList using the check attribute.
You can use Buttons or Image buttons..
private ImageButton mod1,mod2;
mod1 = (ImageButton) findViewById(R.id.mod1);
mod2 = (ImageButton) findViewById(R.id.mod2);
mod1.setOnClickListener(this);
mod2.setOnClickListener(this);
public void onClick(View v) {
mod1.getDrawable().clearColorFilter();
mod2.getDrawable().clearColorFilter();
switch (v.getId()) {
case R.id.mod1:
mod1.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
break;
case R.id.mod2:
mod2.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
break;
}
}