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
}
Related
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 4 button seen below sequentially numbered in my layout file. In my code i want to get in a loop the advert that corresponds to the position we are in the loop e.g for the second time around in the loop i would get advert2 back.
Can anyone help. Thanks
Button advert1 = (Button) findViewById(R.id.advert1);
Button advert2 = (Button) findViewById(R.id.advert2);
Button advert3 = (Button) findViewById(R.id.advert3);
Button advert4 = (Button) findViewById(R.id.advert4);
Button[] btnArr= new Button[4];
int[] arr = new int[4];
arr[0]=R.id.advert1;
arr[1]=R.id.advert2;
arr[2]=R.id.advert3;
arr[3]=R.id.advert4;
for(int i=0;i<4+i++)
{
btnArr[i]=(Button) findViewById(arr[i]);
}
I would like to start different animations at the same time when the app starts. I added my code in the onWindowFocusChanged function for one animated button and I haven't problems. However, when I try to animate another button, the second one don't move, fixed on the first frame.
Anyone knows how can I get over?
Some piece of code:
In the OnCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButtonPlay = (Button) findViewById(R.id.ButtonPlay);
mButtonPlay.setBackgroundResource(R.drawable.button_play);
buttonAnimationPlay = (AnimationDrawable)mButtonPlay.getBackground();
Button mButtonOptions = (Button) findViewById(R.id.ButtonOptions);
mButtonOptions.setBackgroundResource(R.drawable.button_options);
buttonAnimationOptions = (AnimationDrawable)mButtonPlay.getBackground();
}
enter code here
And in the onWindowFocusChanged :
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
if (hasFocus)
{
buttonAnimationPlay.start();
buttonAnimationOptions.start();
}
else
{
buttonAnimationPlay.stop();
buttonAnimationOptions.stop();
}
}
Well, I got it finally! There were a mistake in the code, what a crap! This is the good one:
final Button mButtonPlay = (Button) findViewById(R.id.ButtonPlay);
mButtonPlay.setBackgroundResource(R.drawable.button_play);
buttonAnimationPlay = (AnimationDrawable)mButtonPlay.getBackground();
final Button mButtonOptions = (Button) findViewById(R.id.ButtonOptions);
mButtonOptions.setBackgroundResource(R.drawable.button_play);
buttonAnimationOptions = (AnimationDrawable)mButtonOptions.getBackground();
Anyway, I've also tried the programmatically add of views and, now, it also works. For anyone would use one or the other kind, this is the other one:
RelativeLayout Relative = (RelativeLayout) findViewById(R.id.RelativeForButtons);
RelativeLayout.LayoutParams relativeParamsPlay =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams relativeParamsOptions =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
relativeParamsPlay.addRule(RelativeLayout.BELOW,R.id.Title);
relativeParamsPlay.addRule(RelativeLayout.CENTER_IN_PARENT);
final Button mButtonPlay = new Button(this);
mButtonPlay.setBackgroundResource(R.drawable.button_play);
buttonAnimationPlay = (AnimationDrawable)mButtonPlay.getBackground();
mButtonPlay.setId(1);
mButtonPlay.setText("PLAY");
Relative.addView(mButtonPlay,relativeParamsPlay);
final Button mButtonOptions = new Button(this);
mButtonOptions.setBackgroundResource(R.drawable.button_options);
buttonAnimationOptions = (AnimationDrawable)mButtonOptions.getBackground();
mButtonOptions.setId(2);
mButtonOptions.setText("OPTIONS");
relativeParamsOptions.addRule(RelativeLayout.BELOW, mButtonPlay.getId());
relativeParamsOptions.addRule(RelativeLayout.CENTER_IN_PARENT);
Relative.addView(mButtonOptions,relativeParamsOptions);
All of that is in the OnCreate function. If you want to start animations, use the onWindowFocusChanged function as explained above.
Hope it helps! :)
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
private final Button[] BUTTONS = {
btn1, btn2, btn3,btn4
};
...
btn1 = (Button) this.findViewById(R.id.btn_1);
btn2 = (Button) this.findViewById(R.id.btn_2);
btn3 = (Button) this.findViewById(R.id.btn_3);
btn4 = (Button) this.findViewById(R.id.btn_4);
...
int n = BUTTONS.length;
for(int i=0; i<n; i++) {
if(DEBUG) Log.d(TAG, String.valueOf(i));
BUTTONS[i].setOnClickListener(this);
}
throws NullPointerException, whereas
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
works fine. Doesn't make any sense to me.
I think it's because your Buttons array is created when btn1,... are still null.
So when you call BUTTONS[i].setOnClickListener in the loop you are really saying null.setOnClickListener which will give an exception.
Try setting up the array as a variable and sey AFTER you've assigned btn1, etc.
Haven't tested it but something like this might work better...
private ArrayList mBtns = new ArrayList();
private void initButton(int id) {
button = (Button) findViewById(id);
button.setOnClickListener(this);
mBtns.add(button);
}
...
initButton(R.id.btn_1);
initButton(R.id.btn_2);
initButton(R.id.btn_3);
initButton(R.id.btn_4);
Also unless the buttons do very similar things you may find it better to simply define the onClick attribute on each in the layout and save yourself A LOT of coding (only available in Android 1.6 and higher).