1

I'm new to android and I just can't figure out how to make the actionbar work the way I want it to. I would like to click an item on my action bar that changes between the setContentView to different XML files.

I have the following XML files: activity_main_vertical_view.xml activity_main.xml

(Note that this is not so that I can see it in landscape mode, I'm aware of making a folder called layer-land. I want my xml file to be viewable horizontally in portrait mode which is why I'm trying to change XML).

I have a "change view" item in my actionbar which I'd like to make XMLs switch when I use it. However it's not working. What am I doing wrong?

I have 2 classes: MainActivity.java VerticalView.java

My Manifest.xml has the following code with respect to VerticalView

<activity
    android:name=".VerticalView"
    android:label="@string/title_activity_vertical_view" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.calendar.activity_main" />
</activity>

My Menu for activity_main.xml has the following code:

<item
    android:id="@+id/change_view"
    android:onClick="changeView"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/change_view" >
</item>

my MainActivity.java has the following code:

    public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void changeView(View view) {
        Intent intent = new Intent(view.getContext(), VerticalView.class);
        startActivity(intent);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;

    }
}

Finally, my VerticalView.java has the following code:

public class VerticalView extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_vertical_view);

    }

}
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
Terence Chow
  • 10,755
  • 24
  • 78
  • 141

1 Answers1

1

You did not follow the rules for android:onClick:

The method must be declared in the activity as public and accept a MenuItem as its only parameter, which indicates the item clicked.

Your method takes a View as a parameter, not a MenuItem.

Also note that this approach only works in API Level 11 and higher. For backwards compatibility, you may wish to skip android:onClick and override onOptionsItemSelected() instead.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Looks like I must have missed the documentation for Menu and went straight to actionbar. Adding this link for noobs he need this answered better. Thanks CommonsWare http://stackoverflow.com/questions/7479992/handling-a-menu-item-click-event-android – Terence Chow Sep 02 '12 at 15:51