Search code examples
javaandroidandroid-annotations

Android annotations: Different layout for portrait and landscape


I am using AA and would like to specify two different xml layout files for portrait and landscape orientations.

In my java class, marked as EActivity a method annotated with @Click is provided to respond to click event.

Everything is working fine in portrait mode. But when the phone is rotated, the layout changes and buttons are not responding anymore.

My guess is that the layout provided after @EActivity annotation is the portrait layout.

How can I provide both portrait and landscape layouts ?

Thanks in advance.

Update I 'listen' to orientation changes using two different layout files one, for portrait which is named layout/contact.xml and the other, for landscape which is named layout-land/contact.xml. Android does the rest and uses the correct layout according to phone orientation.

To register listener for click events, I use Android Annotations:

contact.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="match_parent">

    <include
        layout="@layout/header_layout" />

    <!-- Some elements -->
</LinearLayout>

header_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="60dp">

    <ImageButton
        android:id="@+id/menuHome"
        android:layout_width="25dp"
        android:src="@drawable/menu"
        android:paddingTop="22dp"
        android:paddingBottom="22dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_gravity="center"
        android:background="#00000000"
        android:scaleType="fitCenter"
        android:layout_height="match_parent" />
    <!-- Other buttons -->
</LinearLayout>

contact.java

@EActivity(R.layout.activity_contact)
public class ContactActivity extends TopLevelActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public String getPageTitle() {
        return "Nous contacter";
    }
}

** TopLevelActivity.java **

@EActivity
public abstract class TopLevelActivity extends Activity implements ITopLevelActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Click
    public void menuHome () {
        startActivity(new Intent(this, HomeActivity_.class));
    }
}

Solution

  • Add the following code in your java file

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            setContentView(R.layout.activity_main_h);
            button1 = (Button)findViewById(R.id.button1);
            button1.setOnClickListener(this);
        }
        else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            setContentView(R.layout.activity_main_v);
            button1 = (Button)findViewById(R.id.button1);
            button1.setOnClickListener(this);
        }
    }
    

    Also, add the following to your activity in the Manifest: android:configChanges="keyboardHidden|orientation|screenSize"