<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="6dp" android:background="#dddddd" > <ToggleButton android:id="@+id/act_main_btn_telephony" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/button_background" android:drawableLeft="@drawable/icon_phone" android:drawableRight="@drawable/icon_on_off" android:gravity="left|center_vertical" android:textOn="" android:textOff="" android:textSize="24sp" android:textStyle="bold|italic" android:textColor="@color/text_color" android:onClick="onToggleButtonClick" /> </RelativeLayout>
strings.xml
), dimension resources ( dimens.xml
), styles and themes ( styles.xml
). If you are not familiar with the first part of the article, I recommend at least to run through her eyes.
. In order for the application to be translated into several languages, all text constants must be assembled in the strings.xml
file. It has already been created and is in the res/values
directory. Open it and replace its contents with the following:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">MysteriesOfButtons</string> <string name="action_settings"></string> <string name="act_main_telephony"></string> </resources>
act_main_telephony
. Now let's replace the text of the android:textOn
and android:textOff
with the link @string/act_main_telephony
:
<ToggleButton android:id="@+id/act_main_btn_telephony" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/button_background" android:drawableLeft="@drawable/icon_phone" android:drawableRight="@drawable/icon_on_off" android:gravity="left|center_vertical" android:textOn="@string/act_main_telephony" android:textOff="@string/act_main_telephony" android:textSize="24sp" android:textStyle="bold|italic" android:textColor="@color/text_color" />
strings.xml
file strings.xml
located in the res/values
directory. Now we are writing a Russian text to it. Suppose we want to support more and English. To do this, we create the res/values-en
directory, and create another file strings.xml
. It should contain all the same constants as the first file, but in English already. When you start an application, Android searches in the application first of all for the locale that is set for the user in the system by default. If the application does not have resources for this locale, then Android takes resources from the default locale, that is, from the res/values
directory without suffixes. There may be text in any language, not necessarily Russian or English. This resource will be used if the user does not fit any other resource in the application. You can localize not only the values
resources, but also any other, for example, often localize drawable
, if some text is specified by a picture. If you are interested in the details, I recommend reading an interesting article about the localization of applications from Google and how Android selects the most appropriate resource . If there is time, I will try to prepare a separate article on localization with examples, since this question is quite extensive.
activity_main.xml
in Graphical Layout
mode and select the button. Right-click on it and select Extract Style
:
styleOnOffButton
and leave all attributes selected except for android:drawableLeft
, android:textOn
and android:textOff
, which will be different for each button in our application. The remaining attributes will be rendered in style:
res/values/styles.xml
:
<style name="styleOnOffButton"> <item name="android:background">@drawable/button_background</item> <item name="android:drawableRight">@drawable/icon_on_off</item> <item name="android:gravity">left|center_vertical</item> <item name="android:onClick">onToggleButtonClick</item> <item name="android:textColor">@color/text_color</item> <item name="android:textSize">24sp</item> <item name="android:textStyle">bold|italic</item> </style>
<ToggleButton android:id="@+id/act_main_btn_telephony" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/icon_phone" android:textOn="@string/act_main_telephony" android:textOff="@string/act_main_telephony" />
android:layout_width
and android:layout_height
are not rendered to the style. They must be present for each element in the layout file. The android:id
attribute, of course, has its own for each object, so it is also not taken into style. However, the resulting code is compact enough to be convenient to reuse.
style="@style/styleOnOffButton"
. This would be done automatically if we turned on the Set style attribute on extracted elements
option when extracting the style. Both ways will work, but these are not the best options, since this line will need to be assigned to each instance of the button in each window. I would like to avoid this. As a rule, applications use the same style for identical controls, this is one of the basic design rules. That is, all the on / off buttons look the same, differing only in icons and text. Few who make one rectangular, another tidy, and the third diamond.
styles.xml
and find the following text there:
<!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style>
ToggleButton
buttons must have the same style, our style:
<!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <item name="android:buttonStyleToggle">@style/styleOnOffButton</item> </style>
android:buttonStyleToggle
, what else can you stylize, and where to read about it? I could not find full documentation on styles. If someone saw her, please write in the comments. Therefore, I refer directly to the source of Android, fortunately they are open to all. I work with ADT, and my source code for Android styles is here: adt-bundle-windows\sdk\platforms\android-< API>\data\res\values\themes.xml
. Source Window->Android SDK Manager
using the Android SDK Manager utility, which runs directly from Eclipse, the Window->Android SDK Manager
menu.
android:style/Widget.Button.Toggle
- this is the default style for all ToggleButton
:
<style name="styleOnOffButton" parent="android:style/Widget.Button.Toggle"> <item name="android:background">@drawable/button_background</item> <item name="android:drawableRight">@drawable/icon_on_off</item> <item name="android:gravity">left|center_vertical</item> <item name="android:onClick">onToggleButtonClick</item> <item name="android:textColor">@color/text_color</item> <item name="android:textSize">24sp</item> <item name="android:textStyle">bold|italic</item> </style>
android:style/Widget.Button.Toggle
? From the same Android sources, the styles.xml
.
styleOnOffButton
style styleOnOffButton
is an attribute android:textSize
, which is given by the constant 24sp
. If there is any text in our application besides the buttons, we will probably want to make it the same in size to preserve the overall style. And this means that we will use the 24sp
constant more than once in different places. And if we then want to experiment with the size of the text, then we will have to change these constants throughout the application. To avoid this, let's declare a named size constant. Open the res/values/dimens.xml
and replace all its contents with the following:
<resources> <dimen name="text_size">24sp</dimen> <dimen name="activity_padding">6dp</dimen> </resources>
text_size
there is another constant here - activity_padding
. If we recall the code of our layout, then we will see the constant android:padding="6dp"
in the RelativeLayout
tag. And since it is also logical to make the indents from the borders of the screen for all windows of the application the same, this value suggests itself in constants.
24sp
constant in style with a new resource @dimen/text_size
:
<style name="styleOnOffButton" parent="android:style/Widget.Button.Toggle"> <item name="android:background">@drawable/button_background</item> <item name="android:drawableRight">@drawable/icon_on_off</item> <item name="android:gravity">left|center_vertical</item> <item name="android:onClick">onToggleButtonClick</item> <item name="android:textColor">@color/text_color</item> <item name="android:textSize">@dimen/text_size</item> <item name="android:textStyle">bold|italic</item> </style>
RelativeLayout
tag in the activity_main.xml
file with a constant in the resources:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/activity_padding" android:background="#dddddd" >
ToggleButton
, so why not make a style for all windows? Moreover, except for the indents from the borders, all windows will have a common background color. This style will be simple, we do not need to inherit it from anything, let's prescribe it manually. Open the styles.xml
and add the following code to the end before the closing tag:
<color name="activity_background_color">#dddddd</color> <style name="styleActivity"> <item name="android:background">@color/activity_background_color</item> <item name="android:padding">@dimen/activity_padding</item> </style>
color
tag.
RelativeLayout
is the main element of the Activity, so we cannot prescribe the style to all RelativeLayout
, as we did with the ToggleButton
. In this case, the style must be specified explicitly in those elements where it is needed, using the style
attribute. Let's prescribe the style of our layout and see what happened in our toga:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" style="@style/styleActivity" > <ToggleButton android:id="@+id/act_main_btn_telephony" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/icon_phone" android:textOn="@string/act_main_telephony" android:textOff="@string/act_main_telephony" /> </RelativeLayout>
style="@style/styleActivity"
its root element, and the window will look the same as other application windows.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="6dp" android:background="#dddddd" > <ToggleButton android:id="@+id/act_main_btn_telephony" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/button_background" android:drawableLeft="@drawable/icon_phone" android:drawableRight="@drawable/icon_on_off" android:gravity="left|center_vertical" android:textOn="" android:textOff="" android:textSize="24sp" android:textStyle="bold|italic" android:textColor="@color/text_color" android:onClick="onToggleButtonClick" /> </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" style="@style/styleActivity" > <ToggleButton android:id="@+id/act_main_btn_telephony" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/icon_phone" android:textOn="@string/act_main_telephony" android:textOff="@string/act_main_telephony" /> </RelativeLayout>
dimens.xml
, we will not have any copy-paste of the same constants. All string constants are in the strings.xml
file. To localize the application for other languages, it is enough to translate only this file.
Source: https://habr.com/ru/post/206064/