Application styling,impact and performance of the inheritance feature in styling
5 minute(s) read | Published on: Apr 28, 2021 Updated on: Dec 14, 2021 |
It should be noted that all the details related to styling are explained in previous tutorial articles.
The difference between style and theme is that the value we consider for a feature is considered for the whole page and activities in the theme. Still, in styling in separate tags, we give the attributes that apply only to that part.
It should be noted that all the details related to styling are explained in previous tutorial articles.
The difference between style and theme is that the value we consider for a feature is considered for the whole page and activities in the theme. Still, in styling in separate tags, we give the attributes that apply only to that part.
How to style the application?
1- Create a file called Style and MinSDK 16.
2- Open Build. gradle (app).
3- Inside the dependencies, we put the codes inserted below.
4- The appcompat library is added to the project by default.
5- If the library is not entered by default, you must enter it manually by entering the codes listed below.
6- Go to File> Project Structure.
7- In the app section, select the Modules option.
8- Click the Add option, which is indicated by an (+) sign, and select Library dependency.
9- In this window, the list of libraries in the SDK and have been added to the project is displayed.
10- Search for Appcompat in the search bar.
11- To use the appcompat library, you must have
Android Support Library installed.
12- Go to Active.
13- Add three Buttons to activity_main.xml.
14- Attribute All three are the same.
15- Insert the codes listed above for all three buttons.
16- Note that except for the attributes and attributes of id and text, they are all the same.
17- There is a file in the res/values path called styles.xml. This file is used to store style definitions.
18- It should be noted that all styles are defined inside the tags.
19- We must choose a unique name for each style inside the tags.
20- The properties of the styles are also placed inside the tags.
21- The code inserted below is an example and shows that we have created a new style for the buttons in which the buttons' properties are common.
22- We created a style called MyButton, to which we apply the properties in the code below.
23- Put the code below in the styles.xml file.
24- Go back to the activity and delete the common properties of the first button.
25- Add the MyButton style to this button.
26- Attach the other two buttons to the corresponding style and apply the properties.
27- In addition to the above methods, you can create two Text Views that apply the style to items with common properties.
28- In the Preview section, right-click on TextView.
29- Then select Refactor> Extract Style.
30- In the window that opens, select a name for the style.
31- In the Attributes section, select the properties we want to apply to them.
32- The title of the Launch properties is ‘Use Style Where Possible,' which can connect all the items that exist in the activity and have common properties, and there is no need to change them manually.
33- Select the Do Refactor option.
34- A new style was added and applied to both Text Views.
35- The code that should be written in the styles.xml section is as follows:
36- The code that should be written in the main_activity.xml section is as follows:
In this section, we want to use the inheritance feature, so consider several buttons whose properties and characteristics are the same. Still, we want them to be different in the background color. In this case, we need to create a new style and add the required properties, but we can use the inheritance feature and make the necessary changes easily.
In the code above, the style we created is MyButton. Green. The value and property of MyButton. We added it before Green, which makes it have all the parent properties of MyButton.
Then I add the background color with the desired amount.
In this code, we specified that all widgets are defined as MyButton. Green has MyButton properties, but the value of the background property has been replaced with the difference.
We check that it works properly:
The result will be a difference in the background color of the second button with the other buttons.
Consider the name MyButton.SmallButton. BorderButton.Green:
According to the name listed above
SmallButton:
It is a style in which the layout_width value is 100dp and prevents full-width occupation.
BorderButton:
A style to which border-related properties are added so that the color of the bar and the line around the buttons are the same as the background.
Inheritance of a button style that is small in size has a different background color from other buttons and is green in color as follows:
You can enter the code below:
Application theme
Themes generally apply to the entire project, activity, or application.
The general settings of the application are done and set inside the AndroidManifest.xml file:
Inside the application tag, there is a theme property.
Go to styles.xml:
There is a style called AppTheme that also inherits from theme.AppCompat. Light.DarkActionBar. Using Theme.AppCompat, we have access to all the themes in the appcompat-v7 library.
In this part of the tutorial, we defined three buttons and changed their properties. By changing its properties, we saw the result and were able to apply the desired changes by inheriting and having the same properties without defining and creating new styles and tags.
DotNek Android development services