One activity with fragments and a navigation drawer - java

So, I want to build an app that has only one activity and will use multiple fragments. I have a log in activity, and after I make the log in I go to my maintactivity that has a fragment and a view-pager so I can swipe through some fragments(this fragments will be like a day of a calendar). My navigation drawer will be used to open other fragments, no the same as the ones mentioned before. My issues are, after I apply the view-pager, my button to open the drawer doesn't work and my fragments doesn't change when I click in the navigation drawer items, is keeps the fragment on top (I open it with the slide movement, since the button doesn't work). Here is my code:
MainActivity.java
package amsi.dei.estg.ipleiria.pt.projeto;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
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.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private static final int NUM_PAGES = 5;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
setSupportActionBar(toolbar);
final 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);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
//since I have two onBackPressed, on for the drawer and other for my pageview, I left the one I thought would be more useful.
/*#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new Dia();
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
public boolean onNavigationItemSelected(MenuItem item){
int id = item.getItemId();
Fragment fragment = null;
if(id==R.id.nav_dia)
{
fragment = new Dia();
}
else if(id==R.id.nav_meusAlimentos)
{
fragment = new MeusAlimentos();
}
else if(id==R.id.nav_login)
{
Intent login = new Intent(this, LogIn.class);
startActivity(login);
}
if(fragment != null){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.screen_area, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
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">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<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>
content_main.xml
<?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="amsi.dei.estg.ipleiria.pt.projeto.MainActivity"
tools:showIn="#layout/app_bar_main">
<FrameLayout
android:id="#+id/screen_area"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Dia.java
package amsi.dei.estg.ipleiria.pt.projeto;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by nelsu on 24/11/2017.
*/
public class Dia extends Fragment {
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Day");
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootview = (ViewGroup) inflater.inflate(R.layout.activity_dia_content, container, false);
return rootview;
//return inflater.inflate(R.layout.activity_dia_content, null);
}
}
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"
tools:context="amsi.dei.estg.ipleiria.pt.projeto.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_dia_content.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:clickable="false"
android:focusable="false">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/rectangle_dia_macros"
android:orientation="vertical">
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="gráficos e percentagem das macros"
android:textAlignment="center"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="#drawable/rectangle_dia_refeicao"
android:text="Pequeno Almoço" />
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="#drawable/rectangle_dia_refeicao"
android:text="Café da manhã" />
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="#drawable/rectangle_dia_refeicao"
android:text="Almoço" />
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="#drawable/rectangle_dia_refeicao"
android:text="Lanche" />
<TextView
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="#drawable/rectangle_dia_refeicao"
android:text="Jantar" />
<TextView
android:id="#+id/textView12"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="#drawable/rectangle_dia_refeicao"
android:text="Lanche da noite" />
<Space
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
app:srcCompat="#mipmap/ic_launcher_round" />
<TextView
android:id="#+id/label_nome_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio#android.com" />
</LinearLayout>
activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_dia"
android:icon="#drawable/ic_menu_camera"
android:title="Dia" />
<item
android:id="#+id/nav_meusAlimentos"
android:icon="#drawable/ic_menu_gallery"
android:title="Meus Alimentos" />
<item
android:id="#+id/nav_login"
android:icon="#drawable/ic_menu_slideshow"
android:title="Login" />
<item
android:id="#+id/nav_manage"
android:icon="#drawable/ic_menu_manage"
android:title="Tools" />
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_menu_manage"
android:title="Settings" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_menu_manage"
android:title="About" />
<item
android:id="#+id/nav_quit"
android:icon="#drawable/ic_menu_manage"
android:title="Quit" />
</group>
</menu>

You don't have to write your own code for this.
here is a nice library for your problem with all required things.
https://github.com/chrisbanes/cheesesquare

Related

Navigation drawer is not collapsing, no hamburger button is there

