Create EditText with dropdown in android

EditText is a view which is used so frequently in Android application,specially in applications which has forms which allow user to fill information of any kind.
When you have filled any form you must have a faced an problem (specially on mobile devices) ie. deleting the text written in EditText and prepare editable dropdown which allows to enter manual data with dropdown.
So I have created my own custom EditText ie. DropDownEditText.This customized view contains a Button inside the EditText itself. So just on pressing this Button only once you can populate the dropdown with ArrayAdapter.

So here my code goes…
First XML code for customized view.
dropdown_edit_text.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android";
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
>
<EditText
android:id=”@+id/dropdown_edit”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:paddingRight=”35dip”
/>
<Button
android:id=”@+id/dropdown_button”
android:layout_width=”30dip”
android:layout_height=”30dip”
android:layout_alignParentRight=”true”
android:background=”@drawable/image_dropdown”
android:layout_centerVertical=”true”
android:layout_marginRight=”5dip”/>
</RelativeLayout>
For creating any custom view we must extend any existing view class.As I am creating a compound view (ie. Combination of many views) I need to extend a view that can contain many child views in it. So I have used RelativeLayout for it.
In my customized view class ie DropDownEditText I have extended RelativeLayout and override its constructors.In its constructor I have inflated a layout resource which I have created (In our case dropdown_edit_text). Then I have taken references to each of the view it contains (ie. a EditText and a Button).

package com.sample.dropdown;
import com.sample.dropdown.R;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
public class DropDownEditText extends RelativeLayout
{
LayoutInflater inflater = null;
EditText edit_text;
Button btn_clear;
public DropDownEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initViews();
}
public DropDownEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
initViews();
}
public DropDownEditText(Context context)
{
super(context);
// TODO Auto-generated constructor stub
initViews();
}
void initViews()
{
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.dropdown_edit_text, this, true);
edit_text = (EditText) findViewById(R.id.dropdown_edit);
btn_clear = (Button) findViewById(R.id.dropdown_button);

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// TODO Auto-generated method stub

}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s)
{
// TODO Auto-generated method stub
}
});
}
public Editable getText()
{
Editable text = edit_text.getText();
return text;
}
}
So now our Customized view DropDownEditText is ready now we can use it wherever we need it.
like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android";
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
<com.sample.dropdown.DropDownEditText
android:id=”@+id/edit_text_dropdown”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />
</LinearLayout>

 Now you can have the activity to with list of items to show and prepare the ArrayAdapter with list of items. Show this list of items with AlertDialog make the user to select the list of items. Try to get the selected item and show on Edittext and we can manually enter the item as well.

I hope you will like it.

Comments

Popular posts from this blog

How to draw an overlay on a SurfaceView used by Camera on Android?

Android TCP Connection Chat application