Monday, September 28, 2015

Try android:elevation on TextView

Just try some effect of android:elevation on TextView, for various background.

layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:background="#ffffff">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:textSize="22dp"
        android:textStyle="bold"
        android:text="Simple TextView" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:textSize="22dp"
        android:textStyle="bold"
        android:text="TextView with elevation, no background"
        android:elevation="20dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:textSize="22dp"
        android:textStyle="bold"
        android:text="TextView with both elevation and background"
        android:background="#0000ff"
        android:elevation="20dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:textSize="22dp"
        android:textStyle="bold"
        android:text="TextView with both elevation and background (same as parent background)"
        android:background="#ffffff"
        android:elevation="20dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:textSize="22dp"
        android:textStyle="bold"
        android:textColor="#ffffff"
        android:text="TextView with both elevation and background (black)"
        android:background="#000000"
        android:elevation="20dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:textSize="22dp"
        android:textStyle="bold"
        android:text="TextView with both elevation and transparent background"
        android:background="#00000000"
        android:elevation="20dp" />

</LinearLayout>


Test on Nexus 7, Android 5.1.1

If hardwareAccelerated is disabled in AndroidManifest.xml by adding android:hardwareAccelerated="false" in <application>, the shadow effect will disappear. (tested on Nexus 7 running Android 5.1.1)

src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.blogspot.android_er.androidelevation" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:hardwareAccelerated="false" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


android:hardwareAccelerated="false"

No comments: