I have a problem with spinner in my app.It updated when i select value but Problem is that when i close and open activity it shows only first value of my list.can anyone give suggestion for this problem.
public class OtherSettings extends Activity {
Spinner spin1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.historysetting);
addItemsOnSpinner2();
}
public void addItemsOnSpinner2() {
spin1 = (Spinner) findViewById(R.id.timespinner);
List<String> list = new ArrayList<String>();
list.add("5 Minutes");
list.add("10 Minutes");
list.add("20 Minutes");
list.add("30 Minutes");
list.add("40 Minutes");
list.add("1 hour");
dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(dataAdapter);
spin1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
historysetting.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" >
<Switch
android:layout_marginTop="20dp"
android:id="#+id/switch1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Location history"
android:textSize="15dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/historytime"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="historytime"
android:textSize="15dp" />
<Spinner
android:id="#+id/timespinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
</LinearLayout>
Move addItemsOnSpinner2(); to onStart() and call notifyDatasetChanged() on dataAdapter after setAdapter.
Related
Im having hard time integrating search in a custom listview. I have a listview using layout.xml and adapter then when I added search in my existing listview the search is working but the layout in not show in the list.Kindly click here for the file
Android Zip Click here.
Main Activity
public class MainActivity extends Activity{
EditText search;
ListView listview;
ArrayList<Person> list=new ArrayList<Person>();
ArrayList<Person> source=new ArrayList<Person>();
ArrayAdapter<Person> adapter;
AlertDialog.Builder builder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
this.search=(EditText) this.findViewById(R.id.editText1);
this.listview=(ListView) this.findViewById(R.id.listView1);
//populate source
//list.add(new Person(R.drawable.melvin,"Melvin ","PhD Educational Management"));
source.add(new Person(R.drawable.dennis,"Dennis ","Master in Information Technology"));
source.add(new Person(R.drawable.jonathan,"Jonathan ","PhD in Technological Management"));
source.add(new Person(R.drawable.bell,"Bell ","MS Information Technology"));
source.add(new Person(R.drawable.jennifer,"Jennifer ","PhD Technological Management"));
source.add(new Person(R.drawable.gian,"Gian","MS Information Technology"));
//delegatethe list to the adapter
adapter=new ArrayAdapter<Person>(this,android.R.layout.simple_list_item_1,list);
//delegate the adapter to the listview
this.listview.setAdapter(adapter);
//delegate a key watcher
this.search.addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
//clear the listview
list.clear();
//get match
String regex=arg0.toString();
Pattern p=Pattern.compile(regex);
for(int i=0;i<source.size();i++){
Matcher m=p.matcher((CharSequence) list.get(i));
while(m.find()){
list.add(source.get(i));
adapter.notifyDataSetChanged();
}
}
}
});
}
Adapter
package com.example.searchlist;
public class ItemAdapter extends BaseAdapter {
Context context;
ArrayList<Person> list;
LayoutInflater inflater;
public ItemAdapter(Context context, ArrayList<Person> list) {
super();
this.context = context;
this.list = list;
this.inflater=LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
PersonHandler handler =null;
if(arg1==null){
arg1=inflater.inflate(R.layout.item_layout, null);
handler=new PersonHandler();
handler.iv=(ImageView) arg1.findViewById(R.id.imageView1);
handler.name=(TextView) arg1.findViewById(R.id.textView1);
handler.course=(TextView) arg1.findViewById(R.id.textView2);
arg1.setTag(handler);
}
else handler=(PersonHandler) arg1.getTag();
handler.iv.setImageResource(list.get(arg0).getImg());
handler.name.setText(list.get(arg0).getName());
handler.course.setText(list.get(arg0).getCourse());
return arg1;
}
static class PersonHandler{
ImageView iv;
TextView name, course;
}
}
layout
<?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="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0000ff" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ff0000" />
</LinearLayout>
Activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4682b4"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:ems="10"
android:hint="SEARCH"
android:padding="10dp" >
<requestFocus />
</EditText>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#fafad2" >
</ListView>
</LinearLayout>
android spinner is empty by default or when items selected. I tried using the default layout for spinner but still empty. I have checked every question on this website but none helped.
Here is the code:
spinner view on activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/background"
android:orientation="vertical">
<TextView
android:id="#+id/showTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="20sp"
android:textAlignment="center"
android:textColor="#color/textColor"
android:fontFamily="monospace"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
>
</Spinner>
</LinearLayout>
Activity:
public class ShowActivity extends AppCompatActivity {
private List<String> list;
Spinner dropdown;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
TextView titleView = findViewById(R.id.showTitle);
String title = getIntent().getExtras().getString("title");
titleView.setText(title);
list = new ArrayList<>();
dropdown = findViewById(R.id.spinner);
FirebaseFirestore.getInstance().collection(title).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
list.add(document.getId());
}
Log.d("Success ", list.toString());
} else {
Log.d("Failed ", "Error getting documents: ", task.getException());
}
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ShowActivity.this, R.layout.spinner_items, list);
adapter.setDropDownViewResource(R.layout.spinner_items);
dropdown.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
spinner_items.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/spinnerTV"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textSize="20sp"
android:text="Text"
android:gravity="start"
android:padding="10dp"
android:textColor="#color/textColor"
android:layout_marginBottom="3dp"
android:layout_margin="8dp"
/>
thank you in advance.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
FirebaseFirestore.getInstance().collection(title).get().addOnCompleteListener(...) is an asynchronous call. Your code will continue to execute here:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ShowActivity.this, R.layout.spinner_items, list);
adapter.setDropDownViewResource(R.layout.spinner_items);
dropdown.setAdapter(adapter);
adapter.notifyDataSetChanged();
At this time the list is still empty. You need to move this block inside of the OnCompleteListener.
After dropdown.setAdapter(adapter); you have to set up item select/clicked listener, try this:
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//.. do stuff here
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// do default when nothing is selected
}
});
dropdown.setSelection(0) // this makes default selection for dropdown item....
Hope I could help you, best regards,
Csongi
I'm working on a spinner, and I saw this pic from developer.android.com.
This kind of effect is exactly (The way "Home" shows) what I want, but I don't know how to make Spinner looks like it. Android's default effect makes the spinner looks like a bar which is ugly. Could anyone tell me how to do this?
Try this way
<?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"
android:orientation="vertical" >
<Spinner
android:id="#+id/osversions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/selVersion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/osversions"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp" />
</RelativeLayout>
Activity
public class MainActivity extends Activity implements OnItemSelectedListener {
Spinner spinnerOsversions;
TextView selVersion;
private String[] state = { "Cupcake", "Donut", "Eclair", "Froyo",
"Gingerbread", "HoneyComb", "IceCream Sandwich", "Jellybean",
"kitkat" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println(state.length);
selVersion = (TextView) findViewById(R.id.selVersion);
spinnerOsversions = (Spinner) findViewById(R.id.osversions);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, state);
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerOsversions.setAdapter(adapter_state);
spinnerOsversions.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinnerOsversions.setSelection(position);
String selState = (String) spinnerOsversions.getSelectedItem();
selVersion.setText("Selected Android OS:" + selState);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
I created the class below :
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
EditText edit = (EditText) view.findViewById(R.id.editText2);
edit.setText(parent.getItemAtPosition(pos).toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
It results an error when I try to change the value of edittext in the other layout. I put below code in my main activity
rootView = inflater.inflate(R.layout.food, container, false);
Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
After I change the layout of the main activity, then I set the setOnItemSelectedListener for the spinner in that layout.
this is my food.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="#d0dae9"
android:orientation="vertical">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:spinnerMode="dropdown"
android:entries="#array/foodcategory"
android:layout_weight="1" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner1"
android:entries="#array/foodcategory1"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search"
android:id="#+id/button2"
android:layout_weight="1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/editText2"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
</LinearLayout>
and below is the spinner item
<string-array name="foodcategory">
<item>Drinks</item>
<item>Meal</item>
<item>Meat</item>
<item>Snacks</item>
<item>Breads</item>
<item>Cakes</item>
<item>Fruits</item>
<item>Vegies</item>
<item>Other</item>
</string-array>
Please help me out..
update your CustomOnItemSelectedListener class:
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
private final View rootView;
public CustomOnItemSelectedListener(View root) {
rootView = root;
}
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
EditText edit = (EditText) rootView.findViewById(R.id.editText2);
edit.setText(parent.getItemAtPosition(pos).toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
add rootView as argument to CustomOnItemSelectedListener constructor:
rootView = inflater.inflate(R.layout.food, container, false);
Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener(rootView));
I have a spinner within alert dialog. I wanted to reduce padding between spinner items and hence I implemented following:
spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#fff" >
<TextView
android:id="#+id/tvCust"
android:layout_width="200dp"
android:layout_height="30dp"
android:gravity="left|center_vertical"
android:textColor="#000"
android:textSize="15sp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true" />
</RelativeLayout>
Activity code contains following:
spinner= (Spinner) dialog.findViewById(R.id.spinner);
String arr[] = { "1", "2", "3" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
CameraActivity.this, R.layout.spinner_row, R.id.tvCust,arr);
spinner.setAdapter(adapter);
Now as you can see in below screenshot, radio button is getting displayed on spinner which is actually a part of spinner_row.xml. Note that textview width is 200dp while spinner is only 130dp long, so that radio button should not have been displayed on spinner. How can I remove it?
Also, when I click any of the spinner item, spinner pop-up doesn't get disappeared as expected.(note all 3 check-boxes are checked in spinner items list). setOnItemSelectedListener is not getting called on item click.
Any help appreciated.
Edit 1
As per farrukh's suggestion, I tried his code and following is the result.
I have this
and this
with these code of xml
xml for adapter named spinadapt.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#fff" >
<TextView
android:id="#+id/tvCust"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_toLeftOf="#+id/radioButton1"
android:gravity="left|center_vertical"
android:textColor="#000"
android:textSize="15sp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true" />
</RelativeLayout>
and main layout named activity_main.xml
<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">
<TextView
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="Select item"
android:background="#drawable/spin"/>
</RelativeLayout>
and java code is class named MainActivity.java
public class MainActivity extends Activity
{
Spinner sp;
TextView tv;
String[] counting={"One","Two","Three","Four"};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=new Spinner(this);
tv=(TextView)findViewById(R.id.spinner1);
tv.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
sp.performClick();
}
});
sp.setAdapter(new Adapter(MainActivity.this, counting));
sp.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
tv.setText(counting[arg2]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
}
}
and adapter class named Adapter.java
public class Adapter extends BaseAdapter
{
LayoutInflater inflator;
String[] mCounting;
public Adapter( Context context ,String[] counting)
{
inflator = LayoutInflater.from(context);
mCounting=counting;
}
#Override
public int getCount()
{
return mCounting.length;
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = inflator.inflate(R.layout.spinadapt, null);
TextView tv = (TextView) convertView.findViewById(R.id.tvCust);
tv.setText(Integer.toString(position));
return convertView;
}
}
this is working perfect
hope this will help