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.
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'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 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 currently have a tabview with a listview inside. I only have three items in the listview and for some reason there is a bunch of white space at top. That is, the first item doesn't start at the top but rather in the middle. Any ideas? Also, i'm thinking that I am using the wrong layout. anything better than a listview if i only need to display three items? Thanks.
Code:
// Data to put in the ListAdapter
private String[] sdrPlaylistNames = new String[]{ "More Playlists...","Dubstep", "House"};
Intent playbackServiceIntentDUB, playbackServiceIntentHOUSE;
//alert dialog
AlertDialog.Builder builder;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlists_layout);
//fill the screen with the list adapter
playlistFillData();
playbackServiceIntentDUB = new Intent(this, DUBAudioService.class);
playbackServiceIntentHOUSE = new Intent(this, HOUSEAudioService.class);
}
public void playlistFillData() {
//create and set up the Array adapter for the list view
ArrayAdapter<?> sdrListAdapter = new ArrayAdapter<Object>(this, R.layout.list_item, sdrPlaylistNames);
setListAdapter(sdrListAdapter);
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="#id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/selectP" android:stackFromBottom="false">
</ListView>
<TextView
android:id="#id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp" />
<TextView
android:id="#+id/selectP"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="Select A Playlist Above"
android:textColor="#FF3300"
android:textSize="22dp"
android:textStyle="bold" >
</TextView>
</RelativeLayout>
If you want to display only three items then I think you have to use TableLayout.
Android - Table Layout.
Oh I missed your real problem in my previous answer.
Simply set android:layout_alignParentTop="true" to your list view as
<ListView
android:id="#id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#+id/selectP" android:stackFromBottom="false">
</ListView>
Hope this time I am clear..
Set your list view to AlignParentTop = true