Same Toolbar for all activities - Something I'm missing

Multi tool use
Multi tool use


Same Toolbar for all activities - Something I'm missing



What I want: I want to keep same toolbar for all my activities.



What I have tried: I made a BaseActivity and I'm having toolbar in it. I'm extending other activities to BaseActivity.



What is the issue: When I run the app. I'm not finding toolbar to other activities.



Below is my code.



BaseActivity.java


public class BaseActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()){
case R.id.menu_lang_english:
Toast.makeText(this, "English", Toast.LENGTH_SHORT).show();
break;

case R.id.menu_lang_french:
Toast.makeText(this, "French", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}



activity_base.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BaseActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>



MainActivity.java


public class MainActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}



activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

</LinearLayout>



styles.xml


<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />





AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>




<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BaseActivity"
android:label="@string/title_activity_base"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>





OutPut I'm Getting



Output Image





Yeah you are . You have called setContentView in both classes . This will override the Base Activity one .
– ADM
Jun 30 at 9:36


setContentView





You could simply use one Activity. And a bunch of Fragments.
– Kling Klang
Jun 30 at 9:39





If I don't call setContentView for MainActivity how would I suppose to add widgets(e.g TextView, EditText) to activity_main.xml file.@ADM
– Maulik Dodia
Jun 30 at 9:41


setContentView


MainActivity


activity_main.xml





@KlingKlang I can't. My project requirement is like that. This project I made for testing purpose.
– Maulik Dodia
Jun 30 at 9:43





If you want same toolbar in all activities then simply use actionbar. And instead of this Theme.AppCompat.Light.NoActionBar use Theme.AppCompat.Light.DarkActionBar in your AppTheme.
– HeisenBrg
Jun 30 at 9:43


toolbar


actionbar


Theme.AppCompat.Light.NoActionBar


Theme.AppCompat.Light.DarkActionBar


AppTheme




2 Answers
2



your screen can only have one layout at the same time MainActivity setContentView () is Override .



you can use Fragment instead of Activtiy



One way to do this is to add a Framelayout in your activity_base.xml. This layout will be a placeholder to put your specific screens. Hence all your specific screens will have the same toolbar and appearance.



Now from your concrete screens, you will NOT call the setContentView(). Instead call something like putContentViewInTemplate(R.layout.activity_main.xml);. This will replace the content_frame in template with actual screen. Here is a simple implementation,


setContentView()


putContentViewInTemplate(R.layout.activity_main.xml);


protected void putContentViewInTemplate(int id) {
mRootTemplate = (ViewGroup)findViewById(R.id.content_frame);
mRootTemplate.addView(getViewFromLayout(id), 0);
}



Note that you are plugging the layout of your screen to the main template which is your activity_base.xml. Of course, you need to add the content_frame in activity_base.xml.


activity_base.xml


activity_base.xml


<FrameLayout app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

tiOp1,22KoXGbal4MAz7voiKheC GfvtRay,tP uUg0kMulB0ncUW9kR1YNyhqV4B7d4iWYwcRRvSEsyM hcO7aPWqVeM J44qa
BKbcNzIXk,bnuRQYqtjY2sX8 IwhK U HH7an,gmnBq,9Q4es4

Popular posts from this blog

Delphi Android file open failure with API 26

.

Amasya