Android : Focus on TextView? - android

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"

Related

Android : Trigger textView's onClick() forcecfully

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

EditText Android| input text by pressing a button

How to implement the method, when the button is clicked, EditText can be edited. Before this, EditText must be closed for text entry.
code located below does not help
Entertext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Total.setFocusable(true);
}
});
Try this use android:focusable="false" in xml to disable your EditText
<EditText
android:id="#+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false" />
now enabled your Edittext inside button click listener using android:focusable like this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edittext.setFocusable(true);
edittext.setFocusableInTouchMode(true);
edittext.setEnabled(true);
edittext.requestFocus();
}
});
make edittext in xml as focusable false like below ..
android:focusable="false"
then after click event used below code..
Entertext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Total.setFocusable(true);
Total.setFocusableInTouchMode(true);
Total.requestFocus();
}
});
you can also used enable and disable concept in edittext.

OnClick to TextView Android [duplicate]

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

Button Value to EditText Logic - Android

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

Android onClick function for an edittext, How do I call the edittext?

I have an edittext form that can get filled, I want to clear it upon an onClick of a text, so I put an onClick that goes to my clear function in main.
I however, dont know how to properly target the edittext that I want. I want to clear TWO of them.
public void clear(View v) {
#+id/toptext.setText("");
}
This is the XML for that specific text.
<TextView
android:id="#+id/toptext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#303030"
android:text="#string/toptext"
android:textAppearance="?
android:attr/textAppearanceLarge"
android:textColor="#33B5E5"
android:textSize="50sp"
android:onClick="clear" />
Create instances of your EditTexts, using the id of the EditTexts in your xml layout. Then use setText on them and make them blank.
public void clear(View v) {
EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
et.setText("");
et2.setText("");
}
Edit for some reason the method above didn't work. This was the end solution:
// Put one of these in your onCreate method:
TextView tt = (TextView)findViewById(R.id.toptext);
tt.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
et.setText("");
et2.setText("");
return true;
}
});
// or
TextView tt = (TextView)findViewById(R.id.toptext);
tt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
et.setText("");
et2.setText("");
}
});
Try this:
TextView tv = (TextView) findViewById(R.id.toptext);
tv.setText("Whatever you want!");
You can use this code:
public void clear(View v) {
if (v.getId() == R.id.toptext) {
((EditText) v).setText(null);
}
}
Similarly, add another if condition for another EditText.
First, get reference to your TextView by calling findViewById:
TextView textView = (TextView) findViewById(R.id.toptext);
Then use it to clear:
textView.setText("");
I think instead of setting "", u should use null as.
TextView textView = (TextView) findViewById(R.id.toptext);
textView.setText(null);
Use the param passed to the onClick() (v)
public void clear(View v)
{
((TextView)v).setText("");
}
Assuming you want to clear the text in the TextView that you have your onClick attached to. The View passed into clear() is the View that was clicked so you just cast it to the appropriate View (TextView) then you can use it as normal.
So this will clear the text on whichever TextView was clicked.

Categories

Resources