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
}
}
})
Related
I create an application in android studio and I need advice, I got one button, and I need to change the text on the second button clicks through to the first. I have a code that changes only TextView but not the text on the button.
NewText = (TextView)findViewById(R.id.textView1);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
final TextView finalNewText1 = NewText;
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Set Text on button click via this function.
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
Same concept as you did for textView
Button SecondButton,ChangeText; // declaring the buttons
SecondButton = (Button)findViewById(R.id.button2);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//This changes the text on the second button
SecondButton.setText("New Text Here");
}
});
SecondButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do anything
}
});
Button ChangeText;
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//part to change the button text
Button tmp_button = (Button)findViewById(R.id.ch_txt_ger);
tmp_button.setText("Frohe Weihnachten");
//part to change the textview text
TextView NewText
NewText = (TextView)findViewById(R.id.textView1);
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
After Clicking outlooking
Here you go: You can define a temporary button variable and make the change on it if setting the same button on its own clicking is causing problems.
And if the text will not change according to user, and if you know it like On/OFF, Red/Green you can also code it with a selector file which would make the java code look more clean.
A tiny advise: Defining the TextViews and Buttons that will get affected should all be written in the same function and close to the place where they are being changed for you to keep track of where you coded them.
I would add one thing, in case if you want to save the new button name when you close and reopen your app, you could use Shared Preferences: https://developer.android.com/training/basics/data-storage/shared-preferences.html
I have 4 edittext and 1 button.When i clicked Button then Cursor will go Blank Edittext in next Frame.
Please guide me the correct way to achieve my objective.
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
groupno =(EditText)findViewById(R.id.editText1);
start=(EditText)findViewById(R.id.editText3);
end=(EditText)findViewById(R.id.editText4);
groupno.setEnabled(false);
start.setEnabled(false);
end.setEnabled(false);
}
}
Add this inside the setonlick method.U can use this code to move any edittext.
nextEdittext.requestfocus();
use this code :
use outside button click --
EditText.setFocusableInTouchMode(false);
and on button click press :--
ButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
countryText.setFocusableInTouchMode(true);
countryText.requestFocus();
}
});
Try this on your button click.
View view = getCurrentFocus().focusSearch(View.FOCUS_FORWARD);
int id = view.getId();
if (id != View.NO_ID) {
findViewById(id).requestFocus();
What is the logic of adding text to an edittext when button is pressed. Like in simple calculator, when u press a button the numbers will be shown to the edittext. What do you call that logic? I need a developers explanation.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
edittext.setText("text");
}
});
The logic is get the text from button and set it to the edittext,in aappending mode
Just use append() of the EditText. The Argument will be appended at the end of the Editable.
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editText1.append("your string to add at the end.");
}
});
Hi all I am new to android development, infact its my first application. I want to know
<button
android:text="1" />
in above tag, text is the value of the button? if yes then how i can get this value or store it in a variable. if not, then how to define a value behind any button in android?
yes its a value of Button , use following code to fetch Text of Button.
Button b = (Button)findViewById(R.id.button1);
String buttonText = b.getText().toString();
function call
b.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
function();
}
});
You first need to give the button an id, like so:
<Button
android:id="#+id/buttonId"
android:text="1"
/>
And then in your code do something like:
Button b = (Button)findViewById(R.id.buttonId);
b.getText(); // returns the value of your text.
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ButtonText" />
...
#Override
public void onCreate(Bundle savedInstanceState) {
Button btn = (Button) findViewById(R.id.button1);
String text = btn.getText().toString();
}
I want to get the values in the button to be populated in the edit text box. i.e I am creating a login page in which the pin has to entered I have given a set of numbers as buttons, when I click the button the corresponding values has to be populated in the edit text box( just like the ATM action).
I am now using:
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
String text=(String) b1.getText();
pincode.setText(text);
}
});
with the above code I am getting the values populated, but I have a set of buttons, so is there any simplified way to get the values populated?
*The values currently replacing the previous one , I need multiple values in the edit text like if we press buttons 1,2,3 (values) the values in the edit text should be 123 *
To do this, in your activity implements OnClickListener
then add this listener to all your button as
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
and write onClick event as
public void onClick(View v) {
Button b1 = (Button)v;
editText.setText(editText.getText().toString()+b1.getText().toString());
}
and you are done :)
on your button onClick you got to do this
button1.setOnClickListener(new View.OnClickListener() {
yourEditText.setText(yourEditText.getText().toString+"1");
});
button2.setOnClickListener(new View.OnClickListener() {
yourEditText.setText(yourEditText.getText().toString+"2");
});
this way your buttons will work
onClick(View v)
{
editText.setText(editText.getText() + v.getText());
//plz adjust casting and spanned as per requirement
}
You can add text like this:
String a = "random text";
input = your_edit_text_box;
input.setText(a);
Link: http://developer.android.com/reference/android/widget/EditText.html#setText%28java.lang.CharSequence,%20android.widget.TextView.BufferType%29
Button btnNO = (Button) findViewById(R.id.btn);
EditTExt edtText = (EditText) findViewById(R.id.editText);
btnNO.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String text= btnNo.getText();
edtText.setText(text);
// or direct use like, edtText.setText(btnNo.getText());
}
});