I want write code once and use in different activities. I have created a Base Activity class
for that . Also the header of all the layouts in different activiti
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);
}
}
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.
For each activity layout xml you include that component
-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.
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.
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.