Android navigation drawer menu doesn't work clickable? - android

I have a layout. It contains drawer menu. I want when I open this menu , disable background view. I tried many ways but they didnt work. (setClickable, requestDisallowInterceptTouchEvent) .How can solve this ? Guys I'm about to go crazy.
HomeFragment.java
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
.....
.....
setMenu();
}
private void setDrawerLayout() {
drawerLayout = (DrawerLayout) getActivity().findViewById(R.id.DrawerLayout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, getToolBar(),
R.string.openDrawer, R.string.closeDrawer) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
mDrawerToggle.setDrawerIndicatorEnabled(false);
final Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_menu_button___a, getActivity().getTheme());
mDrawerToggle.setHomeAsUpIndicator(drawable);
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (drawerLayout.isDrawerVisible(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
drawerLayout.openDrawer(GravityCompat.START);
}
}
});
drawerLayout.setDrawerListener(mDrawerToggle); // Drawer Listener set to the Drawer toggled
mDrawerToggle.syncState();
}
activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activityRoot"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="#layout/toolbar"></include>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/appBarLayout"
android:elevation="7dp">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/tabs">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_above="#+id/tabs" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
style="#style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_alignParentBottom="true"
android:background="#drawable/bottom_bar_background"
app:tabMode="fixed"
app:tabPaddingEnd="0dp"
app:tabPaddingStart="0dp"
app:tabTextAppearance="#style/MyCustomTabText" />
</RelativeLayout>
</FrameLayout>
<include
layout="#layout/navigation_drawer_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/tabs"
android:layout_gravity="start"
android:layout_marginBottom="?actionBarSize" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
navigation_drawer_menu.xml
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollIndicators="right">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="#dimen/drawer_menu_padding_right">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#414040"></LinearLayout>
</LinearLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:layout_above="#+id/editShelfButton"
android:layout_marginBottom="#dimen/drawer_menu_background_padding">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/orderLayout">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/orderImage"
android:src="#drawable/menu_header_green"
android:scaleType="fitXY" />
<TextView
android:layout_width="match_parent"
android:layout_height="53dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/order"
android:textColor="#android:color/white"
android:id="#+id/orderText"
android:paddingLeft="#dimen/content_title_padding"
android:layout_centerVertical="true"
android:paddingTop="7dp" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/sortTypeList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/drawer_menu_background_padding"
android:layout_marginBottom="#dimen/drawer_menu_background_padding"
android:paddingRight="#dimen/form_padding_low" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/LibraryLayout"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:src="#drawable/menu_header_orange"
android:scaleType="fitXY" />
<TextView
android:layout_width="match_parent"
android:layout_height="53dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/my_library"
android:textColor="#android:color/white"
android:id="#+id/libraryText"
android:layout_centerVertical="true"
android:paddingLeft="#dimen/content_title_padding"
android:paddingTop="7dp" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/libraryShelfList"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/drawer_menu_background_padding"
android:paddingRight="#dimen/form_padding_low" />
</LinearLayout>
</ScrollView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editShelfButton"
android:background="#c80808"
android:layout_marginRight="#dimen/drawer_menu_padding_right"
android:text="#string/edit_shelves"
android:layout_alignParentBottom="true"
android:textColor="#android:color/white" />
</RelativeLayout>
</RelativeLayout>
fragment_home.xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/homeFrame"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/libraryProductList"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</FrameLayout>

Ok. I found the solution . I put a mask Button into framelayout. When I open menu I make it visible
activity_home.xml
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/tabs">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_above="#+id/tabs" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
style="#style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_alignParentBottom="true"
android:background="#drawable/bottom_bar_background"
app:tabMode="fixed"
app:tabPaddingEnd="0dp"
app:tabPaddingStart="0dp"
app:tabTextAppearance="#style/MyCustomTabText" />
</RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/maskButton"
android:background="#null"
android:visibility="gone" />
</FrameLayout>
HomeFragment.java
mask = (Button) getActivity().findViewById(R.id.maskButton);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, getToolBar(),
R.string.openDrawer, R.string.closeDrawer) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
mask.setVisibility(View.VISIBLE);
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
mask.setVisibility(View.GONE);
}
};

Related

Button does not get clicked on the first attempt

