How Do I Change The Layout Of The Items in the Spinner Control?

by Mark Murphy » Mon, 10 Nov 2008 16:20:39 GMT


Sponsored Links
 


If the revised layout is the same for all of them, give the revised 
layout to the adapter constructor. You may need to use the 
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] 
objects) if your layout is more than a single TextView.

If your revised layout varies by item or needs to shift on the fly, you 
will probably need to subclass your adapter and implement getView() to 
return the View you want for each row. If you visit AndroidGuys.com and 
search "Fancy ListViews", you'll find a long list of blog posts on that 
topic, though some are from the M5 SDK and will need tweaks to work on 
the current one.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ Version 1.4 Published!

--~--~---------~--~----~------------~-------~--~----~



How Do I Change The Layout Of The Items in the Spinner Control?

by Matthias » Mon, 10 Nov 2008 16:37:03 GMT


 I've got my first basic app working, but I need the text inside my
spinner widget to lay out in a different way (different gravity,
different text style, etc.) How can I do this? Is there a way to give
the item a different layout setup with the Array Adapter?

Example:

ArrayAdapter myArrayAdapter = new
ArrayAdapter.createFromResource(this, R.array.myArrayResource, [change
something here?]);

If so, what do I change it to? I've tried giving it a basic layout
with a TextView and the app freezes on me. Is there some kind of
format I need to be mindful of?

Or do I need to do something with the spinner itself?

Matthias

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


How Do I Change The Layout Of The Items in the Spinner Control?

by Matthias » Mon, 10 Nov 2008 22:16:29 GMT


 You are so totally the man!

For anyone else reading this, I created a new view called
"my_spinner_view.xml" with the following xml:

<?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="14pt"
                android:id="@+id/spinnerTarget" />

and my ArrayAdapter code looks like this:

ArrayAdapter myArrayAdapter = new
ArrayAdapter.createFromResource(this, R.array.myArrayResource,
R.layout.my_spinner_view);

And my spinner text is formatted exactly the way I want.






--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. opening new screen help

Hello all,

I was just wondering if someone can me understand how we can make a
new screen work in a existing code. I tried this tutorial but the part
which I don't understand very well is where should I put that piece of
java code in my existing code. Rest of it is fairly simple. Some help
please. Thanks


