How to change the Spinner Text color - android

I am having a problem with the spinner item colour.
I am fetching the Spinner items from database.
I changed the spinner item colour.
But when I select a Spinner item, the colour will change to white again.
Here is my code:
public class PersonalInformation extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.info)
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(this,R.array.spinner_array,R.layout.spinner_text);
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
// TODO Auto-generated method stub
}
});
And Here is my Spinner xml file code:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#000000"
android:textColorHint="#000000"
android:textSize="20dp">
</TextView>

Make custom xml file for your spinner item.
spinner_item.xml:
give your customized color and size to text in this file.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:gravity="left"
android:textColor="#FF0000"
android:padding="5dip"
/>
Now use this file to show your spinner items like:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);
You don't need to set drop down resource.it will take spinner_item.xml only to show your items in spinner.
How to change spinner text size and text color?

Related

Creating dynamic Spinner in Android

I want to create Three spinner. In first Spinner, I have to display countries name and according to selection of country name, I have to load the state name of that country in second Spinner and according to selection of state, I have to load cities name of that state in Third Spinner. Can any one post any example and please suggest which technique is better defining spinner data in Java or Xml.
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, locations);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(dataAdapter);
Layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:text="#string/lblAcc" />
<!-- Spinner Dropdown -->
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:layout_marginTop="10dip"
android:entries="#array/acc_type" />
<!-- Select Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:text="#string/lblSubAcc" />
<!-- Spinner Dropdown -->
<Spinner
android:id="#+id/spinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
/>
Resource xml should be like following
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Spinner Example</string>
<string name="action_settings">Settings</string>
<string name="lblAcc">Select Account Type</string>
<string name="lblSubAcc">Select Account Head</string>
<string-array name="acc_type">
<item>Income</item>
<item>Expense</item>
</string-array>
</resources>
And the Java class to use
public class SpinnerEx4Activity extends Activity implements
OnItemSelectedListener{
Spinner s1,s2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_ex4);
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
s1.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String sp1= String.valueOf(s1.getSelectedItem());
Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
if(sp1.contentEquals("Income")) {
List<String> list = new ArrayList<String>();
list.add("Salary");//You should add items from db here (first spinner)
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
}
if(sp1.contentEquals("Expense")) {
List<String> list = new ArrayList<String>();
list.add("Conveyance");//you should add items from db here(2nd spinner)
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
With if else ladder you can add more spinner like 3rd spinner depending on 2nd one and so on.

Something is crashing my app after I implemented Spinner setOnItemSelectedListener

Just to clarify first, I'm a newbie with Java and Android, and I'm still in a heavy learning process.
Anyway...
I'm working on a simple Parking SMS app, and I'm stuck with Spinner and a TextView.
Basically, I'm trying to do this: when I select a Parking zone from a Spinner (it has five parking zones), I want to populate a Parking zone description to a TextView below Spinner in the Layout.
Spinner has its own StringArray, while Parking zone descriptions are each in its own Strings.
So it looks like this:
activity_glavni.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Glavni">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/txGrad"
android:id="#+id/txOdaberiteGrad"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnergradovi"
android:layout_below="#+id/txOdaberiteGrad"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp" />
<Space
android:layout_width="20px"
android:layout_height="20px"
android:id="#+id/space" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txZona"
android:text="#string/txZona"
android:layout_below="#+id/spinnergradovi"
android:layout_alignParentStart="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnerZona"
android:layout_below="#+id/txZona"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txPrikazZone"
android:layout_below="#+id/spinnerZona" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/editTextRegistracija"
android:layout_below="#+id/txPrikazZone"
android:layout_alignParentStart="true"
android:hint="Unesite broj registracije" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/btn_posalji"
android:id="#+id/btn_posalji"
android:layout_below="#+id/editTextRegistracija"
android:layout_alignParentStart="true"
android:layout_marginTop="33dp"
android:layout_alignParentEnd="false"
/>
strings.xml
<resources>
<string name="app_name">ParkingZagreb</string>
<string name="action_settings">Settings</string>
<string name="txGrad">Odaberite grad</string>
<string name="txZona">Odaberite zonu</string>
<string name="btn_posalji">Pošalji SMS!</string>
<string-array name="gradovi">
<item>Zagreb</item>
<item>Velika Gorica</item>
<item>Zaprešić</item>
</string-array>
<string-array name="zone">
<item>Prva zona</item>
<item>Druga zona</item>
<item>Treća zona</item>
<item>Četvrta zona (1)</item>
<item>Četvrta zona (2)</item>
</string-array>
<string-array name="opisi_zona">
<item>6 kn/h, maksimalno 2 h</item>
<item>3 kn/h, maksimalno 3 h</item>
<item>1,5 kn/h, bez ograničenja</item>
<item>5 kn/dan, 7-16 h</item>
<item>10 kn/dan, 7-20 h</item>
</string-array>
Glavni.java
public class Glavni extends ActionBarActivity {
public TextView txPrikazZone;
private Button btn_posalji;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glavni);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
btn_posalji = (Button) findViewById(R.id.btn_posalji);
btn_posalji.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(Glavni.this).create();
alertDialog.setTitle("SMS poslan!");
alertDialog.setMessage("Provjerite SMS aplikaciju. Poruka bi trebala doći za nekoliko trenutaka.");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
});
// the first city selection Spinner
Spinner spinner_gradovi = (Spinner) findViewById(R.id.spinnergradovi);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.gradovi, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner_gradovi.setAdapter(adapter);
// the second Spinner with parking zones
final Spinner spinner_zona = (Spinner) findViewById(R.id.spinnerZona);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.zone, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner_zona.setAdapter(adapter2);
// this is where I'm trying to populate the TextView with the String-Array by selecting an item from the second Spinner
final String[] opisi_zona = getResources().getStringArray(R.array.opisi_zona);
spinner_zona.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
txPrikazZone.setText(opisi_zona[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_glavni, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thanks for any help...
The reason for the null pointer is that you have not initialized txPrikazZone and you are trying to call setText.
You need to initialize it in your onCreate method like this:
txPrikazZone = (TextView) findViewById(R.id.txPrikazZone);
I hope this helps.
Your adapter it´s the same for both Spinners
EDIT
First try wrapping your ZonaX strings into a string-array
<string-array name="zonas">
...
</string-array>
And now in the code change the set of the ItemSelectedListener
final String[] zonas = getResources().getStringArray(R.array.zonas);
spinner_zona.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
txPrikazZone.setText(zonas[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}

Trying to implement an android spinner. but when I touch it, the dropdown menu doesn't open. nothing happens. why?

I was trying to implement an android spinner, a button, if I click should open a dropdown menu showing three things: "bluetooth, speak, headphones"
but all i see now is the button, (no text), and when I click on it, nothing happens. what am i missing?
here is my code:
in the xml i have:
<RelativeLayout
android:id="#+id/audio_routing"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="10dp" >
<Spinner
android:id="#+id/audio_routing_spinner"
android:layout_width="20dp"
android:layout_height="40dp"
android:contentDescription="#string/audio_routing_desc"
android:scaleType="fitXY"
android:src="#drawable/bluetooth" />
</RelativeLayout>
in the strings file i have:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<string name="audio_routing_desc">Audio Routing Button</string>
<!-- Audio Routing Drop Down List -->
<string-array name="audio_routing_array">
<item>Bluetooth</item>
<item>Headphones</item>
<item>Speaker</item>
</string-array>
in my java file i have:
void audio_routing() {
kcLogger.info("audio_routing", "audio_routing function is called");
Spinner spinner = (Spinner) mKCSWindowView.findViewById(R.id.audio_routing_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(service,R.array.audio_routing_array,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
kcLogger.info("audio_routing", "onItemSelected");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
kcLogger.info("audio_routing", "onNothingSelected");
}
});
}
Change android:contentDescription to android:entries
<Spinner
android:id="#+id/audio_routing_spinner"
android:layout_width="20dp"
android:layout_height="40dp"
android:entries="#array/audio_routing_desc"
android:scaleType="fitXY"
android:src="#drawable/bluetooth" />
android:contentDescription is for accessibility features.
android:entries is used to set array data to UI like spinner, listview, gridview,etc.

Weird Spinner behaviour

I have used a custom adapter for populating my Spinner. I have overriden getDropDownView from which I return the view of each row of the dropdown list. Everything works fine except the dropdown list rendered is not getting the width of the Spinner widget. Rather it gets Like this:
So the dropdown list is missing the highlighted width. I dont know why this is happening. I want it to get the full width of the spinner.
My custom adapter:
class CategorySpinnerAdapter extends ArrayAdapter{
private Activity context;
ArrayList<Category> categoryList;
public CategorySpinnerAdapter(Activity context,int resourceID,ArrayList<Category> categoryList)
{
super(context,resourceID,categoryList);
this.context=context;
this.categoryList=categoryList;
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null)
{
LayoutInflater inflater=context.getLayoutInflater();
convertView=inflater.inflate(R.layout.category_spinner_row, parent,false);
}
Category currentCategory=categoryList.get(position);
TextView categoryText=(TextView) convertView.findViewById(R.id.spinnerText);
categoryText.setText(currentCategory.getCategoryName());
return convertView;
}
}
Code, where I am setting this adapter:
Spinner categorySpinner=(Spinner) getActivity().findViewById(R.id.categorySpinner);
ArrayList<Category> categoryList=populateCategoryList();
CategorySpinnerAdapter categorySpinnerAdapter=new CategorySpinnerAdapter(getActivity(), android.R.layout.simple_spinner_item,categoryList);
categorySpinner.setAdapter(categorySpinnerAdapter);
categorySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
int position, long id) {
// TODO Auto-generated method stub
ArrayList<Reward> modifiedList=new ArrayList<Reward>();
//test case: category OK
int categoryID=position+1;
for(int i=0;i<rewardList.size();i++)
{
if(rewardList.get(i).getCategoryID()==categoryID)
{
modifiedList.add(rewardList.get(i));
}
}
adapter.changeDataSet(modifiedList);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
//get default ELECTRONICS category 1 data to populate the list
ArrayList<Reward> defaultCategorizedList=new ArrayList<Reward>();
//test case: category OK
for(int i=0;i<rewardList.size();i++)
{
if(rewardList.get(i).getCategoryID()==1)
{
defaultCategorizedList.add(rewardList.get(i));
}
}
}
});
Declaration of the Spinner Item inside the main xml:
<Spinner
android:id="#+id/categorySpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/customerRewardPointsTextView"
android:background="#drawable/btn_dropdown"
android:spinnerMode="dropdown" />
layout for the dropdown items, category_spinner_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/category_spinner_background" >
<TextView
android:id="#+id/spinnerText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ellipsize="marquee"
android:gravity="center"
android:singleLine="true" />
</RelativeLayout>
How can I fix this issue ?
try this one:
convertView=inflater.inflate(android.R.layout.simple_list_item_1, parent,false);
The problem starts because of "android:background="#drawable/btn_dropdown"" this portion of code. When you remove this you will see the spinner is working as it is supposed to, that means btn_dropdown drawable is conflicting with your spinner's width and eventually it's popup window that's why you are seeing this weird behaviour. I would suggest you to adjust your btn_dropdown drawable so that it does not conflict with spinner's default behaviour. Take a look at these post:
how-to-design-spinner-in-android
change-spinner-background
I haven't tried those but i think you will find what you need.

Android listView styling

I want to change text color of the list item ,below is my code
public class HelloListView extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, name));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
}); }}
My List.xml
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Empty set"
/>
< /LinearLayout >
Thanks :)
You are currently using an android built-in row component as a view for each row :
android.R.layout.simple_list_item_1
If you want to customize it, pick the code and put your own version in your app. The original code can be found on google code project for android :
Then you can customize xml and change the color attribute or change for a new selector state.
If you want to something even more custom, override your list's adapter getView method and provide a custom component inflating this xml and providing additional methods to be filled and display your data for each row.
Regards,
Stéphane
If you want to change the appearance of the elements in the list, Use the CustomListView and CustomAdapter.
http://www.androidpeople.com/android-custom-listview-tutorial-part-2
http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html
This Links may help you......

Categories

Resources