1

I need to keep my tabbed toolbar in my application removing the actionbar. This is how it looks like now: enter image description here

But I want it to look like this:

enter image description here

My code xml is:

<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 it? Thanks in advice

1
  • try set visibility="gone" on Toolbar element in layout Commented Nov 13, 2015 at 12:07

3 Answers 3

5

Simply add this in your MainActivity this will hide the text of your toolbar

getSupportActionBar().setTitle(""); 

To remove the toolbar you can set it's visibility to gone like

android:visibility="gone" 
Sign up to request clarification or add additional context in comments.

3 Comments

toolbar, action bar they are different name of the same thing: i think so
the blue part with "AppName" is the toolbar and these line of code will hide it and it works for me atleast
thanks, i thought the toolbar had the tabs i need inside, first time using it i copied from internet but i got lost.. thanks a lot :)
2

Here are your options:

1. It looks like you don't need Toolbar here at all. You can remove it from your xml file and add this line

getSupportActionBar().hide(); 

to your MyActivity.java instead of

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

2. If you want to keep the Toolbar in case of using it later, you can hide it in either 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 your MyActivity.java after setting Toolbar as support action bar

if (toolbar != null) { setSupportActionBar(toolbar); } toolbar.setVisibility(View.GONE); 

1 Comment

thanks for the reply, this was ok, i set the other as solution only because he wrote first
1

Add following code to disable actionbar and enable toolbar in 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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.