Button event handleling on master-detail activity on Android

Android OS, is still most popular OS platform for the Mobile, Tablet and Smart Electronic devices. There are lots of ways to develop android apps. There are lots of ways to develop your own android UI.

Multi-pane layout is one of the popular layout in android application. Using the fragment is one of the key idea of such layouts. It has two approaches of work, one for small screen devices and another for large screen. Once you added mater-detail activity on your app, you will see at least following layouts with their respective java files

  • activity_…._detail.xml
  • activity_…._list.xml
  • activity_…._twopane.xml
  • activity_…..xml

The list layout is used to list the items that are to be shown in the master. Detail is the activity that handles the activities that are to be shown on responses of list. twopane is the layout specially used when device screen is big enough to show both master and detail activity in same screen.

In master detail model of UI, one of the difficulties that I was facing is Events, when you are loading other independent activity on the Detail Fragment.

With the default template, you can simply load own activity by easy changes in DetailFragment.java files onCreateView event.

For Example:

View rootView = null;
rootView = inflater.inflate(R.layout.activity_dashboard, container, false);

The controls such as buttons which need to handle events, now will not work as it was working on normal fashion. So you have to give the event handler in DetailActivity.java class, and assign the onClick event on the layout’s xml.

Detail activity should be something like

public class DashboardDetailActivity extends FragmentActivity implements OnClickListener

And Event Handler is as follows

public void onClick(View v) {
    switch (v.getId()) {
         case R.id.daily_coll_btn_save:
                     call any function or place code to handle the click
                      break;
    }
 }

And button

 <Button
        android:id="@+id/daily_coll_btn_save"
        style="@style/btn_save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/daily_coll_mode"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/daily_coll_amt"
        android:layout_marginTop="17dp"
        android:text="@string/save"
        android:onClick="onClick"
         />

In this way we can simplify the event handling issue in android master-detail activity.

 

Leave a Reply

Your email address will not be published. Required fields are marked *