banner
月落星河Tsukistar

月落星河Tsukistar

浩瀚中的伟大,孤独间的渺小
github
twitter
youtube
bilibili
email

Android Development Notes (Part 1)

This image was posted by Engin Akyurt on Pixabay, combined with the Android Logo.

During the process of developing a standalone app, I gradually realized that writing some functions separately can reduce the amount of code in OnCreate() and allow other functions to be collapsed when a problem occurs with a certain function. Overall, function modularization makes it easier to modify and read code. Therefore, I made modifications to many codes afterwards. For example, for the example of getting input from a TextView:

Afterwards, when I need to use the same functionality, I would write it like this:

For me, this makes it much more convenient to modify and maintain, as I only need to focus on the parts I need to pay attention to outside of OnCreate, and the rest can be collapsed without having to worry about them.

Keeping the screen in portrait or landscape mode#

For example, when using some social media apps, if certain screens automatically rotate, it can negatively affect the user experience. Therefore, it is better to fix the screen to always be in portrait/landscape mode. The following code keeps the screen in portrait mode:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Customizing the exit confirmation dialog#

When using the back gesture or the back button, many apps will display a dialog asking if you want to exit. This part can be implemented using the following code:

Note that since this code overrides the onKeyDown function, @override cannot be omitted. The code uses AlertDialog, setMessage is used to display the string, usually defined in the string.xml file (for internationalization purposes), setIcon is used to add an icon. NegativeButton and PositiveButton can be treated as two buttons, and you can configure the corresponding functionality in the OnClick based on the text displayed.

Finishing an Activity#

If our program has a button to go back to the previous layer, and we want to actually go back to the previous layer instead of opening a new Activity, we should use finish(); in the click event to close the current Activity. Here is an example of the code:

Removing the default green title bar#

By default, generated apps have a title bar, which is not very aesthetically pleasing. Therefore, I prefer to find a way to remove it. In the AndroidManifest.xml file, in the "android" section, replace the corresponding content with: android:theme="@style/Theme.AppCompat.NoActionBar" and the title bar will be successfully removed.

Webview net:: ERR_CACHE_MISS error#

This error occurs when the application lacks internet access permission. Add the following line below </application> in the AndroidManifest.xml file: <uses-permission android:name="android.permission.INTERNET" /> and the error will be resolved.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.