1

I've got an android app, with a super-class that contains a layout that should be static for each activity, the illustration beneath shows this in a better way rather than my description

enter image description here

This "header" contains a tabBar which contains a ImageButton. I want this header to be static for all the activities in my app. What I tried to do, is to extend my other classes from this superclass. Code for the super class is beneath

public class MySuperClass extends Activity {
 MyHorizontalScrollView scrollView;
 View menu;
 View app;
 ImageButton btnSlide;
 boolean menuOut = false;
 Handler handler = new Handler();
 int btnWidth;

 Button testClass; 

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

    LayoutInflater inflater = LayoutInflater.from(this);
    scrollView = (MyHorizontalScrollView) inflater.inflate(R.layout.horz_scroll_with_list_menu, null);
    setContentView(scrollView);

    menu = inflater.inflate(R.layout.horz_scroll_menu, null);

    app = inflater.inflate(R.layout.horz_scroll_app, null);
    ViewGroup tabBar = (ViewGroup) app.findViewById(R.id.tabBar);

    ListView listView = (ListView) app.findViewById(R.id.list);

    listView = (ListView) menu.findViewById(R.id.list);

    ArrayList<MenuItem> menuItems = getMenuItems();
    listView.setAdapter(new MenuCustomAdapter(this, menuItems)); 

    btnSlide = (ImageButton) tabBar.findViewById(R.id.BtnSlide);
    btnSlide.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch(event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                btnSlide.setImageResource(R.drawable.lincolor);
                break;
            case MotionEvent.ACTION_UP:
                btnSlide.setImageResource(R.drawable.lin);
                break;
            }

            return false;
        }


    });
    btnSlide.setOnClickListener(new ClickListenerForScrolling(scrollView, menu));

    testClass = (Button) app.findViewById(R.id.button1);

    testClass.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(MySuperClass.this, TestClass.class);
            startActivity(intent); 
        }


    });

    final View[] children = new View[] { menu, app };

    // Scroll to app (view[1]) when layout finished.
    int scrollToViewIdx = 1;
    scrollView.initViews(children, scrollToViewIdx, new SizeCallbackForMenu(btnSlide));
}

public ArrayList<MenuItem> getMenuItems() {
    ArrayList<MenuItem> items = new ArrayList<MenuItem>();


    MenuItem m1 = new MenuItem(R.drawable.scroll, "Show history"); 
    items.add(m1);
    MenuItem m2 = new MenuItem(R.drawable.right, "Right"); 
    items.add(m2);

    return items;
}


/**
 * Helper for examples with a HSV that should be scrolled by a menu View's width.
 */
static class ClickListenerForScrolling implements OnClickListener {
    HorizontalScrollView scrollView;
    View menu;
    ImageButton button;
    int pressed;
    int timeout;
    /**
     * Menu must NOT be out/shown to start with.
     */
    boolean menuOut = false;

    public ClickListenerForScrolling(HorizontalScrollView scrollView, View menu) {
        super();
        this.scrollView = scrollView;
        this.menu = menu; 
    }



    @Override
    public void onClick(View v) {
        Context context = menu.getContext();

        int menuWidth = menu.getMeasuredWidth();

        // Ensure menu is visible
        menu.setVisibility(View.VISIBLE);

        if (!menuOut) {
            // Scroll to 0 to reveal menu
            int left = 0;
            scrollView.smoothScrollTo(left, 0);
        } else {
            // Scroll to menuWidth so menu isn't on screen.
            int left = menuWidth;
            scrollView.smoothScrollTo(left, 0);
        }
        menuOut = !menuOut;
    }


}

/**
 * Helper that remembers the width of the 'slide' button, so that the 'slide' button remains in view, even when the menu is
 * showing.
 */
static class SizeCallbackForMenu implements SizeCallback {
    int btnWidth;
    View btnSlide;

    public SizeCallbackForMenu(View btnSlide) {
        super();
        this.btnSlide = btnSlide;
    }

    @Override
    public void onGlobalLayout() {
        btnWidth = btnSlide.getMeasuredWidth();
        System.out.println("btnWidth=" + btnWidth);
    }

    @Override
    public void getViewSize(int idx, int w, int h, int[] dims) {
        dims[0] = w;
        dims[1] = h;
        final int menuIdx = 0;
        if (idx == menuIdx) {
            dims[0] = w - btnWidth;
        }
    }
}

}

And a test class, which extends this superclass.

public class TestClass extends MySuperClass {

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

}

}

Again how can I make the tabBar static for each activity?

Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143

3 Answers3

1

you can use a TabHost right. by click on each tab you can launch separate activity

http://mfarhan133.wordpress.com/2010/11/17/tablayouttabhost-tutorial-for-android-reusing-layout/

Vishwanath.M
  • 6,235
  • 11
  • 42
  • 56
1

I don't know if that is possible. I will post an alternative solution, which requires a little bit of more code but works, and is a best practice, so if nothing comes up this is the best alternative, probably the only one.

When you are trying to use a certain layout again and again, like the tab bar that you are mentioning, there is the <merge> and <include> functionality in the layout. The basic idea is that you make a layout.xml file that you want to include in other layouts.

This post gives a very good example of how to use it. Simple example of <merge> and <include> usage in Android XML-layouts

Community
  • 1
  • 1
10s
  • 1,699
  • 1
  • 12
  • 14
1

There is no way to achieve this in an Activity-scope. The layout are independent from your Acitvity (well, until you bind them).

The solution to your problem, may be in using a common header layout, kept in a xml file under your layout folder, something like this:

header.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/red"
    ... />

And include them in your layouts, using the include tag:

my_activity_layout.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    ....
    .... >

    <include layout="@layout/header.xml" android:id="@+id/header" />

    <!-- Countinue your layout .... -->

</RelativeLayout>
Iñigo
  • 12,723
  • 2
  • 31
  • 31
  • This solution will work on the layout side, but not the OnClickListeners from the inherited class. – Tobias Moe Thorstensen Jul 23 '12 at 08:41
  • Why not? You just have to assign id's for your Views, and get them by calling findViewById in every activity you need them. Then assign the listeners normally. – Iñigo Jul 23 '12 at 08:45