Skip to content Skip to sidebar Skip to footer

Set A Consistent Theme For All The Edittexts In Android

I have finished making my app. Now, I want to reset all my editTexts to have the layout width as fill parent instead of wrap content. android:layout_width='fill_parent' while curr

Solution 1:

You can try this one. Here is the part of the manifest file you need to change to call your custom theme (the custom theme called here is AppTheme:

<applicationandroid:name="YourApplication"android:theme="@style/AppTheme" >

Then in your file styles.xml, create and customize this custom theme:

<stylename="AppTheme"parent="@android:style/Theme.Holo.Light"><itemname="android:typeface">YourTypeFace</item></style>

You can add the parameters you need inside the style. This will apply the style to all your textviews.

Solution 2:

One solution to your problem is to apply a custom theme to all of your activities. In order to do that, you can inherit properties from an existing theme and override the properties that you want to change.

  1. In AndroidManifest.xml, locate the <application> element.
  2. Add the attribute to it:

android:theme="@style/"

  1. Locate styles.xml file in the values folder.
  2. Use the following template:
<stylename="ApplicationStyle"parent="android:Theme.Light"><itemname="@android:editTextStyle">@style/customEditText</item>"
</style>

Names used in the above are just examples, you may use your own. As to the parent theme, that is also up to you.

All that is left is the definition of editTextStyle (or whatever name you have chosen for the style). You should inherit properties from Widget.EditText and override the properties that you want to change, like the following:

<stylename="customEditText"parent="@android:style/Widget.EditText"><itemname="android:textColor" >#ffffff</item></style>

To quote the official android guide:

The parent attribute in the element is optional and specifies the resource ID of another style from which this style should inherit properties. You can then override the inherited style properties if you want to.

I tried to make it easy to understand and follow. I'm a junior dev so while the above solution works for me, it may not be the best one out there. As I said though, it solves the problem rather efficiently.

Solution 3:

Unfortunately it is not possible to set layout attributes (layout_*) from a theme (see Layout Parameters documentation and this answer from an Android framework engineer). You must set them on each element or set the layout attributes in a style and let each element reference the style like this:

<Buttonstyle="@style/ButtonBig"android:text="my text" />

where ButtonBig is defined like this:

<stylename="ButtonBig"parent="android:Widget.Holo.Light.Button"><itemname="android:textSize">20sp</item><itemname="android:layout_width">match_parent</item><itemname="android:layout_height">100dp</item></style>

Solution 4:

I was having this exact issue. For some reason it helped me to drop the android: part in the AppTheme definition, and leave it only as editTextStyle (as mentioned in this answer):

<stylename="AppTheme"parent="android:Theme.Light"><itemname="android:fontFamily">Verdana</item><itemname="editTextStyle">@style/EditTextStyle</item></style>

Solution 5:

You'll have to set the property style="@styles/EditTextStyle" to all of your EditText components in your application.

Post a Comment for "Set A Consistent Theme For All The Edittexts In Android"