patternjavaMinor
Android - Share ToolBar across whole application
Viewed 0 times
shareandroidwholeapplicationtoolbaracross
Problem
I have 3 ToolBars that I use across my whole application
The first one is just a ToolBar with a close button.
The second one is a ToolBar with a close and delete icon.
The third one is a ToolBar with a close and home icon.
Here is my case, I use this ToolBar in many places, and I dont want to use Fragment for some reasons so I am forced to use Activity.
For usability, I created methods to handle their functions, but I am sure I can do something to make this easier as I repeat this code in each activity.
toolbar_layout.xml
toolbar_layout_delete_icon.xml
toolbar_layout_home_icon.xml
java method that handles the ToolBar :
I need to minimize my code as much as possible, and any help with @+id names will be appreciated.
The first one is just a ToolBar with a close button.
The second one is a ToolBar with a close and delete icon.
The third one is a ToolBar with a close and home icon.
Here is my case, I use this ToolBar in many places, and I dont want to use Fragment for some reasons so I am forced to use Activity.
For usability, I created methods to handle their functions, but I am sure I can do something to make this easier as I repeat this code in each activity.
toolbar_layout.xml
toolbar_layout_delete_icon.xml
toolbar_layout_home_icon.xml
java method that handles the ToolBar :
private void setupToolbar() {
Toolbar mytoolbar = (Toolbar) findViewById(R.id.mytoolbar);
TextView toolbarTitle = (TextView) mytoolbar.findViewById(R.id.toolbarTitle);
toolbarTitle.setText(getResources().getString(R.string.submit_claim));
ImageView closeButton = (ImageView) mytoolbar.findViewById(R.id.CloseImageView);
closeButton.setOnClickListener(v -> finish());
ImageView HomeButton = (ImageView) mytoolbar.findViewById(R.id.HomeImageView);
HomeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent = new Intent(PolicyAndContactDetailActivity.this , MainActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mIntent);
finish();
}
});
}I need to minimize my code as much as possible, and any help with @+id names will be appreciated.
Solution
Create a
BaseActivity
Main.xml (Base activity layout)
Now every Activity extends BaseActivity instead of AppCompatActivity or ActionBarActivity so you can get access to the Toolbar in every Activity.
Your MainActivity
Base Activity with ToolBar and let all your activity extend the BaseActivity like below code I've given :BaseActivity
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}Main.xml (Base activity layout)
Now every Activity extends BaseActivity instead of AppCompatActivity or ActionBarActivity so you can get access to the Toolbar in every Activity.
Your MainActivity
public class YourActivity extends BaseActivity{ //your code }Code Snippets
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:theme="@style/toolbarTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green"
android:minHeight="?attr/actionBarSize" />public class YourActivity extends BaseActivity{ //your code }Context
StackExchange Code Review Q#144760, answer score: 3
Revisions (0)
No revisions yet.