Android text style

In your application, if you have some or many user interface components that have the same look and format, it is a good idea to have a style file that can used for the those components. Android allows you to define a style separated from content. This concept is similar to CSS in web design. You need to define a style in an xml file that is separated from the layout files of your user interface. The style files must be saved in the res/values folder. In the style file you can specify properties such as layout_width, layout_height, text color, font size, background color, and much more.

The root node of the XML file must be

<resources>. You can define many styles in a single xml file.  You need to add the <style> tag to each style that you want to define. A style must have a unique name defined by the name attribute. Then you will use the <item> tag for each property of that style. The value of each property can be keyword string, a hex color, or any value depending the style property.  A child style can inherit all the properties from its parent style. When you define a style, you can inherit all properties from a style that was defined by Android system using the parent attribute. In the example xml style file below, the first style (defined for TextView) inherit all properties from TextAppearance.Medium style of Android system. If you inherit properties from a style that was defined by your ownself, you do not have to use the parent attribute. Instead, just write the name of the style you want to inherit followed by a period and the name of your new style. The second style you see in the code below inherits all properties defined in the first style. You should note that when you write properties of a style, you need to make sure that the view that the style will be applied to supports those properties. Otherwise, the style will not work as you expected.


<?xml version="1.0" encoding="utf-8"?>

<resources xmlns:android="http://schemas.android.com/apk/res/android">
   <style name="textstyle" parent="@android:style/TextAppearance.Medium">
     <item name="android:layout_width">fill_parent</item>
     <item name="android:layout_height">wrap_content</item>
     <item name="android:textColor">#086A87</item>
     <item name="android:textSize">20sp</item>
     <item name="android:gravity">center</item>
     <item name="android:padding">10dp</item>  
    
    
   </style>
   <style name="textstyle.editextstyle">
      <item name="android:inputType">text</item>
     <item name="android:textColorHint">#dfdfdf</item>
   </style>
  
</resources>


After you created a style, you can apply it to a View in your layout xml file as shown below.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity"
  android:orientation="vertical"

  >
  <TextView
     style="@style/textstyle"
     android:text="Enter the information about you"
     />
  
  
  <EditText
     android:id="@+id/txt1"
     style="@style/textstyle.editextstyle"
     android:hint="Enter E-mail"    
    
     />
  <EditText
     android:id="@+id/txt2"
     style="@style/textstyle.editextstyle"
     android:hint="Enter Name"
     />
  <EditText
     android:id="@+id/txt3"
     style="@style/textstyle.editextstyle"
     android:hint="Enter country code"
     />
  

</LinearLayout>


Android-text-style

Posted by: Dara | post date: 09-19-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.