I am fetching the data from a database and I am appending it to a TextView. When I do long click on TextView I want to convert it to an EditText.
This is how I set the data on my TextView:
TextView text = (TextView) vi.findViewById(R.id.menutext);
text.setText(itemnames[position]);//comes from database append to text view
Now I want to define a setOnLongClickListener to convert it into a EditText.
text.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
String edititemname=itemnames[position];
System.out.println(edititemname);
return true;
}
});
edititemname holds which item was pressed in a long click. I want to fill the same information into the EdiText. Please help me.
As far as I know you can't convert one to another. What you can is: Have a TextView and an EditText created in xml. EditText is hidden when TextView is showing. Then, on your listener to the onCLick you can:
text.setVisibility(View.GONE);
editText.setVisibility(View.VISIBLE);
editText.setText(edititemname);
The editText variable can be defined where you define the text. You have to use the findViewById.
I have Tested and it is Working :
final EditText et=(EditText)findViewById(R.id.editText1);
final TextView tv=(TextView)findViewById(R.id.txt);
tv.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
tv.setVisibility(4);
final EditText et2=(EditText)findViewById(R.id.editText2);
et2.setVisibility(1);
return false;
}
});
Just Keep EditText as android:visibility="gone"
yes you can for that create a edittext just behind the textview in long press of textView hide textView and show editext as you done hide edittext and make visible textView
as you make invisible textview set edittext text to textview text
Related
How can I make a TextView selectable? By this I do not mean that I want to make the text within the TextView to be selected. What I want is that when the user taps on the TextView, it enters the selected state, and when the user taps another TextView in the layout, it exits the selected state and enters the default state.
You can assign an onClickListener to any view, and do whatever you want when it is clicked.
So bottom line:
findViewById(R.id.yourViewName).setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do whatever you need to do here
}
});
You can create an instance of TextView which will be used to know the last clicked TextView:
private TextView lastClicked;
Then in the onClickListener method of your TextViews, you have to change your lastClicked TextView to the current one.
myTextView.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
lastClicked = (TextView) v;
}
});
This way, you can retrieve the selected TextView from the lastClicked variable.
That already exists, it's not called selected, it's called focused. You can check if a TextView is focused by doing:
myTextView.isFocused()
And in an activity you can get the current focused view by doing
getCurrentFocus()
i have a lot of textviews in my app i want to create a method and set it on the onClick in all the textviews Xml, so when i click on a textview it should get me the text of that textview.
this is my methode:
public void getnum(View v){
Toast.makeText(getApplicationContext(), "hello ", 5).show();
//need to add something here to get the text of the textview that i clicked;}
this is the Xml:
<TextView
android:id="#+id/Numero1"
android:clickable="true"
android:onClick="getnum"
android:text="Numero1" />
Best option would be giving each TextView a tag (android:tag="1"), then in the on click method, call v.getTag(). You cal also access the text of the TextView in that method as well.
If you want the text of the clicked TextView you can use the getText() method:
public void getnum(View v){
TextView clickedTextView = (TextView) v;
String text = clickedTextView.getText().toString();
}
The View passed in parameter is the clicked View, in your case is one of your TextViews.
You can cast the view to a TextView and use that get the text.
public void getnum(View v){
TextView tv = (TextView) v;
String text = tv.getText().toString();
}
In my activity I have the following views
TextView player1;
TextView player2;
TextView player3;
TextView player4;
EditText player1name;
EditText player2name;
EditText player3name;
EditText player4name;
Each of the TextView's has the onclick listener applied to it. and so fires the OnClick function.
When we get to the onClick this is what i am currently doing:
#Override
public void onClick(View v) {
//the v variable is the clicked textview, in this case "player1"
//hide the textview and show the resultant edittext
v.setVisibility(View.GONE);
player1name.setVisibility(View.VISIBLE);
//set focus on edit text and when focus is lost hide it and set the textview text
player1name.requestFocus();
imm.showSoftInput(player1name, InputMethodManager.SHOW_FORCED);
player1name.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View y, boolean x) {
v.setVisibility(View.VISIBLE);
player1name.setVisibility(View.GONE);
imm.hideSoftInputFromWindow(player1name.getWindowToken(), 0);
String name = player1name.getText().toString();
if (name.equals("")) {
v.setText("Player Name1");
} else {
v.setText(name);
}
}
});
}
However with this solution I will need to duplicate this code and change the view names for player2 - player2name, player3 - player3name etc
i can obviously grab the clicked TextView via v, however what i cant seem to do is grab its corresponding EditText.
i had thought of doing this:
View test = v + "name";
//then i replace all references to player1name with the test variable
but it doesnt work it wants me to convert View test; into a string
any suggestions?
EDIT: made it easier to understand my question
View test = v + "name";
will give a compile error. Because "v" is not a string type. and also even if it was String, test is not. This line is pretty wrong.
There a few options to achieve what you want,
You can use hashmap
Declare a global field for hashmap
private final HashMap<Integer,EditText> map = new HashMap<Integer,EditText>();
and in onCreate method put your textview id as key, and put your edittext variables in value.
player1name = (EditText) findViewById(R.id.player1name);
map.put(R.id.textView1, player1name);
// for the rest
in onClick method
EditText e = map.get(v.getId());
Then replace them with "e"
e.requestFocus(); //example
Will you please state your problem clearly? Currently, your language is very ambiguous and I can not figure out, exactly what are you looking for. It will help us to know your problem and in turn solve it.
I want to grab Text when user click on the TextView
For Example :
TextView string = "this is a test for android and textView"
When user click on textview in android position grab android
Anyone have a solution for this ?
You can assign an onClick listener to the textview, make it final and then get its text.
final TextView txt = (TextView) findViewById(R.id.txt);
txt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String getTxt = txt.getText().toString();
}
});
TextView text = (TextView) findViewById(R.id.yourTextViewId);
text.onClickListner(this);
#Override
public void onClick() {
String textOnTextView = text.getText().toString();
}
If you want to split lines and display them in different color then refer to following links.
Split text from string
Apply color to specific text
A button has onClick , I dont think that a TextView has onClick so that a user clicks it.
Correct me if i am wrong
If you want to select part of text, try to use EditText
This is not a solution for your need. But only one step to solution.
Use setTextIsSelectable(boolean) or the TextView_textIsSelectable XML attribute to make the TextView selectable (text is not selectable by default).
Using following code, I managed to get selected text as String. You need to first select string by dragging over it.
NB: you need minimum API 11 to use setTextIsSelectable(boolean)
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(TextView) findViewById(R.id.textView1);
t1.setTextIsSelectable(true);// IMPORTANT
t1.setText("This is Android program");
t1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_UP:
int start=t1.getSelectionStart();
int end=t1.getSelectionEnd();
String sub=t1.getText().subSequence(start, end).toString();
Toast.makeText(getBaseContext(), sub, 1).show();
}
return true;
}
});
}
Some EditText (Like when you write a text message) can be clicked and opened, so you can change the text inside the EditText. It also gives you the keyboard so you can write, etc.
How do I achieve the same thing?
Currently I got this:
public void onDescriptionBoxClick(View view) {
EditText et = (EditText) this.findViewById(R.id.editText1);
}
And I don't know what to do other than that.
Do I have to call the keyboard forth so the user can edit the text in the box?
I would place an EditText element right next to your textView which has the attribute android:visibilty="gone"
then in your code:
public void onDescriptionBoxClick(View view) {
TextView tv = (TextView) this.findViewById(R.id.textView2);
tv.setVisibility(View.GONE);
EditText et = (EditText) this.findViewById(R.id.editText2);
et.setVisibilty(View.VISIBLE);
}
You can't edit text using a TextView. to accomplish what you want you have to replace the TextView with a EditText widget. and then replace it with the TextView containing the text entered in the EditText.
You can accomplish this in a few ways:
Using Fragment, more compicated.
You can use simple TextView and EditText widgets and set their Visability accordingly.