简体   繁体   中英

Tap a text and display a popup message in android studio

I have some text in my app that I created in android studio using kotlin. For this app I want to keep a Pop up message such that I tap the text, a small pop up shows me the same text in that pop up. I am using recyclerview. A snapshot of the UI is shown below.

UI

So, I should be able to tap the text and get a pop-up message containing the same text. Since I am an amateur to android studio, I need some help on this. I've been searching and found solutions like Dialog and AlertDialog but I don't know how they should be used in this scenario. By the way there is no button here, it's a simple textView.

Any help is appreciated.

I found a way to do it using AlertDialog . The code snippet that was useful for me is given below.

val mAlertDialogTextView = holder?.view?.textView_subtopic_title //textView_subtopic_title is the view of my choice which needs to popup a message.
            mAlertDialogTextView?.setOnClickListener {
                val mAlertDialog = AlertDialog.Builder(context)
                mAlertDialog.setMessage(key.toString())
                mAlertDialog.setNegativeButton("cancel",{ dialogInterface: DialogInterface, i: Int -> dialogInterface.dismiss()})
                mAlertDialog.show()
            }

Thank you all for the support.

First set the textView attributes in xml:

android:onClick="onClick"
android:clickable="true"

Add click function to your activity with the Toast inside:

public class MyActivity extends Activity {

      public void onClick(View v) {
        Toast.makeText(getActivity(), textView.getString(),
   Toast.LENGTH_LONG).show();
      }  
   }    

Find your TextView in your activity, and add onClickListener and show a dialog with textviews content

TextView myTextView = (TextView)addView.findViewById(R.id.my_textview_id);

myTextView.setOnClickListener(new OnClickListener() {
@Override
 public void onClick(View v) {
    String stringFromTextView = ((TextView)v).getText().toString();
   showAlertDialog(stringFromTextView);
 }
});

private void showAlertDialog(String stringToShow){
   AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
   builder1.setMessage(stringToShow);
   builder1.setCancelable(true);

   AlertDialog alert11 = builder1.create();
   alert11.show();

}

You can use a simple dialog with no buttons. The user can tap anywhere outside to dismiss it:

        val ad = AlertDialog.Builder(context).create()
        ad.setMessage("Text")
        ad.setCancelable(true)
        ad.show()

You need to pass context .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM