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
}
Related
Am creating a calculator,i want when button is pressed for example say 5,how to set value 5 to the button and if that button is clicked . The value should be printed in edit text!!
My source code:
ed=(EditText)findViewById(R.id.editText);
b=(Button)findViewById(R.id.button);
gt=Double.parseDouble(ed.getText().toString());
ed.setText(""+gt);
}
If I understand your question correctly:
U got some buttons with text or digits.
And if someone presses one of those buttons u want to display the text of the button in your editText box?
If so u can do something like the following:
EditText myEditText = (EditText) findViewById(R.id.editText);
Button myButton0= (Button) findViewById(R.id.button0);
myButton0.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
//Get the button text and display it in the editText
//This will replace all text in the editText box
myEditText.setText(myButton0.getText().toString());
//Or (Thanks to #cherry-wave)
//This will append the text, instead of replacing
myEditText.setText(myEditText.getText() + myButton0.getText().toString());
}
});
Or (to handle all your buttons with only one method):
Add the following line to all your buttons in your layout xml:
android:onClick="onClick"
And place the following method in your activity class file:
//Your onClick method
public void onClick(View v) {
Button myButton = (Button)v;
//Don't forget to declare your edittext
//This will replace all text in the editText box
myEditText.setText(myButton.getText().toString());
//Or (Thanks to #cherry-wave)
//This will append the text, instead of replacing
myEditText.setText(myEditText.getText() + myButton.getText().toString());
}
Hope this helps u out.
Take the value from button and add to edittext .
Just like this .
editText.setText(btnFive.getText().toString());
Before button click get value from editText .
String edtValue = editText.getText.toString();
Than set editText.setText(edtValue+btnFive.getText().toString());
I have MainText1 displays a text, when you click on button 3 in the Menu MainText1 displays text3 which is coming from the super class Text. What I want is that dynamically when you click on any button it reads the number and displayed the respective text, that's all about. ;)
I want to get rid of switch case in my activity, so I'm trying now for 2days :( to change the name of the string variable dynamically, but I think I'm using a wrong code as string variable is different from resources (confused), here's my code, this really challenging me this weekend:
public class MainText1 extends Text {
String
tx1=text1,tx2=text2,tx3=text3,
tx,stringReceived;//text1,text2...textn strings coming from the Super class Text
num = Integer.parseInt(getIntent().getStringExtra("somekey1")); // this data is coming from the menu, it depends on which button is clicked
tx="text"+num; // text is the name of the string variable, it should be in format like that : text1,text2,...textn which have predefined string content
stringId = getApplicationContext().getResources().getIdentifier(tx, "string", getPackageName());
if (stringId > 0) {
stringReceived=getApplicationContext().getResources().getString(stringId);
I guess what you're trying to do is to change a TextView contents. So, you could do the following:
public YourActivity extends Activity {
//Here you will declare your layout items
private Button button1;
private Button button2;
private Button button3;
private TextView txtView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//Here you will get you layout elements
button1 = (Button) findViewById(R.id.button1_id);
button2 = (Button) findViewById(R.id.button2_id);
button3 = (Button) findViewById(R.id.button3_id);
txtView = (TextView) findViewById(R.id.txtview_id);
//Now you will have to set the onClickListeners
button1.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button1
txtView.setText(getResources()
.getString(R.string.button1_string);)
}
});
button2.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button2
txtView.setText(getResources()
.getString(R.string.button2_string);)
}
});
button3.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button3
txtView.setText(getResources()
.getString(R.string.button3_string);)
}
});
}
}
This should do the trick. Although, like the other guys suggested it, you should take a look at some tutorials before coding on
I got null data while fetch data from text Box.
My Code is:
EditText msg=(EditText)findViewById(R.id.Et_Msg);
String setMsg=msg.getText().toString();
Log.v("Messge","Message::"+setMsg);
Please tell me where I am wrong ?
This is your code,
EditText msg=(EditText)findViewById(R.id.Et_Msg);
String setMsg=msg.getText().toString();
Log.v("Messge","Message::"+setMsg);
The first line is initializing the EditText. When it does by default there is no value ( string ) in the edittext.
In the second line you are trying to fetch a string from a blank edittext, that's why it is giving you NullPointerException.
Solution : I suggest you to move your line String setMsg=msg.getText().toString(); to somewhere else where you are actually going to use the value of EditText.
While getting your data from EditText, you must create a listener orelse you get the value as null for an example button click listener..
For an example:
public class A extends Activity implements OnClickListener{
Button btn;
EditText edt;
#Override
public void onCreate(Bundle saved){
super.onCreate(saved);
edt = (EditText) findViewById(R.id.your_id);
btn = (Button) findViewById(R.id.your_id);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v){
if(v == btn){
String setMsg=edt.getText().toString();
Log.v("Messge","Message::"+setMsg);
}
}
}
see.. what you are doing.. immediately after obtaining and EditText's object you are calling getText() on it.. think logically.. obviously there is nothing (it should return blank though not sure why it is returing null) in the EditText unless you have it from the xml.. something like this;
<EditText
...
android:text="Hey there"
...
/>
try this.. or move getText() call under some button click..
Please replace your below line
String setMsg=msg.getText().toString();
with
String setMsg = String.valueOf(msg.getText());
Please try above line. Your problem will be solved.
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);
}
}
I am having EditText and using the numbers inside the EditText for my variables.
I am also having two buttons, one to increase the number in EditText by one and another to decrease it by one.
Can somebody tell me the code to make this possible?
Use the following line in the xml of EditText for setting the input type as number :
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="200"
android:id="#+id/editText1"
android:layout_alignParentLeft="true">
</EditText>
And in source file use the following code to decrease the number by 1:
final EditText ed=(EditText)findViewById(R.id.editText1);
Button b1=(Button)findViewById(R.id.button01);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int a=Integer.parseInt(ed.getText().toString());
int b=a-1;
ed.setText(new Integer(b).toString());
}
});
Similarly add one more button in the xml to increase the number by one.
You need to create a on click listener for each button, doing something like:
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText)findViewById(R.id.edit_text);
int newVal = ... //retrieve the previous val and increment it (or decrement it)
editText.setText(newVal, TextView.BufferType.EDITABLE);
}
});
Simply you have to use the click event of both the buttons, on increase button get the text in edittextbox and increment it and set it in the edittextbox, same way do for decrement button also and decrement the value.
button.setOnClickListener(new OnCLickListener(){
#Override
public void onClick(View arg0) {
if(arg0.equals(button1))
{
String s = editText.getText().toString();
Integer i = Integer.parseInt(s);
i=++i;
s = s.valueOf(i);
editText.setText(s);
}
if(arg0.equals(button2))
{
//decrement
}
}
})