Remove gap at the top of fragment when actionBar is hidden - android

I have a problem with a gap being displayed at the top of a fragment when I do actionBar.hide() (btw I don't see this gap on the simulator, only on a real device). I tried this answer and that one, but nothing works. This fragment is inside MainActivity, which loads different fragments when I click on the menu. It's the only fragment where the actionBar (which is custom) should be hidden, so I cannot set the fullscreen flag on MainActivity onCreate() method. How can I redraw the view so the actionBar empty space disappear?
Here is how it looks now:
This is the mainActivty where I load the fragment:
public void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
showActionBar();
if(Build.VERSION.SDK_INT >= 21){
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
setContentView(R.layout.activity_main);
setUpMenu();
}
On click on the menu, I change the fragment accordingly:
#Override
public void onClick(View view) {
if (view == itemProfile){
mActionBar.hide();
ProfileFragment profile = new ProfileFragment();
profile.setUser(AppController.getInstance().getLoggedInUser());
changeFragment(profile);
}
}
And the custom actionBar
private void showActionBar() {
mActionBar = getSupportActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#ffffff"));
mActionBar.setBackgroundDrawable(colorDrawable);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
mLogo = (ImageView) mCustomView.findViewById(R.id.logo);
ImageButton menuButton = (ImageButton) mCustomView
.findViewById(R.id.menu);
menuButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (resideMenu.isOpened()) {
resideMenu.closeMenu();
} else {
resideMenu.openMenu();
}
}
});
ImageButton mapButton = (ImageButton) mCustomView
.findViewById(R.id.map);
mapButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), MapsActivity.class);
startActivity(i);
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
My theme:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:colorPrimaryDark">#android:color/black</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
My mainActivity layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:fitsSystemWindows="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/main_fragment">
</FrameLayout>
</LinearLayout>
</FrameLayout>
</FrameLayout>
My custom actionBar
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/transparent">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/transparent">
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="7dp"
android:fontFamily="sans-serif-light"
android:text="#string/app_title"
android:textSize="#dimen/textsize_large"
android:textColor="#android:color/black"
android:layout_gravity="center"
android:visibility="gone"/>
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="7dp"
android:layout_gravity="center"
android:src="#drawable/logo"
android:background="#color/transparent" />
<ImageButton android:src="#drawable/earth"
android:background="?attr/actionBarItemBackground"
android:layout_width="?attr/actionBarSize"
android:layout_height="20dp"
android:scaleType="centerInside"
android:id="#+id/map"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="-15dp"
android:tint="#color/black"/>
<ImageButton android:src="#drawable/titlebar_menu_selector"
android:background="?attr/actionBarItemBackground"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:id="#+id/menu"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="-15dp"
android:layout_gravity="left|center_vertical"
android:tint="#color/black"/>
</FrameLayout>
</RelativeLayout>
Any help would be great, I've been fighting with it for days now and I really don't know what to do...

If you have created Bottom Navigation Activity and you don't want the hidden ActionBar's blank gap, Check the Host activities layout XML file,
delete the line mentioned below
android:paddingTop="?attr/actionBarSize"

replace
mActionBar.hide();
with
mActionBar.setVisibility(View.GONE);
You need to add Toolbar to your Activity layout. Find the code snippet below for simple Toolbar Layout.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#2196F3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
Apply the theme to Activity. Here in this step you need to apply the theme which we have created in step-1 to your activity. This can be done, by using android:theme attribute in your application AndroidManifest.xml.
<activity
android:name="com.javatechig.sample.MyActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
</activity>
Now you are almost ready. You just need to instantiate the Toolbar and add it to your activity by using setSupportActionBar(Toolbar) method.
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
public class MyActivity extends ActionBarActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Set a toolbar to replace the action bar.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
Visit the below link to know more..
Using Toolbar as ActionBar

Put this line on your toolbar help me clear the gap, don't know will it works on other components like ur Relative layout. Hope helpful :)
android:fitsSystemWindows="true"
like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true" //add this line
android:background="#color/transparent">

Related

How to align Button in a custom ToolBar to Right

