Displaying ActionBar in PreferenceScreen looks messed up - java

I'm trying to display an ActionBar in my PreferenceScreen. In my MainActivity it works totally fine, but in my PreferenceScreen it lokks messed up, because the title of the actinBar is in the middle of the screen and the layout is placed over the actionBar (see screenshot).
SettingsActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_drawer, new SettingsFragment())
.commit();
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mTitle = getTitle();
mDrawerLayout = findViewById(R.id.settings_drawer);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(R.string.app_name);
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.addDrawerListener(mDrawerToggle);
setNavigationViewListener();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
public static class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
settings_activity.xml:
<androidx.drawerlayout.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/settings_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".SettingsActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:visibility="visible" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>

Don't replace your fragment directly with the DrawerLayout, instead use a FrameLayout inside the DrawerLayout that you can replace your fragment with.
Something along the following lines should work.
<androidx.drawerlayout.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/settings_drawer_base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".SettingsActivity">
<!-- This LinearLayout represents the contents of the screen -->
<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="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:visibility="visible" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="#+id/settings_drawer"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>
P.S -
You'll need to reference your DrawerLayout with the new id I gave it.
mDrawerLayout = findViewById(R.id.settings_drawer_base);

Related

Hamburger icon not opening navigation drawer

When i open my app and click on the hamburger icon, it is does not open the navigation drawer. But when i swipe from the edges it is opening. Also the drawer responds to hamburger icon click events after the swipe action.
I tried every solution on stackoverflow but couldn't resolve the issue. Please someone help. Thanks in advance.
XML Code:
<androidx.drawerlayout.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"
tools:openDrawer="start"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity_main">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="Nexzen"
app:titleTextColor="#color/white"
android:background="#color/dark_green"/>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabLayout"
android:background="#color/dark_green"
app:tabTextColor="#color/white"
app:tabIndicator="#color/receive_message"
app:tabSelectedTextColor="#color/receive_message"
android:layout_below="#id/appBarLayout"/>
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewPager"
android:layout_below="#id/tabLayout"/>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/navmenu"
android:visibility="gone"
app:menu="#menu/drawer_menu"
app:itemTextColor="#color/text_color"
app:itemIconTint="#color/text_color"
app:headerLayout="#layout/nav_header"
android:layout_gravity = "start" />
</androidx.drawerlayout.widget.DrawerLayout>
Activity code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
toggle = new ActionBarDrawerToggle(this, binding.drawer, binding.toolbar, R.string.open, R.string.close);
toggle.getDrawerArrowDrawable().setColor(getColor(R.color.white));
toggle.setDrawerIndicatorEnabled(true);
toggle.setDrawerSlideAnimationEnabled(true);
binding.drawer.addDrawerListener(toggle);
toggle.syncState();
}
#Override
protected void onStart() {
super.onStart();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (toggle.onOptionsItemSelected(item)) {
binding.drawer.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(#Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
toggle.syncState();
}
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
Ensure you haven't set the visibility of the NavigationView in the fragment to invisible or gone. This was the cause for me.
Swiping must make the navigation drawer visible. Clicking the hamburger button does not.

Drawer Layout hamburger and toolbar not visible despite working nav drawer?

I am currently creating a navigation menu without using fragments. I've seemingly created the working system that opens classes and keeps the navigation bar present at each stage by using a BaseActivity and inheriting it into the classes that need the drawer. However upon loading the app seemingly the 3 bar 'Hamburger' icon isn't present and neither is the toolbar at the top (See photo). The navigation bar does seemingly work as I can swipe from the left and click to load other classes. I feel like my onCreate() in my BaseActivity is missing something but with googling I can't seem to find a solution.
Image of current issue:
Image of Nav Bar Working with issue in background:
This is my base activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class BaseActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_closed);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Intent anIntent;
switch (item.getItemId()) {
case R.id.nav_homepage:
anIntent = new Intent(getApplicationContext(), homepage.class);
startActivity(anIntent);
drawerLayout.closeDrawers();
break;
case R.id.nav_add_roster:
anIntent = new Intent(getApplicationContext(), add_roster.class);
startActivity(anIntent);
drawerLayout.closeDrawers();
break;
case R.id.nav_check_schedule:
anIntent = new Intent(getApplicationContext(), schedule.class);
startActivity(anIntent);
drawerLayout.closeDrawers();
break;
}
return false;
}
});
}
}
Here's my homepage which is what is loaded initially:
import android.os.Bundle;
import android.widget.FrameLayout;
public class homepage extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame); //Remember this is the FrameLayout area within your content_main.xml
getLayoutInflater().inflate(R.layout.homepage, contentFrameLayout);
}
}
Here's my activity main:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
You can try this, add the code below in your BaseActivity :
#Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
EDIT:
Enable the home button in your onCreate function of BaseActivity as well :
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
There is missing a toolbar layout in your xml :
<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">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeeee">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Hope this helps;
Sorry for my english.
Try this way,
<?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_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
and in app_bar_toolbar.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:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:id="#+id/relative_tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
and in content_main.xml, use this.
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.esec_0052.fingertips.FingerTipMainActivity"
tools:showIn="#layout/app_bar_finger_tip_main">
<FrameLayout
android:id="#+id/frame_parent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