There are a few buttons in my activity which do not get clicked on the first attempt but they get clicked from the second time onwards. It seems that the button gets focus on the first click and gets actually pressed from the second time onwards. What can be the reason that these buttons do not get clicked the first time?
I have noticed that this behaviour is only shown by buttons that are at the very bottom of my activity.
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"
tools:context="com.me.proj.view.activities.MallActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
app:title=" "
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_mall_showcase_images"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<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>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffe5e5e5"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="#layout/info_card" />
<include layout="#layout/photo_card" />
<include layout="#layout/address_card" />
<include layout="#layout/opening_hours_card" />
<include layout="#layout/review_card" /> <!-- Button in this file has the problem-->
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
review_card.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="0.5dp"
card_view:cardMaxElevation="1dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_marginTop="7dp"
android:background="#color/colorWhite"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite">
<TextView
android:id="#+id/txt_rating"
android:background="#drawable/blue_button_background"
android:text="3.8"
android:textColor="#color/colorWhite"
android:layout_marginTop="10dp"
android:textSize="24sp"
android:gravity="center"
android:layout_marginStart="10dp"
android:layout_width="60dp"
android:layout_height="40dp" />
<TextView
android:id="#+id/txt_reviews_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/primary_text"
android:textSize="16sp"
android:layout_toEndOf="#+id/txt_rating"
android:layout_marginStart="20dp"
android:textStyle="bold"
android:paddingTop="1dp"
android:layout_alignTop="#+id/txt_rating"
android:text="Based on 98 reviews"/>
<TextView
android:id="#+id/txt_read_all_reviews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read All Reviews"
android:layout_alignStart="#id/txt_reviews_title"
android:layout_toEndOf="#id/txt_rating"
android:layout_alignBottom="#id/txt_rating"
android:gravity="bottom"
android:paddingBottom="1dp"
android:textColor="#color/red"
android:layout_below="#id/txt_reviews_title"/>
<View
android:id="#+id/view_dummy1"
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:layout_below="#id/txt_read_all_reviews"
android:layout_marginTop="10dp"
android:background="#color/divider"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="16dp"
android:layout_below="#+id/view_dummy1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0.6"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/txt_how_did_your_visit_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How did your visit go?"
android:textStyle="bold"
android:textSize="14sp"
android:textColor="#color/primary_text" />
<TextView
android:id="#+id/txt_tell_everyone_about_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tell everyone about it!"
android:layout_marginTop="5dp"
android:textColor="#color/secondary_text"
android:textSize="12sp"
/>
</LinearLayout>
<!--This got some problem-->
<Button
android:id="#+id/btn_add_review"
android:layout_width="0dp"
android:layout_height="40dp"
android:text="Add Your Review"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp"
android:textAllCaps="false"
android:textColor="#color/colorWhite"
android:background="#drawable/blue_button_background"
android:layout_weight="0.5"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_add_review).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
}
});
}
Code in my MainActivity.class:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.sub_layout);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do your stuff
Toast.makeText(MainActivity.this, "hi", Toast.LENGTH_SHORT).show();
}
});
}
}
Code in my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="eflair.myapplication.MainActivity">
<include
android:id="#+id/sub_layout"
layout="#layout/sub_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
Code in my sub_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name" />
</LinearLayout>
This issue is actually a bug in the NestedScrollView class for a while now.
I solved this issue my making some tweaks to the source code of the NestedScrollView class, as follows :-
public class NestedScrollView extends FrameLayout implements NestedScrollingParent,
NestedScrollingChild, ScrollingView {
...
#Override
public boolean onTouchEvent(MotionEvent ev) {
switch (actionMasked) {
case MotionEvent.ACTION_DOWN: {
if (getChildCount() == 0) {
return false;
}
//add this condition
if (!inChild((int) ev.getX(), (int) ev.getY())) {
return false;
}
if ((mIsBeingDragged = !mScroller.isFinished())) {
final ViewParent parent = getParent();
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
}
...
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
...
switch (action & MotionEventCompat.ACTION_MASK) {
...
case MotionEvent.ACTION_DOWN: {
...
// replace this line:
// mIsBeingDragged = !mScroller.isFinished();
mIsBeingDragged = false;
...
}
}
}
}
After doing this it should work fine.

Remove Margin in Fragment

I am using a RecyclerView in a Fragment. I want the fragment to match the parent in width and height. But this is my output.
There is that space being left around the fragment. I want to eliminate that. I have tried everything, i.e, making the third parameter false, using parent container , changing the LayoutParams to match_parent but nothing has worked.
My code:-
Fragment.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:id="#+id/RLGEVENT"
android:background="#color/white">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:clickable="true"
android:layout_marginLeft="16dp"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:src="#drawable/postbutton"
android:id="#+id/floatingButton" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
/>
Fragment
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_gevent_main, container, false);
RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.RLGEVENT);
rl.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
rl.invalidate();
//code
return view;
}
Root.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_start"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/white"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:insetForeground="#color/white"
android:theme="#style/AppTheme.Lolwa"
app:headerLayout="#layout/nav_header_start"
app:itemTextColor="#color/black"
app:menu="#menu/activity_start_drawer" />
</android.support.v4.widget.DrawerLayout>
Row.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/card_view"
android:layout_height="wrap_content">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gEventRowRL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#fff">
<ImageView
android:id="#+id/exampleProfilePic"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:scaleType="fitXY"
android:src="#drawable/background_profile_pic" />
<ImageView
android:id="#+id/photoOf"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_below="#+id/gEventsDetails"
android:scaleType="fitXY"
android:src="#drawable/black_total" />
<TextView
android:id="#+id/geventTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_toRightOf="#+id/exampleProfilePic"
android:text="Title"
android:textSize="18sp"
/>
<TextView
android:id="#+id/gEventsDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/exampleProfilePic"
android:layout_margin="10dp"
android:text=""
android:textSize="16sp" />
<TextView
android:id="#+id/gEventsDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/geventTitle"
android:layout_margin="10dp"
android:layout_toRightOf="#+id/photoOf"
android:text="" />
</RelativeLayout>
</RelativeLayout>
app_bar_start.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="com.example.anubhavr.firebase.StartActivity">
<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_start" />
</android.support.design.widget.CoordinatorLayout>
Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
setContentView(R.layout.activity_start);
fragmentManager = getSupportFragmentManager();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
com.example.anubhavr.firebase.Global_Events.MainActivity mainActivity = new com.example.anubhavr.firebase.Global_Events.MainActivity();
setListener(mainActivity);
fragmentManager.beginTransaction().replace(R.id.nav_home, mainActivity, mainActivity.getTag()).commit();
}
The padding should be in content_start

