Set a consistent theme for all the editTexts in Android

佐手、 提交于 2019-12-01 20:51:28

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:

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

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

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

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

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:
<style name="ApplicationStyle" parent="android:Theme.Light">
    <item name="@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:

<style name="customEditText" parent="@android:style/Widget.EditText">
    <item name="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.

Štarke

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):

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

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:

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

where ButtonBig is defined like this:

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

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

define the style attribute for all EditText like below:

   <EditText
    android:id="@+id/editText1"
    android:layout_height="wrap_content"
    style="@style/EditTextStyle">

You have to specify layout_width individually to each and every View. There is no way To escape. You can create a LayoutParams object and set its width and height in it and set in it every EditText like

textView1.setLayoutParams(lParams);
textView2.setLayoutParams(lParams);
...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!