I am trying to populate a spinner but I seem to be missing something in my layout file
ArrayAdapter<String> cuisines = new ArrayAdapter<String>(this, R.layout.spinner_view,
getResources().getStringArray(R.array.cuisines));
I can't find R.layout.spinner_view and can only assume that I have to make it myself in my layout file. How do I do that?
Include spinner in xml file as:
<Spinner
android:id="#+id/spin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
and in activity:
Spinner spinner=(Spinner) findViewById(R.id.spin);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(YourActivity.this, android.R.layout.simple_spinner_item,R.array.cuisines);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Use import com.companyname.product.R;
instead of
import android.R;
If you want to load with default spinner view, then use,
ArrayAdapter<String> cuisines = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
getResources().getStringArray(R.array.cuisines));
Related
I have a problem regarding the spinner design. I am using this code to generate a dropdown spinner:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
LinearLayout layout = new LinearLayout(this);
ArrayList < String > spinnerArray = new ArrayList < String > ();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
ArrayAdapter < String > spinnerArrayAdapter = new ArrayAdapter < String > (this,
android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
layout.addView(spinner);
setContentView(layout);
}
It displays this:
How can I remove the black color that blocks the spinner?
layout.addView(spinner);
setContentView(layout);
When you dynamically add the view to the layout. you are missing some configuration. thats the reason you see a black box.
try the below code:
xml:
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
activity:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
arraydata, android.R.layout.simple_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
spinner.setAdapter(adapter);
Create a layout file simple_list.xml in your layout folder:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:text="Sample Text"
android:padding="5dp"
android:gravity="center"
android:textColor="#android:color/black"
android:background="#android:color/white"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"/>
And refer this in arrayadapter:
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
R.layout.simple_list, spinnerArray);
Update 1 Add this:
spinner.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),android.R.color.white));
update 2
Instead of using constraint use Linear once in your layout file:
<?xml version="1.0" encoding="utf-8"?>
<LineartLayout xmlns:android="schemas.android.com/apk/res/android" xmlns:app="schemas.android.com/apk/res-auto"
android:id="#+id/linearLayout"
xmlns:tools="schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> </LinearLayout>
Set id to LinearLayout (your root view), then use findViewById for this view and add spinner to this rootView and remove setContentView(layout);
Change it like this:
LinearLayout layout = new LinearLayout(this);
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
layout.addView(spinner);
setContentView(layout);
Interestingly your code worked for me. So make sure that there is no element there or some other piece of code related to theme or colors.
Add a linearlayout to your activity_my layout and name it linearLayoutContainer . Then get a reference to it from code.
LinearLayout container = findViewById(R.id.linearLayoutContainer);
Spinner spinner = new Spinner(this);
container.addView(spinner);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
I've created a spinner using the code below. Now I want to use a drawable resource called circle.xml, to set the background for each item in the spinner so that each number appears inside a circle.
Spinner equationSpinner = new Spinner(this);
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("");
spinnerArray.add("1");
spinnerArray.add("2");
spinnerArray.add("3");
spinnerArray.add("4");
spinnerArray.add("5");
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
equationSpinner.setAdapter(spinnerArrayAdapter);
I can use the circle as a background in a TextView so I tried creating the spinnerArray as an array of TextViews but that appeared to be incompatible with the simple_spinner_dropdown_item.
Anybody have an idea how I can do this? I just want each number to appear inside a circle in the spinner. I will also need to be able to access the number selected in the spinner.
Thanks
P.S. Is there any way in Android to create a 'slot machine' style spinner like you have in iOS? I prefer the look of those.
EDIT:
I've now found that I can use my circle resource by creating a custom layout (called spinner_item.xml) like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="28dip"
android:layout_height="28dip"
android:gravity="center"
android:textSize="20sp"
android:background="#drawable/circle"
android:textColor="#ff0000" />
I then set the adapter for my spinner programmatically like this:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.equations, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_item);
equationSpinner.setAdapter(adapter);
However, this method requires that I have a predefined string array set up in my strings.xml file (R.array.equations). I need to be able to specify the array of strings to be used programmatically depending on the state of the app. Can I modify the array once its set up?
EDIT 2:
Additionally I've found I can set the background of the dropdown items and use my own programmatical array like this:
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
equationSpinner.setAdapter(spinnerArrayAdapter);
The only problem then is being able to apply the resource to the background of the spinner itself (not the drop down items). There does not appear to be an equivalent to setDropDownViewResource for the main spinner item.
lets say textview_background.xml is the background you want to give to the textView to Spinner
make a file in drawable and name it this textview_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/holo_blue_dark" />
<corners android:radius="7dp" />
<stroke
android:width="3dp"
android:color="#android:color/tertiary_text_light" />
</shape>
make a Spinner in your activity
<Spinner
android:id="#+id/SpinnerOneActivity_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
make a layout with name list_text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding="4dp"
android:text="some"
android:background="#drawable/textview_background"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#android:color/black" />
make a class which will fill data from a list to your spinner, in this code your Adapter class is taking list of String but you can have your own custom object list given to it to populate the spinner, watch out how adapter's constructor parameters.
first-Context
Second-layout which will be given to each item in spinner
Third-a textView id from the above layout
Forth-your list of objects which you want to populate
TestAdapter.java
public class TestAdapter extends ArrayAdapter {
private ArrayList<String> strings;
private Context context;
private LayoutInflater layoutInflater;
public TestAdapter(Context context, int resource,int id, ArrayList<String> strings) {
super(context, resource, id, strings);
this.context = context;
this.strings = strings;
this.layoutInflater = LayoutInflater.from(this.context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = layoutInflater.inflate(R.layout.list_text_view, parent, false);
TextView textView = (TextView) view.findViewById(R.id.text1);
textView.setText("" + strings.get(position));
return view;
}
}
in your activity you can write this code
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("Two");
list.add("Three");
list.add("four");
Spinner spinner = (Spinner) findViewById(R.id.SpinnerOneActivity_spinner);
spinner.setAdapter(new TestAdapter(getApplicationContext(), R.layout.list_text_view, R.id.text1,list));
As you wanted you can increase the list or generate things dynamically to add or remove from the list once your list is attached to adapter which will be provided to Spinner you can call notifyDataSetChanged() on adapter to updated the list
Check this answer about Android Wheel: https://stackoverflow.com/a/9503372/3677394
There's a very good 3rd party open source project called Android Wheel
that pretty much does everything involved in creating a slot machine
for you. And if you want to customise it, it's under an Apache
licence, so no issues there.
Well I have this
in xml
<Spinner
android:id="#+id/spinnerLanguages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
and In Fragment code file
I have inflated in oncreate
viewInflate = inflater.inflate(R.layout.fragment_search, null);
and
spinnerLanguages = (Spinner) viewInflate.findViewById(R.id.spinnerLanguages);
String[] items1 = new String[] {"lang1", "lang2", "lang3","lang4"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, items1);
spinnerLanguages.setAdapter(adapter);
But i am getting Null exception in this line
spinnerLanguages.setAdapter(adapter);
in jellybean(4.1) but it is working fine in 4.2+
How can i solve this problem?
Thank you.
You are probably giving different version of the layout file fragment_search.xml in your layout-vx directories: check how many fragment_search.xml exist in your layour directories (e.g. inside layout-v17 vs. layout or layout-v16).
I am working on a first basic android app as a university project and I have an array list of names. I want it to be put into a ListView. The problem I have is nothing appears when I bring in the ListViewunder the xml document. I don't get the basic template for item1, sub item 1 etc.
Then when I set up the array to play in the list view still empty list. Below I have the xml document
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="277dp"
android:id="#+id/listView" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:layout_marginTop="7dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
then here is the java code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
populateListView();
}
private void populateListView() {
String[] myItems = {"frank","george","alice", "anna"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.main, myItems);
ListView list = (ListView)findViewById(R.id.listView);
list.setAdapter(adapter);
}
Your problem is that you have the same layout R.layout.main for two different use.
You set the content view as setContentView(R.layout.main); where your ListView is but you also use this layout with your adapter as new ArrayAdapter<String>(this, R.layout.main, myItems);.
So with your code, you don't have any item layout to display your array myItems. You need to create another layout as R.layout.item_layout with a TextView inside (or use an android layout like android.R.layout.simple_list_item_1) for your adapter.
Note: to avoid some bugs with ListView, you have also to set the height to match_parent or fill_parent
Try this for your adapter:
ArrayAdapter<String> adapter=
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myItems);
The adapter has to inflate a TextView. You can make your own TextView (if you want to customize it) in XML.
Then it will be:
ArrayAdapter<String> adapter=
new ArrayAdapter<String>(this, R.layout.yourcustomtextview, myItems);
The default array adapter in Android takes the layout for the row as the second argument.
Change your ArrayAdapter constructor to something like this:
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items)
How can I change the font size in a ListView element? In my main.xml file, I have tried several different values in for android:textSize (pt,px,sp,dp) and nothing seems to change it.
Here is what I have currently for the in my main.xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:textColor="#ffffff"
android:background="#000080"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:dividerHeight="1px"
android:layout_marginTop="5px"
android:textSize="8px"/>
Here is my Java:
package com.SorenWinslow.TriumphHistory;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class TriumphHistory extends ListActivity {
String[] HistoryList;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter;
HistoryList = getResources().getStringArray(R.array.history);
adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,HistoryList);
setListAdapter(adapter);
}
}
Go into layout create a new xml file named mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
now in the code
adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,HistoryList);
change it to
adapter = new ArrayAdapter<String> (this,R.layout.mylist,HistoryList);
2 ways to go:
Copy simple_list_item_1.xml from Android sources, modify it and then use it instead of android.R.layout.simple_list_item_1;
Use BaseAdapter and modify font size in getView(..) call.
I'd suggest you go with latter.
Please create another file named list_item.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="6dp"
android:textSize="21sp">
The quick answer is that you can only change the text size by using your own layout rather than the android.R.layout.simple_list_item one. By doing that you can change the TextView properties such as the text size. However if you haven't worked with lists with your own layouts, they can appear a bit complex to use the first time. I'll try to come back and edit this answer to provide more information later, but I suggest you look for tutorials on custom list views - that will show you how to inflate your own layout files.
Create another xml file for the Text that should be in the list rows..
and just add android:layout_height="?android:attr/listPreferredItemHeight" in your TextView :D (put it in a relative layout rather than a linear layout)
and then use your previous ListView xml for the setContentView(R.layout.listview)
and the other xml file in your ArrayAdapter code
ArrayAdapter adapter = new ArrayAdapter(this,
R.layout.yoursecondLayoutforthelistswithText, R.id.titles, HistoryList)
the R.id.titles is the id for the TextView
you can save font size in sharedprefs and then recreate your adapter
SharedPreferences.Editor ed = mSharedPreferences.edit();
ed.putFloat("fontTwosize", sizeTwo);
ed.commit();
adapterz = new LazyziyaratAdapter(MainActivity.this,
ziyaratList);
listz.setAdapter(adapterz);
and in your getview method of adapter
holder.text.setTextSize(TypedValue.COMPLEX_UNIT_SP,G.mSharedPreferences.getFloat("fontTwosize", 25));