Sync AutoCompleteTextView with a "submit" button - android

I am working with AutoCompleteTextView. I have added few items through Activity. After AutoCompleteTextView , I've given a "submit" button. I want to sync this button with AutoCompleteTextView . I want to click submit button when any item is selected and after clicking button, it should switch to that item's activity. Currently I don't have code with me. Please suggest me with any example.

Here is the code for AutoCompleteTextView
MainActivity.java
public class MainActivity extends Activity {
public static final String[] COUNTRIES = new String[] { "Belgium", "France",
"Italy", "Germany", "Spain" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// TODO Auto-generated method stub
// In the onCreate method
final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.actv_country);
//EditText e = (EditText) findViewById(R.id.editText1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
textView.setAdapter(adapter);
// textView.setThreshold(0);
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/actv_country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</AutoCompleteTextView>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>

Related

spinner clicking on EditText

When I click EditText, I want spinner to open the menu. When I click on textviewplaces, I want the spinner menu to be opened. How can I do this?
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, OnItemSelectedListener {
private Spinner spinnerDevice;
private static final String[] paths = {"......"};
TextView textviewPlaces;
spinnerDevice = (Spinner)findViewById(R.id.spinnerDevice);
ArrayAdapter<String>adapterSpinnerDevice = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,paths);
textviewPlaces = findViewById(R.id.places_textView);
adapterSpinnerDevice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDevice.setAdapter(adapterSpinnerLocation);
spinnerDevice.setOnItemSelectedListener(this);
XML File:
<AutoCompleteTextView
android:id="#+id/places_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="44dp"
android:layout_marginTop="31dp"
android:text="TextView"
android:drawableLeft="#drawable/location"
android:drawablePadding="2dp"
android:ems="10"
android:textStyle="bold" />
Use AutoCompleteTextView instead of it
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_dropdown_item_line,
paths);
//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item, paths);
final AutoCompleteTextView textView;
textView = (AutoCompleteTextView) findViewById(R.id.textView);
textView.setAdapter(arrayAdapter);
/* textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View arg0) {
textView.showDropDown();
}
}); */
Define UI element in your layout file
<AutoCompleteTextView
android:id="#+id/textView"
android:layout_width="match_content"
android:layout_height="wrap_content"
android:ems="10" />
If you want something like this image
AutoCompeleteTextView Exmaple
Arwy Shelke answer can help you, but if you want to open a real spinner you can use this code:
spinner.performClick();

Creating dynamic Spinner in Android

I want to create Three spinner. In first Spinner, I have to display countries name and according to selection of country name, I have to load the state name of that country in second Spinner and according to selection of state, I have to load cities name of that state in Third Spinner. Can any one post any example and please suggest which technique is better defining spinner data in Java or Xml.
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, locations);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(dataAdapter);
Layout xml
<?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"
android:orientation="vertical" >
<!-- Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:text="#string/lblAcc" />
<!-- Spinner Dropdown -->
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:layout_marginTop="10dip"
android:entries="#array/acc_type" />
<!-- Select Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:text="#string/lblSubAcc" />
<!-- Spinner Dropdown -->
<Spinner
android:id="#+id/spinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
/>
Resource xml should be like following
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Spinner Example</string>
<string name="action_settings">Settings</string>
<string name="lblAcc">Select Account Type</string>
<string name="lblSubAcc">Select Account Head</string>
<string-array name="acc_type">
<item>Income</item>
<item>Expense</item>
</string-array>
</resources>
And the Java class to use
public class SpinnerEx4Activity extends Activity implements
OnItemSelectedListener{
Spinner s1,s2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_ex4);
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
s1.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String sp1= String.valueOf(s1.getSelectedItem());
Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
if(sp1.contentEquals("Income")) {
List<String> list = new ArrayList<String>();
list.add("Salary");//You should add items from db here (first spinner)
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
}
if(sp1.contentEquals("Expense")) {
List<String> list = new ArrayList<String>();
list.add("Conveyance");//you should add items from db here(2nd spinner)
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
With if else ladder you can add more spinner like 3rd spinner depending on 2nd one and so on.

How to add ListView items on button click using adapter

How to take data that was typed in EditText and by clicking "submit" in that window should add it to previous activity listview items?
What I need to do is:
Creating EditText and submit button
Creating listview in same Activity
By clicking submit button it should display it in listview.
I saw this similar question here:add items to listview dynamically android
But i couldn't understand the answer.Somebody please explain how to do this.
You just do the following :
Prepare your xml like this :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/addItem"
android:hint="Add a new item to List View" />
<Button
android:id="#+id/addItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Add" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText" >
</ListView>
</RelativeLayout>
Activity looks like following :
public class MainActivity extends Activity {
EditText editText;
Button addButton;
ListView listView;
ArrayList<String> listItems;
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
addButton = (Button) findViewById(R.id.addItem);
listView = (ListView) findViewById(R.id.listView);
listItems = new ArrayList<String>();
listItems.add("First Item - added on Activity Create");
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
listItems.add(editText.getText().toString());
adapter.notifyDataSetChanged();
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_LONG)
.show();
}
});
}
}
Create String Arraylist and initialize it
ArrayList<String> str1 = new ArrayList<String>();
Add some values on it
str1.add("First Row");
str1.add("Second Row");
str1.add("Third Row");
str1.add("Fourth Row");
str1.add("Fifth Row");
Then set Adapter to ListView
adapter=new ListAdapter(this,str1);
list.setAdapter(adapter);
then add your EditText text into str1 and then called adapter.notifyDataSetChanged(); like
str1.add(edit_message.getText().toString());
adapter.notifyDataSetChanged();
Try to add this code into Button onclick()
Demo Output:
Suppose you have an arraylist
ArrayList<String> dataList = new Arraylist ();
So On clicking of button, you need to add the new data item into your data arraylist.
For this first take the value entered in edittext and store in a string.
String editedValue = yourEditText.getText.toString();
Then we need to add this in our datalist.
Like
dataList.add(editedValue);
And then just call adapter.notifyDataSetChanged()
yourAdapter.notifyDataSetChanged();
It will work.

