I'm having problems inflating an AlertDialog with a custom XML. The problem is that it doesn't respect the width/height I've specified.
volume.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="6dp" android:gravity="center"
android:layout_gravity="center_horizontal" android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="250dip"
android:id="#+id/layoutRoot">
<TextView android:layout_width="wrap_content"
android:layout_gravity="left" android:id="#+id/textView1"
android:layout_height="wrap_content" android:text="Volume:"
android:layout_marginBottom="10dp" android:textSize="22dp"
android:layout_marginTop="10dp"></TextView>
<SeekBar android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="#+id/volumeBar"
android:minHeight="10dp" android:maxHeight="15dp" android:paddingLeft="10dp"></SeekBar>
<TextView android:layout_width="wrap_content"
android:layout_gravity="left" android:layout_height="wrap_content"
android:layout_marginBottom="10dp" android:textSize="22dp"
android:text="20%" android:id="#+id/volumeText"></TextView>
</LinearLayout>
Code:
Builder alertbox = new AlertDialog.Builder(this);
AlertDialog alertDialog;
LayoutInflater mInflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View VolumeView = mInflater.inflate(
R.layout.volume, (ViewGroup) findViewById(R.id.layoutRoot));
alertbox.setView(VolumeView);
alertDialog = alertbox.create();
alertDialog.show();
Your problem may be the layoutRoot you are providing as the second parameter of your call to mInflater.inflate(). This will affect the layout of the XML file being inflated.
Your question needs more information for anyone to give a better answer than this.
Related
I have a problem with this specific dialog, I implemented it as an option to be shown in the Options Menu in my toolbar but it doesn't show the dialog's contents when I clicked on the option. I have tried these with other dialog layouts I have implemented and all of them work when I implemented them here so I have a hunch that the problem is with the XML layout but I can't seem to find the problem.
Code:
else if(id == R.id.action_setting)
{
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_settings);
dialog.setTitle("Settings");
CheckBox set_splash = dialog.findViewById(R.id.setting_check);
boolean have_splash =pref.getBoolean(SplashActivity.HAVE_SPLASH,false);
if(have_splash)
set_splash.setChecked(true);
else
set_splash.setChecked(false);
ImageButton confirm_btn = dialog.findViewById(R.id.setting_confirm);
ImageButton cancel_btn = dialog.findViewById(R.id.setting_cancel);
confirm_btn.setOnClickListener(v -> {
boolean new_splash = set_splash.isChecked();
edit.putBoolean(SplashActivity.HAVE_SPLASH,new_splash);
edit.commit();
dialog.dismiss();
});
cancel_btn.setOnClickListener(v -> dialog.dismiss());
dialog.show();
}
XML :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout5">
<ImageButton
android:id="#+id/setting_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/button_highlight_dark"
android:paddingVertical="20dp"
app:srcCompat="#drawable/ic_confirm" />
<ImageButton
android:id="#+id/setting_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/button_highlight_dark"
android:paddingVertical="20dp"
app:srcCompat="#drawable/ic_cancel" />
</LinearLayout>
<TextView
android:id="#+id/setting_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/themeDark"
android:paddingVertical="10dp"
android:paddingLeft="10dp"
android:text="#string/settings"
android:textColor="#color/white"
android:textSize="10pt"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/linearLayout5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/themeLight"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/setting_text">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="6"
android:textColor="#color/white"
android:paddingVertical="20dp"
android:paddingLeft="10dp"
android:text="#string/splash_setting"
android:textSize="10pt" />
<CheckBox
android:id="#+id/setting_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
The match_parent parameters in your dialog are ignored. Therefore, the dialog has a width of 0.
The easiest way to get the correct height and width, is by using an AlertDialog.Builder.
If a customized AlertDialog isn't enough, you can make the dialog full size by following this answer:
Android get full width for custom Dialog
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_settings);
Window window = dialog.getWindow();
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
I have an alertdialog with a custom layout, including a scrollview.
When the dialog appears, the scrollview is "cropped", and appears to be anchored to the top of the Screen, rather than the top of the dialog
I have searched SE for similar problems, and cannot find any.
Using a popup Window has exactly the same issue.
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rock">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/scrollbutton"
android:padding="12dp"
android:text="Room Number"
android:textColor="#android:color/black"
android:textSize="22sp" />
<TextView
android:id="#+id/contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/scrollbutton"
android:gravity="center"
android:padding="12dp"
android:text="Here be Monsters" />
</LinearLayout>
</ScrollView>
And here is the dialog code:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.contents, null);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setView(layout);
AlertDialog alert = alertDialog.create();
alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
alert.show();
Window window = alert.getWindow();
window.setLayout((int)(screenWidth*.8), (int)(screenHeight*.8));
I have tried adding the ScrollView as the root_view in the inflater, and also adding a LinearLayout around the ScrollView and setting this as root_view, but this does not alter the results
You don't set parent to layout inflater. Let dialog manages view arranging DialogFragment
I make setting customtitle of alertdialog builder with a view that contains two webviews for animated gif and textview between them. the webviews are shown in the title of the alert dialog but the textview is missing.
here the view XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="75dp"
android:weightSum="1"
android:background="#android:color/background_dark">
<WebView
android:layout_width="wrap_content"
android:layout_height="75dp"
android:id="#+id/Title_webView1"
android:layout_weight="0.15"
android:layout_gravity="left" />
<TextView
android:id="#+id/titletextView"
android:layout_width="match_parent"
android:layout_height="75dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center"
android:layout_weight="0.7"
android:textAlignment="center"
android:textIsSelectable="false"
android:visibility="visible"
android:gravity="center_vertical|center_horizontal"
android:inputType="none"
android:textColor="#d0d2d116"
android:text="test123"
android:singleLine="true"
android:textSize="32dp" />
<WebView
android:layout_width="wrap_content"
android:layout_height="75dp"
android:id="#+id/Title_webView2"
android:layout_weight="0.15"
android:layout_gravity="right" />
here the view function:
private View setCustomDialogTitle(String Title)
{
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.embark_title, null);
TextView TitleText = (TextView) view.findViewById(R.id.titletextView);
TitleText.setText(Title);
WebView flashingLight1 = (WebView) view.findViewById(R.id.Title_webView1);
flashingLight1.loadUrl("file:///android_asset/flashlight751.gif");
WebView flashingLight2 = (WebView) view.findViewById(R.id.Title_webView2);
flashingLight2.loadUrl("file:///android_asset/flashlight752.gif");
return view;
}
and the function call:
AlertDialog.Builder alrtBuilder= new AlertDialog.Builder(this);
alrtBuilder.setCustomTitle(setCustomDialogTitle("Test123"));
the issue is with setting layout_width for the components and again setting them with weight.
so a small change in xml can help you out.The confusion in rendering will be the cause.
set all the layout_width attribute to "0dp"
android:layout_width="0dp"
for the three components, then it will bring you the exact look as you want. for some reference read this article, it really helps you to understand.
NB: tested and worked for your code
Hello I'm trying to show a custom AlertDialog on the screen.
I'm inflating an layout into the dialog, this layout includes Two TextViews, one RatingBar and one EditText.
When I try to popup the dialog I'm getting this exception :
E/AndroidRuntime(12989):
android.view.InflateException: Binary XML file line #25: Error
inflating class android.widget.EditText
Important point is this error occurs on Android 2.3.x Devices, It works great on Android 4.x
dialog_comment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RatingBar
android:id="#+id/DialogCommentRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:stepSize="1" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/DialogCommentRatingBar"
android:text="Yorumunuz"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/DialogCommentComment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:background="?android:attr/editTextBackground"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine" >
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Oyunuz"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
CommentDialog.java
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater=getActivity().getLayoutInflater();
final View view=inflater.inflate(R.layout.dialog_comment, null);
final RatingBar rating=(RatingBar)view.findViewById(R.id.DialogCommentRatingBar);
final EditText comment=(EditText)view.findViewById(R.id.DialogCommentComment);
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
builder.setView(view);
builder.setTitle("Enter Your Comment!");
return builder.create();
}
How do I show AlertDialog
FragmentManager fm = getSupportFragmentManager();
CommentDialog commentDialog = new CommentDialog();
commentDialog.show(fm, "fragment_comment_dialog");
Replace
android:textAppearance="?android:attr/textAppearanceLarge"
with
android:textAppearance="#android:style/TextAppearance.Large"
Deleted my EditView (Plain Text text field), dropped another one into content_....xml and did not change any styling this time. Everything ran correctly with the default styling on the EditView.
I am designing an "about" menu for an app and have used the following code in an options menu to generate it:
case DIALOG_ABOUT:
builder = new AlertDialog.Builder(this);
Context context = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.about_dialog, null);
builder.setView(layout);
TextView title = new TextView(this);
title.setText(R.string.about_title);
title.setGravity(Gravity.CENTER);
title.setTextSize(20);
builder.setCustomTitle(title);
builder.setPositiveButton("OK", null);
dialog = builder.create();
break;
I have created two different views for the about menu - for both horizontal and vertical viewings.
When displaying vertically, the about menu looks fine, but when displaying horizontally the bottom part of the text is being cut off. I am using the LayoutInflator and View controls without having that much knowledge about how they work, so I assume they are currently filling to some Android-specified size.
How can I create the dialog to take up the whole screen, or at least 90% of it?
Edit - My layouts looked like this:
Vertical Layout:
<ImageView
android:id="#+id/imageView1"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/icon" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/game_name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="#string/game_description"
android:textAppearance="?android:attr/textAppearanceMedium" />
Horizontal Layout:
<ImageView
android:id="#+id/imageView1"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/icon" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/game_name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="#string/game_description"
android:textAppearance="?android:attr/textAppearanceMedium" />
You may call the following on your dialog to fill entire space.
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);