Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click on TextView inside inflated Linear Layout

I have an inflated Linear Layout that contains 2 TextViews inside it.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" >

<TextView
    android:id="@+id/tv_m"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ll_borders"
    android:tag="m"
    android:text="m" />

<TextView
    android:id="@+id/tv_q"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ll_borders"
    android:tag="q"
    android:text="q" />
</LinearLayout>  

All i want is that when this Linear Layout is inflated then i want to get the only TEXTVIEW on which i click. For example if i click on "tv_m" then it shall only return me the text of tv_m.
May b its simple but i am not getting a way to it. So i need help.
Thanks

like image 660
Noman Avatar asked Jan 13 '23 21:01

Noman


2 Answers

After inflating the layout get the textview objects as below

LinearLayout layout = inflater.inflate(<your layout name>, null);
TextView textView1 = layout.findViewById(R.id.tv_m));
TextView textView2 = layout.findViewById(R.id.tv_q));
String selectedText;
textView1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
             selectedText = textView1.getText().toString();
    }
});

Similarly you can put listener for textView2 also. The selectedText will be the final string which you want.

like image 77
Kameswari Avatar answered Jan 29 '23 08:01

Kameswari


You need to set up on click listeners for the text views. Then when one is clicked, it will call a function in your code passing it the view that was touched. Then you can call getText on it.

like image 44
Gabe Sechan Avatar answered Jan 29 '23 09:01

Gabe Sechan