not loading button properly - android

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);
}

Related

I have multiple checkboxes, but only want one of them to get clicked at a time

I am just trying to make my application so the user can only click on one checkbox at a time. So pretty much when the user clicks on one checkbox the other 2 become false. I have tried a few things, but nothing really seems to be working, and I can't find anything about it online. Thanks-
Here is my code...
public void buttonClick() {
imgView = (ImageView) findViewById(R.id.imageView);
button = (Button) findViewById(R.id.button);
blade = (ImageView)findViewById(R.id.imageView4);
final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
standardSound = MediaPlayer.create(this, R.raw.file.mp3);
alternateSound = MediaPlayer.create(this, R.raw.file.mp3);
whiteSound = MediaPlayer.create(this, R.raw.file.mp3);
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean alternate = getPrefs.getBoolean("alternate", false);
boolean white = getPrefs.getBoolean("white", false);
boolean standard = getPrefs.getBoolean("standard",false);
if (blade.getAnimation() == null) {
// no animation, start it
if (alternate == true){
alternateSound.start();
blade.startAnimation(animRotate);
} else if (white == true){
whiteSound.start();
blade.startAnimation(animRotate);
} else if (standard == true) {
standardSound.start();
blade.startAnimation(animRotate);
}
} else {
//animation is showing, stop it
blade.clearAnimation();
standardSound.stop();
standardSound.prepareAsync();
whiteound.stop();
whiteSound.prepareAsync();
alternateSound.stop();
alternateSound.prepareAsync();
}
current_image_index++;
current_image_index = current_image_index % images.length;
imgView.setImageResource(images[current_image_index]);
imgView.invalidate();
}
}
);
}
And my xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Sound Settings">
<CheckBoxPreference
android:key="standard"
android:title="Standard Fan"
/>
<CheckBoxPreference
android:key="alternate"
android:title="Alternate Fan"
/>
<CheckBoxPreference
android:key="white"
android:title="White Noise"
/>
</PreferenceCategory>
</PreferenceScreen>
There are a windget called RadioButton which it does this function.
Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side.

getText().toString().trim().length() == 4 not working

I am Checking if my EditText has text inside of it, i am using this code for that to work:
if (mSwitcher.getText().toString().trim().length() == 4) {
final Button button2 = (Button) findViewById(R.id.button2);
final Animation buttonz = new AlphaAnimation(0.0f, 1.0f);
buttonz.setDuration(3000);
button2.startAnimation(buttonz);
} else {
}
It does not give any errors but it shows up when I open the activity but i dont want that i want it to happen when there is text in mSwitcher (Which is an EditText).
I hope someone can help me.
Since you are just checking if your EditText has string or not, why dont you use.
if(mSwitcher.getText().toString().isEmpty())
{
....
...
}
More over it becomes
if (mSwitcher.getText().toString().isEmpty()) {
final Button button2 = (Button) findViewById(R.id.button2);
final Animation buttonz = new AlphaAnimation(0.0f, 1.0f);
buttonz.setDuration(3000);
button2.startAnimation(buttonz);
} else {
}
I do not recommend to use
final Button button2 = (Button) findViewById(R.id.button2);
final Animation buttonz = new AlphaAnimation(0.0f, 1.0f);
inside if statement.
BTW is mSwitcher an EditText ??
According to the known new requirement.
if(mSwitcher.getText().length()!=4)
{
myButton.setEnabled(false);//change to your button name
}

how to input a number value once a button is pressed to a edit text box

Hi there I have 10 buttons numbered from 0 - 9 and i want to be able to assign those values to the buttons and input that value into the edit text box once i have pressed it.
So far I've created the buttons and set on click listeners to them.
Thank you in advance for any help. :)
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.eigth);
Button q81 = (Button) findViewById(R.id.eightb1);
Button q82 = (Button) findViewById(R.id.eightb2);
Button q83 = (Button) findViewById(R.id.eightb3);
Button q84 = (Button) findViewById(R.id.eightb4);
Button q85 = (Button) findViewById(R.id.eightb5);
Button q86 = (Button) findViewById(R.id.eightb6);
Button q87 = (Button) findViewById(R.id.eightb7);
Button q88 = (Button) findViewById(R.id.eightb8);
Button q89 = (Button) findViewById(R.id.eightb9);
Button q80 = (Button) findViewById(R.id.eightb0);
EditText editText = (EditText) findViewById(R.id.editText1);
editText.getText();
q81.setOnClickListener((OnClickListener) this);
q82.setOnClickListener((OnClickListener) this);
q83.setOnClickListener((OnClickListener) this);
q84.setOnClickListener((OnClickListener) this);
q85.setOnClickListener((OnClickListener) this);
q86.setOnClickListener((OnClickListener) this);
q87.setOnClickListener((OnClickListener) this);
q88.setOnClickListener((OnClickListener) this);
q89.setOnClickListener((OnClickListener) this);
q80.setOnClickListener((OnClickListener) this);
Implement onClick() method to do the following:
Get the character that was clicked.
Get the current text in the text field.
Append the clicked character.
Set the text field's string to the new string value.

.setEnabled only seems to be able to be toggled once

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);
}

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