I have a Spinner, but the prompt message is ignored. I can't find why.
My layout :
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:prompt="#string/age"
android:drawSelectorOnTop="true"
android:background="#drawable/spinner_bg" />
My code :
spinner = (Spinner) findViewById(R.id.spinner);
SpinnerAdapter adapter = new SpinnerAdapter(this, R.layout.spinner_item, getApplicationContext().getResources().getStringArray(R.array.spinnerArray));
spinner.setAdapter(adapter);
adapter.setDropDownViewResource(R.layout.spinner_item);
The SpinnerAdapter :
public class SpinnerAdapter extends ArrayAdapter<String>{
public SpinnerAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Typeface externalFont=Typeface.createFromAsset(getContext().getAssets(), "fonts/PermanentMarker.ttf");
((TextView) v).setTypeface(externalFont);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v =super.getDropDownView(position, convertView, parent);
Typeface externalFont=Typeface.createFromAsset(getContext().getAssets(), "fonts/PermanentMarker.ttf");
((TextView) v).setTypeface(externalFont);
return v;
}
}
And the layout of the spinner_item :
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="38dp"
android:ellipsize="marquee"
android:gravity="center_vertical|left"
android:minHeight="38dp"
android:paddingLeft="5dp"
android:singleLine="true"
android:textSize="18sp" />
Do you have any idea ?
I think you are misunderstanding what android:prompt does... it doesn't set the text in the closed spinner, it sets the header/title of the open spinner.
Closed spinner: Spinner Item 1
Open Spinner: Android Spinner Prompt
----------------------
Spinner Item 1
Spinner Item 2
There are a couple of ways to put the prompt in the closed spinner display. One way is to insert dummy data that will load into your spinner in the first position and then have your listener ignore that if it's selected (I don't like this as it puts junk in your data store).
Another option is to create a custom spinner adapter to insert the prompt as the first entry of the spinner (I like this one as it keeps the prompt in the code and keeps your data what it should be... data).
Hope this helps! Good luck!
Related
I'm trying to build a ListView with a custom layout but so far have not been able to successfully do it. I'm a beginner and have been following tutorials to get it done.
This is what I'm doing:
//this is where I want the custom list items to be displayed.
//GroupAdapter is the constructor of the class in which I'm trying to make a custom adapter
stringArray = new String[10];
groupItemArrayAdapter = new GroupAdapter(this,stringArray);
groupListView = (ListView) findViewById(R.id.mainList);
groupListView.setAdapter(groupItemArrayAdapter);
//this is the code in GroupAdapter.java class
public class GroupAdapter extends ArrayAdapter{
private LayoutInflater inflater;
public GroupAdapter(Activity activity, String[] items){
super(activity,R.layout.group_view);
inflater=activity.getWindow().getLayoutInflater();
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
return inflater.inflate(R.layout.group_view,parent,false);
}
}
When i start the app, a blank screen appears. Initially when i used Android default layout for the list, list items were displayed but not anymore... I don't understand what I'm doing wrong. Can anyone help?
Here is the group_view.xml file
<?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"
android:padding="30dp" >
<TextView
android:id="#+id/gName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Group Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginRight="100dp"
android:textStyle="bold" />
<TextView
android:id="#+id/activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/signIn"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</LinearLayout>
You can replace all your code by next
stringArray = new String[10];
groupItemArrayAdapter = new GroupAdapter(this,stringArray);
groupListView = (ListView) findViewById(R.id.mainList);
groupListView.setAdapter(new ArrayAdapter<String>(this, R.layout.group_view, R.id.gName, stringArray));
it's for case when you want to show strings at gName field, if you want to shot them at activity, then you need to use
groupListView.setAdapter(new ArrayAdapter<String>(this, R.layout.group_view, R.id.activity, stringArray));
Answer for "Why it's blank?"
#Override
public View getView(int position, View convertView, ViewGroup parent){
return inflater.inflate(R.layout.group_view,parent,false);
}
You only inflate empty view and does not assign strings to fields. It would be (simplest version)
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.group_view, parent, false);
TextView textView = (TextView)convertView.findViewById(R.id.gName);
String item = getItem(position)
textView.setText(item);
return convertView;
}
You'll be able to implement better version after read article Making ListView Scrolling Smooth
I have a Spinner in my LinearLayour.
I am trying to center text in spinner:
This is my XML for that element:
<Spinner
android:id="#+id/project_Spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:padding="1dip"
android:prompt="#string/spinner_title"
/>
the declaration of the spinner in the Activity:
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, NomProjets);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
Thanks,
The component that actually creates the items for the Spinner is the adapter. So you should customize it (by overriding the getView() method) to return centered TextView widgets.
In your case, replace the new ArrayAdapter<String> ... initialization with this code:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, NomProjets)
{
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
return setCentered(super.getView(position, convertView, parent));
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
return setCentered(super.getDropDownView(position, convertView, parent));
}
private View setCentered(View view)
{
TextView textView = (TextView)view.findViewById(android.R.id.text1);
textView.setGravity(Gravity.CENTER);
return view;
}
};
Matiash's answer works. Here is another way:
Actually the text in the spinner is wrapped in a TextView , so we can override the android.R.layout.simple_spinner_item.xml(which is located at sdk\platforms\android-xx\data\res\layout) , create an new xml in the /layout folder, adding an attribute:
android:gravity="center" to make the text in the TextView center.
such like this:
am using spinner in android with the layout (android.R.layout.simple_spinner_dropdown_item)
because i need the radio button there , but after i select a value the radio button still showing on the spinner. is their any way to hide it?
this is my spinner:
<Spinner
android:id="#+id/Spinner01"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_gravity="left"
android:background="#drawable/spin" >
</Spinner>
and this is the activity lines:
ArrayAdapter first_adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, start_list);
spinnermo.setAdapter(first_adapter);
and this is the spinner after selection:
ooo i cant dispaly the image but its simmiler to this:
seleced text (0)
Using this Code you can hide your spinner.
spinnermo.setVisibility(View.GONE);
if you want to hide radio button use same code
radioButton.setVisibility(View.GONE);
Yes create two layouts because once you implement ArrayAdapter it will have two methods to be overridded getView and getDropDownView
for getDropDownView you have to create a layout with TextView and set corresponding value in it
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/your_own_unique_id_for_checkedtextview"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"/>
for getView you have to create a layout with CheckedTextView and call the corresponding method
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/your_own_unique_id_for_textview"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
Then in your custom adapter class
override the methods to access the above values respectively
#Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomCheckedTextView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomTextView(position, convertView, parent);
}
// This funtion called for each row ( Called data.size() times )
public View getCustomCheckedTextView(int position, View convertView, ViewGroup parent) {
/********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
View row = inflater.inflate(R.layout.your_own_layoutxml_file_name, parent, false);
/***** Get each Model object from Arraylist ********/
CheckedTextView label = (CheckedTextView)row.findViewById(R.id.your_own_id_for_checkedtextview);
// Set values for spinner each row
label.setText("value");
return row;
}
// This funtion called for each row ( Called data.size() times )
public View getCustomTextView(int position, View convertView, ViewGroup parent) {
/********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
View row = inflater.inflate(R.layout.your_own_layout_xml_file_name_for_textview, parent, false);
TextView label = (TextView)row.findViewById(R.id.your_text_view_id_name);
// Set values for spinner each row
label.setText("value");
return row;
}
I have been unable to set the color on the spinner widget. How is this styled?
Try using this adapter for your spinner:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(Home.Home_Group, R.layout.my_spinner_style, yourstringarray)
{
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(16);
((TextView) v).setTextColor(
getResources().getColorStateList(R.color.white)
);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
v.setBackgroundResource(R.drawable.spinner_bg);
((TextView) v).setTextColor(
getResources().getColorStateList(R.color.spinner_text)
);
((TextView) v).setTypeface(fontStyle);
((TextView) v).setGravity(Gravity.CENTER);
return v;
}
};
Add this xml to your layout,
my_spinner_style.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
And at last,
spinner.setAdapter(adapter);
Simple and crisp ....
private OnItemSelectedListener your_spinner _name= new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
The simplest form is:
m_spnDia = (Spinner)findViewById(R.id.spiDia);
TextView oTextView = (TextView)m_spnDia.getChildAt(0);
oTextView.setTextColor(Color.RED);
If you want to change the text color :
Spinner Widgets Text Color
Else, making your own layout is the best way like JoxTraex said.
A shorter alternative to Andro'd answer is to let the ArrayAdapter create the item views for you from a layout resource:
final List<String> values = [SPINNER VALUES];
final ArrayAdapter<String> adapter = new ArrayAdapter<>(
activity, R.layout.my_spinner_item, values);
adapter.setDropDownViewResource(R.layout.my_spinner_dropdown_item);
spinner.setAdapter(adapter);
Then style your text to suit your needs in my_spinner_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="#style/my_spinner_item_style"
/>
Note: my_spinner_dropdown_item is used when the list of choices appear
For more information read the Spinners documentation.
Try understanding that you are using the dropdown list as provided by the defaults that are available in the SDK.
SIMPLY make your own layout with a custom adapter.
I have code like:
SpecialAdapter adapter = new SpecialAdapter(
this,
list,
R.layout.list_display_item,
from,
to );
myTextListView.setAdapter(adapter);
.
.
.
.
public class SpecialAdapter extends SimpleAdapter
{
public SpecialAdapter(Context context, List<HashMap<String,
String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
}
#Override
public View getView(
int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
}
}
and list_display_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myDisplayItemLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/arabicTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right|center_vertical"
android:lineSpacingMultiplier="3"
android:text="Arabic"
android:textSize="30dp"/>
<TextView
android:id="#+id/translationTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="English"
android:gravity="left|center_vertical"
android:textSize="30dp"/>
</LinearLayout>
I just want to set my own arabic font to android:id="#+id/arabicTextView" and english font to android:id="#+id/translationTextView".
In getView() function of SpecialAdapter class i get View view = super.getView(position, convertView, parent) and view.getId() is actually the id of linearlayout android:id="#+id/myDisplayItemLinearLayout"
I just want to get inner textviews of this linearLayout to set seperate fonts to each using typeface here... how can i get these textViews there??
Is it correct way to set my fonts here or it should be done in someother way???? please help me..
Use the findViewById method of View and the setTypeface method of TextView
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
//assuming you've put your arabic font file in your assets directory
((TextView)view.findViewById(R.id.arabicTextView)).setTypeface(Typeface.createFromAsset(view.getContext().getAssets(), "yourarabicfont.otf"));
//assuming you've put some custom english font in your assets directory
((TextView)view.findViewById(R.id.translationTextView)).setTypeface(Typeface.createFromAsset(view.getContext().getAssets(), "yourenglishfont.otf"));
return view;
}