I am trying to customize the default appearance of a Spinner item.
I have defined a layout in a file by the name of :
<?xml version="1.0" encoding="utf-8"?>
<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="#ff0000"
android:textSize="20sp" />
And here when initializing the spinner I am trying to assign this layout to it:
ArrayAdapter<String> nearByLocationsAdpt = new ArrayAdapter<String>(this, android.R.layout.spinner_item_layout,nearByLocationsArray);
But it keeps saying that spinner_item_layout cannot be resolved or
is not a field however it is present in the layout folder?
Another related question is it possible to view and edit the default spinner layout
xml file i.e. android.R.layout.simple_spinner_item
The problem is this:
android.R.layout.spinner_item_layout
You are using the default layout. You have to supply the one you created instead. Depending on the how you named your layout that would be something like this:
R.layout.my_spinner_item
Related
I want custom spinner in android like below description
- Place Holder Text (Spinner first item - selected item will be centered )
- Drop down list item will be left-aligned
I try to use custom layout adapter for Spinner but just only align for both spinner and drop down list.
I hope somebody can help me with it ! I'm newbie with Android , sorry about that!
This is an example that I've used in an app recently.
Create 2 files in your res/layout folder, one called spinner_list.xml and one called spinner_list_dropdown.xml.
spinner_list.xml
<?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:textSize="#dimen/text_size"
android:padding="10dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:textColor="#color/text_view_text"/>
spinner_list_dropdown.xml
<?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:textSize="#dimen/text_size"
android:padding="10dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:textColor="#color/text_view_text"
android:background="#color/spinner_background"/>
Java code for using the custom spinner:
ArrayAdapter<String> adapter = new ArrayAdapter<>(YourActivity.this, R.layout.spinner_list, strYourArray);
adapter.setDropDownViewResource(R.layout.spinner_list_dropdown);
spinnerCustomList.setAdapter(adapter);
Hope this all makes sense to you, if you need any help with it then please don't hesitate to ask.
I have a spinner which is created programmatically like this:
spinnerLogger = new Spinner(context);
spinnerLogger.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
SpinnerAdapter spinnerAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_dropdown_item, spinnerNames);
spinnerLogger.setAdapter(spinnerAdapter);
in my code I also use xml created spinners which use a style to make sure the user understands they are spinners:
<Spinner
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="#+id/spinnerHour"
android:spinnerMode="dropdown"
style="#style/Widget.AppCompat.Spinner.Underlined" />
I want to apply the Widget.AppCompat.Spinner.Underlined style to the programmatically created spinner but can't seem to find how I can achieve this.
I tried by creating an xml containing only a spinner with the right style (xml's name is spinner_underlined and it's located in the layout folder):
<?xml version="1.0" encoding="utf-8"?>
<Spinner android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/Widget.AppCompat.Spinner.Underlined"/>
when i try to apply this using
SpinnerAdapter spinnerAdapter = new ArrayAdapter<>(context, android.R.layout.spinner_underlined, spinnerNames);
it gives me a cannot resolve symbol 'spinner_underlined' error
You need change to R.layout.... not android.R.layout....
I finished the English version of my application and now I am working on the Arabic localization. Arabic is a right-to-left language, so I need to adjust a lot of things in my layout, including my spinner display.
When I choose an item from the spinner selection menu, I see the following,
As you can see "Allergy" is aligned to the left and on top of the spinner arrow. I need it to be aligned to the right.
NOTE The Arabic webservices aren't finished yet, that's why the data in the image is still in English.
EDIT
Spinner mSpecialtySpinner = (Spinner) findViewById(R.id.specialty_appointment_spinner);
ArrayAdapter<Specialty> adapter = new ArrayAdapter<Specialty>(AppointmentReservationActivity.this,android.R.layout.simple_spinner_item, specialities);
adapter.setDropDownViewResource(R.layout.spinner_item);
mSpecialtySpinner.setAdapter(adapter);
Well you can have another layout for Spinner and pass it to ArrayAdapter's constructor.
make an xml file naming spinner_layout.xml in layout like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="fill_parent"
android:textSize="20sp"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:gravity="right"
/>
and now pass it in ArrayAdapter's constructor like this:
new ArrayAdapter<String>(this,R.layout.spinner_layout,conversionsadd);
Please remember:
what we have provided as R.layout.spinner_layout in ArrayAdapter constructor is layout of Spinner. and what we will provide as setDropdownView() is Spinner item (drop down item).
You can have a look at my another relative answer here
All you need to do is create a new spinner dropdown layout and set android:gravity="right" on the appropriate widget.
Call it with:
yourSpinner.setDropDownViewResource(R.layout.right_aligned_spinner_dropdown_item);
I'm using the ListView to provide a list for the user to choose from.
Here is the main code:
SimpleAdapter adapter = new SimpleAdapter(this,contacts, R.layout.list_contact,
from_contacts, to_contacts);
listview_selected_contact.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listview_selected_contact.setAdapter(adapter);
I wonder why the checkbox doesn't show?
The program run properly just without the visible checkbox.
Can anyone help?
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textAppearance="?android:attr/textAppearanceMedium" android:id="#+id/username"></TextView>
</LinearLayout>
It looks like that your R.layout.list_contact isn't CheckedTextView.
See source of android.R.layout.simple_list_item_multiple_choice
I can recommend you to build custom view which is child of any ViewGroup classes and implementator of interface Checkable.
id of checkBox and listView in R.layout.list_contact should be specific .
so follow ApiDemoes multiple List example for both layout and java code .
I want to create a custom view (extends View) which contains an image and a text.
Is it possible to set an listener just for image if i use canvas.drawBitmap(imageBitmap, ...) method?
Thank you!
If you want your view just to show an image and a text, why not create a specific layout in your XML file and use setContentView to display the view?
If thats what you were looking for, I think that is the easiest approach. XML example coming soon.
Here is the contents of my personal view to display my main application, just to demonstrate how I can switch view's, your view would be different including a TextView and an ImageView.
dblist.xml inside of res/layout folder.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"/>
</LinearLayout>
This contents display my database results onto a Spinner
I can call that view by using:
setContentView(R.layout.dblist);
And if I want to return to my main layout, I can switch the view by calling
setContentView(R.layout.main);
EDIT:
You can manipulate the view you are working with programmatically by using findViewById method. Let's use my dblist.xml file as an example, notice that my ListView has an id of list. So I would call:
ListView myList = (ListView) findViewById(R.id.list);
With corresponds to that layout, so whenever I want to change anything to my XML file, I would reference myList use methods and callbacks to meet my needs.
I hope this helps.