This question already has answers here:
Android - Set text to TextView
(23 answers)
Closed 6 years ago.
How to set text in the Text View by using button on click both are present in the same Activity?
I have already used the Text View on using the same Text View the Stopped working
Use the setter setText() in the button's on click listener.
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myTextView.setText("new text");
}
});
Here is the code that can help you with this.
Button button;
TextView textView;
button = (Button) findViewById(R.id.main_button);
textView = (TextView) findViewById(R.id.main_text);
// set the text view inside the button's OnClickListener() method
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
textView.setText("Hello World");
}
});
Related
Say I have defined an onClickListener for a TextView and I want to trigger it once without having to click on it - Is this possible?
you just have to call performClick() on it
textView.performClick()
textView= (TextView) findViewById(R.id.button);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Add your code here
}
});
to perform automatic click use below
textView.performClick();
This question already has answers here:
Adding Shape to LinearLayout Android
(3 answers)
Closed 5 years ago.
The above image shows that only the edit text box's border changes to red. I need the relative layout to change it's border to red when the edit text field is empty on button click. The "white" portion on the screen is RelativeLayout.
Main activity code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.phone );
final EditText number = (EditText) findViewById ( R.id.mobilenumber );
final String msisdn=number.getText().toString();
Button button = (Button) findViewById(R.id.login_button);
button.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
if(TextUtils.isEmpty(msisdn)) {
number.setBackgroundResource(R.drawable.edit_text_error_background);
number.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.like_selected, 0);
return;
}
}
});
}
You change your editText background color as follows.
button.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
if(TextUtils.isEmpty(msisdn)) {
number.setBackground(R.drawable.your_error_background);
}
else{
number.setBackground(R.drawable.your_normal_bakground);
}
}
});
Use setBackground() method instead of setBackgroundResource() method because its by default accept drawable resource
I hope its work for you. Thank You
I need a button that, when clicked, would add a TextView below it. Also, the TextView cannot be made invisible and appear on click in this case, it must add the TextView. Was looking for this and couldn't find anything that suits my needs.
Set a listener on the button and for each click, create a new TextView and add it to the layout.
final LinearLayout theLayout = (LinearLayout)findViewById(R.id.thelayout);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello");
tv.setTextColor(Color.BLACK);
theLayout.addView(tv);
}
});
Update: Added text color above
I have a TextView in ScrollView I need that when set text on TextView , going foucus on the TextView .I need that doing this work in OnClickListener a Button .
You need to set focusable as true.
txtview.setFocusableInTouchMode(true);
and then in onClick of button, request focus as:
txtview.requestFocus();
button.setOnClickListener(new View.onClickListener(){
public void onClick(View v) {
textbox.requestFocus();
}
});
i really hope i understood the question :)
Why don't you just setText and then requestFocus like:
final TextView textView = findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("Some text");
textView.requestFocus();
}
});
Note:
TextView will have to be focusable. (setFocusable(true), setFocusableInTouchMode(true)).
Or in the xml:
android:focusable="true"
android:focusableInTouchMode="true"
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());
}
});