Applying some styles to control - java

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"
/>

Related

Why boxes are invisible in checkbox?

after created this style:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/offer_text_background</item>
<item name="colorPrimaryDark">#color/app_background</item>
<item name="android:checkboxStyle">#style/settingsNotificationCategory</item>
</style>
<style name="settingsNotificationCategory">
<item name="android:textSize">30sp</item>
</style>
My box from checkbox is removing:
invisible boxes
Without this style:
visible boxes
I need create chceckbox dynamically in kotlin:
var checkBox = CheckBox(this)
checkBox.text = category
checkBox.setTextColor(resources.getColor(R.color.customText))
checkBox.isChecked = true
notificationCategoryLayout.addView(checkBox)
what's happened?
I tried :
var checkBox = CheckBox(this, null, R.style.settingsNotificationCategory)
checkBox.text = category
checkBox.setTextColor(resources.getColor(R.color.customText))
checkBox.isChecked = true
notificationCategoryLayout.addView(checkBox)
but the effect is the same...
Thanks for help
You are given the checkbox a style that contains only the textSize so it will affect on the style of the checkbox you can do 2 things:
first just set the textsize programatically:
checkBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
or adjust your style to be like this:
<style name="settingsNotificationCategory" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="android:textSize">30sp</item>
</style>
to inherit the base style of the checkbox
but when I create checbox in layout:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:textColor="#color/customText"
android:theme="#style/settingsNotificationCategory"/>
this is working without
parent="Widget.AppCompat.CompoundButton.CheckBox"
is possible to set theme in code?
I want set textSize via style because I use layouts to different resolution (for large, xlarge, small sizes)

Custom Alert Style with Multiple Theme

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

in Android, is there any way of use classes to format a groupĀ“of views?

I'm beginning in Android development and I got very familiar with the XML notation, since it's pretty similar to HTML.
By the XML we can set ID's to the views for later managing it with Java, as like we do with HTML and JS.
My point is: Can I define classes or something like that to set similiar attributes to a group of views?
Sorry if I was unclear or anything, and thanks for you help!
You can define styles and use those across widgets.
Here are the official docs for it:
http://developer.android.com/guide/topics/ui/themes.html
You can do this using styles.
in res/values/ create a file named styles.xml [Auto generated in Android Studio]
You can define new style using the style tag :
<style name="styleName"></style>
You can also modify an already present style by using :
<style name="styleName2" parent="alreadyExistingStyle"></style>
Than you use it in xml and as well as Java.
In xml :
style="#style/styleName"
In Java :
R.style.styleName
Example :
Button Style :
<style name="AppButtonLight" parent="#android:style/Widget.Button">
<item name="android:textColor">#FF000000</item>
<item name="android:layout_margin">5dp</item>
<item name="android:textSize">16sp</item>
<item name="android:background">#drawable/hw_btn_default</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
Than use it in xml :
<Button
style="#style/AppButtonLight"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/btnDownload"
android:id="#+id/btnDownload" />
Hope I was able to help you.

Android Lollipop Material Design Overflow Menu Icon color

I try the new Material Design on a Nexus 7 and have the following strange behaviour. The Overflow Menu Icon has a different color on the first app launch.
I changed the android:textColorPrimary color and read this tutorial.
First App launch:
Second App launch:
As you see the color of the primary text color is not set on the first launch. It is only set if i press the home button and relaunch the app. Here is my styles.xml file:
<style name="AppBaseTheme" parent="android:Theme.Material.Light">
<item name="android:colorPrimary">#FF4444</item>
<item name="android:colorPrimaryDark">#CC0000</item>
<item name="android:textColorPrimary">#000000</item>
</style>
Can someone explain, why that behaviour occurs?
I set android:minSdkVersion="21" and don't want to use support libraries.
I experienced the same problem when running a PreferenceActivity which cannot use appcompat-v7 library on a LOLLIPOP device. When this activity is opened for the first time the overflow icon is always white completely ignoring android:textColorPrimary and android:colorControlNormal. Subsequent runs or orientation changes however result in proper coloring.
I created a gist which will help you mitigate this. The code binds a global layout observer to the toolbar and when it finds an overflow icon it replaces it and unbinds the observer. So don't use it in places where you don't expect an overflow icon because the observer won't get unbound in that case.
Link to gist: https://gist.github.com/consp1racy/4b640679de553fdb3046
Just add the secondary text color for the options menu, i.e.:
<item name="android:textColorSecondary">#color/text_color</item>
In some circumstances the secondary color is set to the primary color. I don't know yet why.
Add these items too:
<item name="actionMenuTextColor">#color/white</item>
<item name="android:actionMenuTextColor">#color/white</item>
If this didn't help, then try this:
<style name="AppBaseTheme" parent="android:Theme.Material.Light">
<item name="android:itemTextAppearance">#style/TextAppearance</item>
</style>
<style name="TextAppearance">
<item name="android:textColor">#android:color/white</item>
</style>
This would works for Holo.Light.DarkActionBar
If you want to use material design in pre-21 devices you need to extend Theme.AppCompat.Light.NoActionBar theme. To do that, you need to add compile com.android.support:appcompat-v7:21.0.0 as a dependency of your project, this way you will be able to use the Toolbar in your layout.
Then define your theme in values/themes.xml::
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#color/my_awesome_color</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#color/my_awesome_darker_color</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#color/accent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight & colorSwitchThumbNormal. -->
</style>
and your the toolbar in your layout/my_activity.xml:
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
You have optional attrs to define the theme and popupTheme as well:
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
Chris Banes wrote a good article about this that you should read https://chris.banes.me/2014/10/17/appcompat-v21/
I read on the comments of the questions that you are using eclipse, I highly recommend to use android studio + gradle and no eclipse.
Just add android:theme="#style/ThemeOverlay.AppCompat.Dark"to the Toolbar
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/ThemeOverlay.AppCompat.Dark
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
overflow menu icon will be white now :)

Android Styles For API 10 and API 11

I am trying to do something that I feel to be very simple in concept. I would like my app to be supported all the way back to API 10 (Gingerbread). To make this look good, I need to make a slight change to the color of the text on my buttons when the device is running API 10. Thus, I want to create two styles: one of which will be used when the device is using API 10 (I want the text color of the buttons to be black in this case), and another when the device is using API 11 or above (the text color will be the default ICS grayish in this case). To do this, I am using a values and a values-v11 folder. Inside the values folder is a themes.xml file with the following code:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="buttonColorStyle">
<item name="android:textAppearanceButton">#style/buttonTextColor</item>
</style>
<style name="buttonTextColor">
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
However, when I load up my app with target SDK set to 10, the text color of the buttons is unchanged from the default grayish. Also, here is the code for one of my buttons which should use this style:
<Button
style="#style/buttonColorStyle"
android:id="#+id/thirdSectionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="sectionButtonClicked"
android:text="Section 3"
android:textSize="11.5sp" />
Anyone have any ideas?
You have to do something like this:
<style name="MyStlye">
<item name="android:textAppearanceButton">#style/BtnText</item>
</style>
<style name="BtnText" parent="android:TextAppearance.Small.Inverse">
<item name="android:textColor">#android:color/black</item> // or whatever color
</style>
why not setting the same nice style for all versions?
you can use this library (also suggested at the android developers blog here) for making all of the devices use the holo theme.
alternatively , just leave the current style , so that the user will feel more "at home" with his current style of the OS (since it can change between companies) .

Categories

Resources