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
Related
I have tried to implement navigation drawer in my app with androidx library. So far, the app show hamburger icon on top left corner, but to access the drawer I have to swipe right. If I clicked the icon it will go to previous activity instead. Is it because this activity is not the main activity? How can I fix this? Thank you in advance.
Here is my activity
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
DrawerLayout drawerLayout = findViewById(R.id.drawerLayout);
ActionBarDrawerToggle t = new ActionBarDrawerToggle(this, drawerLayout,R.string.Open, R.string.Close);
drawerLayout.addDrawerListener(t);
t.syncState();
NavigationView nv = findViewById(R.id.navigationView);
nv.setNavigationItemSelectedListener(item -> {
int id = item.getItemId();
switch(id)
{
case R.id.action_open_list:
break;
case R.id.action_closed_list:
Intent closedListIntent = new Intent(this, ClosedListActivity.class);
startActivity(closedListIntent);
break;
default:
return true;
}
return true;
});
}
my layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawerLayout"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/tv_error_message_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="#string/error_message_cannot_connect"
android:textSize="20sp"
android:visibility="invisible" />
<ProgressBar
android:id="#+id/pb_loading_indicator"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_gravity="center"
android:visibility="invisible" />
</FrameLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
android:id="#+id/navigationView"/>
</androidx.drawerlayout.widget.DrawerLayout>
Toolbar needs to be a child of DrawerLayout to work with.
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawerLayout"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.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" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/tv_error_message_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="#string/error_message_cannot_connect"
android:textSize="20sp"
android:visibility="invisible" />
<ProgressBar
android:id="#+id/pb_loading_indicator"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_gravity="center"
android:visibility="invisible" />
</FrameLayout>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
android:id="#+id/navigationView"/>
</androidx.drawerlayout.widget.DrawerLayout>
Then in your Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
....
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id. drawerLayout);
ActionBarDrawerToggle t = new ActionBarDrawerToggle(
this, drawerLayout, toolbar, R.string.Open, R.string.Close);
drawerLayout.addDrawerListener(t);
t.syncState();
....
}
Good Day everyone, I using the left navigation bar and i use fragment for the activity in the navigation bar. But the problem is once I click on the activity, the fragment could not show up. I am not sure what is the problem.
Main Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nav_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initCollapsingToolbar();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
informationList = new ArrayList<>();
adapter = new AlbumsAdapter(this, informationList);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this,
2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2,
dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
prepareAlbums();
//Cover Pic
try {
Glide.with(this).load(R.drawable.project1_cover).into((ImageView)
findViewById(R.id.backdrop));
} catch (Exception e) {
e.printStackTrace();
}
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setupDrawerContent(navigationView);
}
public void selectedItemDrawer(MenuItem menuItem){
Fragment myFragment = null;
Class fragment = null;
switch(menuItem.getItemId()){
case R.id.checkIn:
Toast.makeText(getApplicationContext(), "Check-In",
Toast.LENGTH_SHORT).show();
break;
case R.id.checkOut:
Toast.makeText(getApplicationContext(), "Check-Out",
Toast.LENGTH_SHORT).show();
break;
case R.id.applyOff:
break;
case R.id.reportBug:
break;
case R.id.manageProfile:
fragment = fragment_manageProfile.class;
break;
case R.id.logout:
break;
default:
break;
}
try{
myFragment = (Fragment)fragment.newInstance();
}catch(Exception e){
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frameLayout,
myFragment).addToBackStack(null).commit();
menuItem.setChecked(true);
drawer.closeDrawers();
}
content_main xml File
<?xml version="1.0" encoding="utf-8"?>
<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:background="#color/viewBg"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="info.androidhive.kopilim.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="vertical" />
</RelativeLayout>
app_bar_main xml file
<?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=".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.CoordinatorLayout>
activity_main xml file
<?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:background="#android:color/white"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="#dimen/detail_backdrop_height"
android:fitsSystemWindows="true"
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"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="#android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
</RelativeLayout>
<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/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/frameLayout"
android:background="#drawable/login_background">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
PS fragmentLayout is currently in my activity_main.xml file there. Because I not sure where it has to be. For now, everything i mean the view of my content, my layout is fined. But just for the activity in the navigation bar, fragment there is not showing.
Where did I make my mistake?
Edited: My fragment Class
fragment layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.androidhive.kopilim.fragment_manageProfile">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="35dp"
android:textSize="22dp"
android:textColor="#10019f"
android:textStyle="bold"
android:text="#string/manageProfile"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empEmail"
android:text="#string/email"
android:layout_margin="10dp"
android:textSize="18dp"
android:gravity="center_vertical"
android:textColor="#color/colorAccent"
/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empID"
android:hint="#string/profileID"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empName"
android:hint="#string/profileName"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empIC"
android:hint="#string/profileIC"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radioGroup"
android:layout_margin="5dp"
android:orientation="horizontal"
android:weightSum="1"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/male"
android:id="#+id/maleRadio"
android:textColor="#color/colorAccent"
android:layout_weight="0.3"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/female"
android:id="#+id/femaleRadio"
android:textColor="#color/colorAccent"
android:layout_weight="0.3"/>
</RadioGroup>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empPh"
android:hint="#string/profileHp"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empAddress"
android:hint="#string/profileAddress"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="#+id/profileBtn"
android:text="#string/updateProfileBtn"
android:background="#color/buttonLogin"/>
</LinearLayout>
</ScrollView>
</FrameLayout>
fragment java class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_manage_profile, container,
false);
....
....
//my activity
return v;
}
FrameLayout is ok in your activity layout file because you want to display the fragment on the same screen. About the fragment displaying . You need to have a custom fragment class to show it's view and to support it's business logic.
public void selectedItemDrawer(MenuItem menuItem){
Fragment myFragment = null;
Class fragment = null;
switch(menuItem.getItemId()){
case R.id.checkIn:
Toast.makeText(MainActivity.this, "Check-In",
Toast.LENGTH_SHORT).show();
break;
case R.id.checkOut:
Toast.makeText(MainActivity.this, "Check-Out",
Toast.LENGTH_SHORT).show();
break;
case R.id.applyOff:
break;
case R.id.reportBug:
break;
case R.id.manageProfile:
showMyFragment();
break;
case R.id.logout:
break;
default:
break;
}
}
public void showMyFragment(){
Fragment menuFragment = new MyCustomFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frameLayout, menuFragment).commit();
}
And here you have to declare the custom fragment class :
public class MyCustomFragment extends Fragment {
//this text view is got from the xml below
private TextView tvTest;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
bindView(view);
//start your logic here
}
private void bindView(View view){
tvTest = view.findViewById(R.id.tv_fragment_test);
Toast.makeText(getContext(),tvTest.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
my_fragment_layout.xml
// could be any kind of layout
// for example
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical">
<TextView
android:id="#+id/tv_fragment_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="I'm showing on a fragment"
android:src="#drawable/app_img_small" />
</LinearLayout>
I'm trying to create a sidebar navigation drawer that slides when a user clicks the button. I'm trying to add this feature into my existing layout and I'm having trouble with it. As you can see in my layout, I have created a menu resource file and included into my main layout but it is not working. Any help would be appreciated.
Here's my main layout
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
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:background="?attr/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="113dp"
android:fitsSystemWindows="true"
app:popupTheme="#style/Theme.AppCompat.DayNight.DarkActionBar"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/search_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:paddingLeft="8dp"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:textSize="14sp"
android:hint="#string/search_hint"
android:background="#drawable/bottom_border"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<include
layout="#layout/item_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/relativeAd"
android:layout_below="#+id/search_box"
/>
<LinearLayout
android:id="#+id/relativeAd"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#color/divider"
android:gravity="center"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="50dp"
android:layout_height="53dp"
android:clickable="true"
app:fabSize="mini"
app:srcCompat="#drawable/menu_1"
android:id="#+id/floatingActionButton9"
app:backgroundTint="?attr/actionModeSplitBackground"
app:rippleColor="#android:color/holo_red_dark"
android:layout_gravity="bottom|left"
app:layout_anchor="#+id/frameLayout"
app:layout_anchorGravity="center_vertical|right" />
Here is my screenshot of my layout
My existing layout
Try this out.
This a code for navigation drawer which opens and closes using button and swipe both.
so below is the layout code for setting up navigation drawer.
xml file code:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:theme="#style/NavigationTheme">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.whiskey.servicedog.TrainingProgrammes">
<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.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/menu_navigation"
android:background="#drawable/nav_bg"
android:theme="#style/NavigationViewStyle"
app:itemTextColor="#color/White"
app:itemIconTint="#color/White"
/>
</android.support.v4.widget.DrawerLayout>
now java code for setting up navigation drawer.
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id._aboutus:
Intent aboutusintent = new Intent(TrainingProgrammes.this, AboutUS.class);
aboutusintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(aboutusintent);
break;
case R.id._trainingprog:
Toast.makeText(getApplicationContext(), "Training Programmes", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
case R.id._taskhis:
Intent historyintent = new Intent(TrainingProgrammes.this, TaskHistory.class);
historyintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(historyintent);
break;
case R.id.taskrem:
Intent newintent = new Intent(TrainingProgrammes.this, TaskReminders.class);
newintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(newintent);
break;
case R.id.settingschange:
Intent settingIintent = new Intent(TrainingProgrammes.this, Settings.class);
settingIintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(settingIintent);
break;
case R.id.help_btn:
Intent helpIintent = new Intent(TrainingProgrammes.this, Help.class);
helpIintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(helpIintent);
break;
case R.id.logout:
Intent intent = new Intent(TrainingProgrammes.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
SharedPreferences preferences = getSharedPreferences("MyPREFERENCES", Context.MODE_PRIVATE);
if (preferences.contains("password")) {
SharedPreferences.Editor editor = preferences.edit();
editor.remove("password");
editor.commit();
}
finish(); // Call once you redirect to another activity.
}
return true;
}
});
Hope this helps you out.
Here is the screenshot of above working code.
Use below xml to add a Drawer Layout and include your previous layout
<include layout="#layout/content_home_page" /> by replacing this
name it activity_main.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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<!--Main Content Goes Here-->
<include layout="#layout/content_home_page" />
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header_main"
app:itemIconTint="#color/colorPrimary"
app:itemTextColor="#color/colorPrimary"
app:menu="#menu/navigation_menu"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_menu_header_bg">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/user_placeholder" />
<com.sslwireless.gulshanclub.Fonts.RobotoCondensedBold
android:id="#+id/memberFullName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Mr. John Doe"
android:textColor="#color/colorWhite"
android:textSize="18sp" />
<com.sslwireless.gulshanclub.Fonts.RobotoCondensedRegular
android:id="#+id/memberEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="john.doe#mail.com"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
In your activity:
private DrawerLayout drawer;
private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
inside your button click listener click event,
call drawer.openDrawer(GravityCompat.START);
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'm trying to add a NavigationDrawer to my excisting project get this error both when I run the app and in the design window in android studio:
java.lang.IllegalStateException: Child drawer has absolute gravity LEFT but this DrawerLayout already has a drawer view along that edge
I tried to solve it myself but couldn't find the second drawer the error is talking about, so Why do I get the error? This is the xml fileI
<?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_main"
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:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/drawer_header_main"
app:menu="#menu/drawermenu" />
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
<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:orientation="vertical"
android:weightSum="1"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="34dp">
<TextView
android:id="#+id/labelGold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="#string/labelGold"
android:textSize="20dp" />
<TextView
android:id="#+id/textViewGold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/labelGold"
android:text="0"
android:textSize="20dp" />
</RelativeLayout>
<LinearLayout
android:id="#+id/fragmentParentViewGroup"
android:layout_width="match_parent"
android:layout_height="437dp"
android:orientation="vertical">
<fragment
android:id="#+id/avatarFragment"
android:name="com.owlfinity.zeepblok.taskjournal.AvatarFragment"
android:layout_width="match_parent"
android:layout_height="181dp"
tools:layout="#layout/avatarfragment" />
<fragment
android:id="#+id/taskListFragment"
android:name="com.owlfinity.zeepblok.taskjournal.TaskListFragment"
android:layout_width="match_parent"
android:layout_height="166dp"
android:layout_weight="0.35"
tools:layout="#layout/tasklistlayout" />
</LinearLayout>
<Button
android:id="#+id/buttonNew"
android:layout_width="128dp"
android:layout_height="48dp"
android:layout_gravity="center_horizontal"
android:background="#drawable/button"
android:onClick="newTask"
android:text="#string/button_new" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
EDIT 1 ======================================================
Here is the OnCreate of MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
dbHelper = new DBHelper(this);
textViewGold = (TextView) findViewById(R.id.textViewGold);
taskListFragment = (TaskListFragment) getSupportFragmentManager().findFragmentByTag("taskList");
inventoryFragment = (InventoryFragment) getSupportFragmentManager().findFragmentByTag("inventory");
loadGuiStuff();
if (taskListFragment == null)
{
taskListFragment = new TaskListFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragmentParentViewGroup, taskListFragment);
transaction.commit();
}
}
Fixed it, needed to clean the project ......