AlertDialog custom EditText view and copy-paste menu - android

I'm using the AlertDialog inside a DialogFragment from appCompat 22.2.1 to make something like the following.
But as you can tell the copy-paste menu features are pretty messed up. What can I do to bring the copy-paste menu bar from the background to the foreground? And Also how can I change the "PASTE" Label color?
This is my xml Style for the dialog
<style name="MyDialogStyle" parent="Theme.AppCompat.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">#color/blue_500</item>
<item name="colorControlNormal">#color/blue_500</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#color/grey_200</item>
<!-- Used for the background -->
<item name="android:background">#color/dark_gray</item>
</style>
And My dialog code inside The DialogFragment
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final AlertDialog.Builder alertCompat = new AlertDialog.Builder(getActivity(), R.style.MyDialogStyle);
alertCompat.setTitle("Hello wtf");
alertCompat.setView(R.layout.add_mac_dialog_frag);
alertCompat.setPositiveButton(R.string.ok, this);
alertCompat.setNegativeButton(R.string.cancel, this);
return alertCompat.create();
Any Ideas?

This appears to be the AppCompat problem documented by AOSP Issue 170105. The fix will be in the M release (v23) of AppCompat.

OK since this is a known bug and the appCompat devs love to break a little something every time and then take forever to release new fixes, I ended up using the amazing material dialogs library

Related

Set AlertDialog View Inset via style

I use Acr.UserDialogs to create cross-platform dialogs from a Xamarin Shared/PCL project. Acr.UserDialogs contains a method to create a prompt which (in Android) is an AlertDialog whose View is set to an EditText. Unfortunately the View/EditText has no margin/padding which results in it reaching until the outer left and right limits of the dialog - which looks pretty ugly. Due to it being a library I have no ability to change how exactly the EditText or the AlertDialog are created (rather than editing the libraries code on GitHub myself but that's a way I try to avoid for now if possible). What the library supports is the possibility to pass an AndroidStyleId into the call that it uses instead of the default Android AlertDialog style (new AlertDialog.Builder(activity, passedAndroidStyleId)).
Now, my idea is to create a style that defines a custom margin/padding/inset to the View of the AlertDialog kind of as follows and pass it in.
<style name="CustomPromptDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="viewInset">10dp</item>
</style>
Problem is I can't find any resources listing which "properties" can be set in a style or how they are named, so my only idea was to ask here:
Is there any way to achieve what I'm trying to do? And if yes, how?
You can set the style like this to change the form of the Alertdialog. This the method for globel setting.
<style name="MainTheme" parent="MainTheme.Base">
<item name="android:datePickerDialogTheme">#style/Theme.picker</item>
<item name="alertDialogTheme">#style/Theme.alert</item>
</style>
<style name="Theme.alert" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="colorAccent">#FFC107</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFC107</item>
<!-- Used for the background -->
<item name="android:background">#4CAF50</item>
<item name="viewInset">10dp</item>

How can I style the title of a MediaRouter dialog?

I'm using mediarouter-v7 v23.1.1 to show a chromecast / media router chooser dialog. Now I try to style this thing - while I could change the background color and list item text style with this:
<style name="Theme.MediaRouter.Light">
<item name="android:background">#color/orange</item>
<item name="mediaRouteChooserPrimaryTextStyle">#style/my_custom_style</item>
</style>
I was not able to override the style of the dialog's title. This and other attempts were not successful:
<style name="Theme.MediaRouter.Light">
<item name="android:windowTitleStyle">#style/my_title_style</item>
</style>
<style name="my_item_style">
<item name="android:textColor">#color/blue</item>
</style>
As I understood it, a dialog has its own window, therefore android:windowTitleStyle of the (dialog) theme that I override in my custom styles should be applied, but its not. I also looked into the implementation of the mediarouter dialog to see if they do anything fancy (like hiding the window and adding a custom element as their title), but they do not, they simply call setTitle(...) on the Dialog instance.
What am I doing wrong?
Unfortunately that dialog doesn't follow the standard theme (Dialogs in Android are all pretty unfriendly in general but that one is among the hardest to work with). Since that dialog is provided by media router, you can only provide a customized theme if you replace that completely with your own dialog.
You can try subclassing MediaRouteDialogFactory and override onCreateChooserDialogFragment() method and pass your implementation to the ActionProvide:
mediaRouteActionProvider.setDialogFactory(yourDialogFactoryImlementation)
You can take a look at the CCL where I do a similar thing not for the chooser dialog but for the controller

Styling custom dialog fragment not working

I'm trying to style all my dialog fragments to look the same in my app. The dialogs coming from my settings fragment are styled exactly the way I want it. For my custom dialog fragments, the style is similar but not exactly the same. For some reason the spinner, timepicker, datepicker, radiobuttons, and edittext widgets inside my custom dialog fragments don't pick up the same style. In fact, the widgets blend in with the white background and you can't see that they are there. What am I doing wrong?
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"/>
<style name="Theme.Base" parent="AppTheme">
<item name="colorPrimary">#color/PrimaryBackgroundColor</item>
<item name="colorPrimaryDark">#color/SecondaryBackgroundColor</item>
<item name="colorAccent">#color/ColorBackgroundAccent</item>
<item name="android:textColorPrimary">#color/PrimaryTextColor</item>
<item name="android:alertDialogTheme">#style/AppTheme.DialogStyle</item>
</style>
<style name="AppTheme.DialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColorPrimary">#color/PrimaryBackgroundColor</item>
<item name="colorAccent">#color/ColorBackgroundAccent</item>
</style>
I'm applying the theme to my custom dialog fragment like this:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppTheme_DialogStyle);
My settings dialog looks like this (Exactly how I want it):
Settings Dialog Fragment
My custom dialog fragment looks like this:
Custom Dialog Fragment
As you can see, the radio button selected color red and you can't see the unselected radio button.
Finally got an answer!!!
It's an issue or bug with AppCompat 22+.
Check out link here
Apparently this was a bug with fragments and widgets weren't getting the material themed in a fragment. It seems they fixed this issue, but the issue still holds in a dialog fragment based on what I'm going through.
The problem comes when you use the inflater instance passed to Fragment#onCreateView(). The workaround for now is to instead used the LayoutInflater from getActivity().getLayoutInflater() according to google.
So I changed my code to:
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);
from:
View view = LayoutInflater.from(getActivity().getApplicationContext()).inflate(R.layout.dialoge, null);
All my widgets are now themed. Thanks everyone. Hopes this helps someone else.
I believe you need to set the theme on the actual Dialog and not the Fragment
Use this constructor to create your AlertDialog:
AlertDialog.Builder(Context context, int theme)
ie
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), theme)
I think you need to add one more item in style of your dialog. android:textColorSecondary will show color of un selected checkbox.
in your style add it.
</style>
<style name="AppTheme.DialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColorPrimary">#color/PrimaryBackgroundColor</item>
<item name="colorAccent">#color/ColorBackgroundAccent</item>
<item name="android:textColorSecondary">#000000</item>
</style>
It will make un Checked checkbox or toggle button edge color black. you need to change #000000 to color your want to show.
See if this helps -
Android appcompat-v7:21.0.0 change material checkbox colors
In short, try setting android:textColorSecondary.