I have made a custom ToolBar but recently decided to include a button on it as a home button. I have also defined it in java and it works when a button is clicked but the ToolBar title text shows half-way, i have tried to alignParentRight but it wouldn't work.
Please help me
Below are my codes and what i have tried.
TIps_4.java Activity
public class Tips_4 extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
Button Tips3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the start page passed to us or default to first page
Intent mIntent = getIntent();
int startPage = mIntent.getIntExtra("startPage", 0);
setContentView(R.layout.activity_tips_4);
toolbar= findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
Button toolbar_btn = (Button)findViewById(R.id.toolbarButton);
toolbar_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent toolbarIntent = new Intent(Tips_4.this, MainActivity.class);
toolbarIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Tips_4.this.startActivity(toolbarIntent);
setSupportActionBar(toolbar);
}
});
ViewPager viewPager= (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(new SimpleFragmentPageAdapter(getSupportFragmentManager(),this));
// set the current page to the start page
viewPager.setCurrentItem(startPage);
//Attach the page change listener inside the activity
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
// This method will be invoked when a new page becomes selected
#Override
public void onPageSelected(int position) {
Toast.makeText(Tips_4.this, "Selected Maths Page:" + position, Toast.LENGTH_SHORT).show();
}
// This method will be invoked when the current page is scrolled
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetpixels) {
}
// Called when the scroll state changes:
//SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
#Override
public void onPageScrollStateChanged(int state) {
}
});
tabLayout= (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
}
}
toolbar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:subtitleTextColor="#FF9800"
android:background="#color/colorPrimary">
<Button
android:id="#+id/toolbarButton"
android:layout_width="60dp"
android:layout_height="50dp"
android:foreground="#drawable/ic_home_24dp"
android:layout_marginLeft="300dp"
android:background="#000"/>
</androidx.appcompat.widget.Toolbar>
This is what i did that refused to work
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<Button
android:id="#+id/toolbarButton"
android:layout_width="54dp"
android:layout_height="50dp"
android:background="#056359"
android:layout_gravity="end"
android:padding="11dp"
android:foreground="#drawable/ic_home_24dp" />
</RelativeLayout>
This is the screenshot
Here is the complete code for a toolbar with button at the end
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:subtitleTextColor="#FF9800"
android:background="#color/colorPrimary">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mathematics Pages"
android:textSize="23sp"
android:textColor="#ffffff"
android:textStyle="bold"
android:gravity="right"/>
<Button
android:id="#+id/toolbarButton"
android:layout_width="60dp"
android:layout_height="50dp"
android:foreground="#drawable/ic_home_24dp"
android:layout_alignParentEnd="true"
android:background="#000"
android:layout_alignParentRight="true" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
You can use a relative layout and set its width and height to match_parent now add the button in you're relative layout and set alignParentEnd to true
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:subtitleTextColor="#FF9800"
android:background="#color/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/toolbarButton"
android:layout_width="60dp"
android:layout_height="50dp"
android:foreground="#drawable/ic_home"
android:layout_alignParentEnd="true"
android:background="#000"/>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
If you just want a icon in you're button you can use a ImageButton that works same as a normal button. And If you're icon is of fixed size say 24 X 24 then you can make the button as wrap_content in both width and height.

How is this layout implemented? [duplicate]