Viewpager Tabs Fragment & Normal Fragments

I apologize in beforehand for my bad code and bad knowledge. I have just recently started coding in Android and Java
My problem is that I have Navigation Drawer and Tabs. Although if I choose something from the navigation drawer which loads up in a Fragment it doesn't show. Only the tabs are showing.
My plan is that one navigation drawer tab shall have 3 tabs inside and the rest just normal pages without tabs.
MainActivity.java
http://pastebin.com/TV7aWy9c
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<FrameLayout
android:id="#+id/fragment_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#000000" />
<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/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="se.themeister.hello.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
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.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/AppTheme.AppBarOverlay"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#009900"
android:layout_below="#id/tab_layout"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
I don't know if that's enough or if more information or code is needed
The problem with your code is that your ViewPager with TabLayout should be part of the Fragment, not part of the Activity. And NavigationDrawer should just replace one fragment by another in the viewport (i.e. in the container). Now, in your code, you're trying to somehow insert your new Fragment into the ViewPager.
Here's a very basic app with Navigation Drawer, one Fragment with Tabs and rest fragments without tabs, which you can use as an example:
Activity.xml:
<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">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android: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>
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_marginTop="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
<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/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
The code in the activity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#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);
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithTabs()).commit();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.fragment_1) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithTabs()).commit();
} else if (id == R.id.fragment_2) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithoutTabs()).commit();
} else if (id == R.id.fragment_3) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new Fragment()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
I.e., as you see, I'm replacing fragments in the FrameLayout with id fragmentContainer.
All logic related to the fragment with tabs, incapsulated in FragmentWithTabs:
public class FragmentWithTabs extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_with_tabs, container, false);
ViewPager viewPager = (ViewPager)rootView.findViewById(R.id.viewPager);
TabLayout tabLayout = (TabLayout)rootView.findViewById(R.id.tabLayout);
viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
#Override
public Fragment getItem(int position) {
return new SubFragment(position == 0? Color.BLUE : position == 1? Color.WHITE : Color.RED);
}
#Override
public CharSequence getPageTitle(int position) {
return position+"";
}
#Override
public int getCount() {
return 3;
}
});
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
}
I hope, it helps

Open Navigation Drawer with layout on click event