DialogFragment not rendering style correctly

I am attempting to style a DialogFragment and, despite extensive research within SO, developer.android and elsewhere, it's just not working right.
I am sure that I have applied everything correctly; in my DialogFragment onCreateDialog, I am putting:
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.FibroDialog));
and in values/styles.xml, I have:
<style name="FibroDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#drawable/panel_background</item>
</style>
and in values-v14/styles.xml, I have:
<style name="FibroDialog" parent="#android:style/Theme.Holo.Dialog">
<item name="android:windowBackground">#drawable/panel_background</item>
</style>
If I run that, I get the top row of dialogs in the image below, where nothing has changed at all, but I would expect the window to use my panel_background drawable. If I change windowBackground to background, I get the bottom row of dialogs in the image below, where it all goes wrong.
So how do I change this, so the entire dialogs have the purple background? I know I can use the layoutInflator and put a background colour in the layout, but that only does the middle bit.
I don't know if this is of any relevance, but the DialogFragment is launched from a SherlockFragment

Android: How to set text color for list items in AlertDialog properly

I have an AlertDialog in my application. It contains a list of custom views with TextView widgets inside. Everything works fine on Android 2.x. The AlertDialog is created with white list and black text in it. But when I run my app on Android 3.x devices all TextViews are black and list's background is black too. So I can't see the text until I tap and hold one of the items.
Here's a TextView's definition from the layout file:
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:textAppearance="?android:attr/textAppearanceSmallInverse" />
I thought that using textAppearanceSmallInverse for the textAppearance attribute is a proper way to set text parameters and it must work on all devices but seems I was wrong. So what should I do to make AlertDialog display list items properly on all platforms? Thanks in advance.
The solution is to leverage Android's built-in resource selection system. You should specify two different styles and place them in the proper folders based on the API version. Note that the following examples are not mine, I took them from this tutorial.
res/values-v4/styles.xml:
<resources>
<!-- Text for listboxes, inverted for Andorid prior to 3.0 -->
<style name="MyListTextAppearanceSmall">
<item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
</style>
<style name="MyListTextAppearanceDefault">
<item name="android:textAppearance">?android:attr/textAppearanceInverse</item>
</style>
<style name="MyListTextAppearanceMedium">
<item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
</style>
</resources>
res/values-v11/styles.xml:
<resources>
<!-- Text for listboxes, non-inverted starting with Android 3.0 -->
<style name="MyListTextAppearanceSmall">
<item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
</style>
<style name="MyListTextAppearanceDefault">
<item name="android:textAppearance">?android:attr/textAppearance</item>
</style>
<style name="MyListTextAppearanceMedium">
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
</style>
</resources>
Then, in your TextView, specify the style like so:
<TextView
android:style="#style/MyListTextAppearanceSmall"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee" />
See the tutorial linked above for a lengthier explanation.
Your code for the popup dialog should look similar to this:
// Sets dialog for popup dialog list
AlertDialog dialog;
String[] items = {"exampleItem"};
ListAdapter itemlist = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setAdapter(itemlist, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int item)
{
}
});
dialog = builder.create();
dialog.getListView().setBackgroundColor(Color.WHITE);
Here, you are getting the listview and setting the background color to white. If you want to change the color of the text for each of the textviews then you need to define their color in the layout of the textview, in this case black:
android:textColor="#000000"
The accepted answer seems like a bit of overkill. I simply forced the inverse background by calling:
dialogBuilder.setInverseBackgroundForced(true);
solves the problem just fine.
This probably happens because you aren't specifying a theme, then it falls back to the default theme. In 2.x this should be Theme.Black and in 3.x Theme.Holo (or Theme.Light, not sure on this). Then textAppearanceSmallInverse resolves to different style in each theme.

Categories

Resources