Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top