I can't seem to figure this one out. I have MainActivity and created SecondActivity and ThirdActivity that I want to be able to navigate to.
I'm using BottomNavigation in my MainActivity to navigated between activities:
public class MainActivity extends AppCompatActivity {
protected BottomNavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_menuItem1:
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
return true;
case R.id.navigation_menuItem2:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
return true;
case R.id.navigation_menuItem3:
Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
startActivity(intent);
return true;
}
return false;
}
};
navigationView = (BottomNavigationView) findViewById(R.id.navigation);
navigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
Any clue why it's not switching pages/activities?
EDIT: Added these lines to make it work:
protected BottomNavigationView navigationView;
AND
navigationView = (BottomNavigationView) findViewById(R.id.navigation);
navigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
Thanks for the help!
just in case you missed something, make sure you didn't forget something:
Create a BottomNavigationView in xml of your layout:
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
Create a file here navigation.xml in menu resource folder. This file is used for providing the MenuItems in BottomNavigationView
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_menuItem1"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/menuItem1" />
<item
android:id="#+id/navigation_menuItem2"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/menuItem2" />
<item
android:id="#+id/navigation_menuItem3"
android:icon="#drawable/ic_notifications_black_24dp"
android:title="#string/menuItem3" />
</menu>
Now lets set the listener for the Click Events OnNavigationItemSelectedListener and OnNavigationItemReselectedListener on Menu Items:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_menuItem1:
return true;
case R.id.navigation_menuItem2:
return true;
case R.id.navigation_menuItem3:
return true;
}
return true;
}
};
private BottomNavigationView.OnNavigationItemReselectedListener mOnNavigationItemReselectedListener = new BottomNavigationView.OnNavigationItemReselectedListener() {
#Override
public void onNavigationItemReselected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_menuItem1:
Log.d(TAG, "navigation_menuItem1 Reselected ===");
break;
case R.id.navigation_menuItem2:
Log.d(TAG, "navigation_menuItem2 Reselected ===");
break;
case R.id.navigation_menuItem3:
Log.d(TAG, "navigation_menuItem3 Reselected ===");
break;
}
}
};
bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
bottomNavigationView.setOnNavigationItemReselectedListener(mOnNavigationItemReselectedListener);
EDIT
Add this to your onCreate()
BottomNavigationView bottomNavigationView;
bottomNavigationView = findViewById(R.id.navigation);
Related
I created a setting activity. When the user clicks setting it move from mainactivity to settingsactivity but it does not go to the previous page when I click the back button at the top of the action bar it says app keeps stopping. If I click phone back button it works.
Here I attached my coding correct it. How can I do this?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
public static class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
{
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
It's preferred to use finish() in an activity, since it'll automatically go back to the last activity.
first, add to the tollbar in the XML file
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" >
<ImageView
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_back"/>
</androidx.appcompat.widget.Toolbar>
and
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
When I'm trying to switch between activies using intent in "switch case" the following error I got:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.bottomnavigation.BottomNavigationView.setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation2);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_home:
return true;
case R.id.ic_wallet:
Intent intent_wallet = new Intent(MainActivity.this,Wallet.class);
startActivity(intent_wallet);
return true;
case R.id.ic_status:
Intent intent_status = new Intent(MainActivity.this,Status.class);
startActivity(intent_status);
return true;
case R.id.ic_history:
Intent intent_history = new Intent(MainActivity.this,History.class);
startActivity(intent_history);
return true;
}
return false;
}
};
}
activity_main.xml
<androidx.coordinatorlayout.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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.praveen.gupta.frontpage.MainActivity">
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/navigation2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#drawable/white_grey_border_top"
app:labelVisibilityMode="labeled"
app:menu="#menu/navigation" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="#+id/ic_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="Home"
/>
<item
android:id="#+id/ic_wallet"
android:icon="#drawable/ic_account_balance_wallet_black_24dp"
android:title="Wallet"
/>
<item
android:id="#+id/ic_status"
android:icon="#drawable/ic_star_border_black_24dp"
android:title="Status"
/>
<item
android:id="#+id/ic_history"
android:icon="#drawable/ic_history_black_24dp"
android:title="History"
/>
</menu>
mOnNavigationItemSelectedListener is null thats why you are getting error.You are initializing outside on oncreate method.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//move `mOnNavigationItemSelectedListener` code here .
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_home:
return true;
case R.id.ic_wallet:
Intent intent_wallet = new Intent(MainActivity.this,Wallet.class);
startActivity(intent_wallet);
return true;
case R.id.ic_status:
Intent intent_status = new Intent(MainActivity.this,Status.class);
startActivity(intent_status);
return true;
case R.id.ic_history:
Intent intent_history = new Intent(MainActivity.this,History.class);
startActivity(intent_history);
return true;
}
return false;
}
};
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation2);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
I want to start new activity by bottom navigation bar but it not working.
I have checked many tutorials also, but do not know where problem the problem is.
Here is my code.
public class display extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
Intent i = new Intent(display.this, Addab.class);
startActivity(i);
break;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
break;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
break;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
#Override
public boolean onNavigationItemSelected(#NonNull final MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
startActivity(new Intent(this, Abc.class));
Break;
case R.id.navigation_camera:
startActivity(new Intent(this, Cab.class));
Break;
}
finish();
return true;
}
Also Refer here-->
https://blog.fossasia.org/using-activities-with-bottom-navigation-view-in-phimpme-android/
instead of break; use return true;
I am a newbie to Android development. I am learning UI designing as of now. I want a solution where there is a bottom bar with 5 options linked directly to 5 different activities. I got Java solutions from other stack overflow answers (How to change activity on bottom navigation button click? ) - 2nd Answer by sushil, but it has no activity - XML files in it for me to understand.
Bottom bar like this:
Activity to be loaded based on bottom bar:
As per your attached image, you should used Fragment instead of Activity for 5 different MenuItem or options to achieve your desired output.
1. Create 5 different Fragments for 5 different MenuItem.
For example: MatchingFragment, WatchListFragment, RatesFragment, DealsFragment and ListingFragment.
2. Add OnNavigationItemSelectedListener to your NavigationView and change Fragment as per your selected MenuItem. Use below code to change Fragment:
// Set action to perform when any menu-item is selected.
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Change Fragment
selectFragment(item);
return false;
}
});
/**
* Perform action when any item is selected.
*
* #param item Item that is selected.
*/
protected void selectFragment(MenuItem item) {
item.setChecked(true);
switch (item.getItemId()) {
case R.id.action_matching:
// Action to perform when Matching Menu item is selected.
pushFragment(new MatchingFragment());
break;
case R.id.action_watch_list:
// Action to perform when WatchList Menu item is selected.
pushFragment(new WatchListFragment());
break;
case R.id.action_rates:
// Action to perform when Rates Menu item is selected.
pushFragment(new RatesFragment());
break;
case R.id.action_deals:
// Action to perform when Deals Menu item is selected.
pushFragment(new DealsFragment());
break;
case R.id.action_listing:
// Action to perform when Listing Menu item is selected.
pushFragment(new ListingFragment());
break;
}
}
/**
* Method to push any fragment into given id.
*
* #param fragment An instance of Fragment to show into the given id.
*/
protected void pushFragment(Fragment fragment) {
if (fragment == null)
return;
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
FragmentTransaction ft = fragmentManager.beginTransaction();
if (ft != null) {
ft.replace(R.id.rootLayout, fragment);
ft.commit();
}
}
}
Here is the complete Tutorial: Android Bottom Navigation View Tutorial With Example
Hope this will help~
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favorites:
Intent intent1 = new Intent(this, AnActivity.class);
startActivity(intent1);
break;
case R.id.action_schedules:
Intent intent2 = new Intent(this, AnotherActivity.class);
startActivity(intent2);
break;
case R.id.action_music:
Intent intent3 = new Intent(this, AnotherActivity.class);
startActivity(intent3);
break;
}
return true;
}
});
Base Activity :
public abstract class BaseActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
protected BottomNavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewId());
navigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
navigationView.setOnNavigationItemSelectedListener(this);
}
#Override
protected void onStart() {
super.onStart();
updateNavigationBarState();
}
// Remove inter-activity transition to avoid screen tossing on tapping bottom navigation items
#Override
public void onPause() {
super.onPause();
overridePendingTransition(0, 0);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
navigationView.postDelayed(() -> {
int itemId = item.getItemId();
if (itemId == R.id.navigation_analysis) {
startActivity(new Intent(getApplicationContext(), AnalysisActivity.class));
} else if (itemId == R.id.navigation_dashboard) {
startActivity(new Intent(getApplicationContext(), DashboardActivity.class));
} else if (itemId == R.id.navigation_profile) {
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}
finish();
}, 100);
return true;
}
private void updateNavigationBarState(){
int actionId = getNavigationMenuItemId();
selectBottomNavigationBarItem(actionId);
}
void selectBottomNavigationBarItem(int itemId) {
Menu menu = navigationView.getMenu();
for (int i = 0, size = menu.size(); i < size; i++) {
MenuItem item = menu.getItem(i);
boolean shouldBeChecked = item.getItemId() == itemId;
if (shouldBeChecked) {
item.setChecked(true);
break;
}
}
}
abstract int getContentViewId();
abstract int getNavigationMenuItemId();
}
Make sure to use getApplicationContext in onNavigationSelected().
Dashboard Activity:
public class DashboardActivity extends BaseActivity {
#Override
int getContentViewId() {
return R.layout.activity_dashboard ;
}
#Override
int getNavigationMenuItemId() {
return R.id.navigation_dashboard;
} }
Similarly - make the other activities just like this.
XML PART
bottom_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.BottomNavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation"
/>
navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_analysis"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_analysis" />
<item
android:id="#+id/navigation_dashboard"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_dashboard" />
<item
android:id="#+id/navigation_profile"
android:icon="#drawable/ic_notifications_black_24dp"
android:title="#string/title_profile" />
</menu>
activity_dashboard.xml
<?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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/frame_dashboard">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_dashboard"/>
</FrameLayout>
<include
layout="#layout/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
/>
</LinearLayout>
Similarly - make other xml files just like this.
This should hopefully help
I've got to this point of creating navigation drawer where I don't know how to make the buttons clickable from the drawer.
My MainActivity:
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)){
return true;
}
else{
switch (item.getItemId()){
case R.id.nav_menu:
Toast.makeText(MainActivity.this, "Menu!", Toast.LENGTH_SHORT).show();
return true;
case R.id.nav_setings:
Toast.makeText(MainActivity.this, "Settings!", Toast.LENGTH_SHORT).show();
return true;
}
}
return super.onOptionsItemSelected(item);
}
}
Please note that switch statement in onOptionsItemSelected doesn't work.
My menu xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/nav_menu"
android:title="My Account"/>
<item android:id="#+id/nav_setings"
android:title="Settings"/>
<item android:id="#+id/nav_logout"
android:title="Log Out"/>
</menu>
So, my question is how to make those items from the drawer clickable?
Assuming you also have a NavigationView inside your DrawerLayout, you just have to add an OnNavigationItemSelectedListener:
NavigationView navigation = (NavigationView) findViewById(R.id.navigation);
navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch(item.getItemId()) {
case R.id.nav_menu:
// Handle menu click
return true;
case R.id.nav_settings:
// Handle settings click
return true;
case R.id.nav_logout:
// Handle logout click
return true;
default:
return false;
}
}
});