package com.varun.HelloListView;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class HelloListView extends ListActivity {
        TextView selection;

        ArrayList<HashMap<String, String>> list1 = new
ArrayList<HashMap<String, String>>();
        SimpleAdapter smipleAdapter1;

        ArrayList<HashMap<String, String>> list2 = new
ArrayList<HashMap<String, String>>();
        SimpleAdapter smipleAdapter2;

        ArrayList<HashMap<String, String>> bottom= new
ArrayList<HashMap<String, String>>();
        SimpleAdapter simpleAdapter3;


        @Override
        public void onCreate(Bundle icicle) {
                super.onCreate(icicle);

                setContentView(R.layout.main);


                smipleAdapter1 = new SimpleAdapter(this,
                                                                                
  list1,
                                                                                
  R.layout.list_item,
                                                                                
  new String[] {"line1", "line2"},
                                                                                
  new int[] {R.id.label1, R.id.label2});
                smipleAdapter2 = new SimpleAdapter(this,
                                                                                
  list2,
                                                                                
  R.layout.list_item,
                                                                                
  new String[] {"line1", "line2"},
                                                                                
  new int[] {R.id.label1, R.id.label2});



                //pupulate the lists
                addItems();

                this.setListAdapter(smipleAdapter1);
                // button click
                Button next1 = (Button) findViewById(R.id.Button1);
                next1.setOnClickListener(
                new View.OnClickListener() {
                        public void onClick(View view) {
                                        setListAdapter(smipleAdapter1);
                                        smipleAdapter1.notifyDataSetChanged();
                        }
        }
        );
                this.setListAdapter(smipleAdapter2);
                Button next2 = (Button) findViewById(R.id.Button2);
                next2.setOnClickListener(
                        new View.OnClickListener() {
                                public void onClick(View view) {
                                        setListAdapter(smipleAdapter2);
                                        smipleAdapter2.notifyDataSetChanged();
                                }
                  }
                );


        }


        private void addItems() {
                long ts = System.currentTimeMillis();
                int lastDigit = (int)( ts % 10 );

                for(int i = 0; i < AndroidPhones1.length; i++) {
                        HashMap<String,String> item = new 
HashMap<String,String>();
                        item.put( "line1", AndroidPhones1[i] );
                        item.put( "line2", "lastDigit: "+Integer.toString( 
lastDigit ) );
                        list1.add( item );
                        smipleAdapter1.notifyDataSetChanged();
                }

                for(int i = 0; i < AndroidPhones2.length; i++) {
                        HashMap<String,String> item = new 
HashMap<String,String>();
                        item.put( "line1", AndroidPhones2[i] );
                        item.put( "line2", "varun1" );

                        list2.add( item );
                        smipleAdapter2.notifyDataSetChanged();
                }

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                super.onCreateOptionsMenu(menu);

                menu.add(0, Settings.ENTER_FOOD, 0, "Enter Food");
                menu.add(0, Settings.ANALYZE_FOOD, 0, "Analyze Food");
                menu.add(0, Settings.SETTINGS, 0, "Settings");

                return true;
        }


        static final String[] AndroidPhones1 = new String[] {
                "HTC Evo 1G",  "Google Nexus One", "Motorola Devour",
                "Motorola CLIQ", "Samsung Galaxy S", "Motorola Droid",
                "myTouch 3G Slide", "Droid Eris", "Motorola Backflip",
                "Motorola i1", "HTC Hero", "myTouch 3G Fender",
                "HTC Droid Incredible",  "Samsung Moment", "LG Ally "
        };

        static final String[] AndroidPhones2 = new String[] {
                "HTC Evo 2G",  "Google Nexus One", "Motorola Devour",
                "Motorola 2LIQ", "Samsung Galaxy S", "Motorola Droid",
                "myTouch 3G Slide", "Droid Eris", "Motorola Backflip",
                "Motorola i1", "HTC Hero", "myTouch 3G Fender",
                "HTC Droid Incredible",  "Samsung Moment", "LG Ally "
        };

        static final String[] AndroidPhones3 = new String[] {
                "HTC Evo 3G",  "Google One", "Motorola Devour",
                "Motorola 2LIQ", "Samsung Galaxy S", "Motorola Droid",
                "myTouch 3G Slide", "Droid Eris", "Motorola Backflip",
                "Motorola i1", "HTC Hero", "myTouch 3G Fender",
                "HTC Droid Incredible",  "Samsung Moment", "LG Ally ",
        };

        static final String[] AndroidPhones4 = new String[] {
                "HTC Evo 4G",  "Google Nexus hello", "Motorola Devour",
                "Motorola 2LIQ", "Samsung Galaxy S", "Motorola Droid",
                "myTouch 3G Slide", "Droid Eris", "Motorola Backflip",
                "Motorola i1", "HTC Hero", "myTouch 3G Fender",
                "HTC Droid Incredible",  "Samsung Moment", "LG Ally "
        };
        }

-- 

2. Android Market update bug?

I have a Nexus One using 2.2.1 and was notified that four apps had
updates, so I opened Market and clicked "Update All", exited Market,
hit the power button to put the phone to sleep and put it away. The
next day I look in the Market and those apps still say "Installing..."
I can't cancel them and rebooting the phone did nothing. They just
show the progress bar with the hash marks scrolling by...

I can still use the apps though. I don't want to see what happens if I
uninstall mostly because I'd loose the data in the apps. The battery
doesn't seem effected, so right now it appears to be just an
uncomfortable curiousity.

Anyone seen this?

Ken

-- 

3. android.net.Uri to java.net.URI

4. Generate QR Codes

5. LetterRecognizer API in the Gesture class

6. Kernel Crash in Some hardware ,After some cycle

7. Amazon android market. Anyone take the offer?