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.");
}
});
Related
I am trying to remove a button when the button itself is tapped, I am trying the following:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
tagsView.removeView(button);
}
};
}
This code is working, but when I add the following line of code:
editText.setText(button.getText());
The code stops working and the button does not get removed. I add it like so:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
editText.setText(button.getText());
tagsView.removeView(button);
}
};
}
What is the problem here?
use this in your OnClick method
button.setVisibility(view.GONE);
Your code will look like this
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
editText.setText(button.getText());
button.setVisibility(view.GONE);
}
};
}
Or Try this
Button mybtn = (Button)findViewById(R.id.mybtn_id);
mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mybtn.setVisibility(view.GONE); // or (view.INVISIBLE)
}
});
Depending on what you're trying to achieve, something like deejay proposed would work just fine. If want the button to hide, call button.setVisibility(View.INVISIBLE). However, if you are trying to dismiss it completely from the view hierarchy, call button.setVisibility(View.GONE).
just set button visibility to false
Obviously button.setVisibility(View.GONE) comes to mind but if it doesn't work you should look one level above for the source of the bug. Maybe you don't set OnClickListener you created to the button and hence nothing happens?
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
Edittext(AmountPaid) and Button(Print).
when i clicked print then amountpaid will display another Edittext(TotalAmount).
Each time calculate and display amount.
Please guide me the correct way to achieve my objective.
total=(EditText)findViewById(R.id.editText20);
amountpaid=(EditText)findViewById(R.id.editText8);
Print.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String amntpaid=PaymentPaid.getText().toString();
total.requestfocus();
}
});
use this
total.setText(PaymentPaid.getText().toString().trim());
total.requestfocus();
Try this..
you have to set the text for EditText like total.setText(amntpaid);
Print.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String amntpaid=PaymentPaid.getText().toString();
total.requestfocus();
total.setText(amntpaid);
}
});
Change your click listener code as below to show text in EditText:
String amntpaid=PaymentPaid.getText().toString();
total.setText(amntpaid);
total.requestfocus();
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();
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());
}
});