Using an Alertdialog changes the textviews behind - android

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");

Related

Setting backgroundcolor in theme gives graphical buggy dialog

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;
}

Weird black line above DialogFragment AlertDialog

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.

Android Custom Dialog Textview and Background Color

I have a custom DialogPreference Dialog that has its layout set out in a relativelayout that contains the following:
Checkbox
Textview
Text Field
This is fine in Android 4.0+, because the theme we are using is Theme.Light so the background color of the dialog is white and the default text is black for the Checkbox label and textview. However, in 2.3.3, the background color is dark grey so it becomes hard to see the text... the theme is still Theme.Light however.
I thought that making a custom dialog would be necessary for this because the checkbox enable/disables the text field. At the moment, I'm making the background color of the relativelayout for this Dialog white... but I don't really like this solution as there maybe cases where some other phones on 2.3.3 may not have white as the default dialog background...
Is there a cleaner way to fix this problem?
I notice this person has the same problem: Custom Support Dialog Fragment text is the wrong color
Another with the problem :Android: Issue with showing dialog from Theme.Light activity
EDIT: Attached screenshot. This is what it looks like on LG Optimus 2X running 2.3.3. On my co-worker's samsung galaxy which runs 2.3.3 also, the background is even darker so you can hardly see the text.
You could make custom dialog theme in style files , such as:
less than API 11:
<style name="MyDialogTheme" parent="android:Theme.Light" >
API 11 and forward:
<style name="MyDialogTheme" parent="android:Theme.Holo.Dialog" >
then use "MyDialogTheme" in your Fragment dialog:
... ...
ContextThemeWrapper context = new ContextThemeWrapper(getActivity(), R.style.MyDialogTheme);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
... ...
hope this help.

Custom dialog and Builder dialog have different themes

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();
}

Android: Issue with showing dialog from Theme.Light activity

I am trying To show a dialog from a PreferenceActivity, which is set to Theme.Light. The dialog shows with dark text on a dark background:
I assume it uses dark text because it is inheriting the text color from the parent activity, or something similar. I would like the dialog to either use white text on the dark background, or use a white background with dark text, as the PreferenceActivity does when set to Theme.Light.
This seems to be a known problem, the workarounds I have found involve creating and using a custom style that extends Theme.Dialog and using it to instantiate the dialog. Something like:
<style name="CustomDialog" parent="#android:style/Theme.Dialog">
<item name="android:textColor">?android:attr/textColorPrimaryInverseDisableOnly</item>
</style>
Dialog dialog = new Dialog(context, R.style.CustomDialog);
I tried this, but it made no difference. I also tried a number of different values for textColor, none of which modified the Dialog's text color. As a sanity check, I added:
<item name="android:background">#FFFF0000</item>
to the style, which resulted in a dialog with a red background (so I am sure that I am instantiating the dialog properly).
The closest I have come to a solution is just setting the dialog's background color to white, which gives the below dialog. But this is not a good solution, because some version or some device might not use the same behavior I am seeing when inverting text color:
So, is there a good way to set text color on a dialog displayed from a Theme.Light activity?
I assume that you use AlertDialog.Builder and set the list using one of the setSingleChoiceItems methods which doesn't use your own ListAdapter. Instead it creates its own instead with the wrong style. To fix this, you should call setSingleChoiceItems(ListAdapter adapter, int checkedItem, DialogInterface.OnClickListener listener) and provide such an adapter which would use a layout with the needed style.
Now, why this happens. The actual adapter creation happens in the file com.android.internal.app.AlertController, where the following line selects the layout for single choice lists:
int layout = mIsSingleChoice
? R.layout.select_dialog_singlechoice : R.layout.select_dialog_item;
Here is the aforementioned layout:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/primary_text_light_disable_only"
android:gravity="center_vertical"
android:paddingLeft="12dip"
android:paddingRight="7dip"
android:checkMark="#android:drawable/btn_radio"
android:ellipsize="marquee"
/>
As you can see, the line which sets the text color contains not a reference to a theme, but a hardwired color. That's why when this thing gets inflated during the list creation, it will always use the same color, regardless of what style you want it to use. So the right action to overcome this problem is to use your own layout and your own ListAdapter.

Categories

Resources