I am new to Android and Java but have managed to teach myself and find most answers to questions on stackoverflow without needing to ask questions. Until now....
Here goes, I have many colored buttons which, when clicked, change color to a range of different colors.
There are many buttons defined for example as:
<Button
android:id="#+id/button17"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/orange_button"
android:gravity="center"
android:onClick="onClick" />
Could someone please advise me how to change the android:background using code to change the above example to yellow, for example, when the button is clicked.
In the code below clickedButton is the ID of the button for which I need to change the background.
public void onClick(View v) {
int id=v.getId();
String clickedButton = getResources().getResourceEntryName(id);
Change button to Yellow here??
// Temporary code below to check which button was pressed
// and convert its number to an integer to eventually access an array
final TextView tvGameTimer = (TextView) findViewById(R.id.tvGameTimer);
int buttonNumber = Integer.parseInt(clickedButton.substring(6,8));
tvGameTimer.setText("" + buttonNumber);
}
I am using custom button styles to define the button colors:
res/drawable/yellow_button.xml
res/drawable/blue_button.xml
res/drawable/red_button.xml
res/drawable/orange_button.xml
res/drawable/green_button.xml
For now I just need to work out how to change the button from Orange to Yellow. I can then add the logic to change the colors as and when the app requires.
Many thanks for any help.
I am supposing that the onClick method you post is of the same Button whose background you are trying to change. Use this code.
v.setBackgroundResource(R.drawable.yellow_button);
If onClick is not the method of the same button then, use
findViewById(R.id.button17).setBackgroundResource(R.drawable.yellow_button);
Button btn = (Button) findViewById(R.id.button17);
btn.setBackground(this.getResources().getDrawable(R.drawable.yellow_button));
Try this.
Related
How to remove button background resource i refered this, But my need is to remove button background and that should change background as per deviceDefault theme. Means just removing the resource added last time, not assigning new Resource.
Can anybody help to solve this prob ? Thank You
try assigning background:#null in xml file
for programetically try layout.setBackgroundResource(0);
You can do this by changing the background resource. In the XML file, for the button's attributes.
<Button
...
android:background="#null" />
Should do it.
Instead of setting the background to #null you can add the style:
<Button
...
style="#style/Widget.AppCompat.Button.Borderless" />
If you're only doing this a few times, a simple way is to just save the previous background before altering it. You can store it in the tag field of the Button:
//store previous background drawable
myButton.setTag(myButton.getBackground());
// ... alter background, do whatever
//restore background drawable from tag
myButton.setBackground((Drawable)myButton.getTag());
your_view.setBackground(null);
removes the pre-defined background successfully.
Button b;
b=(Button)findViewById(R.id.button1);
b.setBackgroundResource(R.drawable.ic_launcher);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b.setBackgroundResource(android.R.drawable.btn_default);
}
});
this should help you
I am new to programming but had some spare time and just got a new android tablet so thought its time to learn. I play a board game that has MANY decks that you draw from throughout the course of the game and decided it would be nice to simply have an application showing the 21 decks and you click on one and it randomly displays a card from one of those decks. You read the card, act on it, click on the card and it disappears.
i thus have a layout with all 21 decks (7x3) each an individual button. Thus i have 21 buttons on the 1 screen. According to the tutorial i have been following i need to declare the buttons on the .java file button1 = (Button) findViewById(android.R.id.button1). but it only has the option to declare 3 buttons after which i get the little red x of doom.
How do i go about declaring all 21 buttons? Or do i not need to declare these buttons?
Any help would be great! (may also need help finding a way to randomize the "draw" feature so don't be surprised to see me again)
If you laid out each button in XML (main.xml or something similar), then yes, if you wish to have them do anything, you have to declare the buttons as you said.
Button button1 = (Button) findViewById(R.id.button1);
By typing it this way, I assume you didn't declare the buttons higher up in your code, as class-wide fields. Also, have you run the method setContentView(R.layout.main);?
So let's just be clear: unless you type Button b1; Button b2; Button b3 just below your class line (public class YourClassName() {, each time you try to instantiate a button, you have to say Button b1 = (Button) findViewById(R.id.button1);. If you did make class-wide fields (right below the class line) then you can have code like you showed in your original question, where it's just button1 = (Button) findViewById(R.id.button1). Does this distinction make sense?
You do need to define each button. Use the follow:
Button button1 = (Button)findViewById(R.id.idofbutton1);
Button button2 = (Button)findViewById(R.id.idofbutton2);
Button button3 = (Button)findViewById(R.id.idofbutton3);
Button button4 = (Button)findViewById(R.id.idofbutton4);
so forth so on
Also, if you wanted to do this inside a loop, you could do that too. Might make things a bit easier. Here's a link: https://stackoverflow.com/a/8687807/1231943
Anytime you declare a Button (which is an object):
Button button1 = (Button)findViewById(R.id.idofbutton1);
Button button2 = (Button)findViewById(R.id.idofbutton2);
Make sure you add the "id" to the XML layout:
<Button
android:id="#+id/idofbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:textSize="20px" >
</Button>
<Button
android:id="#+id/idofbutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:textSize="20px" >
</Button>
how to do the button with ON/Off by clicking it must varies and along with that it shows Green for ON else RED. this is possible without graphics or must need graphics?
any one tried like this plz help me?
ToggleButton does the same. And here is tutorial which will tell how to use this.
You can use ToggleButton
If you with to use Button instead, you can make field boolean m_isOn; in your class, and in OnClickListener check this field and set button color (e.g. with setColorFilter()) and text accordingly.
Edit
Small example, if you really wish to avoid ToggleButton and using drawables:
#Override
public void onClick(View v) {
m_isOn ^= true;
((Button)v).getBackground().setColorFilter(m_isOn ? 0xFF00FF00 : 0xFFFF0000, PorterDuff.Mode.MULTIPLY);
((Button)v).setText(m_isOn ? "ON" : "OFF");
}
use ImageButton instead of button, and selector as image.
this XML file you can put into drawable directory
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_prev_active" />
<item android:drawable="#drawable/button_prev_no_active" />
In layout do this:
<ImageButton android:id="#+id/btnPrev"
android:scaleType="fitXY"
android:padding="0px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="prevClickHandler"
android:src="#drawable/button_prev_selector"/>
I have the following code:
<TextView
android:text="Color Yellow"
android:textColor="#000000"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:clickable="True"
/>
The android:clickable="True" was added because I thought it needed to be there (please do correct me if I'm wrong). However, the answer I'm seeking right now is how do I go by making another box (filled with text) pop-up upon clicking the "yellow box".
I would be grateful if someone could provide me with ideas and/or hints regarding how to actually create this scenario.
The android:clickable element does what you think and what it name tells you. It allows you to receive click events for that view (TextView here) to act on these.
To create a popup, you have to assign something to that TextView that tells you when it actually gets clicked. That is a OnClickListener. You can do that either in code or partially in code and XML. I'll just focus on the code example, but for the record, the XML one is also pretty easy. It involves setting the android:onClick="myOnClick" attribute to a certain function name that you like ("myOnClick" here) and creating a function like public void myOnClick(View v) in your activity.
What you have to do in code is
Referencing the TextView that you have in your layout
Assign the OnClickListener
Write an action that will be executed once the click gets registered
First point: To reference your TextView you have to use findViewById
TextView myTextView = (TextView) findViewById(R.id.mytextviewid);
Note that you have to assign a ID to your TextView to identify it. You can set that id via the android:id attribute in your XML layout (e.g. android:id="#+id/mytextviewid").
Second point: Once you have the reference, use TextView.setOnClickListener() to register one.
This usually looks like this:
myTextView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Add an action here
}
});
Third point: All you have to do now is displaying your dialog/message insite the onClick() function. There is more than one way to display that, you may use a Toast or an AlertDialog. Check out the links, there are some examples for that.
The text on my buttons are set in resource files. E.g.
<Button android:id="#+id/up_button" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/up_label" />
The above code defines a button with text #string/up_label. How can I change this text in my program during the application is running.
Thanks.
Button myButton = (Button) findViewById(R.id.up_button);
myButton.setText("my text");
maybe this will help.
Or, if you have the new text in resources (which I think you do), you do
Button myButton = (Button) findViewById(R.id.up_button);
myButton.setText(R.string.down_label);
where down_label is the id of your string.
Button button = (Button) findViewById(R.id.up_button);
For example:
button.setText("This is how to change text at runtime");