5

I wanted to place a spinner in the action bar, just like in the Gmail app. So I created the following layout.

  <?xml version="1.0" encoding="utf-8" ?> 
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
  <TextView android:id="@+id/spinner_list_item_selected_header" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:textStyle="bold" 
android:gravity="center" 
android:text="Sales" /> 
      <TextView android:id="@+id/spinner_list_item_selected_text"  android:layout_width="match_parent"
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:textStyle="bold" 
android:gravity="center" 
android:text="" /> 
      </LinearLayout>

This spinner is then loaded on to the title bar using the following code:

getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getActionBar().setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);

It works as expected, except that in the landscape mode, the text are not fitting in. In Gmail app, the font size gets reduced. Is there a way to auto adjust the font size based on the orientation?

Gmail Portrait

Gmail Landscape

My Portrait

My Landscape

Raj
  • 22,346
  • 14
  • 99
  • 142
  • following your question, do you know how the **selected** element content (I mean the text) can be wrapped ? I tried to reproduce the Actionbar Gmaps and Gmail spinner style but I can't. My spinner width seems to be streched by the "dropdown items" content. If the item within the drop has an element which is too big, then the spinner is stretched. Is there a way to wrap the spinner selected element ? – kaffein Sep 26 '12 at 16:26

2 Answers2

12

//you need to use dimens.xml in values folder

<!-- Text size for action bar titles -->
    <dimen name="action_bar_title_text_size">18dp</dimen>
    <!-- Text size for action bar subtitles -->
    <dimen name="action_bar_subtitle_text_size">14dp</dimen>
    <!-- Top margin for action bar subtitles -->
    <dimen name="action_bar_subtitle_top_margin">-3dp</dimen>
    <!-- Bottom margin for action bar subtitles -->
    <dimen name="action_bar_subtitle_bottom_margin">5dip</dimen>

create one more folder with values-land

in that dimens.xml

 <!-- Text size for action bar titles -->
    <dimen name="action_bar_title_text_size">16dp</dimen>
    <!-- Text size for action bar subtitles -->
    <dimen name="action_bar_subtitle_text_size">12dp</dimen>
    <!-- Top margin for action bar subtitles -->
    <dimen name="action_bar_subtitle_top_margin">-2dp</dimen>
    <!-- Bottom margin for action bar subtitles -->
    <dimen name="action_bar_subtitle_bottom_margin">4dip</dimen>

Ref: go to your /<android-sdk>/platforms/android-15/data/res here you can found this.

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
1

They best way is to know the orientation of the device when the activity is started and use a layout depending on it.

If portrait setContentView(R.layout.layout_with_bigfont);

If lanscape setContentView(R.layout.layout_with_smallfont);

otherwise you need to set the font for that View (in this case TextView) alone dynamically using setTextSize(xdp) instead of using 2 different layouts for different orientations

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • dimens.xml is just to store all the hard coded values in an xml and use them with reference names. if you are hard coding values in a single activity or with different values in many activities why do anyone need a dimens resource? – Archie.bpgc Jul 27 '12 at 10:58
  • is this what you are talking about?...http://stackoverflow.com/questions/3885164/storing-dimensions-in-xml-file-in-android – Archie.bpgc Jul 27 '12 at 11:00