0

Custom Alert dialog width was not fit to the screen size and i tried many methods regards this nothing is working(Alert dialog width should fit to the screen size fully)If you have any suggestion regards this please share your idea.

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;

public class Sample extends Activity {

final Context context = this;
Button btn;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple);

    btn = (Button) findViewById(R.id.buttonShowCustomDialog);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.research);
            dialog.setTitle("Title...");
            dialog.show();

        }
    });
}
}

//simple.xml

<?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" >

<Button
    android:id="@+id/buttonShowCustomDialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show Custom Dialog" />

</LinearLayout>

//research.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#00555555"
    >

<RelativeLayout
    android:id="@+id/pakka"
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_centerInParent="true"
    android:background="#c8c8c8">

    <TextView
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:padding="22dp"
        android:text="Hi"/>

    <TextView
        android:id="@+id/nik"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/hello"
        android:layout_centerHorizontal="true"
        android:padding="5dp"
        android:text="Hi"/>

    <Button
        android:layout_width="360dp"
        android:layout_height="60dp"
        android:layout_below="@+id/peetnik"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Login to see"/>
    </RelativeLayout>


</RelativeLayout>
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
Rajakumar
  • 907
  • 1
  • 7
  • 17

5 Answers5

2

Add this in your style.xml file

  <style name="dialog_theme" parent="android:style/Theme.Dialog">
        <item name="android:windowMinWidthMajor">90%</item>
        <item name="android:windowMinWidthMinor">90%</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

Then add this in your dialog initialization place

final Dialog dialog = new Dialog(context, R.style.dialog_theme);
dialog.setContentView(R.layout.research);
dialog.setTitle("Title...");
dialog.show();
Chamindu
  • 391
  • 1
  • 4
  • 8
0

I found similar issue and the solutions are below.

1) https://stackoverflow.com/a/17724940/1567907

2) https://stackoverflow.com/a/14918253/1567907

Community
  • 1
  • 1
Rikin Prajapati
  • 1,913
  • 2
  • 16
  • 23
-1

try this:

in your res/values/styles:

 <style name="dialog_style" parent="android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

Now in your activity use the following code to show the dialog:

     final Dialog dialog = new Dialog(Sample.this,R.style.dialog_style);
     dialog.setContentView(R.layout.research);
     dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PA‌​RENT, ViewGroup.LayoutParams.MATCH_PARENT); 
     dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
     dialog.setCancelable(true);
     dialog.setCanceledOnTouchOutside(true);
     dialog.show();

Here ViewGroup.LayoutParams.MATCH_PARENT will fill the screen width

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
-1

use dialog.getWindow().setLayout(SCREEN_WIDTH, SCREEN_HEIGHT);

user320676
  • 394
  • 2
  • 19
-1

simply use WindowManager.LayoutParams()

final Dialog dialog = new Dialog(getActivity());
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.research);
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(dialog.getWindow().getAttributes());
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.MATCH_PARENT;

            dialog.show();
            dialog.getWindow().setAttributes(lp);
  • 1
    Here width was not fit to full screen and also that white background came along with this :( – Rajakumar Feb 06 '17 at 10:02
  • for background color you can use dialog.getWindow() .setBackgroundDrawable(new ColorDrawable(Color.WHITE)); white may be anything . Try fill parent instead of match parent – Er Kishor Maharjan Feb 06 '17 at 10:07