0

I have an app that supports different languages. However I have 1 layout file which I want to use no matter what the language.
This layout has a couple of buttons that open different activities but it needs to be in alphabetical order.
i.e. Car(eng) - Auto(de) English version button 3 would open car activity, but German version button 1 would open car activity. How do I ensure this happens, or do I have to create different activities depending on language?
Layout File:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="72dp"
        android:orientation="horizontal" >


    <Button 
        android:id="@+id/btnButton1"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="25dp"
        android:text="1" />

    <Button 
        android:id="@+id/btnButton2"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_toRightOf="@+id/btnButton1"
        android:layout_alignBottom="@+id/btnButton1"
        android:layout_marginLeft="5dp"
        android:text="2"/>

    <Button 
        android:id="@+id/btnButton3"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_toRightOf="@+id/btnButton2"
        android:layout_alignBottom="@+id/btnButton2"
        android:layout_marginLeft="5dp"
        android:text="3"/>
</RelativeLayout>

Class that has buttons:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Locale.getDefault();
        setContentView(R.layout.activity_animal_choice);

        btnButton1 = (Button) findViewById(R.id.btnButton1);
        btnButton1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                    Intent intent = new Intent(Menu.this, Car.class);
                    startActivity(intent);

            }
        });

It would be good to see if this is possible, if not it will probably be quicker and easier to create different apk's per language

2 Answers2

1

You can use the same Activity classes and have different layout or fragment XML for each language. See the section "Design a Flexible Layout" on this page. As it says, it will be extra work for you to make sure all those layout files are kept updated. If you plan to support many languages, you might consider adding the buttons programmatically. Then you'd only have to update the strings for each language. See this thread.

Community
  • 1
  • 1
Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
  • Thanks - But I have read that, but cannot see anywhere in the document that says or points me in the direction of using 1 layout file, and depending on the language will depend on which button does what, unless I'm missing something from that link? – Android-converter May 09 '14 at 15:18
  • I'm suggesting you use several layout files, e.g., `res/layout-de/main.xml`, `res/layout-en/main.xml`, etc. The Activity is completely agnostic about where the buttons are on the screen, so the same Activity can be used with all the layouts. The correct layout will be selected automatically based on the locale setting on the device. – Kevin Krumwiede May 09 '14 at 15:24
  • I might actually go for the adding buttons programmatically, as all my strings are done – Android-converter May 09 '14 at 15:31
0

You need to give all the buttons the same OnClickListener and in that listener check what the text of the button is, something like:

OnCLickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
                Intent intent = new Intent();

                if (v.getText().equals(getString(R.string.car)) {
                    intent.setClass(Menu.this, Car.class);
                }
                else if (v.getText().equals(getString(R.string.whatever)) {
                    intent.setClass(Menu.this, Whatever.class);
                }
                // ...

                startActivity(intent);
        }
    });

button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
ashishduh
  • 6,629
  • 3
  • 30
  • 35