Collapsible Toolbar - Make Fragment Footer Always Visible in Android

I am making an app that has a ProfilePage with three fragments - About | Posts | Gallery, and I am using a collapsible toolbar with user's image. My second and third fragment will have a footer that should always be visible, but this is what I get:
When my image is expanded the footer disappears. What I want is for my two fragments to have these footers always visible and not depending on toolbar being collapsed or not. My gallery footer should be similar to post footer.
profile_layout.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"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
contentScrim="?attr/colorPrimary"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/profile_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:contentDescription="#string/profile_photo"
app:layout_collapseMode="none"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin">
<ImageView
android:id="#+id/toolbarEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:contentDescription="#string/block"
android:paddingEnd="20dp"
android:paddingStart="5dp"
app:srcCompat="#drawable/ic_icon_edit" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.design.widget.TabLayout
android:id="#+id/myProfileTabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
app:tabContentStart="72dp"
app:tabGravity="fill"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="#+id/myProfileViewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
posts_fragment.xml:
<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"
android:background="#drawable/tile_bg"
android:orientation="vertical" >
<ListView
android:id="#+id/list_view_messages"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#null"
android:divider="#null"
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true">
</ListView>
<LinearLayout
android:id="#+id/llMsgCompose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal"
android:weightSum="4" >
<EditText
android:id="#+id/inputMsg"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="#color/bg_msg_input"
android:textColor="#color/text_msg_input"
android:paddingLeft="6dp"
android:paddingRight="6dp"/>
<Button
android:id="#+id/btnSend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/ppdColorOrange"
android:textColor="#color/white"
android:text="#string/send" />
</LinearLayout>
My PostsFragment (not yet implemented):
public class MyProfilePostsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.navdrawer_my_profile_fragment_posts, container, false);
return view;
}
}
I had the same requirement for my one of apps. I just did workaround.
Add your required layout in tab layout:
<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
contentScrim="?attr/colorPrimary"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/profile_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:contentDescription="#string/profile_photo"
app:layout_collapseMode="none"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin">
<ImageView
android:id="#+id/toolbarEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:contentDescription="#string/block"
android:paddingEnd="20dp"
android:paddingStart="5dp"
app:srcCompat="#drawable/ic_icon_edit" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.design.widget.TabLayout
android:id="#+id/myProfileTabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
app:tabContentStart="72dp"
app:tabGravity="fill"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="#+id/myProfileViewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
<-- Your required layout -->
<LinearLayout
android:id="#+id/tab123"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="horizontal"
android:visibility="gone"
android:weightSum="4">
<EditText
android:id="#+id/inputMsg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:paddingLeft="6dp"
android:paddingRight="6dp" />
<Button
android:id="#+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="send" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
Add following code in your Tab activity:
TabLayout tabLayout;
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);//add your viewpager to TabLayout
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
LinearLayout l=(LinearLayout) findViewById(R.id.tab123);
final EditText text=(EditText) findViewById(R.id.inputMsg);
Button send=(Button) findViewById(R.id.btnSend);
if (tab.getPosition() == 0) {
l.setVisibility(View.GONE);
System.out.println("About tab");
} else if (tab.getPosition() == 1) {
l.setVisibility(View.VISIBLE);
System.out.println("Posts tab");
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String msg = null;
msg = text.getText().toString(); //get input
// Perform your desired task.
}
});
} else if (tab.getPosition() == 2) {
l.setVisibility(View.VISIBLE);
System.out.println("Gallery tab");
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String msg = null;
msg = text.getText().toString(); //get input
//Perform your desired task.
}
});
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});

