I am a beginner in android. I have a question why the spinner is not showing in design window?. For your help I have added a code below
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View!"
android:paddingRight="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/brand"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:layout_below="#+id/color"
android:layout_alignLeft="#+id/color"
android:id="#+id/find_beer"
/>
<Spinner
android:id="#+id/color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
>
</Spinner>
enter image description here
The Constraints you are using are incorrect. Change your layout as follows:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Spinner
android:id="#+id/color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></Spinner>
<Button
android:id="#+id/find_beer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
app:layout_constraintBottom_toBottomOf="#+id/color"
app:layout_constraintStart_toEndOf="#+id/color"
app:layout_constraintTop_toBottomOf="#+id/color" />
<TextView
android:id="#+id/brand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="57dp"
android:layout_marginRight="57dp"
android:paddingRight="100dp"
android:text="Text View!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Setting following attribute:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Means following:
The view should be only big enough to enclose its content
Even if you correct your constraints, remember to add some content to the Spinner otherwise you will just see drop down arrow on the screen.
change the layout type like a linearlayout with orientation vertical or relativelayout set layout after you set value with using java code or xml using resources
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
java code
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,
R.array.planets_array, 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);
using layout xml into spinner tag
android:entries="#array/planets_array"
XML code.
<Spinner
android:id="#+id/spCountry"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="25dp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="#android:color/transparent"
android:gravity="center"
android:spinnerMode="dropdown" />
Code for activity
Spinner spCountry;
spRole = (Spinner)getView().findViewById(R.id.spCountry);
String[] arRoleSpinner = {"India","USA","NWZ","SA", "WI","ENG"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, arRoleSpinner);
adapter.setDropDownViewResource(R.layout.spinner_item);
spRole.setAdapter(adapter);
spRole.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
((TextView) adapterView.getChildAt(0)).setTextColor(Color.WHITE);
((TextView) adapterView.getChildAt(0)).setTextSize(12);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
use this code
<Spinner
android:id="#+id/color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp" ></Spinner>
because in spinner (drop down list) we add items at run time (in java code) so it show when we run app. and also every change in java code like color change , Visibility etc show when we run the app.
and also visit this https://www.javatpoint.com/android-spinner-example
Related
I put two spinners in the same activity, one for City and the other for Town. When the user chooses a City, the Town spinner should populate with items according to the City that was chosen.
My problem is, the color of the background and the text is always different from the first one, however they have the same style and attributes. I couldn't find any logical solution and I didn't find any suggestion in web.
Do you have any idea about the reason or the solution?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:onClick="pickDate"
android:text="Select date" />
<TextView
android:id="#+id/viewDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Distribution date"
android:textAlignment="center" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="The governorate"
android:textAlignment="center" />
<Spinner
android:id="#+id/static_spinner"
style="#style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The district"
android:textAlignment="center" />
<Spinner
android:id="#+id/district_spinner"
style="#style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView15"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Place ID" />
<EditText
android:id="#+id/plcID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:onClick="next"
android:text="Next" />
</LinearLayout>
</LinearLayout>
MainInfoActivity file
public class MainInfoActivity extends Activity {
TextView textView, plcID;
Spinner staticSpinner;
Spinner dynamicSpinner;
Spinner districtSpinner;
CharSequence[] arrayDistrict;
ArrayAdapter<CharSequence> districtAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_info_activity);
textView = (TextView) findViewById(R.id.viewDate);
plcID = (TextView) findViewById(R.id.plcID);
//Drop down lists
staticSpinner = (Spinner) findViewById(R.id.static_spinner);
// Create an ArrayAdapter using the string array and a default spinner
final ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter.createFromResource(
this, R.array.governorate_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
districtSpinner = (Spinner) findViewById(R.id.district_spinner) ;
districtAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.Anbar, android.R.layout.simple_spinner_item);
districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
staticSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view1, int i, long l) {
districtAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.Anbar, android.R.layout.simple_spinner_item);
districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
districtSpinner.setAdapter(districtAdapter);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
I recreated your code and it works fine for me.
The only difference I can find meaningful are in the lines below:
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.governorate_array,
android.R.layout.simple_spinner_item);
and
districtAdapter = ArrayAdapter
.createFromResource(getApplicationContext(),
R.array.Anbar,
android.R.layout.simple_spinner_item);
instead of using getApplicationContext() can you try to use this as you did in the staticAdapter?
I once faced something similar where my application had a theme different from my activity and the widgets looked slightly different as well.
UPDATE
I would like to add some more info about the reason why I suggested the change above.
Using the right Context is linked to another one of those behaviors. While the framework will not complain and will return a perfectly good view hierarchy from a LayoutInflater created with the application context, the themes and styles from your app will not be considered in the process. This is because Activity is the only Context on which the themes defined in your manifest are actually attached. Any other instance will use the system default theme to inflate your views, leading to a display output you probably didn’t expect.
Quote from this link: https://possiblemobile.com/2013/06/context/
By default, drop-down views are inflated against the theme of the
{#link Context} passed to the adapter's constructor. More.......
ArrayAdapter source code: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/ArrayAdapter.java#461
I was going to ask for more code, but then I found this similar solution in another question that can be useful for you:
Spinner Theme is Dark
which basically consist on create your own theme for the upper spinner and bottom spinner. Check if this is close or related to your question. If don`t upload the activity where the spinners are added, for recreate the situation.
I tried to make Spinner with white text as my theme but when Spinner popup show then it has default white background so my text isn't visible there. I also tried to set with both android:popupBackground and app:popupBackground but it doesn't reflect there.
Screenshots of Spinner :
Spinner 1
Spinner Popup 2
XML for spinner
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/spinLocation"
style="#style/Base.Widget.AppCompat.Spinner.Underlined"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:popupBackground=""
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
app:backgroundTint="#color/white" />
XML for Spinner Item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txtValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:maxLines="1"
android:paddingBottom="7dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="7dp"
android:text=""
android:textColor="#color/white"
android:textSize="#dimen/_12sdp" />
I know it is because of item textColor but if I do not define it then it showing in popup but not proper in Spinner. So suggest me a best approach for this and also suggest for header if appCompatSpinner supports it. Thanks.
You need to set themes, or create a custom one.
Here is a sample code:
<android.support.v7.widget.AppCompatSpinner
style="#style/Base.Widget.AppCompat.Spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/array_radius"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Light" />
Possibly you can add your own Textview for spinner
<Spinner
android:id="#+id/carMake"
android:textColor="#606060"
android:textSize="12sp"
android:layout_gravity="end"
android:layout_alignParentEnd="true"
android:layout_below="#+id/vin_border"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="14dp">
</Spinner>
java for adding items
List<String> model = new ArrayList<>();
model.add("Ford");
model.add("BMW");
model.add("Audi");
model.add("Hyundai");
model.add("Suzuki");
model.add("Porsche");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(getContext(), R.layout.spinner_textview_layout, model);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
carMake.setAdapter(dataAdapter);
textview for spinner item
<com.tagx.view.CustomTextView
app:font="#string/monte_light"
android:layout_width="wrap_content"
android:maxLines="1"
style="?android:attr/spinnerItemStyle"
android:ellipsize="marquee"
android:paddingStart="=20dp"
android:paddingEnd="40dp"
android:textColor="#color/fragment_owner_title_color"
android:layout_gravity="end"
android:textSize="12sp"
android:layout_height="wrap_content"
android:gravity="center"
/>
I would like to set text for the spinners in the layout reused. However, only the first spinner is set. How to set text for all spinners with same id?
Also, I would like to ask how add another skillfield.xml to fragment_skill.xml when clicking the imageview?
Thank you.
fragment_skill.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/skillInfo" >
<RelativeLayout
android:id="#+id/OCC"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/occ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="#string/occSkill"
android:textSize="20sp" />
<TextView
android:id="#+id/occPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toEndOf="#id/occ"
android:text=""
android:textSize="20sp" />
<include
android:id="#+id/oocSkill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/occ"
layout="#layout/skillfield" />
<ImageView
android:id="#+id/addOccSkill"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="#id/oocSkill"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/ORI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/OCC" >
<TextView
android:id="#+id/ori"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="#string/oriSkill"
android:textSize="20sp" />
<TextView
android:id="#+id/oriPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toEndOf="#id/ori"
android:text=""
android:textSize="20sp" />
<include
android:id="#+id/oriSkill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ori"
layout="#layout/skillfield" />
<ImageView
android:id="#+id/addOriSkill"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="#id/oriSkill"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
</RelativeLayout>
skillfield.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/addSkillField" >
<Spinner
android:id="#+id/selectSkill"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp" />
<TextView
android:id="#+id/skillPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text=""
android:textSize="20sp" />
<ImageView
android:id="#+id/addSkill"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_delete" />
</LinearLayout>
In MainActivity.class
skill = (Spinner) findViewById(R.id.selectSkill);
ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_down);
skill.setAdapter(skillAdapter);
how to setup Spinner with the same ID
It is true that if you do this:
skill = (Spinner) findViewById(R.id.selectSkill);
You will be only able to get the first Spinner. I don't think you can add new Spinner dynamically by using <include>.
Here's the main idea of how to achieve what you need:
In fragment_skill.xml, you need to add an empty container view. This container should be inserted in the place you want to add your Spinner every time the button is clicked. This container will hold your Spinners that are added.
Let's say you want to insert here:
<RelativeLayout...>
<TextView.../>
<TextView.../>
<!-- here is where you want to insert your Spinner -->
<ImageView.../>
</RelativeLayout...>
So you add a LinearLayout at that spot, like this:
<RelativeLayout...>
<TextView.../>
<TextView.../>
<LinearLayout
android:id="#+id/container"
android:orientation="vertical
... />
<ImageView.../>
</RelativeLayout...>
You can add any other type of layout, depending what you need, it doesn't have to be LinearLayout. After that, you grab that LinearLayout from your activity/fragment like this:
LinearLayout mContainer; // make this instance variable
mContainer = (LinearLayout)findViewById(R.id.container);
Also grab the button so that you can set up a listener, let's assume it's called Button mButton:
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addNewSpinner();
});
So when the button is clicked, you will call addNewSpinner(). Now, make a method called addNewSpinner(). Inside, the method, we will be inflating skillfield.xml. The method should look something like this:
public void addNewSpinner(){
View view = getLayoutInflater().inflate(R.layout.skillfield, null);
Spinner skill = (Spinner) view.findViewById(R.id.selectSkill);
ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_down);
skill.setAdapter(skillAdapter);
mContainer.addView(skill);
}
If you are not sure what inflate means, here is the explanation:
Instantiates a layout XML file into its corresponding View objects.
Hope it helps!
I'm setting a list adapter as follows, and if i just change simple_list_item_activated_1 for lv_layout it doesn't work at all.
any ideas?.
Thanks!
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(
getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
DummyContent.ITEMS));
}
How can I attach my custom layout called lv_layout to that adapter?
this is my layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/lv_layout"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" />
<TextView
android:id="#+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" />
<TextView
android:id="#+id/resolution"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
Why you want to use setlistadapter then? You have to create your custom adapter too in that case because serlistapater will only update the textview part of your custom layout and rest part it will ignore which idont think what you want. So
1.make a customadapter
2.override its getview
3.the set the adapter to your custom layout.
Cheers
android.R.* are Android's internal resources.
To use your own layouts from res/layout/ skip the android prefix:
R.layout.lv_layout
Same goes for ids (if you're wondering).
I have a custom layout item for my listview. There is a spinner in the layout item which needs to be populated with values, usually by android:entries
The problem I have with this method is that the end user can't modify the values, which is something I would like to include. As the layout item and subsequently the spinner, is repeated multiple times on the same listview, I imagine there must be a way to populate it once, programmatically. I just can't figure it out.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customListItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exerciselbl" />
<Spinner
android:id="#+id/Exercise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/workout_items" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/repslbl" />
<Spinner
android:id="#+id/Reps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/reps_count" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/weightlbl" />
<EditText
android:id="#+id/Weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</LinearLayout>
</LinearLayout>
So yeah, I want to avoid using android:entries="#array/workout_items" as this means manually typing out every single item for the spinner in an XML resources file AND I can't dynamically add items while the program is running.
Here is a simple example of how it would be done, obviously the generics can change and there are other ways of loading the spinner.
public void populateSpinner () {
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
List<CharSequence> list = new ArrayList<CharSequence>();
for (int i = 0; i < 10; i++) {
list.add("Item " + i);
}
adapter.addAll(list);
adapter.notifyDataSetChanged();
}
One of the more important lines is adapter.notifyDataSetChanged(), this is because it tells the view that it needs to refresh the data associated with this spinner.
For more information, see the ArrayAdapter and Spinner classes.