I have two navigation drawer, one with a ListView and the second with a Layout. The first Navigationdrawer works fine, but the second can't open it on a button click.
Here is the MainActivity xml code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="right"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orange"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:navigationIcon="#drawable/arrow"
app:title="MyGames"
>
<Button
android:id="#+id/rigthMenu"
android:background="#drawable/compose"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginRight="5dp"
android:layout_gravity="right" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollIndicators="bottom">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="2dp"
android:background="#ffffff"
android:scrollbars="vertical"/>
<include
android:id="#+id/layRigth"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
layout="#layout/rigth_menu" />
</android.support.v4.widget.DrawerLayout>
Here is the rigthMenu layout xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="left">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="#+id/checkBox2"
android:layout_gravity="center_horizontal" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="#+id/checkBox"
android:layout_gravity="center_horizontal" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="#+id/checkBox4"
android:layout_gravity="center" />
and here is the MainActivity OnCreate code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// mDrawerLayout2 = (DrawerLayout)findViewById(R.id.layRigth);
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onIcon = !onIcon;
if (onIcon == true) {
mDrawerLayout.openDrawer(mDrawerList);
} else {
mDrawerLayout.closeDrawer(mDrawerList);
}
}
});
Button rigthMenu = (Button)findViewById(R.id.rigthMenu);
rigthMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final LayoutInflater factory = getLayoutInflater();
final View text = factory.inflate(R.layout.rigth_menu, null);
mDrawerLayout2 = (DrawerLayout)text.findViewById(R.id.layRigth);
mDrawerLayout2.openDrawer(mDrawerLayout2);
}
});
Drawable x = toolbar.getNavigationIcon();
getDefaultSettings("Image", this);
// mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.content_frame,new TabFragment()).commit();
// Initializing
dataList = new ArrayList<DrawerItem>();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Add Drawer Item to dataList
dataList.add(new DrawerItem(R.mipmap.ps4miniatura, GREEN, 1));
dataList.add(new DrawerItem(R.mipmap.ps4miniatura, "PC", 0));
dataList.add(new DrawerItem(0, " ", 2));
adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item, adapter,
dataList);
// addItem.giveAdapter(adapter);
mDrawerList.setAdapter(adapter);
mDrawerList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
if (savedInstanceState == null) {
SelectItem(1);
}
}
Logcat error:
12-08 08:40:52.936 15587-15587/com.tutecentral.navigationdrawer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tutecentral.navigationdrawer, PID: 15587
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(android.view.View)' on a null object reference
at com.tutecentral.navigationdrawer.MainActivity$2.onClick(MainActivity.java:75)
at android.view.View.performClick(View.java:4820)
at android.view.View$PerformClick.run(View.java:20157)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5578)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
I have a nullPointException. How can I get the view of the layout rigthMenu?
There are a few errors in the rigthMenu's OnClickListener. Firstly, there's no need to inflate the rigth_menu layout there. You've included it in the main layout, so it will already be inflated. Also, rigth_menu does not contain any View with ID layRigth, so text.findViewById(R.id.layRigth) is returning null, and causing the NullPointerException. And, you've already got a reference to the DrawerLayout - mDrawerLayout - so you don't need to find it again.
rigthMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mDrawerLayout.isDrawerOpen(GravityCompat.END)) {
mDrawerLayout.closeDrawer(GravityCompat.END);
}
else {
mDrawerLayout.openDrawer(GravityCompat.END);
}
}
});
Please use below code to open and close the drawer
public void toggleDrawer() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START))
mDrawerLayout.closeDrawer(GravityCompat.START);
else
mDrawerLayout.openDrawer(GravityCompat.START);
}

Why settings.xml layout is overlapping the ActionBar/Toolbar?

I'm developing a material design app.
After adding settings.xml in SettingsActivity.java & running the app, the activity is appearing like this:
Here's SettingsActivity.java file's code:
public class SettingsActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
SpannableString s = new SpannableString("Settings");
s.setSpan(new TypefaceSpan(this, "Roboto-Medium.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(s);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String SHARE_APP_PREFERENCE_KEY = "pref_key_share_app";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(SHARE_APP_PREFERENCE_KEY)) {
// do something
}
}
}
}
Here's activity_settings.xml file's code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abc.xxx.SettingsActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
</RelativeLayout>
Here's settings.xml file's code:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="#string/pref_notification" >
<CheckBoxPreference
android:defaultValue="true"
android:key="prefNotification"
android:summary="#string/pref_notify_summary" >
</CheckBoxPreference>
</PreferenceCategory>
</PreferenceScreen>
I don't know why the settings.xml layout is overlapping the ActionBar/Toolbar!
Please let me know.
Thanks in advance.
Change your activity_settings.xml to this
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abc.xxx.SettingsActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_below="#id/toolbar"
android:layout_height="match_parent" />
</RelativeLayout>
And change code to this
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new SettingsFragment())
.commit();
When you call the fragment on the activity, don't use
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
Instead use
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.Your_Frame_ID_On_xml, new SettingsFragment())
.commit();
I had the same problem for using android.R.id.content so changing it to the ID you use for the frame to hold the fragment on the xml fixed it.
Discovered that PreferenceActivity in Sunny's solution above may crash due to it loads a content view without a ListView. This is fixed by adding it to activity_settings.xml, but the PreferenceFragment will just ignore it. The activity_settings.xml will the look something like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/common_actionbar" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Categories