My drawer layout is showing as a normal activity without any button in the toolbar also its not collapsing in any way..I want it from right side.
This is my drawer activity layout..
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".myDrawer"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#000"
android:id="#+id/drawer_toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:background="#color/colorPrimaryDark"
android:layout_width="100dp"
android:layout_height="match_parent"
android:foregroundGravity="left"
app:menu="#menu/drawer_menu"
app:headerLayout="#layout/nav_header">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
this is my nav_header.xml layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="300dp"
android:padding="15dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:layout_width="100dp"
android:layout_height="200dp"
android:src="#drawable/hhh"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="pehli line"
android:textColor="#color/colorPrimaryDark"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dusri line"
android:textColor="#color/colorAccent"/>
</LinearLayout>
This is drawer activity java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class myDrawer extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_drawer);
toolbar=findViewById(R.id.drawer_toolbar);
setSupportActionBar(toolbar);
}
}
Output of above code-->
but I want it a navigation drawer, that i can slide from left.
Thank you so much.
Replace your navigation view with below
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/my_menu">
</android.support.design.widget.NavigationView>
Add below method to display toggel
public void addToggel() {
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);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
}
Add below string in string.xml
<string name="navigation_drawer_open">open</string>
<string name="navigation_drawer_close">close</string>
update drawer layout id and navigation view id as per your code
Let me know with more help

Android navigation drawer does not close when click outside the drawer (a fragment in main activity)

For some reason, the navigation drawer does not close when tapping outside the drawer.Its another fragment with ListView in MainActivity:
When tapping the fragment on right it does not close drawer instead it acts like as if the fragment occupies the whole screen and the click listener is still active in the fragment.
activity_main.xml:
<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:id="#+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_orange_dark"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:title="Apartment Guide"
app:titleTextColor="#android:color/white" />
<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:layout_below="#+id/toolbar"
tools:context=".MainActivity">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/white"
android:fitsSystemWindows="true"
app:headerLayout="#layout/header"
app:itemTextColor="#android:color/darker_gray"
app:menu="#menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>
<FrameLayout
android:id="#+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar">
</FrameLayout>
MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawerLayout = findViewById(R.id.drawer);
fragmentContent = findViewById(R.id.fragment_content);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_opened, R.string.drawer_closed) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(R.string.drawer_opened);
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getSupportActionBar().setTitle(R.string.drawer_closed);
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
fragmentContent.setTranslationX(slideOffset * drawerView.getWidth());
mDrawerLayout.bringChildToFront(drawerView);
mDrawerLayout.requestLayout();
}
};
mToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null)
selectDrawerItem(null);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
Not sure where I'm doing wrong as closing drawer when tapping outside the drawer area should be the default behavior in drawerLayouts.
This happens because you NavigationDrawer is not capturing the view because the activity elements are not included in it, and to have it working properly, you have to make the NavigationDrawer is the main root of your Activity layout and the RelativeLayout is a child of it, like the following:
<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:layout_below="#+id/toolbar"
tools:context=".MainActivity">
<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:id="#+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_orange_dark"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:title="Apartment Guide"
app:titleTextColor="#android:color/white" />
<FrameLayout
android:id="#+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar">
</FrameLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/white"
android:fitsSystemWindows="true"
app:headerLayout="#layout/header"
app:itemTextColor="#android:color/darker_gray"
app:menu="#menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>

Navigation menu not working in Child Activity

