Change background of all buttons - android

I would like to change the background of all the Buttons in a View.
android:background="#drawable/button_red"
And i would like to do this in an OnClick() event.
android:onClick="ChangeCouleur"
I would like to do this in a foreach loop, but i am not sure how to do this.
For example:
for( b in ... )
if (b.getid()!=idofthebutton)
b.setbackgroud(button_red)
Thanks for any help!

Put all your Buttons in an Array of Buttons, then cycle to it an change the background.
Button button1 = (Button)this.findViewById(...);
Button button2 = (Button)this.findViewById(...);
Button button3 = (Button)this.findViewById(...);
Button[] buttons={button1, button2, button3};
for (Button currentButton : buttons) {
currentButton.setBackgroundResource(R.drawable.my_new_background);
}

One thing you could do is subclass Button, then make all the buttons in you app instances of your new class. That way, if you decide that you want to adjust the color or change something else, you only have to do it once, and it will change all the buttons in your app. Here's a question that should give you some pointers on how to do that.

For changing the background of the clicked button
public void changeColor(View v) {
v.setBackground(btn_red);
}

You will have to get a reference to all the Buttons. Add them to an ArrayList or something similar. In your ChangeCouleur method use a loop to iterate through all the buttons changing the color of each one.

Create .XML file in your “res/drawable/” and use selector attribute in it.And use different images for the button.Please refer this link.
http://www.mkyong.com/android/android-imagebutton-selector-example/
This will clear your idea. :)

Related

RadioButton with more than text

I'm trying to create a RadioGroup with RadioButtons that have more than text only.
The image below will make things easier to understand.
I want the RadioButton to be selected when the user clicks somewhere in the layout around it.
I've tried do do somthing lik this:
<RadioGroup>
<RelativeLayout>
<RadioButton>
<...other stuff>
</RelativeLayout>
</RadioGroup>
But that didn't work.
Does somebody know how to easily get the requested layout and behaviour? Thanks
EDIT:
I forgot to mention that I don't want to use a list. I want all items to be visible because there are maximum three. Also, the container is a ScrollView
As Der Golem said you can use ListView and set it to be in a single choice mode: list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
But if you need only few layouts you can register Click listener to your layouts and call onClick events on RadioButtons:
bigLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
radioButton.performClick();
}
});
Hope it will help

How can I change the name of buttons on click of a button?

In my calculator app, I want an Inverse button, which when clicked changes the text of other buttons. Like sin to sin inverse etc. Is this possible?
You can just change the text of a button in that button onclick event.
Say For example
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(btn.getText().toString().trim().equals("sin")){
btn.setText("sin inverse");
}else if(btn.getText().toString().trim().equals("sin inverse")){
btn.setText("sin");
}
}
});
I think this will help you
While the other answers are completely right showing you to how rename the buttons, I suggest another solution with a cleaner design: Have different buttons for "sin" and "sin inverse", and just make them visible/invisible when clicking the "Inverse" button. That way you can write clean click handlers and don't have to use a lot of "if (isInverseMode()...)".
To make that work correctly, you just declare some additional buttons for the inverse operations in your XML layout file and set them to android:visibility="gone".
If you then set one the visible buttons to invisible and the next insivible button besides it to visible in the code, then the effect for the user looks like you exchanged one button by the other (so he only notices the text of the button changing).
It's possible.
Just re-set the button text in your onClick method for the button.
buttonId.setText("Your button text");
From what you're saying though it sounds like you want to change the buttons function as well as the text... in which case you need to put an if statement in your onClick method to handle the two button states.
Setting the text on a button would help anytime.
This code may help you..
final Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setText(new StringBuilder(btn.getText().toString().trim()).reverse());
}
});

How to bring an image to front?

I'm translating an image from somewhere to another, and where it goes there is a button. But i want it to be unclickable when the image's over it! It's not seen but still can be clicked! How can i bring the image to front, so the button will not be worked? Thanks in advance.
Implement setAnimationListener in your translate animation and in onAnimationStart(Animation animation) put your bringToFront() method. It does work for me.
Try this:
Button btn = (Button)findViewById(R.id.my_btn);
btn.setEnabled(false);
I think it could help if you make that the ImageView clickable.
This could be achieved by:
Setting the clickable attribute (http://developer.android.com/reference/android/view/View.html#attr_android:clickable) to true in the layout XML
Invoking setClickable(true) (http://developer.android.com/reference/android/view/View.html#setClickable%28boolean%29) on the ImageView object in your code
Change the order of your xml file. The Views that are listed first will be behind the Views that are listed after them.
<ImageView android:id="#+id/iv01"/>
<Button android:id="#+id/btn01"/>
In the code above the button will be placed on top of the ImageView.
<Button android:id="#+id/btn01"/>
<ImageView android:id="#+id/iv01"/>
In the code above the ImageView will be placed on top of the Button.
You can also hide the Button from the layout in your .java (Activity) file.
Button btn = findViewById(R.id.btn01);
btn.setVisibility(View.GONE);
Also you can set the Butt

how to zoom-in interface? Android development

In my Android application, I have an activity with three layouts: A left layout, a middle layout, and a right layout.
The right layout is the main one. I want the right layout to zoom into fullscreen when I click a button. If you have the specific code,it's better.
Thanks very much!
If I got your question right:
Your button's OnClickListener should look like something like this:
OnClickListener listener = OnClickListener() {
public void onClick(View v){
findViewById(R.id.left_layout).setVisibility(View.INVISIBLE);
findViewById(R.id.center_layout).setVisibility(View.INVISIBLE);
};
This by itself will not get your right layout to fullscreen. You'll need to add something the below to your right layout for it to "auto fullscreen":
android:layout_width = "0dp";
android:layout_weight = "1";
if you don't do that, you'll have to resize the right layout from the "listener" above.
Hmm, the question is vague, but if I understand correctly, what you want to do is hide the left and center layouts when a button is clicked, so that the right layout becomes full-screen?
Without more details, it's not easy to give you a more precise answer (please post your current layout), but I would do something like:
// this code in your Activity:
// this method is bound to button onCLick (android:onClick="clickButton")
public void clickButton(View view) {
findViewById(R.id.left_layout).setVisibility(View.INVISIBLE);
findViewById(R.id.center_layout).setVisibility(View.INVISIBLE);
}
And to revert back to a 3-layout display, you do the opposite (View.VISIBLE)

Can I deactivate buttons/edittexts from my layout? (visible but deactivated)

I have a layout with a spinner and a lot of textboxes and buttons.
I need to make appear some elements of the layout when one of the options of the spinner is selected and to make disappear when other is selected. ----> PROBLEM SOLVED!!!
But also, I need to deactivate some elements of the layout. I mean that they have to be visible, but the user can't press buttons or edit edittext's
Yes, you can.
Assuming you have a handle to your layout you can do smth like this:
yourLayoutToShow.setVisibility(View.VISIBLE)
or
yourLayoutToHide.setVisibility(View.GONE)
UPDATE:
To get a handle to your layout (the one you want to show/hide dynamically) you need to do smth like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
LinearLayout viewToShowOrHide =
(LinearLayout) findViewById(R.id.view_to_show_or_hide);
}
You can disable a control by doing ...
myControl.setEnabled(false);

Categories

Resources