Make AlertDialog Custom

2020-06-06 05:56发布

I am using this code to display Alert Dialog

  holder.tv1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder nointernetconnection = new AlertDialog.Builder(
                                temp);
                        nointernetconnection
                                .setIcon(R.drawable.ic_launcher)
                                .setTitle(list.get(position).getAS_name())
                                .setMessage(list.get(position).getDesc_art())
                                .setCancelable(true)
                                .setPositiveButton("OK",
                                        new DialogInterface.OnClickListener() {
                                                   @Override
                                            public void onClick(DialogInterface arg,
                                                    int arg1) {



                                            }
                                        });
                        AlertDialog a = nointernetconnection.create();
                        a.show();

enter image description here

Message body is converted into scrollView automatically in case the text is more but Title text has not been viewed completely nor the title space is scrollable.

So, I want to expand the Title area & also want to make it scrollable & for this i don't wanna use custom Dialog, i want to only implement it by AlertDialog.

7条回答
趁早两清
2楼-- · 2020-06-06 06:32

Here is my customdialogclass. It has several constructors in function of what you want to display: buttons, progress, nothing more than a title and a message... Customizing the layout you will be able to have a longer title or not. You could even insert one custom textview that adopts its font size to the space avaible for it. Hope it helps.

public class CustomDialogClass extends Dialog implements android.view.View.OnClickListener {

public Activity c;
public Dialog d;
public Button yes, no;
private int showButtons;
private String tit, msg, yesT, noT;
private boolean custom=false, all= false, progresss=false, spinner=false, indeterminateputted=false, indet=false;
private TextView title, subtit;
private ProgressBar progressBar, progressBar2;
private int max;
private int progress;

public OnPositiveDialogButtonClicked positive;

public CustomDialogClass(Activity a) {

    super(a);
    this.c = a;
    this.custom = false;
}

public CustomDialogClass(Activity a, int botones) {

    super(a);
    this.c = a;
    this.showButtons = botones;
    this.custom = false;
}

public CustomDialogClass(Activity a, int botones, String tit, String message) {

    super(a);
    this.custom = true;
    this.c = a;
    this.showButtons = botones;
    this.tit = tit;
    this.msg = message;
}

public CustomDialogClass(Activity a, String tit, String message, String yes, String no) {

    super(a);
    this.custom = true;
    this.c = a;
    this.tit = tit;
    this.msg = message;
    this.yesT = yes;
    this.noT = no;
    this.all = true;
}

public CustomDialogClass(Activity a, String tit, String message, int max, int progress) {

    super(a);
    this.progresss = true;
    this.tit = tit;
    this.msg = message;
    this.max = max;
    this.progress = progress;
}

public CustomDialogClass(Activity a, String tit, String message, int max, int progress, boolean spinner) {

    super(a);
    this.tit = tit;
    this.msg = message;
    this.max = max;
    this.progress = progress;
    this.spinner = true;
}

public CustomDialogClass(Activity a, String tit, String message, boolean indet) {

    super(a);
    this.progresss = true;
    this.indeterminateputted = true;
    this.indet = indet;
    this.tit = tit;
    this.msg = message;
}

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.dialog_view);
    yes = (Button) findViewById(R.id.btn_yes);
    no = (Button) findViewById(R.id.btn_no);
    title = (TextView) findViewById(R.id.txt_dia);
    subtit = (TextView) findViewById(R.id.messageDialog);
    progressBar = (ProgressBar) findViewById(R.id.dialogProgress);
    progressBar2 = (ProgressBar) findViewById(R.id.dialogProgress2);

    if(this.indeterminateputted) this.progressBar.setIndeterminate(indet);

    yes.setOnClickListener(this);
    no.setOnClickListener(this);

    if(tit!=null && tit.length()>0) title.setText(tit);
    if(msg!=null && msg.length()>0) subtit.setText(msg);
    if(yesT!=null && yesT.length()>0) yes.setText(yesT);
    if(noT!=null && noT.length()>0) no.setText(noT);
    if(showButtons==0) {

        yes.setVisibility(View.GONE);
        no.setVisibility(View.GONE);
    }
    if(spinner) {

        subtit.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.GONE);
        progressBar2.setVisibility(View.VISIBLE);
        yes.setVisibility(View.GONE);
        no.setVisibility(View.GONE);
    }
    if(progresss) {

        subtit.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.VISIBLE);
        yes.setVisibility(View.GONE);
        no.setVisibility(View.GONE);
        progressBar.setMax(max);
        progressBar.setProgress(0);
    }
    if(all) {

        subtit.setVisibility(View.VISIBLE);
        yes.setVisibility(View.VISIBLE);
        no.setVisibility(View.VISIBLE);
    }
    else if(custom){

        subtit.setVisibility(View.VISIBLE);
        yes.setVisibility(View.GONE);
        no.setVisibility(View.GONE);
    }
}

@Override
public void onClick(View v) {

    switch (v.getId()) {

    case R.id.btn_yes:

        positive.onPositive(true);
        break;
    case R.id.btn_no:

        positive.onPositive(false);
        dismiss();
        break;
    default:

        break;
    }
    dismiss();
}

public void setButtonListener(OnPositiveDialogButtonClicked listener) {

    positive = listener;
}

public void setProgress(int progress) {

    if(progressBar!=null) {

        this.progress = progress;
        progressBar.setProgress(progress);
    }
}

public void setMessage(String msg) {

    if(subtit!=null) subtit.setText(msg);
}

public void setTitle(String titleee) {

    if(title!=null) title.setText(titleee);
}

public int getProgress() {

    return this.progress;
}

public int getMax() {

    return this.max;
}

public void setIndeterminate(boolean indet) {

    this.progresss = true;
    this.indeterminateputted = true;
    this.indet = indet;
}
}