so i made a menu class and now i try to extend my others activitys from that one
i get the button but the slide its not working in the others activitys but its apears on the left top... lest go to the code
this is my xml activity_navigatoin_menu
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer"
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="ar.com.puebloyreforma.pyr.NavigationMenu">
<android.support.design.widget.NavigationView
app:headerLayout="#layout/menuheader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/caldroid_black"
app:itemTextColor="#color/white"
app:itemIconTint="#color/white"
app:menu="#menu/drawermenu"
android:layout_gravity="start"
>
</android.support.design.widget.NavigationView>
this is my menudrawer
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/noticias" android:title="Noticias" android:icon="#drawable/iconews"></item>
<item android:id="#+id/calendario" android:title="Calendario" android:icon="#drawable/icocalen"></item>
<item android:id="#+id/sysacad" android:title="Sysacad" android:icon="#drawable/icosysa"></item>
<item android:id="#+id/contacto" android:title="Contacto" android:icon="#drawable/icocon"></item>
<item android:id="#+id/reglamento" android:title="Reglamento" android:icon="#drawable/icoreg"></item>
</menu>
and here my activity
package ar.com.puebloyreforma.pyr;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
public class NavigationMenu extends AppCompatActivity {
private DrawerLayout mDL ;
private ActionBarDrawerToggle mT ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_menu);
mDL = (DrawerLayout) findViewById(R.id.drawer) ;
mT = new ActionBarDrawerToggle(this , mDL ,R.string.open , R.string.close) ;
mDL.addDrawerListener(mT);
mT.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mT.onOptionsItemSelected(item)){
return true ;
}
return super.onOptionsItemSelected(item);
}
}
and this is one child activity
package ar.com.puebloyreforma.pyr;
import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
public class PdfsActivity extends NavigationMenu {
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String target ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdfs);
Button tt , tm , tn, reg ;
tt = (Button)this.findViewById(R.id.tt);
tm = (Button)this.findViewById(R.id.tm);
tn = (Button)this.findViewById(R.id.tn);
reg = (Button)this.findViewById(R.id.reg);
final String pdfs[] = {"tm" ,
"tt" ,
"tn",
"Ord1549"
} ;
tm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setTarget(pdfs[0]);
Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class);
hola.putExtra("TARGET", getTarget());
startActivity(hola);
}
});
tt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setTarget(pdfs[1]);
Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class);
hola.putExtra("TARGET", getTarget());
startActivity(hola);
}
});
tn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setTarget(pdfs[2]);
Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class);
hola.putExtra("TARGET", getTarget());
startActivity(hola);
}
});
reg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setTarget(pdfs[3]);
Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class);
hola.putExtra("TARGET", getTarget());
startActivity(hola);
}
});
}
that has its onw layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="ar.com.puebloyreforma.pyr.PdfsActivity">
<Button
android:id="#+id/tm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAñANA"
android:layout_marginRight="60dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.758"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="#+id/tt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TARDE"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.335"
android:layout_marginRight="123dp"
app:layout_constraintRight_toRightOf="parent" />
<Button
android:id="#+id/tn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NOCHE"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="68dp"
app:layout_constraintVertical_bias="0.572"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="#+id/reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REGLA"
app:layout_constraintLeft_toRightOf="#+id/tn"
android:layout_marginLeft="0dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintVertical_bias="0.572"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.713" />
</android.support.constraint.ConstraintLayout>
Let me post my code with which I manage to obtain NavigationDrawer in child activity.
MyAppCompatActivity(ParentActivity class)
public abstract class MyAppCompatActivity extends BaseAppCompatActivity {
#Override
public void setContentView(#LayoutRes int layoutResID) {
CoordinatorLayout activityParentBase = (CoordinatorLayout) getLayoutInflater().inflate(R.layout.activity_navigation_drawer, null);
RelativeLayout content = (RelativeLayout) activityParentBase.findViewById(R.id.content);
setContentView(activityParentBase);
getLayoutInflater().inflate(layoutResID, content, true);
super.setContentView(activityParentBase);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
MyAppCompatActivity.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(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_item1) {
} else if (id == R.id.nav_item2) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
}
}
activity_navigation_drawer.xml(ParentActivity XML)
<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=".MyAppCompatActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
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>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content" />
<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:menu="#menu/menu_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>
ChildNavigationActivity (ChildActivity class)
public class ChildNavigationActivity extends MyAppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child_navigation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //we have included "toolbar" in the parentactivity xml. So not need to do it in childactivity xml.
/**NOTE: Do Not use "setSupportActionBar(toolbar)" since you have already done it in your parent activity*/
toolbar.setTitle("ChildNavigationActivity");
}
}
activity_child_navigation(ChildActivity XML)
<FrameLayout 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=".ChildNavigationActivity">
<!--Put all your ChildActivity Widgets here-->
</FrameLayout>

Android Studio have different toolbar items on different activity's

