Custom Alert Style with Multiple Theme - java

I am confused while using custom alert dialogue styles in my application, due to there being multiple themes in my application. I have three themes in my application, called themeGrey, themeTeal and themePink. I have an alert dialogue style like below
style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColorPrimary">#color/colorAccent</item>
<item name="android:textColor">#color/colorAccent</item>
<item name="android:background">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="textColorAlertDialogListItem">#color/colorAccent</item>
<item name="android:textColorSecondary">#color/colorAccent</item>
</style>
I have used it in my app like below
mProgress = new ProgressDialog((this), R.style.AlertDialogTheme);
Now my question is, how can I define a different style for each theme?
I do not want apply with a conditional. Can I do it by declaring a theme as an item?
Thanks

I have solved removing style from java code like below
mProgress = new ProgressDialog((this));
and delcared alert style in theme like below
<item name="android:alertDialogTheme">#style/AlertDialogThemeTeal</item>
Thanks

Related

How to set color icons , texts header bar in Android Studio Kotlin/ Java?

Im trying to set the text and icons colors from header bar( i don't know the name of it exactly) when user click in a square button on menu. Follow the image below
enter image description here
i was trying to set there by style like this
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorOrange</item>
<item name="colorPrimaryDark">#color/colorOrangeDark</item>
<item name="colorAccent">#color/colorWhite</item>
<item name="android:textColorPrimary">#color/colorWhite</item>
<item name="iconTintMode">#color/colorWhite</item>
</style>
but im stuck. How can i do that ?

How to set EditText style from styles.xml in Android?

I'm generating EditText fields and wish to set it's style from styles.xml file. But can't manage to do it. I can set some parameters, but I would rather use a style from styles.xml so it would be easier to change later if needed.
styles.xml
<style name="input_fields_single_line">
<item name="android:padding">8dp</item>
<item name="android:background">#drawable/input_field_shape</item>
<item name="android:maxLines">1</item>
</style>
Java code:
List<EditText> inputFieldList = new ArrayList<EditText>();
public void generateInputFields() {
EditText editTextFieldTitle = new EditText(this);
inputFieldList.add(editTextFieldTitle);
editTextFieldTitle.setHint(R.string.enter_field_title);
editTextFieldTitle.setMaxLines(1);
editTextFieldTitle.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
editTextFieldTitle.setFocusableInTouchMode(true);
editTextFieldTitle.requestFocus();
myLayout.addView(editTextFieldTitle);
}
You create a separate style for the EditText like what you are doing but parented with the Widget.EditText like the following:
<style name="MyCustomeEditTextStyle" parent="#android:style/Widget.EditText">
...add as many items as you need
<item name="android:padding">8dp</item>
<item name="android:background">#drawable/input_field_shape</item>
<item name="android:maxLines">1</item>
</style>
Then call your style inside each edit text in the xml like the following:
<EditText
android:id="#+id/editTextId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/MyCustomeEditTextStyle" />
BUT if you are want it to be globally set once and set without the xml because you are generating your EditText programmatically like this:
EditText editTextFieldTitle = new EditText(this);
you can then set your style in the application style like this:
<style name="AppTheme" parent="#android:style/YourParentTheme">
<item name="android:editTextStyle">#style/MyCustomeEditTextStyle</item>
</style>
I found the solution here. Android: set view style programmatically
editTextFieldTitle = new EditText(new ContextThemeWrapper(getBaseContext(),R.style.input_fields_single_line),null,0);
There is a attribute in EditText called styles use that.and if you want to customize it than make own style in drawable , A new XML and link to the editText using styles attribute.

How to change Android status bar color in all activities together

I learned how to color an Android activity status bar thanks to this solution: How to change status bar color to match app in Lollipop? [Android]
However it doesn't say how to make this for the entire app (all activities).
i don't want to duplicate those 4 lines of code into each Activity, and if I make a Java class for Utils, I can't reach my colors by using R.color.blue or getResources(....).
Is there a way to do this through the Manifest, perhaps? Or any other way?
Thank you!
You should create your own style in values/styles.xml. Then make your own theme with such parameters. Loock code below
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/color_primary</item>
<item name="colorPrimaryDark">#color/color_secondary</item>
<item name="colorAccent">#color/color_accent</item>
<item name="android:statusBarColor">#color/color_primary</item><!--this is what you need-->
</style>
Or just use method (for Lolipop):
public abstract void setStatusBarColor (int color)

Applying some styles to control

As you know in html/css you may apply some classes (and hence some styles) to the tag. It is cery convenient and i would like to have tgis opportunity in android development. Is there a way to apply some styles to one control?
Yes you can implement styles in your values/styles.xml
you can do something like :
<style name="MyMainStyle">
<item name="android:layout_width">250dp</item>
<item name="android:layout_height">60dp</item>
</style>
<style name="MySecondaryStyle" parent="MyMainStyle">
<item name="android:paddingLeft">20dp</item>
</style>
As you can see you can also have parent styles. Then you can use the style in your layout.xml like so :
<Button
style="#style/MyMainStyle"
android:id="#+id/myButton"
/>

Removing the Android Title Bar

I'm developing an android application and I cannot remove the title bar permanently. A solution posted here: How to hide the title bar for an Activity in XML with existing custom theme
(the 600 upvoted one, the others didn't work as described below) worked in general, but it maintained the title bar for a brief millisecond when initially launching the app.
I've tried various solutions throughout stackoverflow and other sites modifying the android theme in the manifest xml and style file(s). However, all of these solutions have all crashed the application before it began. The message in LogCat being that I must use an AppCompact theme. My main activity extends the ActionBarActivity class, and I have tried switching it to just Activity while also removing the :
if (savedInstanceState == null) {getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
that is generated. However, when I do so, all of the views on the main activity disappear and it becomes completely white with nothing else. Is there a way to completely remove the actionbar while extending ActionBarActivity? If not, how can I switch to extending Activity or some other class while maintaining no other errors?
on styles.xml you should make that
parent="Theme.AppCompat.NoActionBar"
Do you use custom view for the ActionBar? if yes, then maybe you'll find the approach I use acceptable. I just set text color for Title and Subtitle the same as background color in my app's theme.
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar" parent="#android:style/Widget.ActionBar">
<item name="android:titleTextStyle">#style/ActionBar.Title</item>
<item name="android:subtitleTextStyle">#style/ActionBar.Subtitle</item>
<item name="android:background">#color/my_color</item>
<item name="android:backgroundStacked">#color/my_color</item>
<item name="android:backgroundSplit">#color/my_color</item>
</style>
<style name="ActionBar.Title">
<item name="android:textColor">#color/my_color</item>
</style>
<style name="ActionBar.Subtitle">
<item name="android:textColor">#color/my_color</item>
</style>
Then I inflate and apply custom ActionBar view in onCreate and set custom title

Categories

Resources