Search code examples
androidandroid-layoutandroid-alertdialogclickable

clickable text in alert dialog


I want to make a text in alert dialog which is clickable. actually the text will be an ID. so when you click it, it will open browser and go to a specific webpage. how should I do it? his is my code:

alertDialog.Builder mBuilder = new AlertDialog.Builder(this);
mBuilder.setMessage("@sampleID");
mBuilder.setCancelable(true);
AlertDialog mAlert = mBuilder.create();
mAlert.show();    

also, how can I put multiple lines in the alert dialog?(different id's at each line)


Solution

  • You should create a layout contains what you want and use setContentView to add the layout to AlertDialog.

    LayoutInflater inflater = LayoutInflater.from(this);
    RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.alert_custom, null);
    TextView tv1 = layout.findViewById(R.id.your_item_0);
    tv1.setOnClickListener(new OnClickListener());
    final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create();
    dialog.setCancelable(true);
    dialog.show();
    dialog.getWindow().setContentView(layout);