I have an alert dialog created using the alertdialog builder. I want to remove the space in the left and right of the dialog... basically extend it from side to side. I know I could you an activity instead of a dialog, but I want to keep the button style and implementing that button style in an activity requires making a layout for different SDKs, which is not convenient in the long run.
Why I need it full width?
Because I need to display AdMob ads and if they are not full width the ads will not load.
Any help is appreciated as I have tried all kinds of theme properties...
Thanks,
Adrian
PS: Here is my current code for creating the dialog:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.DialogTheme));
alertDialogBuilder.setInverseBackgroundForced(true);
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.dial_dialog, null);
AdView adView = (AdView) view.findViewById(R.id.adView);
if (!application.getLicense().isValid()) {
adView.loadAd(new AdRequestWrapper(this));
}
alertDialogBuilder.setView(view);
alertDialogBuilder.setTitle(R.string.dial_dialog_title).setCancelable(false);
alertDialogBuilder.setPositiveButton(R.string.dial_dialog_message_positive_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alertDialogBuilder.setNeutralButton(R.string.dial_dialog_message_neutral_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alertDialogBuilder.setNegativeButton(R.string.dial_dialog_message_negative_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
PS2: Here is an image with what I need... http://www.tiikoni.com/tis/view/?id=f3aed38
I need to loose all the space with red. I don't have any problems with the space marked in yellow. (it can be kept or it can be removed)
I know a little bit late to come back with an answer, but following the recipes from here I managed to solved almost everything. Basically I have the following Dialog theme:
<style name="DialogTheme" parent="android:Theme.Holo.Light.Dialog">
<item name="android:windowBackground">#drawable/screen_background_selector_light</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
</style>
Note the reference to screen_background_selector_light which means I also had to add screen_background_selector_light.xml and (its dependency background_holo_light.xml).
Other that that I only had to specify the activity will use this new theme:
<activity android:name=".zom.xyz.app.activity.MyActivity_" android:label="#string/activity_title" android:theme="#style/DialogTheme"/>
And of course create it as any other activity.
As for the other need I had... easy buttons I did it this way. Here is the layout of the activity
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
INSERT_DIALOG_CONTENT_HERE
</RelativeLayout>
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/cancelButton"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="fill_vertical"
android:layout_weight="1"
android:text="#string/activity_message_negative_text"/>
<Button
android:id="#+id/skipButton"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="fill_vertical"
android:layout_weight="1"
android:text="#string/activity_message_neutral_text"/>
<Button
android:id="#+id/correctButton"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="fill_vertical"
android:layout_weight="1"
android:text="#string/activity_message_positive_text"/>
</LinearLayout>
Using this layout and theme will give you the exact feel of a dialog while giving it full width.
1 )Create your own dialog extended from Dialog and in constructor set style, something like this:
public MyDialog(Context c, Event e) {
super(c, android.R.style.Theme_Light);
}
or
2) when creating instanse, set style:
Dialog dialog = new Dialog(this, R.style.MyDialogTheme);
example here
Related
I have created a custom layout with 3 buttons for alertdialog which is working fine. I am trying to make the layout so that width and height layout will be only wrap its content i.e. no extra width/height but I am unable to do so. Can you help me on this please?
Here is my custom layout for alertdialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#00ffffff"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/rectangle_menu"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/wowButtonId"
android:layout_marginLeft="5dp"
android:src="#drawable/love_icon"
android:background="#drawable/round_button_for_round_menu_like_button"
android:layout_gravity="center"
/>
<ImageButton
android:layout_width="40dp"
android:layout_height="35dp"
android:id="#+id/blehButtonId"
android:layout_marginLeft="5dp"
android:src="#drawable/bleh"
android:background="#drawable/round_button_for_round_menu_like_button"
android:layout_gravity="center"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dislikeButtonId"
android:layout_marginLeft="5dp"
android:src="#drawable/dislike_icon"
android:background="#drawable/round_button_for_round_menu_like_button"
android:layout_gravity="center"
/>
</LinearLayout>
</LinearLayout>
alertDialog implement in adapter code:
AlertDialog.Builder builder=new AlertDialog.Builder(context);
AlertDialog alertDialog=builder.create();
View view1=LayoutInflater.from(context).inflate(R.layout.layout_for_long_like_button_option,null);
ImageButton wow=(ImageButton) view1.findViewById(R.id.wowButtonId);
ImageButton disLike=(ImageButton) view1.findViewById(R.id.dislikeButtonId);
ImageButton bleh=(ImageButton) view1.findViewById(R.id.blehButtonId);
wow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"wow",Toast.LENGTH_SHORT).show();
}
});
disLike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"dislike",Toast.LENGTH_SHORT).show();
}
});
bleh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"bleh",Toast.LENGTH_SHORT).show();
}
});
alertDialog.setView(view1);
alertDialog.show();
try this on your class file where you inflate your alert dialog...
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alertDialog.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
Summary: Use RelativeLayout tag at the root in your custom layout
I had two alert dialogs in the application I was writing. One of them did not wrap content, while the other did. The one with the LinearLayout tag at its root, was expanding a tad more in height, than the space its contents really occupied.
I tried setting the width and height properties to wrap_content on the LinearLayout but to no avail.
The one with the RelativeLayout tightly wrapped its contents. The code structure to achieve this would look like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<!-- More widgets -->
</LinearLayout>
</RelativeLayout>
Although care must be taken, that the inner LinearLayout should still specify wrap_content. You can of course specify match_parent if your intention is to present an elongated dialog, the intentions for doing so, which I would leave it to the developer.
Android Studio still warns me that The LinearLayout layout or its RelativeLayout parent is useless. I do not consider this a solution to the problem, rather it's just a fix. And, I should mention that I've always had these layout problems working with Android. This, I feel, is a more saner approach as it keeps the code modifications down to the layout.
You can use below code for set layout and show dialog
public void showDialog() {
LayoutInflater li = LayoutInflater.from(MainActivity.this);
View promptsView = li.inflate(R.layout.layout_for_long_like_button_option, null);
final TextView txtOk = (TextView) promptsView.findViewById(R.id.txtOk);
final TextView txtCancel = (TextView) promptsView.findViewById(R.id.txtCancel);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(BaseActivity.this);
alertDialogBuilder.setView(promptsView);
final AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
txtOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
txtCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
You can try setting the width of the inflated view directly when the dialog is shown and therefore has a window
val dialog = AlertDialog.Builder(context)
.setView(myView)
.create()
dialog.setOnShowListener {
dialog.window?.setLayout(
myView.width,
ViewGroup.LayoutParams.WRAP_CONTENT
)
}
I am trying to add a custom title to my Dialog, however whenever I run my application it doesn't show a title.
My code for creating the dialog is
final Dialog passwordDialog = new Dialog(this);
passwordDialog.setContentView(R.layout.admin_password_dialog);
passwordDialog.setTitle("Enter An Administrative Password");
passwordDialog.show();
And my layout file is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_confirmPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/edit_adminPassword"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="#string/confirmPassword"/>
<EditText
android:id="#+id/edit_adminPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:ems="10"
android:inputType="textPassword"/>
And here is what I am getting
Is there something I am missing?
you should define your style like this:
<style name="Dialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">false</item>
<item name="android:windowIsFloating">true</item>
</style>
and then pass this style to the constructor of the Dialog
final Dialog passwordDialog = new Dialog(this,R.style.Dialog);
Like the other answer, but more concise
final AlertDialog diag = new AlertDialog.Builder(this)
.setTitle("Enter An Administrative Password")
.setView(R.layout.admin_password_dialog)
.create();
diag.show();
Button diagButton = (Button) diag.findViewById(R.id.btn_confirmPassword);
diagButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// handle button click
EditText input = (EditText) diag.findViewById(R.id.edit_adminPassword);
String s = input.getText().toString();
}
});
You can try this method as well and get different view styles based upon theme used.
<style name="FilterDialogTheme" parent="#android:style/Theme.Holo.Light.Dialog">
<item name="android:windowNoTitle">false</item>
</style>
In Dialog constructor
public FilterDialog(Context context) {
super(context, R.style.FilterDialogTheme);
}
Use #style/Theme.Appcompat.Light.Dialog for your project.
You should use an AlertDialog.Builder instead of just creating a Dialog:
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// 2. Chain together various setter methods to set the dialog characteristics
builder.setView(R.layout.admin_password_dialog);
builder.setTitle("Enter An Administrative Password");
// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
dialog.show();
See here for the Android Developers Guide on Dialogs.
I used AlertDialog (android.support.v7.app.AlertDialog) for showing levels of game as below
AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.DialogLevelsStyle);
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_game_level, null);
builder.setView(view)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (dialog != null) {
dialog.cancel();
}
}
});
dialog = builder.create();
dialog.show();
//styles.xml
<style name="DialogLevelsStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#3caaf0</item>
</style>
But the dialog display the positive button OK at the button-right of the alert dialog, Please help me!
I update the question by added image:
This happens because of Rules for aligning buttons.
Action buttons are typically Cancel and/or OK, with OK indicating the preferred or most likely action. However, if the options consist of specific actions such as Close or Wait rather than a confirmation or cancellation of the action described in the content, then all the buttons should be active verbs. Order actions following these rules:
The dismissive action of a dialog is always on the left. Dismissive actions return to the user to the previous state.
The affirmative actions are on the right. Affirmative actions continue progress toward the user goal that triggered the dialog.
You can't change alignment for your positive/negative buttons. Simply add Button to your view which you inflate for dialog aligned to the center and process it as below:
Button btn = (Button) view.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (dialog != null) {
dialog.cancel();
}
}
});
Update
This is how it should look like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:text="something"
android:id="#+id/id1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
<Button
android:id="#+id/id2"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RelativeLayout>
P.S. I cannot check if everything properly, cuz my design window have some problems in Studio, but this is the right way and seems to work fine.
I'm targeting API level 8+. I need a dialog with a title, a message, a checkbox with some text and two classic bottom buttons.
I'm trying to use a standard AlertDialog, and method setView to add the checkbox:
checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<CheckBox
android:id="#+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:checked="true"
android:textSize="14sp" >
</CheckBox>
</FrameLayout>
showDialog method
private void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Title");
builder.setMessage("Message");
View checkboxView = View.inflate(activity, R.layout.checkbox, null);
CheckBox checkBox = (CheckBox) checkboxView.findViewById(R.id.checkbox);
checkBox.setText("Some checkbox text");
builder.setView(checkboxView);
// First button
builder.setNegativeButton(getString(R.string.not_now),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
});
// Second button
builder.setPositiveButton(getString(R.string.help),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
});
builder.create();
builder.show();
}
My AppBaseTheme has parent "Theme.AppCompat.Light" (/res/values/styles.xml), the same for values-v11 and "Theme.AppCompat.Light.DarkActionBar" for values-v14.
I've problem in 2.3.3, checkbox and text have no margins or padding (I've set them in xml), besides text is black as the background. Everything looks fine in 4.4.
I've read a lot of things about this problem, but I'm still confused and without solution. Can you please help me?
You can force a white background of your dialog by using:
if(Build.VERSION.SDK_INT <= 10) {
builder.setInverseBackgroundForced(true);
}
I am having an issue with a custom view in a dialog on android API 10.
I use AlertDialog.Builder to construct my dialog. I include a custom view panel using the setView command on the builder.
This works on most of the API's I've tested with. The style changes somewhat from device to device, but that is what I want, for the style to match the device default.
My problem is that on API 10, any text that is in my custom view shows up as black on a black background.
Any text I insert using AlertDialog.Builder.setMessage() appears correctly.
What magical attribute/style is the dialog builder using to determine text appearance?
My app's theme is Theme.AppCompat.Light.
Here is my onCreateDialog method:
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.fragment_status_dialog, null);
mStatusTextView = (TextView) view.findViewById(R.id.text_status);
mConnectedDeviceTextView = (TextView) view.findViewById(R.id.text_connected_device);
MainService.ServiceState state = null;
if (getArguments().containsKey(SERVICE_STATUS_ARG_KEY)) {
state = (MainService.ServiceState) getArguments().getSerializable(SERVICE_STATUS_ARG_KEY);
}
setState(state);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);
builder.setMessage("This will show up just fine.");
builder.setTitle(getString(R.string.status_title));
builder.setNegativeButton(R.string.dialog_back_button_text, null);
builder.setNeutralButton(R.string.dialog_connect_to_text, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
mListener.onDialogConnectTo();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
Here's my fragment_status_dialog layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="18dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/status_starting"
android:id="#+id/text_status"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/status_connected_to_unknown"
android:paddingStart="4dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:id="#+id/text_connected_device"
android:layout_toRightOf="#+id/text_status"
android:layout_toEndOf="#+id/text_status"/>
</RelativeLayout>
Note, I've tried https://stackoverflow.com/a/24505312/2350083 but it didn't fix it.
Try calling AlertDialog#setInverseBackgroundForced(true).
What about simply setting the color of the text? Ex:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:text="#string/status_starting"
android:id="#+id/text_status"/>
The TextView is using the default text color of the device (or the app). If you set the color specifically to the TextView it will be overriden on devices irrespective of the API.