Wednesday, August 19, 2015

How to create a transparent EditText in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#ead723"
    >
    <!--
        android:background
            A drawable to use as the background. This can be either
            a reference to a full drawable resource (such as a PNG
            image, 9-patch, XML state list description, etc),
            or a solid color such as "#ff000000" (black).

            May be a reference to another resource, in the form
            "@[+][package:]type:name" or to a theme attribute
            in the form "?[package:][type:]name".

            May be a color value, in the form of "#rgb",
            "#argb", "#rrggbb", or "#aarrggbb".

            We can transparent the EditText by placing a
            transparent background color on EditText widget.
    -->
    <EditText
        android:id="@+id/et"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:hint="Transparent EditText by XML"
        android:background="@android:color/transparent"
        />
    <EditText
        android:id="@+id/et2"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_below="@+id/et"
        android:hint="Transparent EditText programmatically"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.widget.EditText;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        EditText et = (EditText) findViewById(R.id.et);
        EditText et2 = (EditText) findViewById(R.id.et2);

        /*
            setHint (CharSequence hint)
                Sets the text to be displayed when the text of the
                TextView is empty. Null means to use the normal empty
                text. The hint does not currently participate
                in determining the size of the view.
         */
        // Set the initial instructional text of third EditText
        // Set hint of the third EditText
        // Set the placeholder (hint) for second EditText
        et2.setBackgroundColor(Color.TRANSPARENT);
    }
}
More android examples