what makes my spinner view look different? - android

I tried to create a spinner and I want the view to look like this picture
But why do I get a result like this
Here is my code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:orientation="vertical" >
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#+string/spinner_title"
android:drawSelectorOnTop = "true"/>
</LinearLayout>
And my activity
arrSpinner = new Spinner(this);
List L = new ArrayList<String>();
L.add("Test 1");
L.add("Test 1");
arrAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item,L);
arrSpinner.setPrompt("Pilih Jawaban");
addContentView(arrSpinner, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
arrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
arrSpinner.setAdapter(arrAdapter);

I believe you're missing the android:spinnerMode attribute in your XML:
android:spinnerMode="dialog"
Hope that helps!
Edit: You'll need to actually use your XML spinner in the activity as well
arrSpinner = (Spinner) findViewById(R.id.spinner);
...instead of:
arrSpinner = new Spinner(this);

Related

Two custom spinner layouts

I am trying to use two spinners with a custom dropdown yet, only the bottom one is showing with the custom layout when pulled down. I have been trying on my own to figure out why but I cannot.
public class setup extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setuplayout);
Spinner spinner1 = (Spinner) findViewById(R.id.Spinner01);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.numberPlayers, R.layout.spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner1.setAdapter(adapter);
Spinner spinner2 = (Spinner) findViewById(R.id.Spinner02);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.gameDifficulty, R.layout.spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner2.setAdapter(adapter2);
}
}
This is the xml code for spinner_item mentioned above.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:textColor="#F9B12F"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="left"
android:padding="5dip"
android:popupBackground="#000000"
android:background="#000000"
/>
This is the setuplayout 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="match_parent"
android:orientation="vertical">
<ImageView android:id="#+id/mainSetupImage"
android:src="#drawable/setup"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageView>
<ImageView
android:id="#+id/players"
android:clickable="true"
android:src="#drawable/players"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true">
</ImageView>
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawSelectorOnTop="true"
android:id="#+id/Spinner01"
android:textColor="#F9B12F"
android:layout_toRightOf="#+id/players"
android:layout_marginLeft="25dp"
android:layout_centerVertical="true"
android:background="#drawable/spinner"/>
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawSelectorOnTop="true"
android:id="#+id/Spinner02"
android:textColor="#F9B12F"
android:layout_below="#+id/Spinner01"
android:layout_toRightOf="#+id/difficulty"
android:layout_marginTop="25dp"
android:background="#drawable/spinner"/>
<ImageView
android:id="#+id/difficulty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/Spinner02"
android:clickable="true"
android:src="#drawable/difficulty" />
</RelativeLayout>
in 2nd spinner u used adapter rather than adapter2. so use adapter2 and run your code hope it will run. :)
I figured it out with your help. I had used the code "adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)" which was overriding my custom layout. Thanks for highlighting where the two codes were different. That was a huge help. I deleted the quoted lines and it now works.
I'd like to share a little code that will make all people's code much cleaner when making ArrayAdapters.
Just define a static method in a class of your preference (even MainActivity...) to give you the Array adapter without dirting so much your code:
public static ArrayAdapter<CharSequence>
getArrayAdapter( Context c, int arrayId, int idLayout1, int idLayout2){
ArrayAdapter<CharSequence> aa;
aa = ArrayAdapter.createFromResource( c, arrayId , idLayout1);
aa.setDropDownViewResource( idLayout2);
return aa;
}
Then in your code you just need to do it:
myspinner.addAdapter(
this, ThatClass.getArrayAdapter(
R.array.myid, R.layout.id1, R.layout.id2 );
myspinner.setOnItemSelectedListener(this);
/* Remember that the default Android Spinner layout can be get passing these specific layout ids:
android.R.layout.simple_spinner_item as layout1
android.R.layout.simple_spinner_dropdown_item as layout2
*/

Custom Diaglog box android

i am trying to make a custom dialog box with 2 spinners and 2 buttons.
i am doing the following coding
this is xml for custom GUI inside dialog box
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SORT BY" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="#string/prompt1"
android:entries="#array/ordersortby"
android:layout_gravity="center"/>
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ORDER"
/>
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="#string/address"
android:entries="#array/ordersortby1"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
/>
in strings i am declaring following
<string name="prompt1">Order Number</string>
<string-array name="ordersortby">
<item>Order Number</item>
<item>Date Submitted</item>
<item>Date Entered</item>
</string-array>
<string-array name="ordersortby1">
<item>ASC</item>
<item>DESC</item>
</string-array>
and in activity i am doing following
final Dialog dialog = new Dialog(orders.this);
dialog.setContentView(R.layout.orderpicker);
dialog.setTitle("Sort By Dialog");
dialog.show();
when running this , i am getting this
spinner
my problem is why i am not getting any data inside these pickers. please help me.
Let use below code,
spinner_ordersortby = (Spinner)findViewById(R.id.spinner_ordersortby);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.ordersortby, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_language.setAdapter(adapter);
another thing is make sure your array should inside this directory
/res/values/arrays.xml
Use below code before dialog.show(); and define your string array into string.xml file, it will solve your problem.
Spinner mSpinner1 = (Spinner)dialog.findViewById(R.id.spinner_ordersortby);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.string.ordersortby, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner1.setAdapter(adapter);
in your code try something like this
LayoutInflater factory = LayoutInflater.from(this);
View myview = factory.inflate(R.layout.orderpicker, null);
Dialog dialog = new Dialog(orders.this);
dialog.setContentView(myview);
dialog.setTitle("Sort By Dialog");
dialog.show();
First get the view.
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.yourlayout, null);
Now use this view to declare your spinner.
Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
This will solve your problem.

