1

I need to make my dialog activity when the device is a tablet, wider and use more of the screen than it currently is.

Please see screen shot:

enter image description here

I need it to be about 70% of the screen width?

Is there a way to specify the width of this dialog.

To achieve this I have the following:

styles.xml

<resources>

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

  <style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
  </style>

  <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

  <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

  <style name="DialogTheme" parent="AppTheme"/>

</resources>

styles.xml (sw600dp)

<resources>
   <style name="DialogTheme" parent="Theme.AppCompat.DialogWhenLarge"/>
</resources>

Manifest:

<manifest package="stillie.co.za.dialogwhenlarge"
          xmlns:android="http://schemas.android.com/apk/res/android">

  <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>

        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>
    <activity
        android:name=".DialogWhenLargeActivity"
        android:label="@string/title_activity_dialog_when_large"
        android:theme="@style/DialogTheme"> <-----------notice this-----------
    </activity>
  </application>
</manifest>

Does anyone know how to make the screen width wider only when its on a tablet. How I have it set up at the moment, it works fine when the device is a phone its full screen like it should be but the only issue I have is when its a tablet, the dialog theme is too narrow.

aydinugur
  • 1,208
  • 2
  • 14
  • 21
Stillie
  • 2,647
  • 6
  • 28
  • 50

3 Answers3

0

For RESPONSIVE case,

Try this way,

DisplayMetrics metrics = getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
    final Dialog dialog = new Dialog(YourActivity.this);
    dialog.getWindow().setLayout((int) (width*85 / 100), (int) (height*35 / 100));
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 2
    although this works, i dont think its the correct solution, custom code ect im sure there is a way to do this other than coding it like this – Stillie Nov 01 '17 at 06:11
  • @x10sion agee. Check https://stackoverflow.com/questions/19133822/custom-dialog-too-small – IntelliJ Amiya Nov 01 '17 at 06:16
  • Thing with the solution you gave is that the width is too hard coded if you get what i mean? you know with different screen widths etc – Stillie Nov 01 '17 at 06:22
  • 1
    Happy b'day @IntelliJAmiya :) – InsaneCat Nov 01 '17 at 06:32
  • no, im not happy with implementing the code aspect of it, and the minWidth makes the toolbar act weird, i have an action on the right of the toolbar like "Accept" and when I do make the min width wider, the action ends up sitting over the title of the toolbar but the width of the dialog is wider – Stillie Nov 03 '17 at 05:20
  • @x10sion Any solutions yet? – IntelliJ Amiya Nov 06 '17 at 10:07
0

You have two options

  1. Create custom dialog & add below line

    @Override
    public void onAttachedToWindow() {
        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        getWindow().setLayout(width * 70 / 100, height * 30 / 100);
        super.onAttachedToWindow();
    }
    

Note: Change percentage as per your requirement

Or

  1. Do as per IntelliJ Amiya suggestion for current dialog
Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
Rohan Pawar
  • 1,875
  • 22
  • 40
0

Put this code in your OnCreate() method :-

DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int width = displaymetrics.widthPixels - 
common.convertDpToPixel(60, this);
        Log.i("widthval", "width" + width);
        getWindow().setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.CENTER);

This is a "convertDpToPixel" method Declare into your common file :-

 public static int convertDpToPixel(int dp, Context context) {
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        int px = dp * ((int) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
        return px;
    }

I'm putted 60-dp but you can change it for your requirement.

Hope this helps you.

InsaneCat
  • 2,115
  • 5
  • 21
  • 40