I am trying to make a fully custom spinner. I am running into difficulties with making the layout that pops up when you press on it. Here is my code for my adapter:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.my_array, R.layout.spinnertext);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
From what I have read in the documentation, the layout used apears to be set by the line:
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Although every time I change it to a new layout that I make, it makes the app fail when I try and use the spinner. I have tried to look for what "android.R.simple_spinner_dropdown_item" looks like so as to figure out if I am maybe missing anything.
All of my layouts I have tried have been linear or relative layouts, with only a textView.
How can I make a custom layout pop up when the spinner is selected?
row.xml to set up the layout on each row (in this case: one image and text each row):
<?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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"/>
<TextView
android:id="#+id/weekofday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Java:
public class AndroidCustomSpinner extends Activity {
String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.row, R.id.weekofday, DayOfWeek);
mySpinner.setAdapter(adapter);
}
}
Related
I'm trying to show simple spinner
mSpinnerHeaderType = (Spinner) findViewById(R.id.spinner);
String[] items = new String[]{Constants.TYPE_112R, Constants.TYPE_314R};
ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerHeaderType.setAdapter(adapter);
In layout xml
<Spinner
android:id="#+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|left"
android:spinnerMode="dropdown"></Spinner>
But when I click on spinner,
either 1. the dropdown list width is almost zero
OR 2. there are no itmes in dropdown
I tried, 1. giving spinner width as match_parent in xml layout and 2. using dropDownWith property for spinner etc, but nothing working
See image below:
What wrong I'm doing?
try this.
mSpinnerHeaderType = (Spinner) findViewById(R.id.spinner);
String[] items = {Constants.TYPE_112R, Constants.TYPE_314R};
ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, items);
mSpinnerHeaderType.setAdapter(adapter);
xml file
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"></Spinner>
I was working with android application and a simple spinner is behaving like a freak.
I am trying to populate a spinner dynamically, and the spinner simply doesn't show me the list of the elements. All I see is the first element selected in the spinner.
private String[] cSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_s_details);
this.cSpinner = new String[] {
"A", "B", "C", "D", "E"
};
Spinner cName = (Spinner) findViewById(R.id.cmbCName);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_dropdown_item, cSpinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cName.setAdapter(adapter);
}
Here is what my spinner looks like :
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/cmbCName"
android:spinnerMode="dropdown"
android:clickable="false" />
What is going wrong ??
I've found the problem!
android:clickable="false" --> Source of the problem
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/cmbCName"
android:spinnerMode="dropdown"
android:clickable="true" />
try your adapter like this:
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, android.R.id.text1, cSpinner)
i hope it will work.
I am facing problem while putting Spinner in layout. My acctual requirement is to place Spinner once, at the top of layout.
This output I am getting:
I have Relative layout
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/string_array"
android:prompt="#string/spinner_msg"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/notesTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
</TextView>`
MyActivity class is extended by ListActivity here's onCreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView=getListView();
listView.setOnItemClickListener(listener);
spinner1=(Spinner)findViewById(R.id.spinner1);
getDetailsCursor();
String[] from = new String[] {"db_column"};
int[] to = new int[] { R.id.notesTextView};
curAdapter=new SimpleCursorAdapter(MyActivity.this, R.layout.mylist, null, from, to);
setListAdapter(curAdapter);
registerForContextMenu(listView);
}`
Here is the code in which spinner inflates on the top of listview :
listView= (ListView) findViewById(R.id.list);
LayoutInflater inflater = LayoutInflater.from(this);
View mTop = inflater.inflate(R.layout.spin, null);
authorityView.addHeaderView(mTop);
R.layout.spin is that layout which contains only spinner.
And inside the List you have to inflate textView only. as you are doing in mylist.xml ,
just remove spinner from it and made seprate xml for spinner.
so spinner is once, at the top of layout (ListView).
I have some difficulty making out what your actual question is, some layout.xml code would help here, too. I think, that you are placing the spinner inside the listitem.xml instead of the main.xml, so that it gets replicated for each item in your listview. Please share some code.
Since you declare both the TextView and the Spinner in mylist.xml, you get both those elements in each Item of your List.
If you only want one spinner, you shouldn't use a ListActivity, but instead create a normal Activity with a Spinner and a ListView in the Layout. Then define another layout to use for the list items (e.g. with only the TextView).
I have used this code for the row layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/bckimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
and this is for the class that builds the list:
public class ListTestActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
String[] myArray = new String[] {...};
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.listlayout,myArray));
ListView listView = getListView();
listView.setCacheColorHint(0);
listView.setBackgroundResource(R.drawable.bckimage);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
//. . .
});
}
}
This shows an image for each row, not for the whole background. How can I add a single image for background? I saw several methods in here also but nothing worked.
Set the image as the background for the ListView element instead:
getListView().setBackgroundResource(R.drawable.bckimage);
Your list rows probably hide the background image. Also add:
getListView().setCacheColorHint(android.R.color.transparent);
As you stated, set background to your list and not to the row layout.
You can set background to your list in following way:
((ListView)findViewById(android.R.id.list)).setBackgroundResource(R.drawable.bckimage);
Whenever you are using ListActivity, it have ListView with id "list".
I’m tryin to make a list box that has a different image and text for each listed item. I tried to creat two setListAdapter, one for the text string to be displayed and another for the image name. It did not work, I’m assuming there is another way to do this.
My CTICTY CLASS FOR THE LIST
public class cFashions extends ListActivity {
TextView selection;
// list to fill list box wi*/
/*
String[] items={"CASUAL","DR ESSES","CAREER","OUTWEAR","FOOTWEAR",
"JEWELRY","ACCESSORIES"};
*/
String[] items={"CASUAL"};
String[] icons={"icon"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fashions);
setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, R.id.label,
items));
setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, R.id.icon,
icons));
selection=(TextView)findViewById(R.id.selection);
}
}
THE XML FILE FOR THE LAYOUT OF EACH CELL IN THE LIST
<?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="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/icon"
android:layout_width="22px"
android:paddingLeft="2px"
android:paddingRight="2px"
android:paddingTop="2px"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
/>
</LinearLayout>
Ted
You need to use one adapter, not two, where the adapter knows how to handle both your TextView and your ImageView. Here is a free excerpt from one of my books that covers this topic in detail.