xyz rety xyz rety - 5 months ago 25
Java Question

make popupWindow TextView clickable?

I have a PopupWindow on my activity and PopupWindow contain some TextView i want to make that text clickable.please help i m new in android.

main activity.java:

public class ListViewForDeleteContact extends AppCompatActivity {
ListView myListView;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

LayoutInflater layoutInflater=(LayoutInflater)ListViewForDeleteContact.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View dfg= layoutInflater.inflate(R.layout.popupWindow,(ViewGroup)findViewById(R.id.popupId));

PopupWindow popupWindow=new PopupWindow(dfg,420,300,true);

popupWindow.showAtLocation(dfg, Gravity.CENTER, 0, 0);
popupWindow.setOutsideTouchable(true);

}
});
}
}


main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/trans">


<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>

</RelativeLayout>


popupWindow.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popupId"
android:background="#546e7a"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="are you sure"
android:layout_margin="20dp"
android:id="@+id/textView7"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="no"
android:id="@+id/textView8"
android:layout_below="@+id/textView7"
android:layout_centerHorizontal="true"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yes"
android:textColor="@color/colorAccent"
android:id="@+id/textView9"
android:layout_below="@+id/textView7"
android:layout_alignBottom="@+id/textView8"
android:layout_alignEnd="@+id/textView7"/>
</RelativeLayout>


main.xml activity use popupWindow.xml for displaying popup i want to make clickable TextView which are present in popupWindow.xml.

Answer

You can access TextView using findViewById method in your dfg View variable.

Example

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    LayoutInflater layoutInflater=(LayoutInflater)ListViewForDeleteContact.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View dfg= layoutInflater.inflate(R.layout.popupWindow,(ViewGroup)findViewById(R.id.popupId));

    TextView textView7 = (TextView) dfg.findViewById(R.id.textView7);
    textView7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do something when the textview is clicked
        }
    });

    ... // for other textviews

    PopupWindow popupWindow=new PopupWindow(dfg,420,300,true);

    popupWindow.showAtLocation(dfg, Gravity.CENTER, 0, 0);
    popupWindow.setOutsideTouchable(true);

}
Comments