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.
Related
i am newbie in android and i am trying to use two spinner views in an activity. entries are showing in drop down of spinner but when i select an entry, it doesn't appear in spinner control. I have searched all over but it didn't work for me. following is all i am doing/trying.
My activity xml is as
<?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:id="#+id/activity_seventy_thirty"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.elecsysindia.montestapp.SeventyThirtyActivity">
<TextView
android:text="Division"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtDivison"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinnerDivision"
android:layout_marginLeft="32dp"
android:layout_alignTop="#+id/txtDivison"
android:layout_toRightOf="#+id/txtDivison"
android:layout_toEndOf="#+id/txtDivison" />
<TextView
android:text="Station"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtDivison"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:id="#+id/txtStationCode"
android:textSize="20sp"
/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinnerStation"
android:layout_below="#+id/spinnerDivision"
android:layout_alignLeft="#+id/spinnerDivision"
android:layout_alignStart="#+id/spinnerDivision"
android:layout_marginTop="16dp"
android:layout_marginStart="48dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="48dp" />
<Button
android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnSubmit"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_alignLeft="#+id/spinnerStation"
android:layout_below="#+id/spinnerStation"
/>
</RelativeLayout>
and in class:
private static List<String> listDivisions = new ArrayList<String>();
private List<String> listStation = new ArrayList<>();
Spinner spinnerDivision ;
Spinner spinnerStation;
Button btnSubmit;
ArrayAdapter<String> adapterDivision;
ArrayAdapter<String> adapterStation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seventy_thirty);
spinnerDivision = (Spinner) findViewById(R.id.spinnerDivision);
spinnerStation = (Spinner) findViewById(R.id.spinnerStation);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
// adapterDivision = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,listDivisions);
adapterDivision = new ArrayAdapter<String>(getApplicationContext(),R.layout.layout_spinner_item,listDivisions);
adapterDivision.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDivision.setAdapter(adapterDivision);
adapterDivision.notifyDataSetChanged();
adapterStation = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,listStation);
adapterStation.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerStation.setAdapter(adapterStation);
adapterStation.notifyDataSetChanged();
And this is how lists are updated:
for (int i = 0; i < subs.length(); i++){
JSONObject subsDetail = subs.getJSONObject(i);
ArrayList<String> stnList = new ArrayList<String>();
if(subsDetail != null){
stnList.clear();
String DivCode = subsDetail.getString("divCode");
JSONArray st = subsDetail.getJSONArray("stncode");
String city="";
for (int j=0 ; j<st.length(); j++){
stnList.add(st.getString(j));
city = city.concat(st.getString(j)+"-");
}
mapSubscriptions.put(DivCode,stnList);
listDivisions.add(DivCode);
listStation.addAll(stnList);
I am getting data from simple php script and this data is getting inserted to lists.
Also after searching related issue in Sof, i have tried to use a separate layout for spinner as used in
adapterDivision = new ArrayAdapter<String>(getApplicationContext(),R.layout.layout_spinner_item,listDivisions);
but still of no use.
xml file for layout_spinner_item is as follows:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinnerItem"
android:textSize="20sp"
android:gravity="left"
android:textColor="#aaddaa"
android:padding="5dip"
/>
I even tried to make text color change in theme itself by setting property android:textColor. but again no success.
<item name="android:textColor">#FF00FF</item>
can you please help me why text is not appearing in spinner control.
After a lot of struggle, text in one spinner is appearing but that appears only if i restart activity directly , i mean without navigating to activity from main activity.
second spinner (spinnerStation) dont show the text at all. even same source code for both spinners. please see screen shots.
text appear in spinnerDivision
Entries of spinnerStation which dont show text
You have to programmatically select the position.
Like this
spinnerObject.setSelection(INDEX);
And, you should change this
adapterDivision = new ArrayAdapter<String>(getApplicationContext(),R.layout.layout_spinner_item,listDivisions);
to this,
adapterDivision = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,listDivisions);
The problem is, your adapter is not getting TextView from custom layout file
reports = (Spinner)findViewById(R.id.spinner_report);
reportType = getIntent().getStringExtra("report");
private void setTypeReport(){
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.type_report, R.layout.item_spinner_p);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
reports.setAdapter(adapter);
int pos= adapter.getPosition(reportType);
type_report= getResources().getStringArray(R.array.type_report);
String valueAtIndex = type_report[pos];
for(int i = pos; i > 0; i--){
type_report[i] = type_report[i-1];
}
type_report[0] = valueAtIndex;
//now set this array to second Spinner
ArrayAdapter spinnerBArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item,
type_report);
reports.setAdapter(spinnerBArrayAdapter);
newreport = reports.getSelectedItem().toString();
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#eee"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:layout_height="match_parent">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_report"
android:background="#drawable/round_box"
android:visibility="gone"
android:layout_marginBottom="5dp">
</Spinner>
</RelattiveLayout>
item_spinner_p.xml
<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:textColor="#color/black"
android:textSize="17sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"/>
string.xml
<string-array name="type_report">
<item>Male</item>
<item>Female</item>
</string-array>
Default color is black in item_spinner_p.xml ... either male or female , it will show black color.. Problem is , I want to do when reportType = Male (from previous activity), it will change color to Red and if reportType = Female (from previous activity) , it change color to Blue.
I think to use way set color in string.xml .. set color Red for Male .. but i dont know correct way to set it ..
You have to create custom spinner adapter. Look at this page for how to do that: http://mrbool.com/how-to-customize-spinner-in-android/28286
But if you want simple solution you can create two separate layouts for male and females like this :
For males: item_males
<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:textColor="#android:color/red"
android:textSize="17sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"/>
And for females: item_females
<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:textColor="#android:color/blue"
android:textSize="17sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"/>
With text colors set to red and blue for males and females respectively. Then apply a check while setting the adapter like this :
ArrayAdapter<CharSequence> adapter;
if(reportType.equals("Males")){
adapter = ArrayAdapter.createFromResource(this,R.array.type_report, R.layout.item_males);}
else{
adapter = ArrayAdapter.createFromResource(this,R.array.type_report, R.layout.item_females);}
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);
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" />
I have one Activity in which I try to show some simple messages, and then a list of items. So as I understand, I need two views: one for the simple layout, and one for the list of items which will be handled by the adapter.
Here is my code:
ArrayAdapter<SolutionTopic> adapter;
ArrayList<SolutionTopic> problems = new ArrayList <SolutionTopic>( );
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Load your layout
setContentView(R.layout.solution);
SolutionTopic s = new SolutionTopic ();
s.setSolutionTopicName( "Hello" );
problems.add(s);
adapter = new ArrayAdapter<SolutionTopic>(this, R.layout.solution_topic_list,
R.id.label, problems);
TextView solution_title = (TextView) findViewById(R.id.solution_title);
TextView solution_description = (TextView) findViewById(R.id.solution_description);
setListAdapter (adapter);
}
and here is the solution.xml
<?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="wrap_content" >
<TextView
android:id="#+id/solution_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label1"
/>
<TextView
android:id="#+id/solution_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label2"
/>
</LinearLayout>
and here is the solution_topic_list.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
What I am not sure is how to get these two to render together. Ideally what I want to do is display the TextView messages first, and then the list of items.
Any idea how I can do that?
Thanks!
You shouldn't remove the ListView from solution.xml, in that ListView your SolutionTopics will be displayed.
To get the big picture, your interface would have three Views:
-TextView: #+id/solution_title
-TextView: #+id/solution_description
-ListView: #android:id/list
The ListView contains an undefined number of entries of solution_topic_list.xml.
PD: The third parameter of ArrayAdapter (R.id.label) should be the id of the TextView in solution_topic_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
You also should implement toString method in your SolutionTopic class, to get the desired String.