Tuesday, August 18, 2015

How to set default text on 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="#e4e9e8"
    >
    <!--
        android:text
            Text to display.
    -->
    <EditText
        android:id="@+id/et"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:hint="Input your country"
        android:padding="10dp"
        android:text="Bangladesh"
        />
    <EditText
        android:id="@+id/et_city"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:hint="Input your City"
        android:padding="10dp"
        android:layout_below="@+id/et"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

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 et_city = (EditText) findViewById(R.id.et_city);

        /*
            setText(CharSequence text)
                Sets the string value of the TextView.
         */
        // Set the default text of second EditText widget
        et_city.setText("Khulna");
 }
}


More android examples