Reputation: 14213
I am trying to create custom dialog.
Base Layout (I have also tried various modified layouts):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@color/dialogHeaderBackground"
android:gravity="center"
android:minHeight="@dimen/minHeightDialogTitle"
android:text=""
android:textStyle="bold" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/title"
android:layout_marginTop="5dp"
android:text="" />
<LinearLayout
android:id="@+id/loutButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/message"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<Button
android:id="@+id/btnNeutral"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
<Button
android:id="@+id/btnNegative"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
<Button
android:id="@+id/btnPositive"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
</LinearLayout>
</RelativeLayout>
Java code:
Dialog dialog = new Dialog(this);
// Dialog dialog = new Dialog(this,R.style.Theme_Dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
Button btnPositive=(Button) dialog.findViewById(R.id.btnPositive);
Button btnNegative=(Button) dialog.findViewById(R.id.btnNegative);
Button btnNeutral=(Button) dialog.findViewById(R.id.btnNeutral);
TextView txvTitle=(TextView)dialog.findViewById(R.id.title);
TextView txvMessage=(TextView)dialog.findViewById(R.id.message);
...
so on and so forth
I get following dialog, which is fullscreen, which I do not want:
Solutions already tried, which give same results as above screenshot:
MATCH_PARENT
in height. As said in this Stack overflow answerWRAP_CONTENT
for dialog based on answer here | Implemented Java Code in PastebinRelativeLayout
inside a parent LinearLayout
, see the 3rd comment in following answerThe only time I can get decent dialog is when specifying height for the XML layout, like this android:layout_height="150dp"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp" >
Dialog when fixed height is specified:
Don't suggest the above (specify fixed height) as solution as it is not efficient and not looking that good and also not dynamic, it's static.
What to do specify,decent looking custom dialog which is customizable too?
Upvotes: 5
Views: 1791
Reputation: 13
This was my approach when I faced the same problem.
I extended DialogFragment (from support.v4 lib)
And then inflate:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.dialog_whatever, null);
builder.setView(view);
return builder.create();
}
This is the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corners"
android:padding="20dp">
<TextView
android:id="@+id/confirm_dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@color/black"
android:text="@string/are_you_sure_declining_whatever"/>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:orientation="horizontal">
<Button
android:id="@+id/cancel_action"
android:layout_marginRight="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:text="@string/cancel"
android:textColor="@color/accent_color"/>
<Button
android:id="@+id/confirm_action"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:textColor="@color/accent_color"
android:text="@string/reject"/>
</LinearLayout>
</LinearLayout>
This approach also follows Material design guidelines!!
Hope this is of help to you
Best regards,
pedrovic90
Upvotes: 0
Reputation: 2417
Ya, it happens with relativelayout. in its google docs clearly mention.
Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM
As you are working with dialog you have to use linearlayout as per your requirement. It will defiantly work for you.
further ideas may be not applicable for you, but I just mention it for reference
you can use java code for make fix size dialog, or you can set layout margin from layout params so that you can maintain responsiveness in different screen size. You also can one thing, get size of screen
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
than you can calculate your new dialog size and set it as @guobao's Answer
DisplayMetrics displayMetrics = new DisplayMetrics();
dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = (new width);
params.height = (new height);
ask me if any further specification needed.
Upvotes: 2
Reputation: 224
you can set this code in your dialog
DisplayMetrics displayMetrics = new DisplayMetrics();
dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = (the width you want to set);
params.height = (the height you want to set);
Upvotes: 0
Reputation: 2559
remove android:layout_alignParentBottom="true"
line from linear layout
Upvotes: 6
Reputation: 6025
<LinearLayout
android:id="@+id/loutButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/message"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
remove android:layout_alignParentBottom="true"
from above code and check.
Upvotes: 3