I am working with auto Complete Text View. when I choose item from it, it is displaying first Item instead of showing selected item.
here is my code
Utilities.setAutoCompleteTextViewAdapter(this, districtsNameList, district);
district.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//selectedDistrict = (String) adapterView.getItemAtPosition(position);
selectedDistrict = districtsNameList.get(position);
Log.d("tag","============positioj===="+position)
Log.d("tag", "22222222222222222222222222===" + selectedDistrict);
district.setText(selectedDistrict);
}
});
and it is the adapter Method
public static void setAutoCompleteTextViewAdapter(Context context, ArrayList<String> arrayList,
AutoCompleteTextView autoCompleteTextView) {
int layoutItemId = android.R.layout.simple_dropdown_item_1line;
ArrayAdapter<String> adapter = new ArrayAdapter<>(context, layoutItemId, arrayList);
autoCompleteTextView.setAdapter(adapter);
autoCompleteTextView.setThreshold(0);
}
// have you increased the size of Threshold
public static void setAutoCompleteTextViewAdapter(Context context, ArrayList<String> arrayList,
AutoCompleteTextView autoCompleteTextView) {
int layoutItemId = android.R.layout.simple_dropdown_item_1line;
ArrayAdapter<String> adapter = new ArrayAdapter<>(context, layoutItemId, arrayList);
autoCompleteTextView.setAdapter(adapter);
// here you have to change the code
autoCompleteTextView.setThreshold(3);
}
Make Custom textview for show selected item text, have look
list_view_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >
<TextView
android:id="#+id/textViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Item name here..."
android:textSize="15dp" />
</RelativeLayout>
now set adapter like this:
myAdapter = new YourAdapter(this, R.layout.list_view_row_item, list);
myAutoComplete.setAdapter(myAdapter);
Now implement itemClick listener like this:
myAutoCompleteText.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
RelativeLayout rl = (RelativeLayout) arg1;
TextView tv = (TextView) rl.getChildAt(0);
myAutoComplete.setText(tv.getText().toString());
}
});
Related
Appreciate some help here on a spinner issue I'm getting.
The list appears fine when clicking on the drop-down arrow. However, when clicking on the selection, the spinner view still appears as blank. The selection's text does not appear. What gives?
On Android Studio's preview, it appears fine from my assigned android:entries. Screenshot here: (https://imgur.com/a/vmdPA)
As you can see, the background is grey, and everything else is white background as well. So I don't think the color is the issue here.
I've checked and changed background colors, and even removed some widget so that I can see what if anything was blocking the selection to appear.
Is there something aside from the normal declaration of Spinner, Arraylist, creating a new arrayadapter, setDropDwonViewResource, setting the arrayadapter to the spinner that I need to do?
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mStoreSpinner.setAdapter(adapter);
The XML for the spinner is also as "simple" as can be:
<Spinner
android:id="#+id/s_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/recyclerord"
app:layout_constraintLeft_toRightOf="#+id/orderID"
app:layout_constraintTop_toBottomOf="#+id/header"
app:layout_constraintRight_toRightOf="#+id/ConstraintLayout"
android:visibility="visible"
android:layout_marginStart="0dp"
android:entries="#array/array_test"
>
</Spinner>
Thank you.
Edited:
This is what I've added.
mStoreSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int postion, long arg3) {
// TODO Auto-generated method stub
String spinnerValue= parent.getItemAtPosition(postion).toString();
Log.d(TAG, "test");
Toast.makeText(getBaseContext(), "Selected item" + spinnerValue, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mStoreSpinner.setAdapter(adapter);
I have not implemented the onClickListeners yet - do they need to be there before the spinner will work fine?
I guess yes.
Add a setOnItemSelectedListener to your Spinner like this :
mStoreSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v,
int postion, long arg3) {
// TODO Auto-generated method stub
String spinnerValue= parent.getItemAtPosition(postion).toString();
Toast.makeText(getBaseContext(),
"Selected item" + spinnerValue,
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Also feel free to see this tutorial to understand it a little bit more
EDIT
You should follow steps :
Declare your Spinner
Spinner spinner = (Spinner) findViewById(R.id.s_spinner);
Create the ArrayAdapter
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(),
spinerArray, android.R.layout.simple_spinner_item);
Set the DropDown
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Set the adapter
spinner.setAdapter(adapter);
There are two ways to implement the setOnItemSelectedListener()
Implementing its interface : implements OnItemSelectedListener
Using setOnItemSelectedListener(new OnItemSelectedListener() {...}
first of remove in xml in spinner control in this line android:entries="#array/array_test" because if you are passing in adapter in list then already spinner control containing arraylist there for remove it and used below code ...
List<String> spinnerArray=new ArrayList<>(); // hear you can add in any array.
spinnerArray.add("Color");
spinnerArray.add("abd");
spinnerArray.add("cde");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
This is working code, may this helps you:
<Spinner
android:id="#+id/spinner"
style="?android:attr/textViewStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:spinnerMode="dialog"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/colorBlack"
android:textColorHint="#color/colorGray"
android:textSize="#dimen/_14sdp" />
Make custom R.layout.list_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/_4sdp"
android:paddingLeft="#dimen/_14sdp"
android:paddingRight="#dimen/_14sdp"
android:paddingTop="#dimen/_4sdp"
android:text="Test"
android:textColor="#color/colorGray"
android:textSize="#dimen/_14sdp" />
Set adapter like this:
SpinnerAdapter adapter = new SpinnerAdapter(mActivity, R.layout.list_item,
android.R.id.text1, yourListHere);
spinner.setAdapter(adapter);
SpinnerAdapter code:
public class SpinnerAdapter extends ArrayAdapter {
public SpinnerAdapter(#NonNull Context context, #LayoutRes int resource, #IdRes int textViewResourceId, #NonNull Object[] objects) {
super(context, resource, textViewResourceId, objects);
}
public SpinnerAdapter(#NonNull Context context, #LayoutRes int resource, #IdRes int textViewResourceId, #NonNull List objects) {
super(context, resource, textViewResourceId, objects);
}
#Override
public int getCount() {
return super.getCount();
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setPadding(0, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom());
return view;
}
}
I had same problem. As per my experience with this:
If we create ArrayList suppose of String type and we used ArrayAdapter to Bind list. Then Please make sure you have converted your ArrayList to String Array.
ArrayAdapter<String> yourAdapter =
new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,
yourArrayList.toArray(new String[yourArrayList.size()]));
This works!
I made a listview and I wanted to make an user input a choice. How can I do this? Atm I only managed to create the ListView and display the data.
Is there an easy way to get the value of a selected item of the ListView and use it later? Something like a ListView RadioButton.
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"
tools:context="com.example.tiagosilva.amob_android.TubeDataArchive" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_tubeData"
android:choiceMode="singleChoice">
</ListView>
Listview:
public class TubeDataArchive extends Fragment {
public TubeDataArchive() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tube_data_archive, container, false);
ListView tubeDataList = (ListView) view.findViewById(R.id.lv_tubeData);
//load tube data
SharedPreferences settings = getActivity().getSharedPreferences("PREFS",0);
String tubeDataString = settings.getString("tubeData", "");
String[] tubeDataSplit = tubeDataString.split("\n");
List<String> tubeDataItems = new ArrayList<>();
for(int i=0; i<tubeDataSplit.length;i++)
{
tubeDataItems.add(tubeDataSplit[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, tubeDataItems);
// Assign adapter to ListView
tubeDataList.setAdapter(adapter);
return view;
}
}
Is there an easy way to get the value of a selected item of the
ListView and use it later?
Add onItemClickListener after the for loop.
tubeDataList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// do whatever you want
Log.d("############","Items " + tubeDataSplit[arg2] );
}
});
Something like a ListView RadioButton
If you want to have a listview with a radio button, then you need to create a custom listview layout.
Add ItemClickListener to your listview
tubeDataList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getapplicationcontext(), tubeDataItems.get(id),
Toast.LENGTH_LONG).show();
}
});
How do I click a ListView. I mean how to make intent on it.
JAVA
public class Login extends ActionBarActivity {
ListView listView;
ArrayAdapter<String> adapter;
String[] grocery_categories = {"Beverages", "Bakery", "Canned Goods", "Condiments", "Dairy", "Snacks", "Frozen Foods",
"Meat", "Produce", "Cleaners", "Paper Goods", "Personal Care", "Others"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
listView = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, grocery_categories);
listView.setAdapter(adapter);
}
}
XMS
<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"
tools:context="com.example.admin.mobilegroceryapp.Login"
android:id="#+id/rl_login">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/login_bckgrnd"
android:src="#drawable/login_bckgrnd"
android:scaleType="centerCrop"
/>
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
I'm an student by the way and exploring Android Studio for my thesis.
You can use an OnItemClickListener to determine which list item gets clicked.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemChosen = (String) parent.getItemAtPosition(position);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("groceryItem", itemChosen);
startActivity(intent);
}
}
set on itemclick listener to your listview
you can explore more about listview here http://developer.android.com/guide/topics/ui/layout/listview.html
if you want use intent on click of listview just put your code in listview click listener
try
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
String grocery = (String) listView.getAdapter().getItem(position);
Intent intent = new Intent(listView.getContext(),/*Your activity*/);
listView.getContext().startActivity(intent);
//or create other intents here
}
});
Now your code should look like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
listView = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, grocery_categories);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
String grocery = (String) listView.getAdapter().getItem(position);
Intent intent = new Intent(listView.getContext(),/*Your activity*/);
listView.getContext().startActivity(intent);
//or create other intents here
}
});
}
I have add two spinners in my code. But bydefault the item of the spinner is displayed inblack color. I want to change the color of the spinner item.I have add the color in simple_spinner_item.xml file. but color is not changed. I have pasted my code here.plz help me...
public class Alert_activity extends Activity implements OnItemSelectedListener{
Spinner s1,s2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_activity);
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) {
String sp1= String.valueOf(s1.getSelectedItem());
if(sp1.contentEquals("Events")) {
List<String> list = new ArrayList<String>();
list.add("Heavy Rain(mm/hr)");
list.add("Smoke/Fog");
list.add("Heat/Cold Wave");
list.add("Strong Winds");
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);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
String item = adapter.getItemAtPosition(position).toString();
Log.d("Item",item);
FragmentTransaction tx = getFragmentManager() .beginTransaction();
if(item=="Heavy Rain(mm/hr)")
{
tx.replace(android.R.id.content, new heavyrain());
tx.commit();
}
});
}
if(sp1.contentEquals("Forecast")) {
List<String> list = new ArrayList<String>();
list.add("Heavy Rain(mm/hr)");
list.add("Cyclone");
list.add("Cloudburst");
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);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
String item = adapter.getItemAtPosition(position).toString();
Log.d("Inside loop","inside loop");
FragmentTransaction tx = getFragmentManager() .beginTransaction();
if(item=="Cyclone")
{
tx.replace(android.R.id.content, new Cyclone());
}
else if(item=="Heavy Rain(mm/hr)")
{
tx.replace(android.R.id.content, new forecast_heavyrain());
}
else if(item=="Cloudburst")
{
tx.replace(android.R.id.content, new forecast_cloudburst());
}
tx.commit();
}
});
}
}
simple_spinner_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"
android:textColor="#FFFFFF"/>
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
((TextView) parent.getChildAt(0)).setTextSize(5);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
or refer this link:How to change spinner text size and text color?
if your simple_spinner_item.xml contains the background color for your spinner item then use
dataAdapter.setDropDownViewResource(R.layout.simple_spinner_item);
instead of dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
similarly,
dataAdapter.setDropDownViewResource(R.layout.simple_spinner_item); instead of dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
you might also have to change the layout set in the ArrayAdapter to a custom layout to solve the issue.
I want a Spinner which contains last Item as "Add more items"
and when i click on it, then i can add next item. The item i have added should get displayed in spinner list and will have same last item as "Add more items"..
I tried using Adapter but how can i keep last element as "Add more items"
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == spinner.getItemIdAtPosition(spinner.getCount()))
// my code for adding item to list using Adapter
else
// spinner.setSelection();
}
did i wrote anything wrong??
any help??..Thanks ...
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dropDownVerticalOffset="2dp"
android:dropDownWidth="500sp"
android:spinnerMode="dropdown" />
</RelativeLayout>
MainActivity.java :
public class MainActivity extends Activity {
private Spinner spinner;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> data = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
data.add("item " + i);
}
data.add("Add New Item");
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
tv.setText(spinner.getSelectedItem().toString());
if (spinner.getSelectedItem().toString().equals("Add New Item")) {
data.remove(position);
data.add(position, "item " + position);
data.add((position + 1), "Add New Item");
updateAdapter(data);
Toast.makeText(getApplicationContext(), "item " + position,
Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
public void updateAdapter(ArrayList<String> data) {
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
data);
adapter.notifyDataSetChanged();
}
}