AutoCompleteTextView suggestion is not popping

I am adding the View to the Header of ListView in the ListActivity, after inflating which has AutoCompleteTextView and not showing the suggestions on entering text. Though it works absolutely fine in normal case but not working fine when added to Header of ListView.
header_route.xml :
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/lblride_From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtFrom"
android:layout_alignParentTop="true"
android:text="#string/lblride_Route"
/>
<AutoCompleteTextView
android:id="#+id/txtFrom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/lblride_From"
android:layout_marginLeft="20dp"
android:layout_marginTop="2dp"
android:completionThreshold="1"
android:ems="10"
android:width="290dp"
android:imeOptions="actionGo"
android:popupBackground="#android:color/black" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="#+id/hdbtnOkRoute"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtFrom"
android:layout_below="#+id/txtFrom"
android:layout_marginTop="2dp"
android:minHeight="40dp"
android:text="#string/txtBtnOk"
android:width="290dp" />
</RelativeLayout>
public class BusRouteActivity extends ListActivity implements TextWatcher {
AutoCompleteTextView actvFrom;
Button buttonOK;
TextView txtViewRouteItem;
ListView lstViewRoute;
ArrayAdapter<String> listAdapter;
DataBaseHelper databaseHelper;
TextView tvFooter;
Context appContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.nameBusRouteActivity);
appContext = getApplicationContext();
lstViewRoute = getListView();
LayoutInflater inflater = (LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.header_route, null);
DataBaseHelper dbh = new DataBaseHelper(getApplicationContext());
try {
dbh.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dbh.openDataBase();
databaseHelper = dbh;
actvFrom = (AutoCompleteTextView) view.findViewById(R.id.txtFrom);
actvFrom.addTextChangedListener(this);
actvFrom.setOnEditorActionListener(actionListener);
ArrayAdapter<String> aroutes = new ArrayAdapter<String>(appContext, R.layout.routeitem, dbh.GetRoutes());
actvFrom.setAdapter(aroutes);
buttonOK = (Button)view.findViewById(R.id.hdbtnOkRoute);
buttonOK.setOnClickListener(OkOnClickListener);
tvFooter = new TextView(this);
tvFooter.setTextAppearance(getApplicationContext(), R.style.footerStyle);
String text = getResources().getString(R.string.footer);
tvFooter.setText(Html.fromHtml(text));
listAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.routeitem);
lstViewRoute.addHeaderView(view, null, false);
lstViewRoute.addFooterView(tvFooter, null, true);
lstViewRoute.setClickable(true);
lstViewRoute.setSelected(true);
lstViewRoute.setAdapter(listAdapter);
}
Please let me know if I am missing any setting? Thanks in advance.
Here is one code that might help you!!
http://www.javatpoint.com/android-autocompletetextview-example

Print lines in Listview using an adapter

I need to output data from lines cm1 and cm2 in a ListView, every time when I press the button. Line should be shown in various TextView.
Code TEST.java:
public String cm1;
public String cm2;
public class TEST extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyList = (ListView) this.findViewById(R.id.listStrings);
setListAdapter();
// button
Button setup = (Button) this.findViewById(R.id.send);
setup.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
cm1 = "cm1text";
cm2 = "cm2text";
//xxxx
}
});
}
// adapter
private void setListAdapter() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.row //idknow what im must writehere to set cm1 string to cm1text tv
List.setAdapter(adapter);
}
Code TEST.xml:
<?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"
android:orientation="vertical" >
<ListView
android:id="#+id/listStrings"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
Code row.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"
android:orientation="vertical" >
<TextView
android:id="#+id/cm1tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/cm2tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Should look ListView after 2 clicks:
cm1
cm2
cm1
cm2
Maybe what you should/can do is have this;
String cm = cm1 + "\n" + cm2
Which should output;
cm1
cm2
Then you change the xml to just have one textview and the adapter code as follows;
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.row, R.id.textViewCM, listItems);
yourListView.setAdapter(aa);
Then, whenever you press the button;
// ...code to initialise/change cm
listItems.add(cm);
aa.notifyDataSetChanged(); // notifies listView to update it's view
That should do what you're after unless you've got to have the two individual textViews, in which case, you'll need to write a custom adapter, I've done a bit of a tutorial on my website if you follow that link.

Categories

Resources