Cannot convert from View to Button - android

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

Related

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

Finding id of a button

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;

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

Get programmatic set EditText value

Edit 4: I did my own numberpicker, so I don't need help with this anymore.
But I think the problem is that I didn't use dialog.findViewById()...
I'm trying to get a EditText value, that is created programmatic. But it don't work, when the onClick function run the code breaks.
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
numberValue = (EditText) findViewById(R.id.number_picker_value);
Log.d("****2**", numberValue.getText().toString());
dialog.dismiss();
}
});
The value and ID is set from another class, NumberPicker.java.
private void initValueEditText( Context context )
{
value = new Integer( 0 );
valueText = new EditText( context );
valueText.setId(R.id.number_picker_value); //ID set here
valueText.setTextSize(25);
...
//value set sooner, look in the link for full code.
}
Full source code of NumberPicker.java can be found here here.
The R.id.number_picker_value is defined in an XML file with
<item type="id" name="number_picker_value" />.
EDIT:
What I mean with "the code breaks" is that i get a Force close dialog.
EDIT2:
Logcat output. (Is it this you want?)
EDIT3:
Btw, I never call on NumberPicker.java. It start itself when I load the XML file with this bit of code:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content">
<!-- This seem to start NumberPicker.java, so I have no object to refer to. -->
<com.android.GissaSiffran.NumberPicker
android:id = "#+id/numberPickerDialog"
android:orientation = "horizontal"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:gravity = "center">
</com.android.GissaSiffran.NumberPicker>
<!-- Canel / ok button -->
<Button
android:id = "#+id/cnfrm"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:textSize = "13dp"
android:textStyle = "italic"
android:text = "#string/cnfrmBtn"></Button>
</LinearLayout>
Maybe I do some wrong here?
When I run dialog.setContentView(R.layout.pick_a_number_dialog); dialog.show(); the XML loads and the number picker is in the dialog and i can choose number.
But I have never started the NumberPicker.java with ie NumberPicker np = new NumberPicker(getApplicationContext(), null)); maybe I do wrong here? (I'm new to java).
Btw my first post here. :)
For future referencers, I'm using the same NumberPicker.java I would guess. In order to get the value of the EditText, I created a reference to the EditText in the OnCreateView method like this:
private EditText txtQty;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_numberpicker,container,false);
txtQty = (EditText) view.findViewById(idText);
return view;
}
Then in the onClick method:
String newOnHand = txtQty.getText().toString();

Categories

Resources