HsRaja HsRaja - 4 years ago 169
Android Question

Alert dialog custom layout width was not fit to the screen

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>

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download