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();
}
});
Related
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.
I am using sliding panel layout and in that i have ONE floating buttons.Now when i Click on the button the view Slides first and then i have to click again on button to perform action.
I want to perform action on only single click without the sliding movement of parent view.
Look at these images
Look at 1 images
look at this
Here is my
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:id="#+id/Sliding"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:id="#+id/leftSide"
android:background="#ef3"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/contact"
android:background="#drawable/bg_key"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:id="#+id/rightSide"
android:layout_marginLeft="170dp"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="mini"
android:focusable="false"
app:srcCompat="#drawable/ic_menu_camera"
android:id="#+id/call"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/message" />
</RelativeLayout>
</android.support.v4.widget.SlidingPaneLayout>
here is my java code
SlidingPaneLayout sp;
#Override
protected void onCreate(Bundle savedInstanceState)
{
context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
call = (FloatingActionButton) findViewById(R.id.call);
sp = (SlidingPaneLayout) findViewById(R.id.Sliding);
assert sp != null;
sp.setPanelSlideListener(this);
sp.setSliderFadeColor(Color.TRANSPARENT);
sp.openPane();
mList = new ArrayList<Contacts>();
call.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Log.e("CALL","CALL");
try
{
if(number.isEmpty())
{
Toast.makeText(context,"Please select contact first",Toast.LENGTH_SHORT).show();
}
else
{
calling();
number=null;
}
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(context,"This is not the valid contact",Toast.LENGTH_SHORT).show();
}
}
});
}
Use this code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v4.widget.SlidingPaneLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:id="#+id/Sliding"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:id="#+id/leftSide"
android:background="#ef3"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/contact"
android:background="#drawable/bg_key"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v4.widget.SlidingPaneLayout>
<RelativeLayout
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:id="#+id/rightSide"
android:layout_marginLeft="170dp"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="mini"
android:focusable="false"
app:srcCompat="#drawable/ic_menu_camera"
android:id="#+id/call"
android:layout_alignParentTop="true" />
</RelativeLayout>
</RelativeLayout>
I have this layout example:
<?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:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#FFF880"
android:gravity="center_vertical"
android:orientation="horizontal">
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/action_bar_layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/emotion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/holo_blue_bright" />
<EditText
android:id="#+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:inputType="textMultiLine|textCapSentences"
android:singleLine="false"
android:text="On sait depuis longtemps "
android:textColor="#android:color/black" />
<FrameLayout
android:id="#+id/photo"
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="#EE00FF">
</FrameLayout>
</LinearLayout>
</ScrollView>
<FrameLayout
android:id="#+id/action_bar_layout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="#990000"
android:gravity="center_vertical"
android:orientation="horizontal">
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
And my sample Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edittext);
final ScrollView scrollView = (ScrollView)findViewById(R.id.scroll);
final EditText edittext = (EditText)findViewById(R.id.edit);
// Setup scrollview
scrollView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_MOVE:
// Hide keyboard
UIUtils.hideKeyboard(edittext, EditTextActivity.this);
break;
}
return false;
}
});
public static void hideKeyboard(View view, Activity activity) {
if (view != null) {
view.clearFocus();
if (activity != null) {
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
InputMethodManager imm = (InputMethodManager) activity.
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
So I would like the same behavior like Facebook new post: when you want to scroll the keyboard disappears automatically.
But in my case, there is a strange behavior when you want to scroll when the keyboard has been hide.
Thanks guys for your help!
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/action_bar_layout"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#FFF880"
android:gravity="center_vertical"
android:orientation="horizontal">
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/emotion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/holo_blue_bright" />
<EditText
android:id="#+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:inputType="textMultiLine|textCapSentences"
android:singleLine="false"
android:text="On sait depuis longtemps "
android:textColor="#android:color/black" />
<FrameLayout
android:id="#+id/photo"
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="#EE00FF">
</FrameLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/action_bar_layout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="#990000"
android:gravity="center_vertical"
android:orientation="horizontal">
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
Try this hope it will solve your problem.
Instead of hiding keyboard on scroll ,you can hide keyboard on outsidetouch of edit text
Make the parent view(content view of your activity) clickable and focusable by adding the following attributes
android:clickable="true"
android:focusableInTouchMode="true"
Implement a hideKeyboard() method
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
and set the onFocusChangeListener of your edittext.
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
or if you want to hide on scroll then you can add on touch listner for your main view instead of scroll view
findViewById(R.id.mainLayout).setOnTouchListener(this)
set android:focusableInTouchMode="true" on main container
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);
}
};
I've been searching for a while with this issue and i cant find solution yet.
For my design of DrawerLayout, I will not be using listview but linearlayout as container for my views. I test it first by adding a button inside the linearlayout but i cant click it.
Heres the XML.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/left_drawer"
android:background="#color/white"
android:orientation="vertical"
android:layout_gravity="start"
android:layout_width="250dp"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_test"
android:text="TEST"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<FrameLayout
android:id="#+id/container_fragment2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="1dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
>
</FrameLayout>
In java, I also tried to put text in the button once it is being loaded and its working.The only thing that is not working is when i click it. the drawer will just close.
/**Init Drawer*/
private void initDrawer(){
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerContainer = (LinearLayout) findViewById(R.id.left_drawer);
//mDrawerContainer.setOnClickListener(new DrawerItemClickListener());
mDrawerContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,"initDrawer",Toast.LENGTH_SHORT).show();
}
});
btn_test = (Button)mDrawerContainer.findViewById(R.id.btn_test);
btn_test.setOnClickListener(new View.OnClickListener() { //this is not working,i cant click the button
#Override
public void onClick(View v) {
Log.e("MyActivity","test");
Toast.makeText(context,"test",Toast.LENGTH_SHORT).show();
}
});
btn_test.setText("hooray"); //this one is working,it change the text to hooray once loaded
}
Im looking forward for your input guys,
Thanks a lot.
Its so ironic, I just change the positions of my layout. I transferred the linearlayout below the fragment and its now working.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container_fragment2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="1dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
>
</FrameLayout>
<LinearLayout
android:id="#+id/left_drawer"
android:background="#color/white"
android:orientation="vertical"
android:layout_gravity="start"
android:layout_width="250dp"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_test"
android:text="TEST"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Check this ! reference
you have to set a TouchListener for your drawer layout.
eg/
gestureDetector = new GestureDetector(this, new CustomGestureListener());
mDrawerLayout.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
});
Remove the OnClickListener that you've set on the DrawerLayout itself.
check it----
cast your button with layout where u keep the button
here is my xml
<RelativeLayout
android:id="#+id/whatYouWantInLeftDrawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#EBF4FA">
<RelativeLayout
android:id="#+id/top_view_rel"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/blue_color">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:visibility="visible">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/drawer_driver_name"
android:textColor="#color/text_color"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/name"
android:layout_marginLeft="135dp"
android:text="name"
android:textAllCaps="true"
android:textColor="#color/text_color"
android:textSize="16sp"
android:visibility="gone" />
<Button
android:id="#+id/edit"
android:layout_width="30dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:background="#color/colorTransparent"
android:drawableRight="#drawable/edit1"
/>
in activity--
leftDrawerRel = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
edit= (Button)leftDrawerRel.findViewById(R.id.edit);
edit.setOnClickListener(this);