0

I'm applying a hard-coded value ---- into the message body of a custom AlertDialog. In my device, it fits well since I used its dimesions, like below:

Normal device

However on a large screen device, it will definitely appear different like this:

enter image description here

and the code to produce the dialog:

new AlertDialog.Builder(context)
    .setTitle("Title goes here...")
    .setMessage("-------------------------------")
    (…)

I can use DisplayMetrics to get the width of the dialog, but how do I now apply the width caught to the actual message?

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
final int DisplayWidth = displayMetrics.widthPixels;
Boron
  • 99
  • 10
  • 34
  • AlertDialog.Builder gives you a AlertDialog. You can call `findViewById` on it and get any view on the AlertDialog. Then you can get width of the message textview. Also, you can use a custom layout for your alert dialog using `setLayout` in the builder. – momvart Jan 20 '19 at 11:55

3 Answers3

0

you can create xml for custom AlertDialog and in xml file set title and below line(View Tag with layout_heigh = "1dp" layout_width = "match_parent").

Afshin kh
  • 16
  • 4
0

I think, you want to show the layout.xml in alertDialog. And apply as in this example. You can put an ImageView instead of the line.

AOK
  • 118
  • 1
  • 1
  • 11
0

If you want just 1 line in the message of the dialog, then set the message

"-------------------------......"

to be more than long enough for any device's line.
The message of the AlertDialog is a TextView that can be found by findViewById() and invoke its method setSingleLine(true) or setlines(1).

    AlertDialog ad = new AlertDialog.Builder(context).create();
    TextView message = ad.findViewById(android.R.id.message);
    message.setSingleLine(true);
    ad.setTitle("Title goes here...");
    ad.setMessage("----------------------.....");
    ad.show();
forpas
  • 160,666
  • 10
  • 38
  • 76
  • `TextView message = ad.findViewById(android.R.id.message); message.setSingleLine(true);` is returning a NullPointerException – Boron Jan 20 '19 at 15:27
  • Which line of the 2 returns NPE? – forpas Jan 20 '19 at 15:31
  • `message.setSingleLine(true);` this, even after initializing it – Boron Jan 22 '19 at 05:13
  • I really can't see why `ad.findViewById(android.R.id.message)` cannot find the message `TextView` inside the `AlertDialog`. Do you have this exact line in the imports of your class: `import android.support.v7.app.AlertDialog`? – forpas Jan 22 '19 at 09:57