banner
月落星河Tsukistar

月落星河Tsukistar

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

Android Development Notes (1)

This image was published by Engin Akyurt on Pixabay and combines the Android Logo.

During the process of developing an independent app, I gradually realized that writing some functions separately can reduce the amount of code in OnCreate() and allow for collapsing other functions when a problem occurs in one function. Overall, functionizing the features makes it easier to modify and read the code. Therefore, I made many modifications to the code afterwards, such as for the example of getting input from a TextView:

username.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                username_text = s.toString();
            }
        });

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

OnCreate(){
    username.addTextChangedListener(textwatcher);
}
TextWatcher textwatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                username_text = s.toString();
            }
        };

This makes it much easier for me 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 always in landscape or portrait 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 landscape/portrait mode. The following code keeps the screen always in portrait mode:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Customizing the exit confirmation dialog#

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

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            AlertDialog.Builder bdr = new AlertDialog.Builder(this);
            bdr.setMessage(R.string.app_name);
            bdr.setIcon(R.drawable.icon);
            bdr.setMessage(R.string.whether_quit);
            bdr.setNegativeButton(R.string.exit, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            bdr.setPositiveButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            bdr.show();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

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, and NegativeButton and PositiveButton can be used as two buttons. Configure the corresponding functionality in the OnClick based on the text displayed by them.

Terminating an Activity#

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

backtoindex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

Removing the default green title bar#

By default, generated apps have a title bar, which is not 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's 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" />

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