the interface for the buttons:

public interface OnPositiveDialogButtonClicked {

public void onPositive(boolean clickedYes);
}

the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/white" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/gradientbackground"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/txt_dia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="10dp"
        android:textColor="@android:color/white"
        android:textSize="16sp"
        android:textStyle="bold"
         >
    </TextView>
</LinearLayout>

<TextView
    android:id="@+id/messageDialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:textColor="@color/black"
    android:textSize="13sp"
    android:visibility="gone"
    android:textStyle="bold" >
</TextView>

<ProgressBar
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:id="@+id/dialogProgress"
    android:layout_margin="10dp"
    android:visibility="gone"
    android:layout_gravity="center"
    android:layout_width="250dp"
    android:layout_height="4dp"
/>

<ProgressBar
    style="?android:attr/progressBarStyleLarge"
    android:id="@+id/dialogProgress2"
    android:visibility="gone"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="5dp"
    android:background="@color/white"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_yes"
        android:layout_width="100dp"
        android:layout_height="45dp"
        android:background="@android:color/white"
        android:clickable="true"
        android:text="Yes"
        android:textSize="13sp"
        android:textColor="#5DBCD2"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btn_no"
        android:layout_width="100dp"
        android:layout_height="45dp"
        android:layout_marginLeft="5dp"
        android:background="@android:color/white"
        android:clickable="true"
        android:text="No"
        android:textSize="13sp"
        android:textColor="#5DBCD2"
        android:textStyle="bold" />
</LinearLayout>

查看更多
\"骚年 ilove
3楼-- · 2020-06-06 06:34

This example is some what a typical hack... You don't need a custom View also...

private void showDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(R.drawable.ic_launcher);
    final String title = "This is a Big Title. This is a Big Title. This is a Big Title. This is a Big Title. This is a Big Title. This is a Big Title. ";
    builder.setTitle(title);
    builder.setMessage("This is a Message. This is a Message. This is a Message. This is a Message.");
    builder.setCancelable(false);
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    AlertDialog alertDialog = builder.create();
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            AlertDialog alertDialog = (AlertDialog) dialog;
            ViewGroup viewGroup = (ViewGroup) alertDialog.getWindow()
                    .getDecorView();
            TextView textView = findTextViewWithTitle(viewGroup, title);
            if (textView != null) {
                textView.setEllipsize(null);
                textView.setMaxHeight((int) (80 * alertDialog.getContext().getResources().getDisplayMetrics().density)); 
                textView.setMovementMethod(new ScrollingMovementMethod());
            }
        }
    });
    alertDialog.show();
}

private TextView findTextViewWithTitle(ViewGroup viewGroup, String title) {
    for (int i = 0, N = viewGroup.getChildCount(); i < N; i++) {
        View child = viewGroup.getChildAt(i);
        if (child instanceof TextView) {
            TextView textView = (TextView) child;
            if (textView.getText().equals(title)) {
                return textView;
            }
        } else if (child instanceof ViewGroup) {
            ViewGroup vGroup = (ViewGroup) child;
            return findTextViewWithTitle(vGroup, title);
        }
    }
    return null;
}
查看更多
Emotional °昔
4楼-- · 2020-06-06 06:36

You have 2 options-

  1. You could create a custom dialog view and show ur content. Here is an example

            final Dialog dialog1 = new Dialog(CatchTheCatActivity.this);
        dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog1.setContentView(R.layout.custom_alert);
        Button ok = (Button) dialog1.findViewById(R.id.button1);
    
        TextView title = (TextView) dialog1.findViewById(R.id.textview1);
        TextView content = (TextView) dialog1.findViewById(R.id.textview2);
        title.setText("your long title")
        content.setText("your long content");
    
        ok.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                    dialog1.dismiss();  
    
            }
        });
        dialog1.show();
    

where R.layout.custom_alert is UI You want to show (in your case 2 textview with a button at the bottom). Ref

  1. Use popupwindow. Here is an example
查看更多
干净又极端
5楼-- · 2020-06-06 06:43

You can use the .setCustomTitle method of the AlertDialog.Builder class, to specify a custom layout file for the title element of the dialog .(As this is still using the AlertDialog class and not a custom (or subclassed) dialog, I think it's a worthy answer). Like so:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View view=inflater.inflate(R.layout.titlebar, null);
alert.setCustomTitle(view);

Android docs reference .setCustomTitle(View customTitleView)

Or you could just make the title font a smaller size, but depending on how much content there is, it may become pointless even having the title there, if it's too small to read.

查看更多
贪生不怕死
6楼-- · 2020-06-06 06:46

*You can do custom alert dialog by creating custom layout.

*create a custom XML file in re/layout folder

*you can design it in your way.

*in you activity class you have to write in on create method

i home this will be useful for you.

Dialog d = new Dialog(MainActivity.this);

d.setcontentview(R.layout.custom);

//For example you have one edit text and button than you can make it by declaring

EditText ed = (EditText)d.findViewById(R.id.ed1);


Button b = (Button)d.finviewById(R.id.b1);

Button b = (Button)d.finviewById(R.id.b1);

//you can on click listner on button like

b.setOnClickListner(new .....);

Alert alert = d.create(); d.show();

查看更多
Root(大扎)
7楼-- · 2020-06-06 06:48

If you really do not want to use a custom title view, you can make the dialog title multiline:

TextView title = (TextView) dialog.findViewById(android.R.id.title); 
title.setSingleLine(false);

If you would like to have a scrolling title, or use a custom title, Then you only (good) option would be to use a custom alertdialog title. It's not very hard to apply:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
TextView textView = new TextView(context);
textView.setText("your very long title here");
builder.setCustomTitle(textView);
查看更多
登录 后发表回答