Spinner with long text not working fine

I have some problems with the spinner. Depending of my dates, I must add to a TableRow a TextView with an EditText or a Spinner. My array that must be display in Spinner is a little long. I tested my code with an array with short texts, and it looks like this :
Here the single problem is that spinner is not fill_parent.
If I put my array to spinner it looks like this :
In this case, the spinner doesn't look like a spinner and the EditText is not visible any more. When I choose the spinner, it appears this view :
Here I need to display all the text of the array.
This is my code :
TableRow.LayoutParams lp = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT , TableRow.LayoutParams.WRAP_CONTENT);
tablerow_product[i] = new TableRow(viewToLoad.getContext());
tablerow_product[i].setLayoutParams(lp);
product_spinner[i] = new Spinner(viewToLoad.getContext());
product_spinner[i].setLayoutParams(lp); product_spinner[i].setBackgroundResource(R.drawable.spinner_selector);
String[] proba={"red","blue"}; //first image is with this test array
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(viewToLoad.getContext(), com.Orange.R.layout.my_spinner_textview,spinnerArray); spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
product_spinner[i].setAdapter(spinnerArrayAdapter);
tablerow_product[i].addView(product_spinner[i]); Themes_TableLayout.addView(tablerow_product[i],new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
and my_spinner_textview.xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#drawable/textorange_selected"
android:gravity="left"
android:singleLine="false"
android:ellipsize="end"/>
Can anyone help me to solve it? Any idea is welcome. Thanks in advance.
For my problem I found this solution :
Spinner language = (Spinner) findViewById(com.Orange.R.id.current_language_text);
ArrayAdapter adapter = new ArrayAdapter(this,
com.Orange.R.layout.my_spinner_textview, languages);
adapter.setDropDownViewResource(com.Orange.R.layout.multiline_spinner_dropdown_item);
language.setAdapter(adapter);
where languages is a String[] and my_spinner_textview.xml is :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview_spinner"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#drawable/textorange_selected"
android:paddingLeft="5dp"
android:singleLine="true"
android:ellipsize="end"
/>
simply I did with custom text view like:
ArrayAdapter myAdapter = new ArrayAdapter(context, R.layout.text_view, reasonTypesList);
myAdapter.setDropDownViewResource(R.layout.text_view);
mySpinner.setAdapter(myAdapter);
and here is the text_view layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTextView"
style="?android:attr/spinnerItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/padding"
android:text="Custom Text with multi lines" />

EditText in Header of ListView is uneditable

I have an EditText in the Header of a ListView along with a Spinner and Button. For some reason, the EditText is not allowing me to type within it. The soft keyboard pops up and a cursor appears in the box onclick, and the keyboard recognizes that I am typing, however nothing appears in the box.
Here's the java for the onCreate that handles making the header.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keyvalue);
m_items = Utils.getTrackerCursor(this, 0, "");
m_items.getCount();
startManagingCursor(m_items);
String[] columns = new String[] {"name","value","result"};
int[] to = new int[] {R.id.txtkeyValue_rowTitle, R.id.txtkeyValue_rowValue, R.id.txtkeyValue_rowResult};
m_adapter = new trackerAdapter(this, R.layout.keyvalue_row, m_items, columns, to);
Button btnRollAll = new Button(this);
btnRollAll.setText("Roll All");
final Spinner spinTrackerGroups = new Spinner(m_Context);
m_trackerGroups = Utils.getTrackerGroups(m_Context);
String[] spinVals = triMap.getArray(m_trackerGroups);
ArrayAdapter<CharSequence> Adap_spinTrackerGroups = new ArrayAdapter<CharSequence>(
this, android.R.layout.simple_spinner_item, spinVals);
Adap_spinTrackerGroups.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinTrackerGroups.setAdapter(Adap_spinTrackerGroups);
EditText search = new EditText(m_Context);
search.setHint("Search");
search.setWidth(200);
LinearLayout lay = new LinearLayout(m_Context);
lay.addView(spinTrackerGroups);
lay.addView(search);
lay.addView(btnRollAll);
getListView().addHeaderView(lay);
this.setListAdapter(m_adapter);
LinearLayout.LayoutParams params = (LayoutParams) spinTrackerGroups.getLayoutParams();
params.width = 230;
spinTrackerGroups.setLayoutParams(params);
spinTrackerGroups.setSelection(1);
}
I'm including the listview's xml as well as the individual listview row's xml for good measure.
<?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="#android:id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<?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" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="50dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txtkeyValue_rowTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:layout_weight=".5"
android:ellipsize="middle"
android:paddingLeft="10dp"
android:text="TextView"
android:textSize="16sp"
android:singleLine="true" />
</LinearLayout>
see this thread please: Focusable EditText inside ListView

Can't select spinner item in android

I select item from spinner but this item can't select and show. Help me (I write english not good)
This code.Layout
<?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"
android:padding="15dp" >
<Spinner
android:id="#+id/spn_nganhang"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Activity
ArrayList<String> arr_listbank = new ArrayList<String>();
ArrayAdapter<String> arra_listbank = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, arr_listbank);
arra_listbank.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spn_listbank = (Spinner)findViewById(R.id.spn_nganhang);
spn_listbank.setAdapter(arra_listbank);
Full code: http://qhoang.org/paste/activity.txt
Help me.
Add something to array list:
ArrayList<String> arr_listbank = new ArrayList<String>();
arr_listbank.add("Hello");
arr_listbank.add("Hi");
and see if you are getting anything or not.

Categories

Resources