UMAR-MOBITSOLUTIONS
UMAR-MOBITSOLUTIONS

Reputation: 77984

How to implement master and child page like Activity in Android?

I want to place a common banner and menu on each Activity with footer too.

Can anyone guide me how can I implement master and child page like asp.net in Android???

Any help would be appreciated.

Upvotes: 6

Views: 3549

Answers (4)

Gelion
Gelion

Reputation: 34

ViewStub is the solution

activity_masterpage.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >    
<ViewStub android:id="@+id/stub_content"
               android:inflatedId="@+id/subTree"
               android:layout_width="match_parent"
               android:layout_height="match_parent" />

stub = (ViewStub) findViewById(R.id.stub_content);
stub.setLayoutResource(R.layout.content_layout);
stub.inflate(); 

Upvotes: 0

Ahmad Ronagh
Ahmad Ronagh

Reputation: 800

The solution was pretty easy.

You need to extends "Activity" Class,in onCreate function SetContentView to your base xml layout and also need to override setContentView in base Activity Class

For Example:

1.Create "base_layout.xml" with the below code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
      android:background="#000000"
      android:padding="15dp" >
    <LinearLayout android:orientation="horizontal" android:background="#000000"
           android:layout_width="fill_parent" android:layout_height="wrap_content"
           android:minHeight="50dp" android:paddingLeft="10dp">
           <ImageView android:layout_width="wrap_content" android:id="@+id/ImageView01"
               android:adjustViewBounds="true" android:layout_height="wrap_content"
               android:scaleType="fitCenter" android:maxHeight="50dp" />
   </LinearLayout>
   <LinearLayout android:id="@+id/linBase"
     android:layout_width="fill_parent"
       android:layout_height="fill_parent" >
   </LinearLayout>
</LinearLayout>    

2.Create "BaseActivity.java"

public class BaseActivity extends Activity {
    ImageView image;
    LinearLayout linBase;     
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        super.setContentView(R.layout.base_layout);        
        linBase = (LinearLayout)findViewById(R.id.linBase);
    }
    @Override
    public void setContentView(int id) {
        LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(id, linBase);
    }
}

and

public class SomeActivity extends BaseActivity {    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.some_layout);
       //rest of code
    }
}

The only thing I noticed so far was that when requesting a progress bar (requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)) this needs to be done before calling super.onCreate. I think this is because nothing can be drawn yet before calling this function.

This worked great for me and hopefully you will find this useful in your own coding.

Upvotes: 1

skyman
skyman

Reputation: 2472

I've had the same problem and solved it using ActivityGroup. I suppose that menu items will move user to another activity, so with the same menu in every activity closing application with BACK button can be almost impossible (after some time user will have to go back through all activities he had ever seen).

I haven't found any good tutorials in english so have written mine some time ago (it's somewhat too short and in polish only, but Google Tranlslated version should be understandable) check this

You can also check how the TabHost works

Upvotes: 0

Christopher Orr
Christopher Orr

Reputation: 111565

You could have each of your Activities extend a common base class which has a onCreateOptionsMenu method which inflates the menu from the same XML each time. Though as you can't have multiple inheritance, this may be tricky when you want to have plain activities and list activities, for example.

Another way would be to have a Util class where you have a method like setupMenu(Menu) which each of your Activities can call if you're doing some more complex menu setup.

In terms of the XML UI layout for each of your Activities, you can include a common banner by using the <include/> tag.

Upvotes: 8

Related Questions