2

i am a new android developer,i know align two text in one button using by /n, but don't know align two text in one button one text right alignment and another one is left align please any one help me and solve my problem, since I use Arabic language (right to left) I want to do this programmatically, not by xml design. I need to align two texts one below another ie, english text on top and arabic in bottom Thanks in advance

mohamed
  • 303
  • 1
  • 6
  • 18
  • Do you mean you want left-aligned and right-aligned text in the button at the same time? eg: `LTR...........RTL` ? If so, you're going to have to have a positioning element inside the button although you could add it programatically – Basic May 24 '12 at 13:41
  • Sorry I need to align two texts one below another ie, english text on top and arabic in bottom – mohamed May 24 '12 at 13:43
  • I havne't touched Android in a while so I'm not going to post as an answer but I _believe_ you'll need to set the contents of the button to be a layout element (StackPanel?) with 2 text boxes. You might wrap this inside a button control of your own which would have `.LTRText` and `.RTLText` parameters. I'm sure someone else can give you a more detailed/better solution. – Basic May 24 '12 at 13:45

1 Answers1

3

Use following code:

Button  availableText = (Button)findViewById(R.id.request); 
            Spannable span =Spannable.Factory.getInstance().newSpannable(availableText.getText());
            span.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_NORMAL),0, 7, 
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
            span.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE),8, 
            span.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
            availableText.setText(span); 

Assuming you have a button defined in XML or can create one in code with text "English\nArbi".

The output is as follows:

Button layout example

Imran Rana
  • 11,447
  • 7
  • 42
  • 51