arnoldssss
arnoldssss

Reputation: 488

Dialog cannot set the size of the width window

Hi community I'm having an issue with setting the size of a Dialog to fit the width of the window.

I've tried several answers here like: Android dialog width Android get full width for custom Dialog

But I cannot reach the full size of the width in my phone. It actually gets the property of TOP (gravity) but the width doesnt.

The code Im using is:

    package bem.banorte.bem;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * Created by IDS Comercial on 09/07/2018.
 */

public class ThirdActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.CustomDialogTheme));

        TextView title = new TextView(this);
        // You Can Customise your Title here
        title.setText("Error en la aplicacion");
        // title.setBackgroundColor(Color.);
        title.setPadding(0, 30, 0, 30);
        title.setGravity(Gravity.CENTER);
        title.setTextColor(Color.WHITE);
        title.setTextSize(20);

        builder.setCustomTitle(title);

        //antes era alertDialog
        Dialog dialog = builder.create();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.gravity = Gravity.TOP | Gravity.LEFT;
        lp.x = 0;   //x position
        lp.y = 85;   //y position
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

        dialog.show();
        dialog.getWindow().setAttributes(lp);

    }

}

The code of the style is this:

<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
        <item name="android:textColor">@color/black</item>
        <item name="android:windowBackground">@color/warningA</item>
        <item name="android:background">@color/warningA</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:typeface">normal</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>

And the activity is like this:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="bem.banorte.bem.ThirdActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

enter image description here

Upvotes: 0

Views: 150

Answers (1)

Amit K. Saha
Amit K. Saha

Reputation: 5951

Update:

After cross checking build.gradle and everything with OP, it seems a device specific issue. This code does take full width in Nexus 5 (api 23) real device and Pixel2 (Api 23) AVD.

Our suspect is - @android:style/Theme.Dialog is not same in this Motorola G Play (Android 6.0.1 api 23) device.

Original:

Your dialg is getting the full width. I tried your code but switching colors as following in your theme-

<item name="android:windowBackground">@android:color/holo_blue_light</item>
<item name="android:background">@android:color/holo_red_light</item>

And I am seeing this

  • Occupied Dialog width

Upvotes: 1

Related Questions