Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customise the TextView inside a Spinner?

I have a Spinner with an ArrayAdapter that feeds Values into it. The layout for this views looks something like this:

        <TextView
           android:text="Household Income: "
           android:layout_width="wrap_content"
           android:layout_height="fill_parent"
           android:gravity="center_vertical" />

        <Spinner
           android:id="@+id/incomespinner"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:padding="2dip"  />

The Problem is that the text is to long for the view and the result is a very very ugly spinner. As can be seen in the screenshot:

alt text
(source: janusz at janusz.de)

I tried to pass the Id of my own TextView into the Adapter but everytime the spinner should be shown I get an Exception that the Id I supplied is not valid:

04-26 17:38:39.695: ERROR/AndroidRuntime(4276): android.content.res.Resources$NotFoundException: Resource ID #0x7f09003a type #0x12 is not valid

Where do I have to define the TextView? In a separate xml file? With a surrounding viewgroup?

It would help me a lot if I could see an example of the adapter initialization and the textview definition?

like image 894
Janusz Avatar asked Apr 26 '10 15:04

Janusz


People also ask

Can we Nest TextView inside TextView?

In Android all layout can be nested one another.

Can we change the text in TextView?

TextView tv1 = (TextView)findViewById(R. id. textView1); tv1. setText("Hello"); setContentView(tv1);

How do I set TextView in center in RelativeLayout?

android:gravity controls the appearance within the TextView. So if you had two lines of text they would be centered within the bounds of the TextView. Try android:layout_centerHorizontal="true" . Notice that this won't work if a RelativeLayout contains an attribute android:gravity="center_horizontal".

How do you create a TextView control in an activity file?

Modify src/MainActivity. java file to add necessary code . Modify the default content of res/layout/activity_main. xml file to include Android UI control.


3 Answers

Override the getView method of the Adapter to create a custom list view item.

View getView(int position, View  convertView, ViewGroup  parent) {
     TextView tv = null;
     if(covertView instanceof TextView) {
          tv = (TextView) convertView;
     } else {
          tv = new TextView(this);
          tv.setText("Awesome Text");
     }
     return tv;
}

Also when you create the adapter you can pass in a layout to use for the item, so in that case ConvertView would be a RelativeLayout or LinearLayout.

like image 168
Robby Pond Avatar answered Oct 06 '22 05:10

Robby Pond


The answer was to remove the padding from the definition of the Spinner. Every time I set the padding of the Spinner the Text gets out of shape.

The spinner drawable is using the padding box from the nine patch to determine where to draw the the content. If I set a padding manually the definition of the padding from the padding box will be overridden.

If you want to set a padding in addition to a padding box you need to wrap everything inside that paddinbox into an additional Layout container.

like image 32
Janusz Avatar answered Oct 06 '22 04:10

Janusz


use /home/user/android-sdk-linux/tools/./draw9patch (it's for linux), on Windows go to tools on android-sdk-windows directory and execute draw9patch.

and use so image, ex:

enter image description here

Attention for de black lines in image. (this black lines are generates by draw9patch)

Image results add on extension file 9, ex: img.9.png (IMG EXPANSIVE HORIZONTAL AND VERTICAL)

like image 31
Namor Alves Avatar answered Oct 06 '22 04:10

Namor Alves