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);}
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
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.
Android AutoCompleteTextView search and returns the result in List instead of default dropdown.
Is there a way that I could redesign and put the result of an AutoCompleteTextView into a list other than to its default list design?
In my MainActivity, I have this:
String[]fruits = {"apple", "avocado", "banana", "blackberry", "blueberry", "coconut", "durian", "mango", "melon"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, fruits);
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocom);
autoCompleteTextView.setAdapter(stringArrayAdapter);
autoCompleteTextView.setTextColor(Color.RED);
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(stringArrayAdapter);
}
In My xml,
<?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: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.example.android.autocompletenativesample.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:text="Pick a Fruit"
android:id="#+id/tv"
/>
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="#+id/autocom"
android:layout_below="#+id/tv"
android:layout_marginLeft="36dp"
android:layout_marginTop="17dp"
android:completionThreshold="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ListView Implementation"
android:layout_below="#+id/autocom"
android:id="#+id/tv2"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lv"
android:layout_below="#+id/tv2"
>
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RecyclerView Implementation"
android:layout_below="#+id/lv"
/>
</RelativeLayout>
Furthermore, here is a Visual Explanation. It will search in AutoCompleteTextView field but the result will not be displayed below instead on another layout.
Should I edit the ArrayAdapter ang change the layout, or have a CustomAutoCompleteTextView that implements AutoCompleteTextView or and EditText that implements AutoCompleteTextView.
I desire for my AutoCompleteTextView's Result to be formatted like this.
RecyclerView and CardView Result design
Thanks.
i can give you the logic. check out this link.
http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ and this too http://www.tutorialsbuzz.com/2014/08/android-filters-for-simple-listview.html
and filteration is done in adapter so first bind listview with your empty adapter and in textWatcher bindlistview with datasource filteration and notifie for changes.
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.
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" />