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" />
Related
I am developing an Android application and I've never worked with MultiAutoCompleteTextView before.
In my layout.xml I've added:
<MultiAutoCompleteTextView
android:id="#+id/autocomplete_subtest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:ems="10"
android:text="" />
And in my DialogFragment:
try {
subtestAutoCompleteTextView = (MultiAutoCompleteTextView) view.findViewById(R.id.autocomplete_subtest);
ArrayAdapter<String> subtestAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, subtests);
subtestAutoCompleteTextView.setAdapter(subtestAdapter);
subtestAutoCompleteTextView.setThreshold(1);
subtestAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
} catch (Exception e) {
e.printStackTrace();
}
The items, however, appear with a transparent text:
If I click on the list item, with the empty text, it completes my MultiAutoCompleteTextView with an expected option.
Any idea why the text does not show on the item?
You should change the text color of list item. It`s may be white so change to black.
EDIT 1
Just to explain how to solve this, I followed this link: How to change text color of simple list item
Basically, create a custom layout.xml file with the TextView:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tv"
android:textColor="#color/font_content"
android:padding="5sp"
android:layout_width="fill_parent"
android:background="#drawable/rectgrad"
android:singleLine="true"
android:gravity="center"
android:layout_height="fill_parent"/>
And in my code I changed:
ArrayAdapter<String> subtestAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.my_layout_file, subtests);
Change The ArrayAdapter, it will work 100%. I test it myself for you. use this
ArrayAdapter subtestAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,subtests);
And subtests object to String Array. Like this
String [] subtests = {"Enamul","Ashraful", "Tonu", "Shawan","Morshed"};
If you initialize it from database or other place and use ArrayList<String> subtests like this. Then you can assign it to an String Array from ArrayListusing 3 line of code like this
String [] subtests2 = new String[subtests.size()];
for (int i = 0; i < subtests.size(); i++) {
subtests2[i]=subtests.get(i).toString();
}
ArrayAdapter subtestAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,subtests2);
Good Luck...
<MultiAutoCompleteTextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:ellipsize="none"
android:ems="10"
android:inputType="textCapWords"
android:scrollHorizontally="true"
android:scrollbars="none"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black"
android:background="#null"
/>
Try this above code in your xml file.its working in my case.
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'm using the AutoCompleteTextView component to filter member data. I've set it up in my layout file as follows
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/autocomplete_swap_pool"
android:layout_width="match_parent"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:layout_height="#dimen/nfl_my_listings_and_my_bids_spinner_height"
android:layout_toLeftOf="#id/icon_on"
android:dropDownSelector="#color/black"
android:dropDownVerticalOffset="5dp"
android:dropDownWidth="wrap_content"
android:inputType="textAutoComplete|textAutoCorrect"
android:popupBackground="#color/white"
android:ems="10"
android:text="" />
I've then initialised it in my code like this
autoTextView = (AutoCompleteTextView) EngineGlobals.iRootActivity.findViewById(R.id.autocomplete_swap_pool);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(EngineGlobals.iApplicationContext, android.R.layout.simple_dropdown_item_1line, Cards);
autoTextView.setThreshold(1);
autoTextView.setAdapter(adapter);
When I enter text the dropdown box appears but the items are not visible, they are there because if I click on the dropdown box items appear. It looks as if they are white font on a white background.
What am I doing wrong?
I think you need to apply textColor to your hint. For that you need custom row layout for your AutoCompleteTextView.
hint_item.xml
<TextView
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#android:color/holo_green_dark" />
YourActivity.java
AutoCompleteTextView autoTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete_swap_pool);
ArrayAdapter<String> autoadapter = new ArrayAdapter<String>(this, R.layout.hint_item, new String[]{"One", "Two", "Three"});
autoTextView.setAdapter(autoadapter);
Output
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 a listview and two textveiws in it for two columns. My goal is to set the colour of e.g. the 5th row of the listview to blue. Details:
config.xml has the layout for the acitivty: buttons and the listview. Part of it:
<ListView android:id="#+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/TextView01">
</ListView>
row.xml defines the two coloumns for the listview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linlay0">
<LinearLayout android:orientation="horizontal"
android:id="#+id/linlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/left"
android:layout_width="160px"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:paddingLeft="5px"/>
<TextView android:id="#+id/right"
android:layout_width="140px"
android:layout_height="wrap_content"
android:maxWidth="140px"
android:singleLine="false"/>
</LinearLayout>
</LinearLayout>
MainActivity.java is for the management for the activity, as well as the listview. So contentview is set to config.xml:setContentView(R.layout.config);
This is how i upload the listview with data:
lv1=(ListView)findViewById(R.id.ListView01);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
for (int i=0; i<31; i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("left1", date[i]);
map.put("right1", name[i]);
mylist.add(map);
}
simpleAdapter = new SimpleAdapter(this, mylist, R.layout.row,
new String[] {"left1", "right1"}, new int[] {R.id.left, R.id.right});
lv1.setAdapter(simpleAdapter);
date[i] and name[i] are arrays declared and uploaded at the beginning of the class.
I am running some queries, comparing arrays and now i want to the set colour of a specific row in the ListView to blue. Like i said contentview is set to config.xml, while the TextViews of the ListView is in row.xml.
So TextView txt1 = (TextView) findViewById(R.id.left); is uninterpretable for Eclipse.
I would have solved this by implemented my own adapter, and used the position parameter in the getView() method of the adapter to set the background of the LinearLayout by code.
In your CustomAdapter in getView method use this
if(position==5)
{
convertView.setBackgroundColor(Color.BLUE);
}
simpleAdapter is a system adpater,you can not control it.In my opioion,if you want set color at 5th row,you should implementation any adapter and override the getView or bindView method
,accord the position(row number) you can do anything you want