I want to add the navigation drawer functionality to my activity, but the default menu icon on left does not appear although when I swipe right, the drawer opens up. Here's my layout file:
<?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">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinatorLayout"
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:theme="#style/AppTheme.AppBarOverlay"
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"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#android:color/white"
app:tabSelectedTextColor="#android:color/white"
app:tabIndicatorColor="#android:color/white"
app:tabIndicatorHeight="6dp"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</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_one_more"
app:menu="#menu/activity_one_more_drawer" />
</android.support.v4.widget.DrawerLayout>
My Activity file:
public class PartThreeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppThemeBlue);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_part_three);
initToolbar();
initViewPagerAndTabs();
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);
}
private void initToolbar() {
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
setTitle(getString(R.string.app_name));
mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
}
private void initViewPagerAndTabs() {
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(PartThreeFragment.createInstance(20), getString(R.string.tab_1));
pagerAdapter.addFragment(PartThreeFragment.createInstance(4), getString(R.string.tab_2));
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.one_more, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
static class PagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public PagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
public void addFragment(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
#Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
}
}
And here are the screenshots:
Here is the link to my project:
https://drive.google.com/open?id=0B6_hNGJUq8_2RUtxTl9pTGlqb2c
Please help me find out why the menu is not appearing, thanks.
Your toolbar reference is not initialized because Toolbar mToolbar is a local variable which will have no effect because you are using toolbar reference while adding toogle which currently is not initialized
Change this
private void initToolbar() {
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
setTitle(getString(R.string.app_name));
mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
}
into this
private void initToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
//^^^^ initialize the reference which is being used to add toggle
setSupportActionBar(toolbar);
setTitle(getString(R.string.app_name));
toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
}
Related
I need some help with my android application. The problem I had faced is that I had created Base activity, that controls Drawer Layout and toolbar. When I am extending it in activity for Relative Layout and nested Recycle View, I get a problem that a can't scroll it (But, when activity is starting, I can catch the moment and do it, but one time). Another situation - I opened an application without extending Base activity in my activity for RecycleView and it works okay.
RecyclerView:
<?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"
tools:context=".EntryList"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/entry_list">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:padding="4dp"
android:scrollbars="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
</RelativeLayout>
Main activity (Drawer):
<?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/nav_header_main"
app:menu="#menu/activity_main_drawer" />
AppBar included to Drawer:
<?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:id="#+id/coordinator_main"
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="match_parent"
android:background="#color/colorWhite"
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="#drawable/gradient_color"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<FrameLayout
android:id="#+id/activity_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Base Activity:
public abstract class BaseActivity extends AppCompatActivity {
protected Context context;
protected Toolbar toolbar;
private DrawerLayout drawer;
private ProgressBar loadingProgressBar;
private NavigationView navigationView;
private ActionBarDrawerToggle toggle;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
context = BaseActivity.this;
setContentView(R.layout.activity_main);
}
#Override
public void setContentView(int layoutResID) {
DrawerLayout fullView = (DrawerLayout)getLayoutInflater().inflate(R.layout.activity_main, null);
FrameLayout activityContainer = fullView.findViewById(R.id.activity_content);
getLayoutInflater().inflate(layoutResID, activityContainer, true);
super.setContentView(fullView);
initToolBar();
}
private void initToolBar() {
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setUpNavigation();
toggle.syncState();
}
private void setUpNavigation() {
drawer = findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(BaseActivity.this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
ActionBar actionBar = getSupportActionBar();
if (actionBar == null) {
throw new NullPointerException("ActionBar is null");
}
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Handle navigation view item clicks here.
if (item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
drawer.closeDrawers();
Intent intent;
switch (item.getItemId()) {
case R.id.nav_home:
intent = new Intent(BaseActivity.this, EntryList.class);
startActivity(intent);
return true;
}
return false;
}
});
toggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
toggle.onConfigurationChanged(newConfig);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item))
return true;
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Adapter:
public class EntryListAdapter extends RecyclerView.Adapter<EntryListAdapter.EntryListViewHolder> {
private List<ExampleEntryItem> items;
public static class EntryListViewHolder extends RecyclerView.ViewHolder {
private TextView specieNameRuTextView;
private TextView entryDateTextView;
private TextView specieNameLatTextView;
private TextView entryPlaceTextView;
public EntryListViewHolder(#NonNull View itemView) {
super(itemView);
specieNameRuTextView = itemView.findViewById(R.id.specie_name_ru);
entryDateTextView = itemView.findViewById(R.id.entry_date);
specieNameLatTextView = itemView.findViewById(R.id.specie_name_lat);
entryPlaceTextView = itemView.findViewById(R.id.entry_place);
}
}
public EntryListAdapter(List<ExampleEntryItem> items) {
this.items = items;
}
#NonNull
#Override
public EntryListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.example_entry_item, parent, false);
return new EntryListViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull EntryListViewHolder entryListViewHolder, int position) {
ExampleEntryItem item = this.items.get(position); // TODO: 06.05.2019 test for null
entryListViewHolder.entryPlaceTextView.setText(item.getEntryPlace());
entryListViewHolder.entryDateTextView.setText(item.getEntryDate());
entryListViewHolder.specieNameRuTextView.setText(item.getSpecieNameRu());
entryListViewHolder.specieNameLatTextView.setText(item.getSpecieNameLat());
}
#Override
public int getItemCount() {
return this.items.size();
}
RecycleView control class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entry_list_content_main);
List<ExampleEntryItem> items = new ArrayList<>();
adding items...
RecyclerView recyclerView = findViewById(R.id.recycler_view);
RecyclerView.Adapter adapter = new EntryListAdapter(items);
adapter.notifyDataSetChanged();
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
Thanks, everybody, who will help me
You first need to remove
adapter.notifyDataSetChanged()
After this if you use recycler view inside nested scrolling
You need to add a single line
recyclerView.setNestwdScrollingenable(false)
I resolved the problem. I replace my logic for recyclerview in frament, and mainly change, that I do is - in layout with appbar, I replaced <include layout="#layout/content"/> to outside of AppBarLayout and change line:
android:layout_height="match_parent"
android:background="#color/colorWhite"
to this:
android:layout_height="wrap_content"
In MainPage, it has NavigationDrawer and BottomNavigationBar. When I clicked Home, the navigation drawer icon and action bar title missing.
MainPage
public class MainPage extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
navigation.setSelectedItemId(R.id.navigation_home);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomePage();
loadFragment(fragment);
return true;
case R.id.navigation_dashboard:
return true;
case R.id.navigation_notifications:
return true;
}
return false;
}
};
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the main; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.rl, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
HomePage
public class HomePage extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.app_bar_main, container, false);
RecyclerView mRecyclerView = view.findViewById(R.id.recycleView);
GridLayoutManager mGridLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setLayoutManager(mGridLayoutManager);
mRecyclerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
List <Book> bookList= new ArrayList<>();
Book book = new Book();
book = new Book("Introduction to Java Programming", 15.00, "4.5", Uri.parse("android.resource://com.example.seng.sechandapp/drawable/book").toString());
bookList.add(book);
book = new Book("Java", 15.00, "4.5", Uri.parse("android.resource://com.example.seng.sechandapp/drawable/book").toString());
bookList.add(book);
HomePageAdapter homePageAdapter = new HomePageAdapter(getActivity(), bookList);
mRecyclerView.setAdapter(homePageAdapter);
return view;
}
}
app_bar_main
<?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.example.seng.sechandapp.MainPage">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:orientation="vertical">
<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="#color/blueviolet"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/home_page"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
home_page
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycleView">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
Try this
Make below Changes in you code
First Change
Use this in your HomePage Fragment
View view = inflater.inflate(R.layout.home_page, container, false);
instead of
View view = inflater.inflate(R.layout.app_bar_main, container, false);
Second Change
Now in your home_page add android:layout_marginTop="?actionBarSize" in your FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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_marginTop="?actionBarSize"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Some Other suggestion
Use implementation instead of compile in your build.gradle
because
Configuration compile is obsolete and has been replaced with implementation.
It will be removed at the end of 2018
You are inflating app_bar_main.xml instead of home_page.xml inside the Fragment. Replace View view = inflater.inflate(R.layout.app_bar_main, container, false); with View view = inflater.inflate(R.layout.home_page, container, false);. And where is the view with ID R.id.rl?
I need to set one imageView and Textview on the right side of toolbar in the navigationDrawer activity.But the thing is that the values for imageview and textviews are different for each fragment activity. And also I need to make the toolbar transparent.I am using default navigation drawer activity.Can anyone help? Any help will be appreciated.
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"
android:fitsSystemWindows="true"
tools:context="com.unitybees.photo.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
android:background="#null">
<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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="#+id/right_icon"
android:layout_gravity="right"
android:layout_width="40dp"
android:layout_height="40dp"/>
<TextView android:id="#+id/right_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textColor="#000000"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"/>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
public static final int right_image = 1;
public static final int right_texts = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView t = (TextView) findViewById(R.id.right_text);
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);
this.setTitle("");
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
displaySelectedScreen(item.getItemId());
return true;
}
private void displaySelectedScreen(int itemId) {
android.support.v4.app.Fragment fragment = null;
switch (itemId) {
case R.id.nav_menu1:
fragment = new Book_now();
break;
case R.id.nav_menu2:
fragment = new Payment();
break;
case R.id.nav_menu3:
fragment = new Account();
break;
}
if (fragment != null) {
FragmentTransactionft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
}
my Account.java
public class Account extends android.support.v4.app.Fragment {
TextView txt;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.account, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("");
}
}
Hello Try this if it may help
1) Put this method in your activity
public void initToolBar(String title) {
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(title);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.mipmap.ic_launcher_round);
toolbar.setNavigationOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "clicking the toolbar!", Toast.LENGTH_SHORT).show();
}
}
);
}
2) To update the method from your fragment call mehtod using
((MainActivity) getActivity()).initToolBar("Fragment Title");
im new in android im creating an app and i have a problem with a listfragment, because the list its showing but its overlapping the title bar(i had to add margin top to change that), and also im using a drawer and when im trying to open the list it shows over the options drawer too, letme paste you the code and the image so you can understan more:
The 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:weightSum="1">
<ListView
android:layout_width="match_parent"
android:layout_height="420dp"
android:id="#android:id/list"
android:layout_weight="0.82"
android:layout_marginTop="60dp" />
</LinearLayout>
The listFregment code:
public class FragmentoPrincipalChofer extends ListFragment {
private List<ParseObject> mViaje;
private ListView mLista;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.fragmento_principal_chofer, container, false);
mLista = (ListView)x.findViewById(android.R.id.list);
return x;
}
#Override
public void onResume() {
super.onResume();
//obtener pedido de taxi
ParseQuery<ParseObject> query = ParseQuery.getQuery("Viaje");
query.whereEqualTo("chofer", "prueba3");
query.addDescendingOrder("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> viajes, com.parse.ParseException e) {
if (e == null) {
mViaje=viajes;
String[] nombreUsuarios = new String[mViaje.size()];
int i=0;
for (ParseObject viaje : mViaje){
nombreUsuarios[i] = viaje.getString("cliente");
i++;
}
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1,nombreUsuarios);
mLista.setAdapter(adaptador);
} else {
}
}
});
}
}
the main drawer code:
public class DrawerPrincipal extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,FragmentoPrincipalUsuario.OnFragmentInteractionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_principal);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//cargar fragment principal usuario
FragmentoPrincipalChofer fragmento=(FragmentoPrincipalChofer) getSupportFragmentManager().findFragmentByTag("fragmento");
if (fragmento==null){
fragmento=new FragmentoPrincipalChofer();
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();
}
/*
FragmentoPrincipalUsuario principal = new FragmentoPrincipalUsuario();
android.support.v4.app.FragmentTransaction FragmentTransaction =
getSupportFragmentManager().beginTransaction();
FragmentTransaction.replace(R.id.contenedor_principal,principal);
FragmentTransaction.commit();
*/
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);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pantalla_principal_usuario, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.cuenta) {
// Handle the camera action
} else if (id == R.id.historial_viajes) {
} else if (id == R.id.contacto) {
} else if (id == R.id.compartir) {
} else if (id == R.id.version) {
} else if (id == R.id.salir) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
list fragment overlapping
here its the activity_drawer_principal.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_drawer_principal"
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_drawer_principal"
app:menu="#menu/activity_pantalla_principal_usuario_drawer" />
</android.support.v4.widget.DrawerLayout>
here its the app_bar_drawer_principal:
<?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=".DrawerPrincipal">
<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" />
<FrameLayout
android:id="#+id/contenedor_principal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<include layout="#layout/content_drawer_principal" />
</android.support.design.widget.CoordinatorLayout>
your problem is your setting your list fragment to the whole view Id with this line
// android.R.id.content is the WHOLE screen of your Activity
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();
Create a FrameLayout in your content_drawer_principal.xml:
<FrameLayout android:id="#+id/list_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
then do:
transaction.add(R.id.list_content,fragmento,"fragmento");
transaction.commit();
UPDATE
The real problem here is that your telling your FragmentTransaction to load your FragmentoPrincipalChofer into android.R.id.content which is a reserved id
android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID.
Revers the order of your activity layout, put list view brfore any other main content
im new in android im creating an app and i have a problem with a listfragment, because the list its showing but its overlapping the title bar(i had to add margin top to change that), and also im using a drawer and when im trying to open the list it shows over the options drawer too, letme paste you the code and the image so you can understan more:
The 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:weightSum="1">
<ListView
android:layout_width="match_parent"
android:layout_height="420dp"
android:id="#android:id/list"
android:layout_weight="0.82"
android:layout_marginTop="60dp" />
</LinearLayout>
The listFregment code:
public class FragmentoPrincipalChofer extends ListFragment {
private List<ParseObject> mViaje;
private ListView mLista;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.fragmento_principal_chofer, container, false);
mLista = (ListView)x.findViewById(android.R.id.list);
return x;
}
#Override
public void onResume() {
super.onResume();
//obtener pedido de taxi
ParseQuery<ParseObject> query = ParseQuery.getQuery("Viaje");
query.whereEqualTo("chofer", "prueba3");
query.addDescendingOrder("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> viajes, com.parse.ParseException e) {
if (e == null) {
mViaje=viajes;
String[] nombreUsuarios = new String[mViaje.size()];
int i=0;
for (ParseObject viaje : mViaje){
nombreUsuarios[i] = viaje.getString("cliente");
i++;
}
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1,nombreUsuarios);
mLista.setAdapter(adaptador);
} else {
}
}
});
}
}
the main drawer code:
public class DrawerPrincipal extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,FragmentoPrincipalUsuario.OnFragmentInteractionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_principal);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//cargar fragment principal usuario
FragmentoPrincipalChofer fragmento=(FragmentoPrincipalChofer) getSupportFragmentManager().findFragmentByTag("fragmento");
if (fragmento==null){
fragmento=new FragmentoPrincipalChofer();
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();
}
/*
FragmentoPrincipalUsuario principal = new FragmentoPrincipalUsuario();
android.support.v4.app.FragmentTransaction FragmentTransaction =
getSupportFragmentManager().beginTransaction();
FragmentTransaction.replace(R.id.contenedor_principal,principal);
FragmentTransaction.commit();
*/
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);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pantalla_principal_usuario, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.cuenta) {
// Handle the camera action
} else if (id == R.id.historial_viajes) {
} else if (id == R.id.contacto) {
} else if (id == R.id.compartir) {
} else if (id == R.id.version) {
} else if (id == R.id.salir) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
list fragment overlapping
here its the activity_drawer_principal.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_drawer_principal"
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_drawer_principal"
app:menu="#menu/activity_pantalla_principal_usuario_drawer" />
</android.support.v4.widget.DrawerLayout>
here its the app_bar_drawer_principal:
<?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=".DrawerPrincipal">
<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" />
<FrameLayout
android:id="#+id/contenedor_principal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<include layout="#layout/content_drawer_principal" />
</android.support.design.widget.CoordinatorLayout>
your problem is your setting your list fragment to the whole view Id with this line
// android.R.id.content is the WHOLE screen of your Activity
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();
Create a FrameLayout in your content_drawer_principal.xml:
<FrameLayout android:id="#+id/list_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
then do:
transaction.add(R.id.list_content,fragmento,"fragmento");
transaction.commit();
UPDATE
The real problem here is that your telling your FragmentTransaction to load your FragmentoPrincipalChofer into android.R.id.content which is a reserved id
android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID.
Revers the order of your activity layout, put list view brfore any other main content