Change AppCompatSpinner popup background color - android

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"
/>

Related

Remove underline from TextView which uses Widget.EditText style

I have a TextView for showing user's wallet balance in my app. In order to set error indicator on low balance I added Widget.EditText style to the TextView. My issues is after adding this style, the TextView is showing an under line. How can I remove that underline?
My TextView
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/hm_bal"
android:textColor="#color/primary_dark"
android:textSize="14sp"
android:textStyle="bold"
android:layout_gravity="left"
android:padding="3dp"
android:gravity="top"
android:text="----"
style="#android:style/Widget.EditText" />
I don't want to change the style since using this theme was perfect for my requirement except the underline.
Try to set transparent background in your textview
android:background="#android:color/transparent"
LIKE
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/hm_bal"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp"
android:textStyle="bold"
android:layout_gravity="left"
android:padding="3dp"
android:gravity="top"
android:text="----"
android:background="#android:color/transparent"
style="#android:style/Widget.EditText" />
set background null
android:background="#null"

Android: Custom Spinner xml

I'm New to Android and want to customize the spinner in an app I'm making.
This is what I want my spinners to look like http://prntscr.com/bk4pbt. I tried setting the background of the spinner to an image of the shape but it not looking how i want it to be.
<TextView
android:id="#+id/textViewStore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Store:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/text" />
<Spinner
android:id="#+id/store_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="#string/store_prompt" />

How to use spinner in robotium?

Register.xml file,
<RelativeLayout
android:id="#+id/spinner_relay"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#id/fd_register_header"
android:layout_marginBottom="#dimen/dm_5dp"
android:layout_marginLeft="#dimen/dm_10dp"
android:layout_marginRight="#dimen/dm_10dp"
android:layout_marginTop="#dimen/dm_5dp"
android:background="#drawable/spinner_background" >
<TextView
android:id="#+id/fp_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:drawablePadding="#dimen/dm_10dp"
android:drawableRight="#drawable/icondown"
android:gravity="left|center"
android:padding="#dimen/dm_10dp"
android:text="Select city"
android:textColor="#color/white"
android:textSize="#dimen/dm_18sp" />
</RelativeLayout>
if i used this code,
int pos = solo.getCurrentSpinners().get(0).getSelectedItemPosition();
getting error,
cannot resolve symbol getCurrentSpinner
Try using the following codes for working with Spinners and this is what worked for me
solo.pressSpinnerItem(0, -5); //selects the item in the spinner
Or
solo.isSpinnerTextSelected(0,"Items") //checks whether the spinner item is selected

android - set text for spinner in <include />

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!

Changing Android Spinner Layout/Design

I am trying to modify the design of Spinner widget. I can change the background, but I can't find a way to change the arrow icon on the right side. Is there a way to do it?
Thanks!
The whole thing is a single 9 patch png image. I've modified the entire look of spinners before by replacing the images. See this page: http://androiddrawableexplorer.appspot.com/
Specifically look at btn_dropdown_normal.9, btn_dropdown_pressed.9, btn_dropdown_selected.9 and btn_dropdown_disabled.9
You just need to provide your own versions of those images.
Also, you can place your "spinner bar" layout in a FrameLayout, together with the real spinner but set to invisible:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="32dip"
>
<Spinner
android:id="#+id/theSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="32dip"
android:background="#drawable/my_background"
android:padding="6dip"
android:clickable="true"
android:onClick="spinnerBarReplacementClicked"
>
<ImageView
android:id="#+id/replacementSelectImg"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/my_drawable"
/>
<TextView
android:id="#+id/replacementSelectText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="4dip"
android:layout_toLeftOf="#id/replacementSelectImg"
android:textColor="#000000"
android:textSize="14sp"
android:ellipsize="marquee"
android:singleLine="true"
/>
</RelativeLayout>
</FrameLayout>
and pass the clicks from your layout to the real spinner
private Spinner mSpinner;
mSpinner = (Spinner) findViewById(R.id.theSpinner);
public void spinnerBarReplacementClicked(View pV){
mSpinner.performClick();
}

Categories

Resources