How do I center text in a spinner in Android - android

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:

Related

android:prompt is ignored

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!

hide radio button from the spinner selected value

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;
}

how do you set the spinner text color?

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.

Change text color for ListView items

How do I change the text color for the items that are added to a ListView. I need to change the colors programmatically in code based on certain conditions and changing different rows to different text colors(e.g. row 0 = red, row1= white, row3= blue etc). Setting a text color in the xml layout will not meet my requirements. Here is my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
setListAdapter(new ArrayAdapter<String>(ListViewEx.this,
R.layout.list_item_1, Global.availableDecks));
//something like this
//listview.getPosition(0).setTextColor(red);
//listview.getPosition(1).setTextColor(white);
//listview.getPosition(2).setTextColor(blue);
and my xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="match_parent"
android:layout_height="35dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30px"
android:layout_marginLeft="5px"
android:singleLine="true"
/>
Implement your own ArrayAdapter and override the getView() method:
public class Adapter1 extends ArrayAdapter<String> {
public Adapter1(Context context, int resID, ArrayList<String> items) {
super(context, resID, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if (position == 1) {
((TextView) v).setTextColor(Color.GREEN);
}
return v;
}
}
Don't forget to provide an alternative else clause to set the color to the default so you don't have problems when you're dealing with a recycled row.
Then in your activity:
setListAdapter(new Adapter1(ListViewEx.this,
R.layout.list_item_1, Global.availableDecks));
use android:textColor="hex code" parameter inside the TextView tag
You can change through xml and java code(runtime) also....
in xml widget you need to define ::
android:textColor="Hex code"
Like ::
android:textColor="#000000"
at runtime you need to define ::
TextView tv = (TextView) aView.findViewById(R.id.txvx);
tv.setTextColor(Color.RED);

Editing a TextView from ListAdapter?

I have a ListView, which is backed by an ArrayAdapter. They are defined as such:
ListView listView = (ListView)findViewById(R.id.listview);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.list_item);
listView.setAdapter(listAdapter);
list_item.xml contains the following:
<?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="match_parent"
android:padding="10dp"
android:textSize="16sp">
</TextView>
What I would like to do when adding an item to the list, is edit the TextView associated with the item, like changing it's colour for example. How would I go about doing this?
You could override the method getView() of your ArrayAdapter (link to the reference) for example:
ListView listView = (ListView)findViewById(R.id.listview);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.list_item){
public View getView (int position, View convertView, ViewGroup parent){
View v = super.getView(position,convertView,parent);
TextView tx = (TextView) v;
//do something with tx
return v;
}
};
listView.setAdapter(listAdapter);
You should go watch this Google I/O talk: http://www.google.com/events/io/2010/sessions/world-of-listview-android.html it explains the basics of ListView and how you would go about doing these sorts of things.

Categories

Resources