0

I'm working on my first android app and have been trying to make a header consisting of three image buttons for back, home, and next. I was wondering if it's possible to make the header in a separate XML file, and then use include so that I may cut down on redundancy and reuse the header with multiple activities. I've been able to do this so that the navigation buttons display, but have not found a way to make the buttons navigate between activities. Thanks!

Kara
  • 6,115
  • 16
  • 50
  • 57

1 Answers1

0

You can use an intent to navigate across activities on button pressed. First, in the xml drag and drop a button. Then initialize this button in your activity. Eg.

Button BackBtn=(Button) findViewById(R.id.button1);

Once you have got reference to the button you can set onclick listener to this button and onclick of it, you can use to below code to navigate across activities.

Intent i =new Intent();
i.setClass(fromClass,ToClass.class);
startActivity(i);

I hope the above code would have helped.

Regarding the second part of the question, although you can define a seperate xml containing 3 buttons for navigation, you need to redefine its functionality in every activity you use them in. So instead i suggest you to create a sub-layout inside your current layout and in that add three buttons. From next activity onwards, you can copy paste that layout in your new xml.

Zax
  • 2,870
  • 7
  • 52
  • 76