Finding id of a button - android

I have a button in my android app. Its reference is button1i.e.
button1 = (Button) findViewById(R.id.tv6);
In some function, I receive a string with value button1. Using this string I need to get the id of the above button. How to do that.

use following:
int resID = getResources().getIdentifier(idName, "id", getPackageName());
By this method you would get id, and by id you can get View.

use this one
Button mButton = (Button)findViewById(R.id.button1);

Having button in xml file as:
<Button android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello"/>
You can get id of it as:
Button button=(Button) findViewById(R.id.button1);

It's just this:
int buttonId = R.id.button1;

Related

Changing background image on button click android app development

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

How to manipulate the contents of a button without using id; Android?

I'm new in android world and I have a problem, well, I'm making a project very simple it is about an activity where i have a button and an EditText. The button has an event onClick in XML.
My problem is it: I need the button value and send this value to
EditText but my button don't have a id. Help me I don't know how manipulate a element if it dont have a id.
XML Code:
<View
android:layout_height="#dimen/cell_height"
android:background="#color/red"/>
<Button
android:layout_marginLeft="#dimen/button_margin"
android:layout_gravity="center_vertical"
android:text="#string/hex_red"
android:textColor="#color/red"
android:onClick="copy"/>`
Java code:
public void copy(View boton){
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
String color = boton; <-- here need the button value
txtSelected.setText(color);
}
I need your help, Thanks
you can say boton.getText().toString()
Modify your copy() function like this:
public void copy(View boton) {
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
Button btn = (Button) boton; // << key point.
String color = btn.getText().toString();
txtSelected.setText(color);
}`
public void onClickBtn(View view) {
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
Button btn = (Button)(view);
String value = (String) btn.getText();
txtSelected.setText(value);
txtSelected.setSelection(value.length()); // cursor will be at the end of the text
}

How do I add a custom propperty on a button and retrieve it when clicked in android?

I have a couple buttons that are hard coded that will need to store some specific data and provide that attribute when clicked such as, one button is "non-fiction" and the other "fiction". I need to use an attribute and not the button text since the button text may change down the line but the attribute is need for database calls.
That is, "non-fiction" could become "true stories" but "non-fiction" will still need to be returned.
I've done something similar programmatically with btn.setTag(...) and btn.getTag(...) but those buttons are generated based on the database not hard coded into the app.
How do I set a custom attribute to a button then retrieve it?
something like:
<Button
android:id="#+id/fictionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_weight="1"
android:onClick="showTools"
android:text="#string/fiction_button"
custom:bookType="fiction" />
<Button
android:id="#+id/nonfictionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_weight="1"
android:onClick="showTools"
android:text="#string/non_fiction_button"
custom:bookType="nonfiction" />
----- edit -----
I've changed my approach based on the answer so far.
I've set before the onCreate:
Button fictionButton;
Button nonfictionButton;
Inside the onCreate I've placed:
fictionButton = (Button) findViewById(R.id.fictionButton);
nonfictionButton = (Button) findViewById(R.id.nonfictionButton);
fictionButton.setTag("bookType","Fiction");
nonfictionButton.setTag("bookType", "Non Fiction");
when the button is clicked I'm getting the tag and storing to a SharedPreference
However now I'm getting an error at fictionButton.setType("bookType","Fiction"); ADT doesn't like the key and wants to remove it.
----- edit -----
The set tag is working but now the getTag is throwing the NullPointerException. I'm using Button b to target all buttons and attempting to get the tag when any button is clicked inside the onClick event. buttonID is initialized before the onCreate and declared as R.id.fictionButton in the onCreate:
b = (Button) view;
String buttonText = b.getTag(buttonID).toString();
----- edit -----
my java file before and with the onCreate:
public class Crossroads extends baseActivity {
FlyOutContainer root;
Button b;
Button fictionButton;
Button nonfictionButton;
Integer buttonID;
public static final String PREFS_NAME = "myPrefs";
SharedPreferences storedInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.root = (FlyOutContainer) this.getLayoutInflater().inflate(
R.layout.activity_crossroads, null);
setContentView(R.layout.activity_crossroads);
buttonID = R.id.fictionButton;
fictionButton = (Button) findViewById(R.id.fictionButton);
nonfictionButton = (Button) findViewById(R.id.nonfictionButton);
fictionButton.setTag(buttonID,"Fiction");
nonfictionButton.setTag(buttonID,"Non Ficiton");
this.setContentView(root);
}
...
}
----- edit -----
Fixed the final piece by comparing ids and setting the string accordingly:
Integer viewId = view.getId();
String buttonText;
// setContentView(R.layout.activity_crossroads);
if(viewId == R.id.fictionButton )
buttonText = fictionButton.getTag(buttonID).toString();
else
buttonText = nonfictionButton.getTag(buttonID).toString();
i think your main problem is that you app won't compile
because you have written:
setTag(String, String);
but the setTag(..) function doesn't take an String as key but an int
so you could do like this:
public static int KEY_BOOK_TYPE = 0; //some int
fictionButton = (Button)findViewById(R.id.fictionButton);
nonfictionButton = (Button)findViewById(R.id.nonfictionButton);
fictionButton.setTag(KEY_BOOK_TYPE, "Fiction");
nonfictionButton.setTag(KEY_BOOK_TYPE, "Non Fiction");
setType(String, String) doesn't exist in Button and so ADT wants to remove that line
--- edit ---
be careful and call:
setContentView(R.layout.your_layout);
before calling any findViewById(R.id.something) else it returns null
--- edit ---
if you call setTag(...) with the button id too, thats really good idea!
but you cannot simply cast any view to the button you want
you have to save the button you got from findViewById like this:
class MainActivity extends Activity
{
int non_fiction_id;
int fiction_id;
Button button_non_fiction;
Button button_fiction;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
non_fiction_id = R.id.button_non_fiction;
fiction_id = R.id.button_fiction;
button_non_fiction = (Button)findViewById(non_fiction_id);
button_fiction = (Button)findViewById(fiction_id);
}
void SomeOtherPlace()
{
String non_ficiton_tag = button_non_fiction.getTag(non_fiction_id);
}
}

how to find a view by a custom made attribute in android

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

Cannot convert from View to Button

Very frustrating problem I have here. I have this code:
Button b = findViewById(android.R.id.button1);
And I'm getting this error on it:
Type mismatch: cannot convert form View to Button
But button1 is a button!! In my XML layout document the button has been declared like this:
<Button
android:id = "#+id/button1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Next Activity"
/>
And in my R.java:
public static final class id {
public static final int button1=0x7f050000;
}
Why I get and error saying that my button is a view when it actually is indeed a button... is a mystery.
You need to cast the view to Button:
Button b = (Button) findViewById(android.R.id.button1);
More details at http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
In addition, as answered by others, the id is wrong.
remove android.R from packages and import your R.
import com.companyname.productname.R;
and change also button refrence
Button b = (Button)findViewById(R.id.button1);
^^^^^^^^^^^^
Your mistake is here-Button b = findViewById(android.R.id.button1);
Replace the above line by findViewById(R.id.button1);

Categories

Resources