1

How would I go about changing the orientation of the text in my buttons, so that they are written vertically rather than horizontally?

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_weight="0.10"
    android:text="Previous" />

Is it possible?

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
OmniOwl
  • 5,477
  • 17
  • 67
  • 116
  • I think that if you set the button to be vertical (the form factor), the text should follow. Another thing is if you want the text to be displayed vertically and from bottom to top. – g00dy Mar 06 '13 at 11:44
  • check out this question.... http://stackoverflow.com/questions/8604932/android-text-view-text-in-vertical-direction – Praful Bhatnagar Mar 06 '13 at 11:46
  • or you can use Html.fromHtml() with likes breaks e.g. S
    a
    , not elegent solution but will work
    – Waqas Mar 06 '13 at 11:47
  • You can do this by setting fixed width for button like changing android:layout_width from wrap_content to 37dp(size which fulfill your requirement.).This is not elegant solution but it will work – krushnakant Mar 06 '13 at 11:56

3 Answers3

7

This is the screenshot after setting the width to 12dp

enter image description here

Try this

place \n character in your string for line breaks in your Strings.xml like this

1\n 2\n 3\n

and you can directly set it like this

android:text="@String/yourstring"
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • If your requirement is to rotate the button you can make by using rotation attribute for your button – Pragnani Mar 06 '13 at 11:54
  • Nah, it has to be the text in the button. – OmniOwl Mar 06 '13 at 12:01
  • I did, and it doesn't work as intended. The text have weird spaces between the letters and I have to adjust the buttons width manually (which is not desired) for it to be seen. – OmniOwl Mar 06 '13 at 12:08
  • @Vipar Simple Solution is set Width of the button to 5dp and set the height to fill parent...5dp in the sense size, set the width so that it will allow only one character.. It will do the trick – Pragnani Mar 06 '13 at 12:29
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25686/discussion-between-pragnani-and-vipar) – Pragnani Mar 06 '13 at 12:30
  • @Vipar udpate your xml file after you've set the width, I have tested it is working like charm – Pragnani Mar 06 '13 at 12:32
  • set width to 12dp it will work and the height should be fill parent – Pragnani Mar 06 '13 at 12:34
  • Same thing happens. Button disappears from screen. – OmniOwl Mar 06 '13 at 12:39
2

Tried and tested this one it's working absolutely fine.

 public class buttonCustom extends Button{
String s="";

public buttonCustom(Context context) {
    super(context);
}


public buttonCustom(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}


public buttonCustom(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}


@Override
public void setText(CharSequence text, BufferType type) {
    // TODO Auto-generated method stub

    // TODO Auto-generated method stub

    for(int i=0;i<text.length();i++)
    {
        if(s==null)
            s="";

        s= s+String.valueOf(text.charAt(i))+ "\n";
    }



    super.setText(s, type);
}

}

Override button class and override the setText function

Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37
0

I fixed it myself. I made a separate folder called layout-land and put a separate XML file in there to take care of that. Now the layout looks fine in both, and I can use hardcoded weights for attributes.

OmniOwl
  • 5,477
  • 17
  • 67
  • 116