How to: Define theme (style) item for custom widget

Yes, there’s one way: Suppose you have a declaration of attributes for your widget (in attrs.xml): <declare-styleable name=”CustomImageButton”> <attr name=”customAttr” format=”string”/> </declare-styleable> Declare an attribute you will use for a style reference (in attrs.xml): <declare-styleable name=”CustomTheme”> <attr name=”customImageButtonStyle” format=”reference”/> </declare-styleable> Declare a set of default attribute values for the widget (in styles.xml): <style name=”Widget.ImageButton.Custom” parent=”android:style/Widget.ImageButton”> … Read more

How to reference style attributes from a drawable?

In my experience it is not possible to reference an attribute in an XML drawable. In order to make your theme you need to: Create one XML drawable per theme. Include the needed color into you drawable directly with the @color tag or #RGB format. Make an attribute for your drawable in attrs.xml. <?xml version=”1.0″ … Read more

How can I style an Android Switch?

You can define the drawables that are used for the background, and the switcher part like this: <Switch android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:thumb=”@drawable/switch_thumb” android:track=”@drawable/switch_bg” /> Now you need to create a selector that defines the different states for the switcher drawable. Here the copies from the Android sources: <selector xmlns:android=”http://schemas.android.com/apk/res/android”> <item android:state_enabled=”false” android:drawable=”@drawable/switch_thumb_disabled_holo_light” /> <item android:state_pressed=”true” android:drawable=”@drawable/switch_thumb_pressed_holo_light” … Read more

Set theme for a Fragment

Setting Theme in manifest is usually used for Activity. If you want to set Theme for Fragment, add next code in the onGetLayoutInflater() of the Fragment: override fun onGetLayoutInflater(savedInstanceState: Bundle?): LayoutInflater { val inflater = super.onGetLayoutInflater(savedInstanceState) val contextThemeWrapper: Context = ContextThemeWrapper(requireContext(), R.style.yourCustomTheme) return inflater.cloneInContext(contextThemeWrapper) }

UnsupportedOperationException: Can’t convert to dimension: type=0x1

After 2 days I found the solution; from the layout as defined in my question, I have a Spinner which is bound with a custom TextView: <?xml version=”1.0″ encoding=”utf-8″?> <TextView xmlns:android=”http://schemas.android.com/apk/res/android” android:id=”@+id/listTextViewSpinner” … android:textSize=”@dimen/spinner_list_item_text_size” … /> Here, I have an extracted dimension resource: @dimen/spinner_list_item_text_size. This has been defined in dimens.xml in the following directories: values-sw600dp … Read more

How to center align the ActionBar title in Android?

To have a centered title in ABS (if you want to have this in the default ActionBar, just remove the “support” in the method names), you could just do this: In your Activity, in your onCreate() method: getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); getSupportActionBar().setCustomView(R.layout.abs_layout); abs_layout: <?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_gravity=”center” android:orientation=”vertical”> <android.support.v7.widget.AppCompatTextView android:id=”@+id/tvTitle” style=”@style/TextAppearance.AppCompat.Widget.ActionBar.Title” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:gravity=”center” … Read more

Full Screen Theme for AppCompat

When you use Theme.AppCompat in your application you can use FullScreenTheme by adding the code below to styles. <style name=”Theme.AppCompat.Light.NoActionBar.FullScreen” parent=”@style/Theme.AppCompat.Light.NoActionBar”> <item name=”android:windowNoTitle”>true</item> <item name=”android:windowActionBar”>false</item> <item name=”android:windowFullscreen”>true</item> <item name=”android:windowContentOverlay”>@null</item> </style> and also mention in your manifest file. <activity android:name=”.activities.FullViewActivity” android:theme=”@style/Theme.AppCompat.Light.NoActionBar.FullScreen” />

How to change the background color of Action Bar’s Option Menu in Android 4.2?

In case people are still visiting for a working solution, here is what worked for me:– This is for Appcompat support library. This is in continuation to ActionBar styling explained here Following is the styles.xml file. <resources> <!– Base application theme. –> <style name=”AppTheme” parent=”Theme.AppCompat.Light”> <!– This is the styling for action bar –> <item … Read more

Can’t Find Theme.AppCompat.Light for New Android ActionBar Support

You need to do next: File->Import (android-sdk\extras\android\support\v7). Choose “AppCompat” Project-> properties->Android. In the section library “Add” and choose “AppCompat” That is all! Note: if you are using “android:showAsAction” in menu item, you need to change prefix android as in the example http://developer.android.com/guide/topics/ui/actionbar.html