Apply settings to keep the screen active for a while without touching it
9 minute(s) read | Published on: Apr 29, 2021 Updated on: Dec 14, 2021 |
In other words, it should be said that when designing and developing the application, it should be designed in such a way that when an application runs on the Android system and can be turned on or active for a certain period without touching it. For example, consider a situation where a user is using the Google Maps app while driving and cannot touch the phone screen for a certain period but needs to have the screen turned on to see the app. In this part of the tutorial, we want to increase the duration of the screen being active and on using two different methods.
To increase the brightness and activation time of the screen while running the application without touching the screen, we must create a project so that we can use it and apply this change in two different ways.
1- We create an Android project with the desired name, for example, KeepScreenOn.
2- The activity that we should use in this project is better to choose it as Empty Activity.
3- The language used in this project to do this is Java.
Keep the screen on and off using the first KeepScreenOn method.
It should be noted that this method is one of the easiest ways to keep the screen on and active when running the application without touch.
KeepScreenOn is located in the XML activity layer.
1- We define this feature in the parent view so that the screen is on and active when an application is running.
2- The codes that should be in the activity_main.xml section to apply the necessary changes are as follows:
In the code listed above, android: KeepScreenOn is defined with a value of true inside the layout, which makes the screen active and on while the activity is running.
Keep the screen on and off using the second method FLAG_KEEP_SCREEN_ON
In the previous method, by making some changes, we caused the screen to be active and bright while the activity is running, but consider that the user may not have specified the destination and route when navigating. This mode may cause the screen to turn on and off unnecessarily for no reason, reducing battery life. Therefore, the best way to keep the screen on and active so that we can use the application that is running, we use the second method, which we describe in this part of the tutorial.
In this part of the tutorial and the second method, I use the switch buttons to keep the screen active.
The code to be entered in the activity_main.xml field is as follows:
The code that should be in the MainActivity.java section is as follows:
We first remove the KeepScreenOn feature in the code above, which was the first way to keep the screen on while running the app. The KeepScreenOn feature is located inside XML files. Then we added a Switch Button to use the second method of keeping the screen on and active.
Inside the onCheckedChanged method, we must define the code switch button and the condition that the following code is executed when the switch button is activated:
It should be noted that the management of all pages in Android systems is the responsibility of the WindowManager.LayoutParams class. FLAG_KEEP_SCREEN_ON is also an indication that the screen is on and active. Suppose this ID is placed inside getWindow (). AddFlags (), the on and off mode of the screen will be activated for this activity, and the screen will remain on.
Note that instead of calling the addFlags () method, we call the clearFlags () method to disable it. As follows:
GridLayout on Android
There are various View Groups in Android, including GridLayout, which can be used for cell building and screen networking as Grid means to network and cellular design. We want to tell you how to cellularize and network the screen in this part of the tutorial.
GridLayout can split the screen into several rows and columns, causing the screen to consist of some rows and columns. Of course, it should be noted that GridLayout is not specific to screens and Android systems and can be used in areas such as CSS and web design.
The difference between TableLayout and GridLayout
It should be noted that TableLayout and GridLayout are different from each other and are not the same. In this case, it can be said that TableLayout is actually for managing views that are in a row or row. But in GridLayout, in addition to managing rows, we can also manage columns. The difference between the two is that in GridLayout it is possible to manage columns and rows. For example, it is possible to merge two-row columns.
What are the benefits of GridLayout?
It should be noted that GridLayouts can be used in complex layers, which are difficult to implement and manage by ViewGroups.
GridLayout features can generally be categorized as follows:
- It is possible to manage all cells from both column and row directions.
- It is possible to create and manage complex layers without using other layouts.
- The possibility of creating layers that do not have a specific order.
1- In Android Studio, we created a project called GridLayout.
2- Select the type of activity from the Empty Activity type.
3- The following XML tag is created inside the user interface file:
4- Delete the layout that is inside activity_main.xml.
5- Replace it with a GridLayout.
In the code above, we created six TextViews that could be used as cells for GridLayout.
The cells are placed horizontally behind each other, and you can also change the arrangement and choose one of the horizontal or vertical modes.
Note:
We did not define the orientation property in the code above, so it selects the horizontal value by default.
In the following code, we define this property and assign a vertical value to it:
After inserting these codes, the cells will be arranged vertically.
There are two properties in GridLayout. The row Count property is used to specify the maximum number of rows, and the column Count property is used to specify the maximum number of columns.
In the code below, we set the value of three to column Count and select the horizontal value for the layout so that the cells are in three columns:
Assign a style to TextViews.
Insert the following code into the styles.xml file:
The result after applying the changes will be that three cells are in the first row and three cells are in the second row. Their style and color also change.
In the following codes, we want to change the placement of them and assign a value of two to row Count to see the result of the changes:
Add the word length to text five to make it longer:
After extending the length of the fifth cell, the size and length of the second and fifth cells differ, and we said at the beginning that one of the features of GridLayout is the simultaneous management of columns and rows. To manage it, we can use layout_gravity with fill_horizontal value to see the result.
The result is that it makes the size and length of cell two the same as cell five, called row and column management.
Add another row:
All the cells in the second column will be as large as we changed cell five and cell two. In other words, cell five is in the second column, and we resized it to length, then applied a property to it that all the cells in the second column are the same size.
Another feature of GridLayout is moving cells, such as swapping cells two and five, for example.
We use the layout_row and layout_column properties to move in GridLayout.
Note:
Cell counts start from zero.
In the code above for cell two, I defined the layout_row property with a value of 1, so that cell two is in the second row. By doing this, cell three also goes to the second row, which I again define the layout_row property with a value of 0 for cell three to return to the first row.
For the fifth cell, we define the layout_row property with a value of 0 and the layout_column property with a value of 1. Cell six also changes with cell five, again setting the layout_row property to cell six with a value of 1 to return to row two.
In this part of the tutorial, you learned a lot about the screen:
- How to keep the screen on when running the application using two methods
- How to keep the screen on and active without touching the screen
- How to network and apply some widgets to the screen
- How to use GridLayout to screen the cell
- How to style cells created by GridLayout
- How to resize cells
- How cells move
DotNek Android development services