Practical Android Layout and UI Implementation Tips

  1. Define gradient backgrounds with layer-list You can create gradient backgrounds for layouts using a layer-list XML resource. For example, this creates a vertical gradient divider with distinct start, center, and end colors:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:startColor="#ffb0b0b0"
                android:centerColor="#ffe0e0e0"
                android:endColor="#fff0f0f0"
                android:height="1px"
                android:angle="90"
                android:dither="true" />
        </shape>
    </item>
</layer-list>
  1. Add text shadows to TextView Enhance TextView readability or style by adding text shadows using shadow attributes:
  • shadowDx: Horizontal offset of the shadow
  • shadowDy: Vertical offset of the shadow
  • shadowColor: Color of the shadow
  • shadowRadius: Blur radius of the shadow
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="24dp"
    android:gravity="bottom"
    android:text="@string/app_name"
    android:textColor="#333333"
    android:textSize="32sp"
    android:typeface="serif"
    android:shadowColor="#f5f5f5"
    android:shadowDx="1"
    android:shadowDy="3"
    android:shadowRadius="1.5" />
  1. Remove the app title bar Hide the default title bar in two ways:
  • In AndroidManifest.xml, add the theme attribute to you're activity: android:theme="@android:style/Theme.NoTitleBar" (use Theme.AppCompat.NoActionBar for AppCompat-based apps)
  • Programmatically call requestWindowFeature(Window.FEATURE_NO_TITLE); before setContentView()
  1. Retrieve app version name Get the application's version name using the PackageManager:
private String getAppVersionName() {
    try {
        PackageInfo packageInfo = getPackageManager().getPackageInfo(
            getPackageName(), 0);
        return packageInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return "Version unavailable";
    }
}
  1. Check network connectivity Verify if the device has an active network connection using ConnectivityManager:
private boolean isNetworkConnected() {
    ConnectivityManager connectivityManager = 
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isAvailable();
}

Note: Ensure your app requests the android.permission.ACCESS_NETWORK_STATE permission in the AndroidManifest.xml.

  1. State-based drawables for TabHost Create a selector XML file to switch drawables for TabHost tabs based on their selected state:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/tab_selected" android:state_selected="true"/>
    <item android:drawable="@drawable/tab_unselected" android:state_selected="false"/>
</selector>
  1. Reuse layouts with include and merge Optimize layout code reuse by extracting common UI components into separate layout files. Use the <include> tag to reference these layouts in other files. For included layouts that don't need a root view group, use the <merge> tag to eliminate redundant view hierarchy layers and improve performance.

  2. Customize ListView dividers and selection states Adjust ListView dividers and selection visuals using XML attributes or code:

  • In XML:
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/transparent"
        android:dividerHeight="4dp"
        android:listSelector="@drawable/list_item_selector" />
    
  • Programmatically set a transparent divider:
    listView.setDivider(new ColorDrawable(Color.TRANSPARENT));
    listView.setDividerHeight((int) getResources().getDimension(R.dimen.divider_height));
    

Example of setting an adapter:

listView.setAdapter(new ArrayAdapter<>(this, R.layout.list_item, R.id.tv_item_title, itemList));

Tags: Android Development Android Layout Mobile UI Android XML App Optimization

Posted on Sat, 09 May 2026 09:21:02 +0000 by shergar1983