Android removes the toolbar containing the control panel

I need my tabbed toolbar in my application to remove the action bar. Here's what it looks like now: enter image description here

But I want it to look like this:

enter image description here

My xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary"/> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content"/> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> 

This is my .java:

 public class MyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_layout); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); ViewPager viewPager = (ViewPager) findViewById(R.id.pager); if (toolbar != null) { setSupportActionBar(toolbar); } viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager())); tabLayout.setupWithViewPager(viewPager); } public class SectionPagerAdapter extends FragmentPagerAdapter { public SectionPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: return new myFragment1(); case 1: default: return new myFragment2(); } } @Override public int getCount() { return 2; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "myfragment1"; case 1: default: return "myfragment2"; } } } } 

How can i achieve this? thanks for the consultation

0
java android android-actionbar tabs toolbar
source share
3 answers

Just add this to your MainActivity, it will hide the text of your toolbar

 getSupportActionBar().setTitle(""); 

To remove a toolbar, you can set its visibility as

 android:visibility="gone" 
+5
source share

Here are your options:

1. It doesn't seem like you need a toolbar here at all. You can remove it from your XML file and add this line

 getSupportActionBar().hide(); 

in MyActivity.java instead

 if (toolbar != null) { setSupportActionBar(toolbar); } 

2. If you want to save the toolbar if you use it later, you can hide it in any xml file

 <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" android:background="?attr/colorPrimary"/> 

or in MyActivity.java after setting the toolbar as a support action bar

 if (toolbar != null) { setSupportActionBar(toolbar); } toolbar.setVisibility(View.GONE); 
+2
source share

Add the following code to disable the action bar and enable the toolbar in the following files: -

Add this code to styles.xml

 <!-- 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> <item name="android:windowNoTitle">true</item> <item name="android:windowActionBar">false</item> </style> 

for custom toolbar create layout / toolbar.xml

 <ImageView android:id="@+id/backButton" android:layout_width="48dp" android:layout_height="48dp" android:layout_marginLeft="16dp" android:background="@drawable/back" android:padding="24dp" android:visibility="gone" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/toolbarText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="@color/white" /> </LinearLayout> 

in layout / activity.xml

 <include android:id="@+id/mallToolbar" layout="@layout/toolbar" /> 

in MainActivity.java Add: -

  toolbar = (Toolbar) findViewById(R.id.mallToolbar); setSupportActionBar(toolbar); 

Now you have added a custom toolbar to your Android activity.

+1
source share

All Articles