How to style android default components - android

As soon as my app launches, an AsyncTask retrieves a JSON. While it's being downloaded, I show a progress dialog as usual:
protected void onPreExecute() {
Context c = ((EventoMainActivity)mCallback);
mDialog = new ProgressDialog(c, R.style.my_Dialog);
mDialog.setTitle("Descargando datos del evento ");
mDialog.setMessage("Espere un instante...");
mDialog.setIndeterminate(true);
mDialog.show();
}
As I want to customize the dialog, I pass my style with the constructor to change colors, etc, achieving the following result:
(Some styles come from inheritance from Widget.Sherlock.Light.ProgressBar)
So far so good. With the color text property I changed it to red, but that's all I managed to do. The problem is that I want to give style to the title text, to the message text, and that blue colored line.
My approach was looking for the structure of this elements in the android source (their layout xml files). I found out that the progressDialog comes from the alertDialog. And there I found the structure that defines the title parameter and the message parameter:
From alert_dialog.xml
...
<com.android.internal.widget.DialogTitle android:id="#+id/alertTitle"
style="?android:attr/textAppearanceLarge"
android:singleLine="true"
android:ellipsize="end"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
...
From progress_dialog.xml
...
<TextView android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
...
The problem is that android.R.id does not contain references to those IDs so I'm unable to reach this views in code with findByID and apply to them the style I wish.
I know how to do a custom layout but I wonder if there's some way to style the default components.
Thanks

You can use the wrapper, where you can set a theme (style) to anything in the current context. You can set R.style.MyTheme as style to the alert dialog and customize that view to your own taste.
ContextThemeWrapper ctw = new ContextThemeWrapper( this, R.style.MyTheme );
AlertDialog.Builder builder= new AlertDialog.Builder( ctw );

After researching for a while I think it's not possible. The default components are styled using ids not reachable from your app which makes it not doable unless you create your own custom component.

Related

How to design dialog to have Checkboxes and one EditText box?

I'm trying to design the dialog xml file. It's a bit hard to follow the Material Design guidelines. Basically I want the dialog window to allow users to multi-check the checkboxes and on the bottom to have an option to add custom option (one EditText). For example:
Choose the options
[x] Option1
[] Option2
[x] Option3
[] Option4
Add custom options:
__________
[Cancel] [Ok]
The code I have:
public void onClick(View view) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(AddData.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_adding_data,null);
// More code here
mBuilder.show();
}
As I understand I'm using a custom dialog layout. But in the material design guidelines I didn't see an option to do something like that. Also I'm not sure how to to design the dialog_adding_data file. How can I design the dialog_adding_data so it will follow the material design guidelines and have the same functionality?
To achieve your UI requirements, you need to set a custom view to your dialog. The key point here is to use the following:
mBuilder.setView(mView);
If you are not using view/data binding, when referencing the views, make sure to use mView.findViewById and not findViewById like the following:
EditText editText = mView.findViewById(R.id.edit_text);
// Add your code logic, etc.
Concerning dialog_adding_data.xml that you have created, it will simply be like any other layout. Its hierarchy, depending on your exact needs, may look something like this:
<ScrollView>
<LinearLayout>
<CheckBox />
<CheckBox />
<!-- This one controls the EditText below. -->
<CheckBox android:text="Add custom options" />
<EditText />
</LinearLayout>
</ScrollView>
If the check boxes count are dynamic, then you might need to use a RecyclerView with 2 view types (one for a normal option, and the other for the custom options) instead.

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

TextView shows in AndroidStudio but not on device nor DebugMonitor

I have a popup which is shown when I receive a GCM notification. The popup is implemented as a LinearLayout which is setContentView'd in the popup activity. The layout render in Android Studio looks like this:
However, on the device and on the DebugMonitor View Hierarchy dump it does not show, although it is there:
The TextView has the default text "Where?" replaced in the extended Activity class:
String lightName = getIntent().getStringExtra(LIGHT_NAME_KEY);
final TextView lightNameLabel = (TextView) findViewById(R.id.lightNameLabel);
lightNameLabel.setText(lightName);
I am at a loss here. I grep'ed through the project files and there are no other uses of the TextView's id other than in the snippet above. Could you please give me some pointers where to investigate why the TextView doesn't show?
[edit] I am including the .xml snippet for the respective TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/lightNameLabel"
android:layout_gravity="center_horizontal"
android:layout_marginTop="1dp"
android:text="Where?"
android:visibility="visible" />
[edit 2] A link to the whole layout .xml file: http://pastebin.com/2uqkzBSa
It was helpful that you showed a screenshot, as the problem is likely that you're displaying the layout in a dialog. If you select the Holo dialog theme in Android Studio's graphical editor pane, you'll observe that the default text color is white. Since you've provided a light background, the light text is simply illegible against it.
There are different solutions:
Provide a different theme when displaying the dialog as to ensure that the primary colors are dark; or
Define your own theme and provide it when displaying the dialog; or
Modify the layout to specify a text color.

Popup visual decoration

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/

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