I would like to have an Activity that stars with the drawer layout open at the beginning. I have tried simply putting
mDrawerLayout.openDrawer(gravity.END());
in various places in my code but regardless the items become unclickable until I close and re-open the drawer.
Has anyone had any luck with this? It is mostly for user experience reasons to add this feature.
My Drawer Layout has a list view in it, which clicks change my RecyclerView in the activity.
Here is my XML file for anyone who might need it.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:orientation="vertical">
<!--android:elevation="4dp"-->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The navigation drawer -->
<ListView
android:id="#+id/right_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:alpha="0.8"
android:background="#color/colorPrimary"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:alpha="1"
android:background="?attr/colorAccent"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat" />
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_below="#+id/my_toolbar2">
<android.support.v7.widget.RecyclerView
android:id="#+id/listview2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#color/colorPrimary"
android:dividerHeight="8dp" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:src="#drawable/ic_add_circle_outline_white_24dp"
android:visibility="visible"
app:elevation="24dp"
app:fabSize="auto"
app:layout_anchor="#+id/listview2"
app:layout_anchorGravity="bottom|end"
app:rippleColor="#FFF" />
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Try to do like this in your onCreate(Bundle savedInstanceState) of Activity:
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout.postDelayed(new Runnable() {
#Override
public void run() {
if (mDrawerLayout != null) {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
}
}, 200);
Related
I am very new to Android Development and have been trying to get this to work for a while now. The basic structure of my Activity is two fragments between which you can switch by swiping. Both fragments have different layouts and contain different buttons and I am in the process of getting the buttons to work.
When I try to set the OnClickListener for any button in onCreate(), the App no longer starts. Here is my onCreate() Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
imgButton = (ImageButton)findViewById(R.id.imageButton);
OnClickListener btnListener = new OnClickListener() {
#Override
public void onClick(android.view.View view) {
//Do Something
}
};
imgButton.setOnClickListener(btnListener);
}
The App Crashes with a null object reference, saying that imgButton's value is null. I certainly am doing this wrong, but I wasn't able to find the correct way of setting up button listeners for two fragments. Can anybody point me in the right direction?
EDIT: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.dansoft.daily_surprise.MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
fragment_main.xml:
<RelativeLayout 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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/background"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.dansoft.daily_surprise.MainActivity$PlaceholderFragment">
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:src="#drawable/box_1"
android:background="#android:color/transparent"
/>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/img_1"
/>
</RelativeLayout>
fragment_sec.xml:
<RelativeLayout 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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/background"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.dansoft.daily_surprise.MainActivity$PlaceholderFragment">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/img_2"
/>
<Button
android:id="#+id/button"
android:layout_width="290dp"
android:layout_height="220dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:background="#android:color/transparent"
/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="151dp"
android:text="22"
android:textColor="#android:color/black"
android:textSize="200sp" />
</RelativeLayout>
Your should track button clicks on your fragments, not on your activities.
Your fragments are not laid-out after onCreate. So your findViewById returns null.
Move your listener code to your fragments.
I'm having trouble implementing HomeUpEnabled when using CollapsingToolbarLayout, I can see the icon but noting happens when I press it.
Here is the CollapsingToolbarLayout
avivity.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">
<ImageView
android:id="#+id/header_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
Activity.java
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I have declared the activity in my manifest. Any help would be appreciated.
----------------------------EDIT-----------------------------------
THIS IS WHAT FIXED IT:
I don't know if it is correct, but it works.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
And in my xml I changed the background of the toolbar to:
android:background="?android:attr/selectableItemBackground"
to get the ripple effect on the icon.
Hope this helps someone.
Overrited Following method and specify your action in method block with menu id as home :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
You have to set the parent activity name in your manifest.xml
Your code is correct but only displays the back arrow on your toolbar, you have to tell your activity which is the activity to come back to.
<activity android:name="YourActivity" android:parentActivityName="YourParentActivity" />
Ok, Create 3 different xml file for activity_main, app_bar and content_main
app_bar
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#color/colorPrimaryDark"
app:contentScrim="?attr/colorPrimaryDark"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
app:tabIndicatorHeight="5dp"
android:background="#color/material_blue_grey_800"
app:tabIndicatorColor="#color/colorPrimaryDark"
app:tabSelectedTextColor="#color/colorPrimaryDark"
app:tabGravity="fill"
app:tabMode="fixed"/>
</android.support.design.widget.CollapsingToolbarLayout>
content_main
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="MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v4.view.ViewPager
android:id="#+id/tabs_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
activity_main
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"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top"
android:background="#color/colorPrimaryDark"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:title="" />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/drawerLayout">
<include
layout="#layout/app_bar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/mystuff"
app:menu="#menu/drawermenu" />
</android.support.v4.widget.DrawerLayout>
I have been implementing a FloatingActionButton but somehow hasn't been responding at all. There's no error, nothing happens.
Here is respective Java and xml code:
I have replaced the code in Click event with a Log Event
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("ABCD","ABCD");
}
});
activity_main.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"
android:fitsSystemWindows="true"
tools:context="edu.ahduni.seas.gyapak.MainActivity">
<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>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:clickable="true"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
content_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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/textlayout"
tools:context="edu.ahduni.seas.gyapak.MainActivity">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:padding="10dp" />
</LinearLayout>
Lets put the Toolbar and AppbarLayout below the fab, and see what happens:
<?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"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<!-- include your layout here -->
</LinearLayout>
<!-- when the screen is loading, in your case optional -->
<LinearLayout
android:id="#+id/progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<include layout="#layout/layout_loading"></include>
</LinearLayout>
<!-- when the screen is empty no data, in your case optional -->
<LinearLayout
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<include layout="#layout/layout_empty"></include>
</LinearLayout>
<!-- a fab -->
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
app:backgroundTint="#009688"
style="#style/FabStyle"
android:layout_gravity="bottom|end"
android:layout_marginBottom="20dp"
android:visibility="invisible" />
<!-- the toolbar layout -->
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:title="#string/app_name"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
First check the fab click without the content, then try it with the content in content linear layout.
I modified your layout to this (removed all the content) and the click is
working
<?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"
android:fitsSystemWindows="true">
<!--<include layout="#layout/content_main" />-->
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="bottom|end"
android:clickable="true"
android:src="#android:drawable/ic_dialog_email"
app:backgroundTint="#FF0000"
app:rippleColor="#FFF" />
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
So your grid view must be taking all the click passthroughs, let me check and get back again.
[Edit]
It is not GridView after all. There was one unnecessary line in the code.
Voila found your problem, remove this line on your onPostExecute() of ParseActivity
MainActivity.this.setContentView(R.layout.activity_main);
near Line number 286 probably.
this sets the layout again and doesn't register the on click listener, thats why when it was in the layout file (DownloadZip), it was working.
If it is necessary for you then reinit the layout i.e. find the fab and put another setOnClickListener. you could put those code in a method and call them on the places you need to reinit layout.
Replace the code of activity_main with these :
<?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"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"></android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="20dp"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
The code is right. Are you sure that you are override
protected void onCreate(#Nullable Bundle savedInstanceState)
instead of
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
?
Example:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_incoming_call);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabCall);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("mylog" , "it happens...");
}
});
}
Implementing a NavigationView drawer - I am able to generate the drawer and see it, but cannot close it with swiping. Additionally, the NavigationItemSelectedListener does not appear to be set up correctly, as I cannot detect click events on the items.
MainActivity.java
private NavigationView navigationView;
private DrawerLayout navDrawerLayout;
private ListView navDrawerList;
private ArrayList<NavDrawerItem> navDrawerItemList;
private ActionBarDrawerToggle navDrawerToggle;
private void setupNavDrawer() {
this.navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Utility.showDebugToast(String.valueOf(item.getItemId()));
if(item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
}
navDrawerLayout.closeDrawers();
return true;
}
});
this.navDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
this.navDrawerList = (ListView) findViewById(R.id.left_drawer);
this.navDrawerItemList = new ArrayList<>();
this.navDrawerItemList.add(new NavDrawerItem("NAME", NavDrawerItemType.NAME));
this.navDrawerItemList.add(new NavDrawerItem("Create Mesh", NavDrawerItemType.CREATE_MESH));
this.navDrawerList.setAdapter(new NavDrawerAdapter(this, R.layout.drawer_item, R.id.drawer_tab_text, this.navDrawerItemList));
this.navDrawerToggle = new ActionBarDrawerToggle(this, navDrawerLayout, R.string.accept, R.string.accept) {
#Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
#Override
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
}
};
this.navDrawerLayout.addDrawerListener(navDrawerToggle);
navDrawerToggle.syncState();
}
activity.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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"
android:background="#drawable/purpose_background" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/appbar_grey"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
/>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
style="#style/LobbyTabLayout"
android:layout_width="match_parent"
android:layout_height="44dp"
app:layout_scrollFlags="scroll|enterAlways"
app:tabMode="scrollable"/>
<LinearLayout
android:id="#+id/search_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_scrollFlags="scroll|enterAlways" >
<EditText
android:id="#+id/search_box"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="3"
android:lines="1"
android:textSize="16sp"
android:hint="#string/search_editText_placeholder"/>
<Button
android:id="#+id/action_search_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="#string/search_button"/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_lobby_container">
<LinearLayout
android:id="#+id/lobby_search_area"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Lobby"
android:focusableInTouchMode="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/grid_fragment"/>
</LinearLayout>
<com.nhaarman.supertooltips.ToolTipRelativeLayout
android:id="#+id/mesh_list_tooltip"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/dialog_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"/>
<include android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/spinner_overlay"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/toggle_fragment_button"
android:background="#color/background_white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="20dp"
android:alpha=".9"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<ListView android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#000"
android:alpha="0.7"
/>
</android.support.design.widget.NavigationView>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
drawer_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_tab"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="76dp"
android:layout_height="76dp"
android:src="#drawable/logo_icon_statusbar"
app:border_color="#FF000000"
android:layout_marginLeft="24dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginStart="24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingBottom="4dp"
android:id="#+id/drawer_tab_text"
android:layout_alignLeft="#+id/profile_image"
android:layout_alignStart="#+id/profile_image" />
</LinearLayout>
Why can't I slide the Drawer in and out?
With regards to the drawer closing behaviour, I expect this is to do with the structure of your activity.xml.
As per the guidance here:
To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent. Add drawers as child views after the main content view and set the layout_gravity appropriately.
So this is saying that in your case where you have a content section and one drawer, the DrawerLayout should have two children; first the content, then the drawer. In your xml currently, the DrawerLayout only has one child, everything is in the CoordinatorLayout.
Try moving your NavigationView outside of the CoordinatorLayout so your activity looks something like this:
<DrawerLayout>
<CoordinatorLayout>
content
</CoordinatorLayout>
<NavigationView>
drawer stuff
</NavigationView>
</DrawerLayout>
I'm build an app that contain two tabhost (top and botton) and in one tab, I want to put a Google Map through the Google API. All works well but the map does not display correctly. You can see better in this picture:
My code is:
fragment_mapa.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
MapaFragment.java
public class MapaFragment extends Fragment {
public MapaFragment(){}
public GoogleMap mapa;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mapa, container, false);
if (container != null) {
container.removeAllViews();
}
GoogleMap map = ((SupportMapFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
((MainActivity) getActivity()).setBar();
//Asigno el nombre de la vista e integro tabhost-sliding
((MainActivity) getActivity()).setTitle("Mapa");
((MainActivity) getActivity()).integrationMenu();
return rootView;
}
public void onDestroyView() {
super.onDestroyView();
Log.d("DESTROY", "onDestroyView");
Fragment f = getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map);
if (f != null) {
getActivity().getSupportFragmentManager()
.beginTransaction().remove(f).commit();
}
}
}
And my activity_main.xml that contain the two tabhost and a framelayout that call the fragments:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Estructura de los dos tabhost -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TABHOST SUPERIOR -->
<RelativeLayout
android:id="#+id/l1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="right"
android:layout_above="#+id/l2"
android:layout_alignParentTop="true">
<android.support.v4.app.FragmentTabHost
android:id="#+id/tabhost_sup"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_weight="0"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0"/>
</ScrollView>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</RelativeLayout>
<!-- TABHOST INFERIOR -->
<RelativeLayout
android:id="#+id/l2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_weight="1"
android:gravity="right"
android:layout_alignParentBottom="true"
android:background="#android:color/background_light" >
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/RelativeLayout1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginBottom="0dp"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</RelativeLayout>
</RelativeLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
<ListView
android:id="#+id/right_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
</android.support.v4.widget.DrawerLayout>
Removing scrollview is not convinient solution.Instead of it try android:FillViewPort="True" for Scrollview in your layout solve your problem for all screen where you may require scroll for your activity content.Hope this helps you!!
SOLUTION (by Hardik):
Remove the scrollview in my activity_main.xml and it works well.
Use hierarchiviewer of android tools to check layout values.
For the future:
If you have a ScrollView then elements must specify their height (width for horizontal scroll view) otherwise what height should they have, infinite?
I also had an issue where the exact thing happened when placing the map fragment inside a Relative Layout that had height = Wrap_Content, with minimum height.
Setting a constant height resolved the issue, same idea.
came across this same issue but my map fragment is inside a scrollview and it needs to have a scrollview since there are also other views aside from the map. if it only contains the map, then yes scrollview is not needed.