0

I'm trying to change the text message of the AlertDialog message. To be a clikable message. With this code the text is blue, but not clikable

I want the phone, email and site from text to be clikable

            final SpannableString All =
             new SpannableString(textmess + "\n\nTel :\n " + phone + "\n\nEmail : \n " + email + "\n\nAddress: \n " + address + "\n\nWeb site : \n " + site );
            Linkify.addLinks(All, Linkify.ALL );


            AlertDialog.Builder builder = new AlertDialog.Builder( new 
                       ContextThemeWrapper(context, R.style.AlertDialogCustom)  );

            builder.setTitle(title).setMessage(All);

            builder.create();
            //not working
            //TextView textView = (TextView) findViewById(android.R.id.message);    
            builder.show();
            GeoPoint1.close();
            db1.close(); 

2 Answers2

0

Create a custom layout for the dialog and also use setMovementMethod to get it done

Ex:

AlertDialog.Builder dialog = new AlertDialog.Builder(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate(R.layout.dialog_custom, null);
TextView dTitle = (TextView) view.findViewById(R.id.textDialogTitle);
TextView dMessage = (TextView) view.findViewById(R.id.textDialogMessage);
dMessage.setMovementMethod(LinkMovementMethod.getInstance());
dTitle.setText("Title");
dMessage.setText(ALL);
dialog.setView(view);       
dialog.show();
Joel Fernandes
  • 4,776
  • 2
  • 26
  • 48
0

This example will help you :

res/layout/dialog.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000">  
<TextView
    android:id="@+id/tv_pc_forDialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/tv_pc_dialogTitle"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="20dp"
    android:ems="10"
    android:layout_centerHorizontal="true"
    android:text="THIS IS A CLICKABLE MESSAGE"
    android:singleLine="true"
    android:textColor="#FFFFFF" >
</TextView>
<TextView
    android:id="@+id/tv_pc_dialogTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="5dp"
    android:text="SEARCH"
    android:textColor="#FFFFFF"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
    android:id="@+id/bt_pc_goButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tv_pc_forDialog"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:text="GO"
    android:textColor="#FFFFFF"
    android:textSize="25sp" /> </RelativeLayout>

res/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="177dp"
        android:text="Open Dialog" />

</RelativeLayout>

MainActivity.java :

package com.bhavit.stackoverflow;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button openDialog = (Button) findViewById(R.id.button1);
        openDialog.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                Dialog dialog = new Dialog(MainActivity.this); // here write the name of your activity in place of "YourActivity"
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.dialog);
                TextView tv = (TextView)dialog.findViewById(R.id.tv_pc_dialogTitle);
                Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/GOTHIC.TTF"); // here GOTHIC.TTF is the font-file I have pasted in the asset/fonts folder in my project 
                tv.setTypeface(tf); // Set the typeface like this

                Button bt = (Button) dialog.findViewById(R.id.bt_pc_goButton);
                bt.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        // Write here, what do you want to do on button click

                    }
                });

                TextView tview = (TextView) dialog.findViewById(R.id.tv_pc_forDialog);
                tview.setOnClickListener(new View.OnClickListener() {  // set onClickListener to the textview

                    @Override
                    public void onClick(View v) {

                        // Write here, what do you want to do on Message click
                        Toast.makeText(getApplicationContext(), "You clicked on message", Toast.LENGTH_LONG).show();

                    }
                });

                dialog.show();


            }
        });



    }


}

What I have done here is that I made a customised dialog, and set an onClickListener on the TextView shown as a message. Like this, you can achieve your goal.

Bhavit S. Sengar
  • 8,794
  • 6
  • 24
  • 34