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
Related
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 have used 2 spinners in my android activity,one is for country and other is for states,country spinner displays data as dropDown List(down side),But the state spinner gives me data enlisted to upside as shown in image.I want to display it same as country spinner(down side),Please help me.my spinner code is as below:
code
<Spinner
android:id="#+id/spr_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_cntry_id"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:spinnerMode="dropdown" />
<Spinner
android:id="#+id/spr_states"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/spr_country"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:spinnerMode="dropdown" />
Spinner does not have Sufficient space to display spinner popup to downside. Android system will automatically popup it to upside.
The answer may be staring right at me but I just can't see it!
Am getting a null pointer exception when setting the text on a Textview with a string from a ArrayList of Strings. Have initialised everything and checked the ArrayLiist has data, everything seems to check out. But when the textview is been declared in onCreate() I can see in the debugger the value remains null.
OnCreate():
private TextView title;
//onCreate()
setContentView(R.layout.gallery);
imageNames = new ArrayList<String>();
title = (TextView) this.findViewById(R.id.tv_imageDescriptionGallery);
setImageAndTitleArrays();
//check size of arrays and build correctly
Log.d("GALLERY VIEW:" , "IMaegNaem array Size: "+imageNames.size() + " Image Array Size: "+ bitmapArray.size());
Log.d("GALLERYVIEW: ", "First Image Name of Array: "+ imageNames.get(0));
//set default image title and description, we will update every tome a image clicked
//exception throne here as title=null on debugger
title.setText(imageNames.get(0));
The textview :
<TextView
android:id="#+id/tv_imageDescriptionGallery"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Image Name"
android:textColor="#55bbfa"
/>....
Gallery XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Astro Gallery"
android:textColor="#55bbfa"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Gallery
android:id="#+id/gallery1"
android:layout_marginTop="100dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/splitter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/sample_note"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="#drawable/splitter" />
<ImageView
android:id="#+id/imageView1"
android:layout_marginTop="100dp"
android:layout_width="420dp"
android:layout_gravity="center_horizontal"
android:layout_height="350dp"
android:src="#drawable/camera1" />
<TextView
android:id="#+id/tv_imageDescriptionGallery"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Image Name"
android:textColor="#55bbfa"
/>
<Button
android:id="#+id/send"
android:layout_marginTop="10dp"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Set Wallpaper"
android:background="#drawable/button_bg"
android:textColor="#fff" />
<ImageView
android:id="#+id/shadow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="#drawable/shadow" />
</LinearLayout>
</ScrollView>
Only 2 hypothesis :
Maybe you tried to import R.java and you imported the wrong one ?
Or try to remove "this" in front of findViewById?
The 2 only ideas that come to mind, hope it can help.
Although I checked all the imports, removed all the ref to 'this, rename the class and all refs to it, cleaned and build, could not resolve the NPE.
I deleted the class and xml from the project, then made a new class and xml file with same code and worked fine.
I had a class with exact same name in a separate project, not sure if this has anything to do with it.
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();
}