I'm trying to do navigation drawer with 2 fragment(later i will add), and 4 activities. In my code when i call activity and close. It's very laggy. I want it very smooth. Please see my code.
ublic class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private NavigationView navigationView;
FragmentManager fragmentManager;
private String[] pageTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pageTitle = new String[]{getString(R.string.frag_home),
getString(R.string.nav_introduction),};
drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle(pageTitle[0]);
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.drawer_open, R.string.drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView)findViewById(R.id.nav_view);
// assert navigationView != null: Log.wtf("haha", "OMG");
assert navigationView != null;
navigationView.setNavigationItemSelectedListener(this);
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame, new Fragment_home()).commit();
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.nav_introduction: {
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame, new Fragment_Introduction()).commit();
}
break;
case R.id.nav_settings: {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
} break;
case R.id.nav_feedback: {
Intent intent = new Intent(MainActivity.this, FeedbackActivity.class);
startActivity(intent);
}
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onBackPressed() {
assert drawerLayout != null;
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}
else{
super.onBackPressed();
}
}
public void onClickHome(View view){
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame, new Fragment_home()).commit();
}
public void onClickFilter(View view){
Intent intent = new Intent(MainActivity.this, FIlterActivity.class);
startActivity(intent);
}
}
And my main activity code is following:
<LinearLayout 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">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
app:titleTextColor="#android:color/white" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="right">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4">
</LinearLayout>
<TextView
android:text=""
android:layout_width="0dp"
android:layout_height="match_parent"
android:textColor="#android:color/white"
android:gravity="right|center"
android:layout_weight="1"
android:onClick="onClickHome"/>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="3dp"
android:gravity="center"
android:src="#drawable/filter_icon"
android:onClick="onClickFilter"
/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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/drawer_menu" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
It's like when i use Activity and Fragment it's very laggy. Is there other way to do smooth?
Solution: I used handler. But it's not best.
case R.id.nav_settings: {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
}
}, 250);
} break;
Related
I'm trying to add clickable items from menu.xml. It just not working, even Toast effect.
Layout is copy/paste below so you can check it.
There is my MainActivity.java when Im trying using id's.
MainActivity:
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
private SectionsPagerAdapter mSectionsPagerAdapter;
private static final String TAG = "MainActivity";
private ViewPager mViewPager;
ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.hamburgermenu);
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
tabLayout.getTabAt(0).setText("Home").setIcon(R.drawable.homeicon);
tabLayout.getTabAt(1).setText("Section 1").setIcon(R.drawable.explorer);
tabLayout.getTabAt(2).setText("Section 2").setIcon(R.drawable.innovator);
tabLayout.getTabAt(3).setText("Section 3").setIcon(R.drawable.collar);
tabLayout.setTabTextColors(getResources().getColorStateList(R.color.white));
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.dashboard:
Intent intentExplorer = new Intent(this, Tab1Explorer.class);
startActivity(intentExplorer);
break;
case R.id.action_search:
//Display Toast for test
Log.d(TAG,"test");
Toast.makeText(this,"Search!", Toast.LENGTH_SHORT).show();
break;
case R.id.action_help:
Intent intentContact = new Intent(this,Tab3Collar.class);
startActivity(intentContact);
break;
case R.id.action_about:
Toast.makeText(this,"This is simple About Demo", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(this,"XYZ", Toast.LENGTH_LONG).show();
return super.onOptionsItemSelected(item);
}
Toast.makeText(this,"XYZ", Toast.LENGTH_LONG).show();
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0:
Tab0Home tab0 = new Tab0Home();
return tab0;
case 1:
Tab1Section tab1 = new Tab1Section();
return tab1;
case 2:
Tab2Section tab2 = new Tab2Section();
return tab2;
case 3:
Tab3Section tab3 = new Tab3Section();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
// Show 4 total pages.
return 4 ;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "Home";
case 1:
return "Section11";
case 2:
return "Section22";
case 3:
return "Section33";
}
return null;
}
}
}
menu_main.xml file looks fine. I have android:id's, icons, titles...
Activity_main.xml file looks:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.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:theme="?attr/actionBarTheme"
app:title="#string/app_name"/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="#style/CustomTextStyle">
<android.support.design.widget.TabItem
android:id="#+id/tabItem0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_0" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_1" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_2" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_3" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start">
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/white"
app:menu="#menu/menu_main">
</android.support.design.widget.NavigationView>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
I just want to display for example Toast when I click "Search" from Menu, but it not working. It just colapse hamburger menu without effect.
What should I change?
The navigation view does not use the onOptionsItemSelected method, instead you need to call navigationView.setOnNavigationItemSelectedListener
I have a working Navigation Drawer through out multiple activities and I am looking to highlight the current Navigation Item Selected after it changes the activity. How can I save the Item Selected Listener through out the whole app while the onCreateDrawer its recreating itself on every other activity?
If you need any other relevant activity or have any feedback, dont hesitate to indulge me!
BaseActivity
public class BaseActivity extends AppCompatActivity {
public NavigationView mNavigationView;
protected ActionBarDrawerToggle mToggle;
protected Toolbar mToolbar;
public DrawerLayout mDrawerLayout;
protected void onCreateDrawer() {
//Instantiate Navigation Drawer
setupNavDrawer();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.logout) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mToggle.onConfigurationChanged(newConfig);
}
//Set up Navigation Drawer
private void setupNavDrawer() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
mNavigationView= (NavigationView) findViewById(R.id.navigation_view);
setSupportActionBar(mToolbar);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.setDrawerListener(mToggle);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull final MenuItem item) {
mDrawerLayout.closeDrawers();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
switch (item.getItemId()){
case R.id.nav_home:
Intent intent0 = new Intent(BaseActivity.this, Home.class);
startActivity(intent0.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
mNavigationView.setCheckedItem(R.id.nav_home);
break;
case R.id.nav_settings:
Intent intent1 = new Intent(BaseActivity.this, Settings.class);
startActivity(intent1.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
mNavigationView.setCheckedItem(R.id.nav_settings);
break;
case R.id.nav_selection:
Intent intent2 = new Intent(BaseActivity.this, Selection.class);
startActivity(intent2.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
mNavigationView.setCheckedItem(R.id.nav_selection);
break;
case R.id.nav_privacy_policy:
Intent intent3 = new Intent(BaseActivity.this, PrivacyPolicy.class);
startActivity(intent3.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
mNavigationView.setCheckedItem(R.id.nav_privacy_policy);
break;
default:
Intent intent4 = new Intent(BaseActivity.this, Home.class);
startActivity(intent4.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
mNavigationView.setCheckedItem(R.id.nav_home);
break;
}
}
}, 225);
return true;
}
});
}
}
Home
public class Home extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
super.onCreateDrawer();
}
Selection
public class Selection extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selection);
super.onCreateDrawer();
}
Activity_Home.XML
<LinearLayout
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/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar" />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" >
<TextView
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<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:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#FFFF"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu"/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Activity_Selection.XML
<LinearLayout
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/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar" />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.navigationdrawer.Teste">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" >
<TextView android:text="Selection" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewTeste" />
</RelativeLayout>
<!-- <include layout="#layout/toolbar_actionbar" /> -->
<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:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#FFFF"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu"/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Once I also had similar problem. And here is what worked for me:
Change menu ids and class names as per your situation.
private void setupNavDrawer() {
// ...
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
switch (menuItem.getItemId()) {
case R.id.mainSection:
drawerLayout.closeDrawers();
startNewActivity(MainActivity.class);
break;
case R.id.allGames:
drawerLayout.closeDrawers();
startNewActivity(GamesActivity.class);
break;
return true;
}
});
setNavigationViewCheckedItem();
// .....
}
And here setNavigationViewCheckedItem mehod:
private void setNavigationViewCheckedItem() {
if (this.getClass().equals(MainActivity.class)) {
navigationView.setCheckedItem(R.id.mainSection);
} else if (this.getClass().equals(GamesActivity.class)) {
navigationView.setCheckedItem(R.id.allGames);
}
}
I have item on DrawerLayout which wraps items , basically I want to change the title on firing .setNavigationItemSelectedListener. I tried menuItem.setTitle(" New title "); but not working.
I saw on my main activity on the last part that this DrawerLayout is wrapped ActionBarDrawerToggle then title set on that part.
MainActivity :
public class MainActivity extends AppCompatActivity{
..
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
/* Setup click events on the Navigation View Items.*/
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
if (menuItem.getItemId() == R.id.nav_item_komisia) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
menuItem.setTitle(" New title "); // NOT WORKING
}
return false;
}
});
/* Setup Drawer Toggle of the Toolbar
R.string.app_name = Drawer With Swipe Tabs
*/
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
}
and activity_main.xml
<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
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:title="Drawer With Swipe Tabs" />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/drawerLayout"
>
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/containerView">
</FrameLayout>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/shitstuff"
app:itemTextColor="#color/black"
app:headerLayout="#layout/header_layout"
app:menu="#menu/drawermenu"
android:layout_marginTop="-24dp"
/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Try to add this line
toolbar.setTitle(menuItem.getTitle());
into setNavigationItemSelectedListener
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
// Add this
toolbar.setTitle(menuItem.getTitle());
if (menuItem.getItemId() == R.id.nav_item_komisia) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
menuItem.setTitle(" New title "); // NOT WORKING
}
return false;
}
});
I want use NavidationDrawer for application menu, but when click on NavigationItems not action! I want when click on NavigationItems, start other Activities.
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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundPrimary"
tools:context="com.tellfa.dastanak.Activities.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/main_appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.tellfa.dastanak.Components.CodeSaz_TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:singleLine="true"
android:textColor="#color/textLight"
android:textSize="25sp" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/main_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#color/tabIndicatorColor" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/main_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/menu_header"
app:itemIconTint="#color/colorPrimary"
app:itemTextColor="#color/colorPrimary"
app:menu="#menu/menu_main_page" />
</android.support.v4.widget.DrawerLayout>
Main code :
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
private DrawerLayout mDrawerLayout;
private ViewPager viewPager;
private TabLayout tabLayout;
private DataBase dataBase;
private Cat1_frag_AsyncTask task;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataBase = new DataBase(this);
try {
dataBase.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
toolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
TextView toolbar_text = (TextView) findViewById(R.id.toolbar_title);
toolbar_text.setText(R.string.app_name_fa);
final ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("");
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_driver);
actionBar.setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
viewPager = (ViewPager) findViewById(R.id.main_viewPager);
if (viewPager != null) {
setupViewPager(viewPager);
}
tabLayout = (TabLayout) findViewById(R.id.main_tabs);
if (tabLayout != null) {
tabLayout.setupWithViewPager(viewPager);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main_page, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
case R.id.action_recyclerView:
startActivity(new Intent(getApplicationContext(), Recycler_Page.class));
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_recyclerView:
startActivity(new Intent(getApplicationContext(), Recycler_Page.class));
}
return onNavigationItemSelected(item);
}
private void setupViewPager(ViewPager viewPager) {
Main_frag_adapter mainFragAdapter = new Main_frag_adapter(getSupportFragmentManager());
mainFragAdapter.addFragment(new Cat1_fragment_recycler(), "آموزنده");
mainFragAdapter.addFragment(new Cat2_fragment_recycler(), "عاشقانه");
mainFragAdapter.addFragment(new Cat3_fragment_recycler(), "بزرگان");
mainFragAdapter.addFragment(new Cat4_fragment_recycler(), "متفرقه");
viewPager.setAdapter(mainFragAdapter);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
return true;
}
});
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void forceRTLIfSupported() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
}
}
How can i it? tnx <3
put this line
startActivity(new Intent(getApplicationContext(), Recycler_Page.class));
in
private void setupDrawerContent(NavigationView navigationView)
so updated method is like:
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
startActivity(new Intent(getApplicationContext(), Recycler_Page.class));
mDrawerLayout.closeDrawers();
return true;
}
});
}
I'm trying to use the DrawerLayout with ActionBarDrawerToggle.
see my code below:
public class MainActivity extends ActionBarActivity {
private static final String TAG = MainActivity.class.getName();
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
FeedsFragment feeds = new FeedsFragment();
if (findViewById(R.id.main) != null) {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportFragmentManager().beginTransaction().add(R.id.main, feeds).commit();
} else if (findViewById(R.id.content) != null) {
NavDrawerFragment nav = new NavDrawerFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.nav, nav)
.add(R.id.content, feeds)
.commit();
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
mDrawerToggle.onConfigurationChanged(config);
}
}
and my view:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--<include layout="#layout/nav" />-->
<fragment
android:layout_width="240dp"
android:layout_height="match_parent"
android:name="com.primeirochute.com.primeirochute.fragment.NavDrawerFragment"
tools:layout="#layout/nav" />
</android.support.v4.widget.DrawerLayout>
But when I run my project, the left menu (DrawerLayout) just show on my screen doesn't make the slider event.
Finaly works:
I just added the gravity="start" at my second view
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--<include layout="#layout/nav" />-->
<fragment
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.primeirochute.com.primeirochute.fragment.NavDrawerFragment"
tools:layout="#layout/nav" />
</android.support.v4.widget.DrawerLayout>
Uses the function onOptionsItemSelected and control the drawer event:
public boolean onOptionsItemSelected(MenuItem item) {
int itemID = item.getItemId();
switch (itemID) {
case android.R.id.home: {
DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
if (drawerLayout.isDrawerOpen(Gravity.LEFT)) {
drawerLayout.closeDrawers();
}
else {
drawerLayout.openDrawer(Gravity.LEFT);
}
}
break;
return true;
}
I hope it works for your case.