Monday 11 February 2013

Android how to set the hint color of EditText and setting the error tag.

There are two ways to change the hint color of an EdiText.







 Default Hint text Color.



 New supplied Hint text color










Solution 1)
use this Tag:--- android:textColorHint ans supply your color code.

 <EditText
        android:id="@+id/edittext2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="@string/password"
        android:textColorHint="#110F10" />   


Solution 2)

We can set the hint color dynamically.

EditText  customedittext=new EditText();

customedittext.setHintTextColor(Color.parseColor("#110F10"));

* One more thing  in order to set the length of EdiText dynamically  we can achieve like this given below.


/** Setting the length of EdiText Dynamically using  InputFilter */

        EditText  edittextLength =new EditText();
        InputFilter[] FilterArray = new InputFilter[1];
        FilterArray[0] = new InputFilter.LengthFilter(5); // 5 is the length i am passing
        edittextLength .setFilters(FilterArray);


For Complete reference create a project named as CustomeditText.
Please go through the given below classes and layout.


layout>edittext.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:orientation="vertical"
    tools:context=".EdiTextActivty" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="20dp"
        android:text="@string/app_name"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/edittext1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="@string/username" />

    <EditText
        android:id="@+id/edittext2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="@string/password"
        android:textColorHint="#110F10" />

    <Button
        android:id="@+id/clickhere"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="20dp"
        android:textSize="15sp"
        android:text="@string/submit" />

</LinearLayout>


Activity: EdiTextActivty

package com.amit.edittext;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.text.InputFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class EdiTextActivty extends Activity implements OnClickListener{
    private EditText customedittext;
    private Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edittext);
       
        customedittext=(EditText)findViewById(R.id.edittext1);
        login=(Button)findViewById(R.id.clickhere);
        login.setOnClickListener(this);
       
       
        /** Setting the length of EdiText Dynamically using  InputFilter */
       
        InputFilter[] FilterArray = new InputFilter[1];
        FilterArray[0] = new InputFilter.LengthFilter(5); // 5 is the length i am passing
        customedittext.setFilters(FilterArray);
       
        /** Setting the line space dynamically of an EditText */
        customedittext.setLineSpacing(2, 1);
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.edittext, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        if(customedittext.getText().toString().length()<1)
        {

          
            customedittext.setError("Please enter Username");
            /** Setting dynamically the EditText Hint text color */
            customedittext.setHintTextColor(Color.parseColor("#110F10"));
        }
    }

}


string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Custom EditText</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="username">UserName</string>
    <string name="password">Password</string>
    <string name="submit">Enter</string>

</resources>


* One more thing we can do with EditText is instead of showing Dialog for prompt message we can use the Error Tag.

@Override
    public void onClick(View v) {
        if(customedittext.getText().toString().length()<1)
        {

           /** Setting the error tag */
            customedittext.setError("Please enter Username");
           
            /** Setting dynamically the EditText Hint text color */
            customedittext.setHintTextColor(Color.parseColor("#110F10"));
        }
    }

























Please click here CustomeditText for Complete Source code.

No comments:

Post a Comment