For most of my Android app's dialogs I use Builder's built-in methods, such as setSingleChoiceItems, but I need 2-3 dialogs that are custom AlertDialogs using my own views.
The custom dialogs look very different from the ready-made ones (text size, background etc).
How can I make my custom dialogs look like the Builder ones?
I have not specified any theme for any of the dialogs.
I created this one using an AlertDialog.Builder with the setSingleChoiceItems(...) method.
This one was created using builder.setView(layout).
You can use custom view with setView, but I recommend to use DialogFragment, in DialogFragment you can use any View and just View event handlers like onClickListener etc.
try out this
set custom theme in dialog to set background and no title of dialog as below
<style name="ActivityDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#drawable/dialog_background_image</item>
<item name="android:windowNoTitle">true</item>
</style>
and make a layout of UI
protected void createDialog() {
// TODO Auto-generated method stub
dialog = new Dialog(this,R.style.ActivityDialog);//this take context and style parameter
dialog.setContentView(R.layout.dialogUI); // here load coustom UI layout
Button okbtn = (Button)dialog.findViewById(R.id.button1);
Button cancelbtn = (Button)dialog.findViewById(R.id.rep_cancel_Button);
okbtn .setOnClickListener(okClickListener);
cancelbtn .setOnClickListener(cancelClickListener);
dialog.show();
}
Related
I'm using an AlertDialog to show the result of a game. My problem is that the dialog seems to changes the depiction of my normal activiymain layout while the dialog is open.
That's the layout without the dialog
That's the layout while the dialog is open
As you can see the Textviews that i use as buttons are colored weirdly.
My Dialog is created like this:
AlertDialog.Builder = new AlertDialog.Builder(MainActivity.this);
turnout.setTitle("Dialog");
The mainactivity layout is a relative layout and the textviews are colored in #ffffff (meaning white)
By default Android dims the background (displays the "grey shadow" behind the dialog) whenever a dialog is displayed. Changing this default behavior may decrease the readability of a dialog and confuse a user.
That being said, you can disable the background by setting the parameter android:backgroundDimEnabled to false in your dialog's style.
You add the theme to your styles.xml:
<style name="NoDimAlertDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="android:backgroundDimEnabled">false</item>
</style>
Make sure to inherit the default AlertDialog's style.
Additionally you can control the transparency of the dim, using android:backgroundDimAmount. This parameter takes a value from 0 (no dim) to 1 (background completely black). By default Android seems to use 0.6.
<style name="LessDimAlertDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="android:backgroundDimAmount">0.25</item>
</style>
Next, pass the style name to theAlertDialog.Builder's constructor in your MainActivity.java:
new AlertDialog.Builder(this, R.style.NoDimAlertDialog)
.setTitle("Some title")
.setMessage("Some message")
.show();
You may as well consider using a Fragment to display your AlertDialog. Android comes with handy DialogFragment class for that scenario. A simple DialogFragment in your case may look like that:
class MyDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getContext(), R.style.NoDimDialog)
.setTitle("Some title")
.setMessage("Some message")
.create();
}
}
Then you add your Fragment in MainActivity.java:
new MyDialogFragment().show(getSupportFragmentManager(), "DialogTag");
My current method to customize my UI is using the usual android DatePicker then use DatePicketDialog.getDatePicker() to get the inside component out, and customize it.
Now the result is in the image at https://dl.dropboxusercontent.com/u/3286004/Screen%20Shot%202557-08-29%20at%202.52.21%20AM.png
The Question is ... I want to customize the black line above the DONE button to another color.
Could you suggest how I can get that line component out, so I can change it.
Thank you in advance :D
This is absolutely possible, actually you could do whatever you want with it. Really, one of options is to use style and theme which however would not work in 4.x. The more, lets say, proper or easy way is to use views itself like following:
// we need this listener since only here all views are really drawn and accessible
yourDialog.setOnShowListener(new DialogInterface.OnShowListener() {
private boolean areButtonsFixed;
#Override
public void onShow(DialogInterface dialog) {
if (areButtonsFixed)
return;
// both buttons - you could search for only positive button or whatever button your dialog has
final Button btnPositive = getButton(DatePickerDialog.BUTTON_POSITIVE);
final Button btnNegative = getButton(DatePickerDialog.BUTTON_NEGATIVE);
final Button btnNeutral = getButton(DatePickerDialog.BUTTON_NEUTRAL);
// buttons layout parameters, change it into material style (gravity right)
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) btnPositive.getLayoutParams();
lp.weight = 0; // was 1 to fill 50% horizontally
// positive button, set your own label
btnPositive.setText(R.string.dialog_ok_label);
// set text color and size
btnPositive.setTextColor(ResHelper.getColor(R.color.blue_bright));
btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResHelper.getDimensPx(R.dimen.text_size_14));
btnPositive.setLayoutParams(lp);
// divider above buttons
((LinearLayout) btnPositive.getParent().getParent()).setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
areButtonsFixed = true;
}
This (prelast line) will remove divider above buttons at all. If you wish to customize it instead do it like following:
((LinearLayout) btnPositive.getParent().getParent()).setDividerDrawable(R.drawable.yer_drawable);
One way would be to use another theme. This theme is Holo i think, so you can't change colors.
I think you can create your dialog with a custom layout.
If you used a custom layout, you can change colors.
Or, you should use another theme, or create your own theme.
EDIT
Yep, at run-time too.
Many things using on your layout are locked, like colors, especially on widgets (searchView for example)
In default dialog it is impossible, this line has system color. You should convert this dialog to activity, then you can change color there.
In my activity theme in the themes.xml I have set a background color in order to move away from the default (transparent/white?) background color to my own.
<item name="android:background">#color/red</item>
Unfortunately, when the I am showing my loading dialog the color shines halfway through that dialog now. Was this to be expected?
I have tried to use different themes, also defined by own dialog theme subclassing from Holo Light setting the background color explicitly to white, but the problem persists, only the currently still white areas are changed in this case.
What can I do? The only alternative is currently to use the Tradiotional Dialog Theme.
Try to set android:windowBackground instead. android:background attribute is applied to all nested views. Here is the discussion: What's the difference between windowBackground and background for activities style?
It looks like there's some padding or margins to the left and right of the title. If you're using the built-in ProgressDialog I'd suggest creating your own Dialog instead, that way you can change anything you want about it. Just create your own xml layout and create the dialog like this:
protected static Dialog getProgressDialog(Activity activity) {
Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View progressDialogView = inflater.inflate(R.layout.custom_progress_dialog, null);
dialog.setContentView(progressDialogView);
dialog.setCancelable(false);
return dialog;
}
I have a screenshot below of a random dark/black slightly downwards gradient line appearing above my dialog fragments.
These are build with a dialogfragment class that has been overridden, and an alertdialog builder is being used to construct them (happens with and without the title/buttons) inside the following method
public Dialog onCreateDialog(Bundle savedInstanceState)
Anyone had this happen to them before or have any ideas?
Ive tried to theme them differently, and the same happens with both API14 holo and holoeverywhere library. Ive tried to set the backgrounds to transparent ect... but havent achieved anything except making the dim go away.
You need to add your custom theme for your dialog and provide android:windowContentOverlay parameter.
<style name="MyDialogTheme">
<item name="android:windowContentOverlay">#null</item>
</style>
Then, in your DialogFragment in onCreate call:
setStyle(/* desired style */, R.style.MyDialogTheme);
The Weird Line appears because of the title bar. You just need to hide the title bar and it automatically hides the weird line:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
// make your dialog here
return dialog;
}
Try this,
private Dialog custom_dialog;
private Window window;
custom_dialog = new Dialog(context);
custom_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
custom_dialog.setContentView(R.layout.share_dialog);
custom_dialog.setCancelable(true);
//Below code is used to remove wired black line
window = custom_dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
window.setBackgroundDrawableResource(R.drawable.empty);
Found one way of getting around my problem.
So the deal was that the FrameLayout (with id: android:id/content) that the system makes itself had a foreground drawable on it (the shadow at the top of the frame). I couldnt for the life of me deal with it with styles or anything, nor could i figure out why it was happening on these two custom alert dialogs inside dialogfragments.
note: im using a HoloEverywhere fork thats compatible with v7 AppCompat, but keeping as many of the classes ect... from the actual support libraries.
I could however access the framelayout and remove the foreground drawable with this:
(note: it had to be in the onresume as i dont think there are any calls before that resulted in a "android.util.AndroidRuntimeException: requestFeature() must be called before adding content" error.
public void onResume() {
super.onResume();
((FrameLayout) getDialog().getWindow().getDecorView().findViewById(android.R.id.content)).setForeground(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
If theres a better safer way of doing this, or if anyone knows why this is happening, let me know.
I need to implement user popup dialogs, but they don't look like standard. I guess, there's a way of implementing custom popups by means of Android's functionality (custom dialogs), but is it possible to decorate them ?
Yet it might not be the best solution for this problem - should I try something else like hidden layouts ?
Thank you.
Yes you can change your Dialog box as you want.You can do this by creating a custom dialog box.
Step1. Create a style in String file in res
<style name="myQuitDialog" parent="android:Theme.Dialog">
<item name="android:gravity">center_horizontal</item>
</style>
Step2. Create the xml file in layouts
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_quit"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image which u want to show"
>
</RelativeLayout>
Step3. Write code of custom dialog box in src
class Custom_Dialog extends Dialog {
protected Custom_Dialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
}
private void show_alert() {
final Custom_Dialog alertbox = new Custom_Dialog(this, R.style.myQuitDialog);
Window window = alertbox.getWindow();
window.setBackgroundDrawableResource(android.R.color.transparent);
window.requestFeature(window.FEATURE_NO_TITLE);
alertbox.show();
alertbox.setCancelable(true);
alertbox.setCanceledOnTouchOutside(true);
alertbox.dismiss();
}
}
Your question is not so clear to me because you dont specify weather you want to customize and use the alert dialog or pop up dialog.In android both are different.In android pop up dialog named quick actions.I am posting below a link that will give you brief idea about quick action and explain brilliantly how to implement it in ur app.So go for it.
http://www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/