I want to create a toolbar like the following image as proposed in the material design guidelines:
I can achieve this via using the toolbar with an relative layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Toolbar/>
<LinearLayout android:layout_marginTop="-17dp" />
</RelativeLayout>
I am not sure this is the correct way or not. Any help is appreciated.
Thanks Gabriele for all the help. Here is working code:
Activity :
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mai);
Toolbar toolbar = (Toolbar) findViewById(R.id.card_toolbar);
if (toolbar != null) {
// inflate your menu
toolbar.inflateMenu(R.menu.main);
toolbar.setTitle("Card Toolbar");
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
return true;
}
});
}
Toolbar maintoolbar = (Toolbar) findViewById(R.id.toolbar_main);
if (maintoolbar != null) {
// inflate your menu
maintoolbar.inflateMenu(R.menu.main);
maintoolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
return true;
}
});
}
}
}
Layout XML File:
<RelativeLayout 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" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="#dimen/action_bar_size_x2"
android:background="#android:color/holo_orange_dark"
android:minHeight="?attr/actionBarSize" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar_main"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="#dimen/action_bar_size"
android:orientation="vertical" >
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="#+id/card_toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/action_bar_size"
android:background="#android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/app_name"
android:textSize="24sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
Make sure your activity theme is extending Theme.AppCompat.Light.NoActionBar.
Here is what it will look like:
Few things to note:
If you are using card elevation then you need to alter the margin top so that your card aligns with the main toolbar.
I am still seeing 1-2 pixel margin between bottom of main toolbar and card toolbar. Not sure about what to do in this case. For now, I am aligning it manually.
You can obtain it using a Toolbar as ActionBar, a CardView and another Toolbar(standalone) inside the Card.
For the Toolbar standalone inside a Card you can use something like this:
<android.support.v7.widget.CardView>
<LinearLayout>
<Toolbar android:id="#+id/card_toolbar" />
//......
</LinearLayout>
</CardView>
Then you can inflate your menu to obtain the icon actions.
Toolbar toolbar = (Toolbar) mActivity.findViewById(R.id.card_toolbar);
if (toolbar != null) {
//inflate your menu
toolbar.inflateMenu(R.menu.card_toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
//.....
}
});
}
For the toolbar used as action bar and the main layout you can use:
option1:
<RelativeLayout>
<toolbar/> //Main toolbar
<View
android:background="#color/primary_color"
android:layout_height="#dimen/subtoolbar_height"/>
<CardView /> //described above
</RelativeLayout>
Option2: An extended toolbar (as actionbar) and a CardView as described above playing with margins.

Setting a toolbar in a List Activity

I have a ListActivity that I want to use the toolbar in but I get the error can't resolve getSupportToolbar. Everything works fine in my main activity but here I get errors.
found this excerpt on an Android dev blog post. You have to use the toolbar as a standalone object and add your own items.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blah);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle the menu item
return true;
}
});
// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.your_toolbar_menu);
}
According to me, you haven't set the toolbar in your activity. First you have to set the app theme to#style/Theme.AppCompat.Light.NoActionBar,
Then you have to set the custom toolbar in your MainActivity like
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar_main_activity);
setSupportActionBar(toolbar);
TextView toolbar_title = (TextView) toolbar.findViewById(R.id.title_toolbar);
getSupportActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
I hope this might solve your problem.
This is how I had set up a toolbar in my List Activity. I will post the layout file details
activity1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_activity" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list" />
</LinearLayout>
toolbar_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/toolbar_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:minHeight="?attr/actionBarSize"
android:background="#color/windowBackground"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<Button
android:id="#+id/buttonReturn"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:background="#drawable/arrow_left" />
</RelativeLayout>

Button beneath navigation drawer not working when pressed