I am very new when it comes to android app development and android studio.
I have been searching around for some couple days but still havent found the solution.
How can i have different toolbar Items on different activity's is their an way to do that ?
Do you have an example so i could take a look at that?
Do you know an tutorial that has that in it?
already much thanks for the help.
MainActivity.java
package com.example.stage.absa;
import android.Manifest;
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class MainActivity extends AbsRuntimePermission {
private ZXingScannerView scannerView;
private static final int REQUEST_PERMISSION = 10;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
requestAppPermissions(new String[]{
Manifest.permission.CAMERA},
R.string.msg, REQUEST_PERMISSION);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (id == R.id.action_settings) {
setContentView(R.layout.activity_settings);
return true;
}
return super.onOptionsItemSelected(item);
}
public void browser1(View view) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.suppliance.nl/"));
startActivity(browserIntent);
}
public void scanCode(View view) {
scannerView = new ZXingScannerView(this);
scannerView.setResultHandler(new ZxingScannerResultHandler());
setContentView(scannerView);
scannerView.startCamera();
}
class ZxingScannerResultHandler implements ZXingScannerView.ResultHandler {
#Override
public void handleResult(Result result) {
String resultCode = result.getText();
Toast.makeText(MainActivity.this, resultCode, Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
scannerView.stopCamera();
}
}
#Override
public void onPermissionsGranted(int requestCode) {
//Do anything when permisson granted
Toast.makeText(getApplicationContext(), "Toegang geaccepteerd", Toast.LENGTH_LONG).show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
<!-- Layout for content is here. This can be a RelativeLayout -->
<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="com.example.stage.absa.MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="browser1"
android:layout_margin="2dp"
android:src="#drawable/suppliance" />
<Button
android:id="#+id/S"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="scanCode"
android:layout_margin="5dp"
android:backgroundTint="#color/colorPrimary"
android:text="Scan" />
</RelativeLayout>
</LinearLayout>
SettingsActivity.java
package com.example.stage.absa;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
public class SettingsActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}
}
activity_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
app:titleTextColor="#android:color/white"
android:layout_height="?attr/actionBarSize">
<ImageView
android:id="#+id/tv_header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginRight="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Name"
android:textColor="#ffffff"
android:gravity="center"
android:id="#+id/brand_name" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_marginLeft="5dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C Name :"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:textColor="#ffffff"
android:id="#+id/t1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name :"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:textColor="#ffffff"
android:id="#+id/t2" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<!-- Layout for content is here. This can be a RelativeLayout -->
<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="com.example.stage.absa.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Settings" />
</RelativeLayout>
</LinearLayout>
menu_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_back"
android:title="Back"
app:showAsAction="ifRoom" />
</menu>
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_settings"
android:title="Settings"
app:showAsAction="ifRoom" />
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu1, menu);
return true;
}
Just pass different menu resources for the different activities ie R.id.menu1 or R.menu.menu2.
You can set custom toolbar layout for any activity like.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
app:titleTextColor="#android:color/white"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay">
<ImageView
android:id="#+id/tv_header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginRight="20dp"
android:src="#drawable/siyaram_logo"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Name"
android:textColor="#ffffff"
android:gravity="center"
android:id="#+id/brand_name" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_marginLeft="5dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C Name :"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:textColor="#ffffff"
android:id="#+id/t1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name :"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:textColor="#ffffff"
android:id="#+id/t2" />
</LinearLayout>
</android.support.v7.widget.Toolbar>

Blank Actionbar on first Android app

This is my first android app and I'm trying to add the Actionbar at the top but i cant seem to get it to work. All I get is a Blank blue bar across the top with no title and no overflow menu. To create this i used the instructions found here http://developer.android.com/training/appbar/index.html but even following the instructions to a T it doesn't seem to work. Even though I am not getting a compile error i was wondering if i was doing something wrong?
MainActivity.java
package com.caseybowman.studdybuddy;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
RelativeLayout main_view = (RelativeLayout) findViewById(R.id.Semesters);
switch(item.getItemId()){
case R.id.add:
//do something
return true;
case R.id.edit:
return true;
case R.id.settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.caseybowman.studdybuddy.MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Semesters">
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.caseybowman.studdybuddy.MainActivity">
<item
android:id="#+id/add"
android:orderInCategory="100"
android:title="#string/Add"
app:showAsAction="ifRoom|withText" />
<item
android:id="#+id/edit"
android:orderInCategory="100"
android:title="#string/Edit"
app:showAsAction="IfRoom|withText" />
<item
android:id="#+id/settings"
android:orderInCategory="100"
android:title="#string/Settings"
app:showAsAction="IfRoom|withText" />
</menu>
Try to add this code
getSupportActionBar().setTitle("Your Title");
Or you can customize your toolbar by defining one TextView inside the toolbar and assign your title like this.
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Title"/>
</android.support.v7.widget.Toolbar>
Try this
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/id_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="#+id/id_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

Categories

Resources