0

i created some buttons in an xml layout. They get their names from string resources in 3 different languages. Therefore the buttons aren't always in the correct alphabetical order. So there's my question. What can i do to make the buttons being sorted alphabetically in all 3 languages?

Sincerely John

JohnD
  • 201
  • 2
  • 4
  • 12
  • From XML file its not possible. You have to code it dynamically in your Activity. – user370305 Jun 19 '12 at 10:06
  • you can create three different xml file and add in layout- folder – Sudar Nimalan Jun 19 '12 at 10:11
  • Or make a 3 different layouts for different languages and at activity created time set according to current language. You can get language using Locale.getDefault().getDisplayLanguage(); – user370305 Jun 19 '12 at 10:12
  • @user370305 Yes, i assumed that, but how can i code it dynamically? – JohnD Jun 19 '12 at 10:23
  • Look at http://stackoverflow.com/questions/3011092/how-can-i-dynamically-create-a-button-in-android and http://stackoverflow.com/questions/1851633/how-to-add-button-dynamically-in-android – user370305 Jun 19 '12 at 10:36
  • @user370305 and neilmw Perfect, i think i can do it now. Thanks a lot. :-) – JohnD Jun 19 '12 at 10:38
  • But best approach is use only one layout for buttons, and set its name according to alphabet on creation of activity using get current language. – user370305 Jun 19 '12 at 10:40

2 Answers2

1

Assuming the three buttons are in a LinearLayout, you can subclass LinearLayout and override the getChildAt() method to return the Views in the order you want (which you could work out once the Strings are loaded).

E.g.

@Override
public View getChildAt(int index)
{
  // translate index into the ordering you want.
  // and put into newIndex...

  return super.getChildAt(newIndex);
}
nmw
  • 6,664
  • 3
  • 31
  • 32
0

You can create a Layout for each language (check this document)

Another solution is to load the strings in a List, sort them and then use Button.setText to set the strings of each button from the sorted List and Button.setTag to give each button a unique tag for this text. After that you can use the tag to set the onClickListener accordingly.

Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47