Open top menu



In this blog implement AutoCompleteTextView  in Android. The AutoCompleteTextView  is a suggestible and editable Text filed. Provided suggestion when user write the text in AutoCompleteTextView  the list of suggestion drop down as a menu then user choose and replace previous value..show this example and try ...


<AutoCompleteTextView    
android:layout_width="match_parent"    
android:layout_height="wrap_content"    
android:id="@+id/XYZ"    
android:textStyle="bold"    
android:hint="XYZ"    
android:layout_column="0"     /> 



Implement the full code....

activity_main.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="fill_parent"    >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40px"
android:text="Write month and week name it will show month in autocomplete"
android:textColor="#000000"android:layout_marginLeft="10dip"
android:id="@+id/textView">
</TextView>
<AutoCompleteTextView           
android:layout_width="match_parent"            
android:layout_height="wrap_content"
android:id="@+id/Months"
android:textStyle="bold"
android:hint="Month"
android:layout_column="0"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="46dp"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/my_heder_img"
android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true" />
</RelativeLayout>

........................................................



MainActivity.Java.
.......................................................

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends Activity implements AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener {

    AutoCompleteTextView textView;
    private ArrayAdapter<String> adapter;

    
    String monthsname[]={
            "Sunday", "Monday", "Tuesday", "Wednesday",
            "Thursday", "Friday", "Saturday","January", "February", "March", "April",
            "May", "June", "July", "August",
            "September", "October", "November", "December"    };


    @Override    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);


        setContentView(R.layout.activity_main);

        textView = (AutoCompleteTextView) findViewById(R.id.Months_and_week);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, monthsname);
        textView.setThreshold(1);
        textView.setAdapter(adapter);
        textView.setOnItemSelectedListener(this);
        textView.setOnItemClickListener(this);

    }
    @Override    public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
                               long arg3) {
    }

    @Override    public void onNothingSelected(AdapterView<?> arg0) {
        InputMethodManager imm = (InputMethodManager) getSystemService(
                INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

    }

    @Override    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
      

       
        Toast.makeText(getBaseContext(), "Position:"+arg2+" Name:"+arg0.getItemAtPosition(arg2),
                Toast.LENGTH_LONG).show();

       

    }

}
.............................................................................................







0 comments