get ID of a textview that i clicked - android

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

Related

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.

how to get "View" clicked in ListView not "Complete Row" :: android

We make listener for Items(Rows) click listener in ListView. How to get know, in a particular row a specific View get clicked?
R1 --> TextView1 | TextView2
R2 --> TextView1 | TextView2
I can get which row is clicked. but i also want to know which View of the row get clicked.
You need to set position as tag to textView, and then onClick you can receive its tag.
So basicly you set textView's staff on adapter's getView method
textView.setTag(position);
textView.setOnClickListener(listener);
And then you'll have click listener like this
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
int position = Integer.parseInt(v.getTag().toString());
// Do whatever you like...
}
};
You can add specific click handlers to the textViews:
<TextView
android:onClick="clickHandler">
Then in your activity:
public void clickHandler(View v) {
// obtain parent row
LinearLayout parentView = (LinearLayout) v.getParent();
// with the parentView you can get all other views as well:
TextView textView1 = (TextView) parentRow.getChildAt(0);
}
I do it like this:
assign onClickListener to whatever can be clicked
I assign tag to it to identify v.setTag(R.id.itemId, item.id);
In onclick I do:
#Override
public void onClick(View v) {
final int id = (Integer) v.getTag(R.id.itemId);
}

Select a part of TextView text

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

How to convert a TextView to EditText in android?

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

How to retrieve TextView value when we Touch on that and The TextView is added into LinearLayout?

Here is my code for how i make adding Dynamically TextView in to Table Layout using LinearLayout for it.
table_personalData.removeAllViews();
int i=0;
for(String header : headerDetail){
TextView label=new TextView(this);
TextView value=new TextView(this);
label.setTextAppearance(this, R.style.TextBase);
value.setTextAppearance(this, R.style.TextBase);
label.setText(String.format("%s:", header));
value.setText(String.format("%s", lead.data[i]==null?"":lead.data[i]));
i++;
LinearLayout rowview=new LinearLayout(this);
rowview.setOrientation(0);
rowview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rowview.addView(label);
rowview.addView(value);
table_personalData.addView(rowview);
}
By Above code i get my table layout with dynamically added textview in that .
here i use Liner Layout for added two textview in One Row .
headerDetail is a ArrayList<String> headerDetail
lead.data[i] i used this to get my values for particular label.
All this work fine for me now what my problem is
Now i want to do one thing that is when we click one of the TextView of my LinearLayout i want that TextView in dynamically.
So finally my question is about to get retrieve textview value when we touch or click any textview which is in Linearlayout and LinearLayout is in TableLayout.
Anyone have idea what should i have to do with this .
i also try something like below but i don't get i can i get TextView Value from below code.
table_personalData.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<table_personalData.getChildCount();i++){
//CharSequence desc=table_personalData.getChildAt(i).getContentDescription();
//Log.v("log_tag", "the values are"+ desc);
}
}
});
EDIT:
if you want the Value of TextView when we Touch on that There is Two way that i write Below :
value.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView txt;
txt=(TextView)findViewById(v.getId());
String tempEmail;
tempEmail=txt.getText().toString().trim();
String aEmailList[]={tempEmail};
pattern=Pattern.compile(EMAIL_PATTERN);
if(validate(tempEmail)){
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,aEmailList );
startActivity(emailIntent);
}else{
pattern=Pattern.compile(PHONE_No_PATTERN);
if(validate(tempEmail)){
tempEmail=tempEmail.replaceAll("[^\\d]", "");
String uri="tel:" +tempEmail;
Intent mIntent=new Intent(Intent.ACTION_CALL);
mIntent.setData(Uri.parse(uri));
startActivity(mIntent);
}else{
//Toast.makeText(LeadDetailActivity.this, "Please Touch on Email or Mo:Number", Toast.LENGTH_SHORT).show();
}
}
}
});
1)Create how many text view u want,but set id for each one,and aslo set same click listener for all
TextView value1=new TextView(this);
value1.setid(1);
value1.setOnClickListener(clickListener);
TextView value2=new TextView(this);
value1.setid(2);
value2.setOnClickListener(clickListener);
TextView value3=new TextView(this);
value3.setid(3);
value3.setOnClickListener(clickListener);
2)second create one onclick listener,inside this listener u will get all cliks. so based on view id u can dynamically do anything
OnClickListener clickListener=new OnClickListener() {
public void onClick(View v) {
int id=v.getId();
}
};

Categories

Resources