Common Header in different activities using BaseActivity in android

落爺英雄遲暮 提交于 2019-11-27 13:37:43

For this you have to create one header.xml which will be included in each and every layout for your activities as follows

header.xml

<RelativeLayout>
  <TextView android:id="@+id/txtHeading"
      .... />
</RelativeLayout>

activity_main.xml

<RelativeLayout>
  <!-- include your header here -->
  <include layout="@layout/header"
     ... />

  <!-- Rest of your views -->

</RelativeLayout>

BaseActivity

abstract class BaseActivity extends Activity {
  protected TextView txtHeading;
  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
  }


  protected void setHeading(int resId) {
     if(txtHeading == null)
     txtHeading = findViewById(R.id.txtHeading);
     if(txtHeading != null)
       txtHeading.setText(resId);
  }
}

MainActivity

class MainActivity extends BaseActivity {
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      setHeading(R.string.heading_main);
   }
}

You can put as many views you want and manage common things in BaseActivity or BaseListActivity.

If you are making inheritance with activities and your base activity calls setContentView and after that the real activity calls setContentView the last call will set the layout for activity. So if you are looking for a solution where all activies have the same header component the are 2 ways.

  1. For each activity layout xml you include that component

  2. -You make function for baseActivity e.g. setContent(int layout_id) -You call that with your activity always. -Baseactivity inflates a root view with header and inflates layout_id view to that layout. -Then calls the actual setContentView with that component.

Ankitkumar Makwana

I think you should achieve it using Fragment, this may helps you.

1 - in main.xml, add:

<fragment
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    class="com.package.name.HeaderPanel" />

//remaining is same 

2 - the BaseActivity which extends FragmentActivity:

public class BaseActivityMenu extends FragmentActivity {

    private static final String TAG = Default.class.getName() + " - ";
    private int mResLayoutId;

    public void onCreate(Bundle savedInstanceState, int resLayout){
        super.onCreate(savedInstanceState);
        setContentView(resLayout);
        mResLayoutId = resLayout;
        switch(mResLayoutId){
            // here change with your xml file
            case R.layout.home:
                // set here common control like header textview
                break;
            default:
                break;
        }
    }
}

3 - Now, you can extend your Activity with the BaseActivity. This will allow the Activity to be extended by FragmentActivity:

public class ABCActivity extends BaseActivityMenu {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.home);
    }
}

In code, your base activity is called ExampleActivity, but in your child class you are extending BaseActivityMenu. Don't know where its coming from.

Perhaps change:

public class ABCActivity extends BaseActivityMenu

To this:

public class ABCActivity extends ExampleActivity

Moreover, I would suggest you to define your base activity (ExampleActivity) as an Abstract class. For example:

public abstract class ExampleActivity extends Activity

Doing so will not define your base class as concrete and will make it easier to debug in case of problems.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!