How do I get a component by referring its attribute?
Button attribute1 = "#+myAttributes/one"/>
//something like
Button Button1 = (Button) findViewByattribute1(R.myAttributes.one);
Try with the following code.
Button buttonName = (Button) findViewById(R.id.yourButton);
Related
I'm trying to create a tic tac toe game for practice using simple buttons that change their text to x or o. Once a winner is found, all buttons should be reset to have the text "..." instead of x/o. It feels stupid to be doing this for each button but I can't figure out how to iterate over them. The buttons are named button1, button2, ..., and button9. Right now I'm using the same 3 lines of code for each button and just repeating that and that's not very DRY-friendly.
Tried to do a for loop and string concatenation (sort of like: findViewById(R.id."button"+i), but that obviously doesn't work hahah). I'm sure this is a stupid question but I've had this problem a couple of times now and I really wanna figure out how to so this better. [Image of the App][1]
if(winnerFound){
// print winner on screen and reset game
resetGame();
}
}
public void resetGame(){
// set all buttons clickable again and set text to "..."
// could be done in a for loop but idk how
Button button1 = (Button) findViewById(R.id.button1);
button1.setText("...");
button1.setEnabled(true);
Button button2 = (Button) findViewById(R.id.button2);
button2.setText("...");
button2.setEnabled(true);
Button button3 = (Button) findViewById(R.id.button3);
button3.setText("...");
button3.setEnabled(true);
Button button4 = (Button) findViewById(R.id.button4);
button4.setText("...");
button4.setEnabled(true);
// ... (9 buttons in total)'''
[1]: https://i.stack.imgur.com/1ocbZ.jpg
Simple solution based on your code:
Create a member variable holding the list of your buttons List<Button> and iterate them setting the text to empty.
class MyActivity: Activity {
private val buttonList: MuttableList<Button>
private val button1: Button
private val button2: Button
fun onCreate(...) {
button1 = findViewById(R.id.button_01)
button2 = findViewById(R.id.button_02)
buttonList.add(button1)
buttonList.add(button2)
....
}
}
More consideration about your code:
You shouldn't use findViewById every time you want to use a button referente. It should be ideally only used once in your onCreate method of your activity(onViewCreated if it's a fragment) and set to a member variable .
Example
class MyActivity: Activity {
private val button1: Button
fun onCreate(...) {
button1 = findViewById(R.id.button_01)
}
}
I am trying to change the background image of an activity
on button click, but not being able to do so. Can you guys tell me how can I can do that?
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try
{
Activity activity = Selection.this;
//Drawable db = Drawable..createFromPath(R.drawable.love1);
// The below line gives error because setbackgroundImage
// is supposed to take a drawable object as an argument.
// Now what to do?
activity.findViewById(android.R.id.content)
.setBackground(Drawable.createFromPath("res/love1.jpg"));
// What to do to correct the above line?
mySong = MediaPlayer.create(Selection.this, R.raw.song1);
mySong.start();
}
catch(Exception ee){
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(ee.getMessage());
}
}
});
I tried with Color.RED using setBackgroundColor but that too is not working.
PS: I have not changed anything in the xml file for accomplishing this.
I think the easiest way is to use layout in your xml file for your activity.. for example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayoutID"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_img1" >
and then change it from your Activity when you want e.g. after the button click:
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linearLayoutID);
mLinearLayout.setBackgroundResource(R.drawable.background_img2);
Change setBackground(Drawable) to setBackgroundResource(R.drawable.filename)
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.LayoutID);
mLinearLayout.setBackgroundResource(R.drawable.image_name);
I have a edit_text field in activity_X.xml. I have a button B in activty_X.xml. On clicking button B i need to assign a value to edit_text field. How to do?
you should try something on your own then post any question if you are getting any problem in that...anyways here's how you can implement this
Button buttonOne = (Button) findViewById(R.id.button1);
buttonOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
EditText editText1 = (EditText) findViewById(R.id.textidinxml);
editText1.setText("Some text to show on edittext");
}
});
Well, the title says everything, but there's a little problem, I can't use a fragment or a loader, because I'm developing for android 2.3, so, how can I update just a part of the activity?
You can still use a fragment even in 2.3 by using the android-support-v4 jar
You can reference the views by ID. As an example to change a EditText you can:
EditText text = (EditText) findViewById(R.id.input);
test.setText("New Text");
Just change the cast to the proper type, so for a button:
Button btn = (Button) findViewById(R.id.button);
So for your code if you have a button with the xml element android:onclick="increment" you can have a function such as:
public void increment(View view) {
TextView number = (TextView) findViewById(R.id.number);
number.setText(Integer.toString(Integer.parseInt(number.getText()) + 1));
}
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