android navigationLayout use headerview can't close in Fragment

I customize my DrawerLayout HeaderView, and when I click the button on HeaderView, it can't close the drawer like click the button on drawer menu.
I try to use in official demo , it's the same,too.
So i just create a new class BackListener to override onBackPress();
and just new it in my button, it doesn't work, too.
Who has the solution tell me, please. I will be grateful.
DrawerLayout activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.taiwandigestionsociety_v11.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<android.support.v7.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="#+id/mainFrame"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#01000000"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
HeaderView layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="250dp"
android:layout_height="wrap_content"
android:background="#66CDAA"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView3"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_grid_on_black_24dp" />
<TextView
android:id="#+id/logIn"
android:textSize="15dp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pageLogIn"
android:textColor="#android:color/background_light"/>
<Button
android:id="#+id/logOut"
android:layout_marginLeft="15dp"
android:layout_width="60dp"
android:layout_height="38dp"
android:text="#string/logOut" />
</LinearLayout>
</LinearLayout>
I tried the method:
public class BackListener extends AppCompatActivity {
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
System.out.println("back onKeyDown()");
return false;
}else {
return super.onKeyDown(keyCode, event);
}
}
}
MainActivity:
logIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switchFragment(LogInFragment.newInstance());
new BackListener();
}
});

match_parent is not working in custom toolbar

I have made custom toolbar with search and setting icon . when i click on search icon edittext should apper with match_parent and should occupy available space till the search icon . However it is not happening . it just appear with very small space at the top-left side . Please help where i am wrong
I would like to share my whole code with xml
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<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:background="#DCDCDC"
android:padding="0dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="myresolver.faisal.home.com.myresolver.MainActivity">
<include
android:id="#+id/toolbar"
layout="#layout/my_toolbar"></include>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="1dp"
android:weightSum="4"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96"></ImageView>
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
mytoolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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="wrap_content"
android:background="#color/orange"
app:titleTextColor="#color/white"></android.support.v7.widget.Toolbar>
my main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu 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">
<item
app:showAsAction="always"
android:title="#string/search"
android:icon="#drawable/ic_search_white_24dp"
android:id="#+id/search"
></item>
<item
app:showAsAction="always"
android:title="#string/setting"
android:icon="#drawable/ic_settings_white_24dp"
android:id="#+id/setting"></item>
</menu>
my search_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/searchEditText"
android:layout_width="match_parent" // THIS IS NOT WORKING
android:layout_height="wrap_content"
android:imeOptions="actionSearch"
android:inputType="text" />
</LinearLayout>
My activity
EditText searchEditText;
boolean isSearchBoxOpen = false;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchedHandler();
break;
case R.id.setting:
Toast.makeText(this, "Setting is UnderConstruction", Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}
public void onSearchedHandler() {
if (isSearchBoxOpen) {
String word = searchEditText.getText().toString();
if (word != null) {
Toast.makeText(this, "Searching for ..." + word, Toast.LENGTH_LONG).show();
}
} else {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.search_layout);
actionBar.setDisplayShowTitleEnabled(false);
searchEditText = (EditText) actionBar.getCustomView().findViewById(R.id.searchEditText);
searchEditText.requestFocus();
InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
keyboard.showSoftInput(searchEditText, InputMethodManager.HIDE_IMPLICIT_ONLY);
isSearchBoxOpen = true;
}
}
#Override
public void onBackPressed() {
if (isSearchBoxOpen) {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
isSearchBoxOpen = false;
return;
}
super.onBackPressed();
}
Use the below code for toolbar xlm layout :
<android.support.v7.widget.Toolbar
xmlns:app="schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primaryColor"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp" />
When the custom view is inflated as a result of calling setCustomView(resId), wrap_content is forced upon it (if you look into the view dump, you can see that the wrapping LinearLayout is shrunk, not the EditText).
You can set the custom view in the following way:
View customView = LayoutInflater.from(this).inflate(R.layout.search_layout, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
actionBar.setCustomView(customView, params);
just add this line to take full width
app:contentInsetStart="0dp"
The padding in the RelativeLayout in main_activity.xml is preventing it from taking the full space so instead of having
<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:background="#DCDCDC"
android:padding="0dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="myresolver.faisal.home.com.myresolver.MainActivity">
you move the paddings to the scrollview such that
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:fillViewport="true">

Categories

Resources