Yet, when the button is placed in the hierarchy so that it can be seen on top of the navigation drawer, the button functions properly. However, the button should be hidden behind the navigation drawer when it is slid out, so this is not desirable.
Below is the code from MainActivity.java
public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks {
public ProgressDialog progBar;
public final static boolean DEBUG = false;
public final static String TAG = "AppGetter";
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
ImageButton cart_button = (ImageButton) findViewById(R.id.button2);
cart_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
start_request();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public void onNavigationDrawerItemSelected(int position) {
Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
}
#Override
public void onBackPressed() {
if (mNavigationDrawerFragment.isDrawerOpen())
mNavigationDrawerFragment.closeDrawer();
else
super.onBackPressed();
}
public void start_request()
{
String pkg = getPackageName();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName(pkg,pkg+".RequestActivity"));
startActivity(intent);
if(DEBUG)Log.v(TAG,"Intent intent: "+intent);
}
}
I am assuming the issue lies within the class above, but for completion's sake, I pasted the two XML files of interest below.
activity_main.xml As you can see, the ImageButton is currently "above" the navigation drawer in hierarchy so as to make it be covered by the navigation drawer. Moving the ImageButton "below" makes the button work properly, but causes it to appear on top of the navigation drawer (and not tinted like the rest of the layout).
<FrameLayout
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">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fontify="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#color/myPrimaryColor">
<View
android:id="#+id/block1"
android:layout_height="240dp"
android:layout_width="match_parent"
android:layout_below="#+id/toolbar_actionbar"
android:background="#drawable/block_primary"
/>
<TextView
android:id="#+id/title1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/toolbar_actionbar"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:text="#string/title"
android:textColor="#color/white"
android:textSize="34sp"
android:fontFamily="sans-serif"
/>
<include
android:id="#+id/toolbar_actionbar"
layout="#layout/toolbar_default"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<ImageButton
android:id="#+id/button2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="268dp"
android:src="#drawable/ic_chevron_up"
android:background="#drawable/fab_simple"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="#+id/toolbar_actionbar"
>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment
android:id="#+id/fragment_drawer"
android:name="com.onepersonco.iconrequestbase.NavigationDrawerFragment"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
fragment_navigation_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!-- Provides a margin at the top of the navigation drawer. -->
<View
android:id="#+id/navWhiteSpace1"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#color/myNavigationDrawerBackgroundColor"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_below="#+id/navWhiteSpace1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:scrollbars="vertical"
android:scrollbarDefaultDelayBeforeFade="0"
android:scrollbarFadeDuration="0"
android:overScrollMode="never"
android:focusable="true"
android:background="#color/myNavigationDrawerBackgroundColor"/>
</RelativeLayout>
I ran into the same issue when attempting to follow various navigation drawer tutorials online.
What worked for me was using Mike Penz's MaterialDrawer library on GitHub. He has a very simple tutorial in the "readme" file found on the bottom of the page.
Hopefully someone else with a better understanding of Java can explain why your code failed.

How to create a card toolbar using appcompat v7

I want to create a toolbar like the following image as proposed in the material design guidelines:
I can achieve this via using the toolbar with an relative layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Toolbar/>
<LinearLayout android:layout_marginTop="-17dp" />
</RelativeLayout>
I am not sure this is the correct way or not. Any help is appreciated.
Thanks Gabriele for all the help. Here is working code:
Activity :
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mai);
Toolbar toolbar = (Toolbar) findViewById(R.id.card_toolbar);
if (toolbar != null) {
// inflate your menu
toolbar.inflateMenu(R.menu.main);
toolbar.setTitle("Card Toolbar");
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
return true;
}
});
}
Toolbar maintoolbar = (Toolbar) findViewById(R.id.toolbar_main);
if (maintoolbar != null) {
// inflate your menu
maintoolbar.inflateMenu(R.menu.main);
maintoolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
return true;
}
});
}
}
}
Layout XML File:
<RelativeLayout 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" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="#dimen/action_bar_size_x2"
android:background="#android:color/holo_orange_dark"
android:minHeight="?attr/actionBarSize" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar_main"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="#dimen/action_bar_size"
android:orientation="vertical" >
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="#+id/card_toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/action_bar_size"
android:background="#android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/app_name"
android:textSize="24sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
Make sure your activity theme is extending Theme.AppCompat.Light.NoActionBar.
Here is what it will look like:
Few things to note:
If you are using card elevation then you need to alter the margin top so that your card aligns with the main toolbar.
I am still seeing 1-2 pixel margin between bottom of main toolbar and card toolbar. Not sure about what to do in this case. For now, I am aligning it manually.
You can obtain it using a Toolbar as ActionBar, a CardView and another Toolbar(standalone) inside the Card.
For the Toolbar standalone inside a Card you can use something like this:
<android.support.v7.widget.CardView>
<LinearLayout>
<Toolbar android:id="#+id/card_toolbar" />
//......
</LinearLayout>
</CardView>
Then you can inflate your menu to obtain the icon actions.
Toolbar toolbar = (Toolbar) mActivity.findViewById(R.id.card_toolbar);
if (toolbar != null) {
//inflate your menu
toolbar.inflateMenu(R.menu.card_toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
//.....
}
});
}
For the toolbar used as action bar and the main layout you can use:
option1:
<RelativeLayout>
<toolbar/> //Main toolbar
<View
android:background="#color/primary_color"
android:layout_height="#dimen/subtoolbar_height"/>
<CardView /> //described above
</RelativeLayout>
Option2: An extended toolbar (as actionbar) and a CardView as described above playing with margins.

Categories

Resources