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;
Related
I add to my app Bottom Navigation Bar it's work but the problem now I want move from one button in Bottom Navigation Bar to activity that activity have to fragment to view (TabLayout,ViewPager).
When I try to make it like that selectedFragment = new MainActivityOderLIstFargmant(); the show me error like that ..
and also like that and also not work.. startActivity(new Intent(NafMain.this, MainActivityOderLIstFargmant.class));
I need to move to other activity to show two (TabLayout,ViewPager).
this activity I want to move to it ..
public class MainActivityOderLIstFargmant extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
PageAdapterOrderList pageAdapterOrderList;
TabItem tabChats;
TabItem tabCalls;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mani_order_list);
tabLayout = findViewById(R.id.tablayout);
tabChats = findViewById(R.id.tabChats);
tabCalls = findViewById(R.id.tabCalls);
viewPager = findViewById(R.id.viewPager);
pageAdapterOrderList = new PageAdapterOrderList(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pageAdapterOrderList);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
}
from here
public class NafMaintest extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_naf_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.nav_home:
selectedFragment = new MainActivityOderLIstFargmant();//I try like that but not work
break;
case R.id.nav_favorites:
selectedFragment = new FragmentHome();
break;
case R.id.nav_search:
selectedFragment = new FragmentProfile();
break;
case R.id.nav_s:
selectedFragment = new MainActivity();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new FragmentHome()).commit();
}
}
}
If anyone know how solution it help me
you are trying to instantiate MainActivityOderLIstFargmant activity in the switch case R.id.nav_home and put it into a Fragment variable which is called selectedFragment, so both types are different (Fragment & Activity). That is why the screenshot you provided says Incompatible types.
To get your code works you can change the switch case as below
case R.id.nav_home:
Intent intent = new Intent(NafMaintest.this, MainActivityOderLIstFargmant.class);
startActivity(intent);
return true; // use return to avoid triggering the fragment transaction after the `swtich` block
I have created a Bottom Navigation Bar that switches between 4 fragments. It works fine, however, I need to double tap a menu item in order to switch between fragments. This is not a feature I want nor do I know how it even came about.
The tutorials I was following all require a single tap to switch between fragments so I am very confused.
My MainActivity.java
public class MainActivity extends AppCompatActivity
{
private FirebaseAuth auth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() == null) {
startActivity(new Intent(MainActivity.this, Login_Page_Activity.class));
}
BottomNavigationView bottomnav = findViewById(R.id.bottom_navigation);
bottomnav.setOnNavigationItemReselectedListener(navListener); //this
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new explore_fragment()).commit();
}
private BottomNavigationView.OnNavigationItemReselectedListener navListener=
new BottomNavigationView.OnNavigationItemReselectedListener() {
#Override
public void onNavigationItemReselected(#NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.nav_explore:
selectedFragment = new explore_fragment();
break;
case R.id.nav_add:
selectedFragment = new add_events_fragment();
break;
case R.id.nav_events:
selectedFragment = new events_fragment();
break;
case R.id.nav_chat:
selectedFragment = new Chats_fragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
}
};
Maybe the problem is Item ReselectedListener
try item selected listener
public class MainActivity extends AppCompatActivity
{
private FirebaseAuth auth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() == null) {
startActivity(new Intent(MainActivity.this, Login_Page_Activity.class));
}
BottomNavigationView bottomnav = findViewById(R.id.bottom_navigation);
bottomnav.setOnNavigationItemSelectedListener(navListener); //this
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new explore_fragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener=
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public void onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.nav_explore:
selectedFragment = new explore_fragment();
break;
case R.id.nav_add:
selectedFragment = new add_events_fragment();
break;
case R.id.nav_events:
selectedFragment = new events_fragment();
break;
case R.id.nav_chat:
selectedFragment = new Chats_fragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
}
};
I have MainActivity which contains fragments (for example 3 fragments) and I can select fragment by BottomNavigationView.
Also i have button in OneFragment which start SecondActivity (which also contains BottomNagigationView).
My question: When in SecondActivity i select bottomNV itemId two, I need to back to MainActivity with selected TwoFragment.
How can i realize it?
MainActivity.class
public class MainActivity extends AppCompatActivity {
private BottomNavigationView navigationView;
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView = findViewById(R.id.navigationBottom);
navigationView.setOnNavigationItemSelectedListener(navListelener);
getSupportFragmentManager().beginTransaction().replace(R.id.content_main,
new OneFragment()).commit();
}
public BottomNavigationView.OnNavigationItemSelectedListener navListelener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
setTitle(item.getTitle());
switch (item.getItemId()){
case R.id.bottom_one:
selectFragment(new OneFragment());
break;
case R.id.bottom_two:
selectFragment(new TwoFragment());
break;
case R.id.bottom_three:
selectFragment(new ThreeFragment());
break;
}
return true;
}
};
private void selectFragment(Fragment selectedFragment){
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_main, selectedFragment)
.commit();
}
}
SecondActivity.class
public class SecondActivity extends AppCompatActivity {
private BottomNavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seconda);
navigationView = findViewById(R.id.navigationBottom);
navigationView.setOnNavigationItemSelectedListener(navListener);
Menu menu = navigationView.getMenu();
MenuItem menuItem = menu.getItem(1);
menuItem.setChecked(true);
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.bottom_one:
finish();
break;
case R.id.bottom_two:
//need to return to MainActivity with selected TwoFragment
break;
case R.id.bottom_three:
break;
}
return true;
}
};
}
I have seen in your code that the second activity is called SecondaActivity instead of SecondActivity. I supposed it is being misspelled and my code reflects the last one.
You can start the second activity using (someRequestCode is just a random int you can choose):
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, someRequestCode);
Then in your second activity you can return OK in case 2 and KO in other cases (just for simplicity. If you want to elaborate a bit more you response, you can add some extra):
case R.id.bottom_two:
//need to return to MainActivity which selected TwoFragment
Intent data = new Intent();
data.putExtra("key", "value"); // You can add data if needed. For example, number of fragment to be changed
setResult(RESULT_OK, data);
finish();
break;
And finally in your MainActivity, override onActivityResult (someRequestCode is the same value as stated before):
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == someRequestCode) {
if (resultCode == RESULT_OK) {
// Here you can get key, value pair from extra and act accordingly
selectFragment(new TwoFragment());
} else {
// Do something else
}
}
}
case R.id.bottom_two:
//need to return to MainActivity which selected TwoFragment
Intent intent = new Intent(SecondaActivity.this,MainActivity.class);
intent.puExtra("fragment_no",2);
startActivity(intent);
finish();
break;
In MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView = findViewById(R.id.navigationBottom);
navigationView.setOnNavigationItemSelectedListener(navListelener);
Bundle extras = getIntent().getExtras();
if(extras!=null)
{ intent number = extras.getInt("fragment_no");
if(number == 2)
getSupportFragmentManager().beginTransaction().replace(R.id.content_main,
new TwoFragment()).commit();
}
else
getSupportFragmentManager().beginTransaction().replace(R.id.content_main,
new OneFragment()).commit();
}
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);
I have problem with the back button in action bar to fragment
my code fragment:
public class Server extends Fragment {
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_server, container, false);
Button server = (Button) view.findViewById(R.id.status);
/** Button Check Status Server **/
server.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(view.getContext(), ServerStatus.class);
startActivityForResult(myIntent, 0);
getActivity().finish();
}
});
return view;
}
}
my code in activity:
public class ServerStatus extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server_status);
}
}
My code in Fragment
#Override
public void onNavigationDrawerItemSelected(int position) {
Fragment objFragment = null;
switch (position) {
case 0:
objFragment = new Account();
break;
case 1:
objFragment = new AllNews();
break;
case 2:
objFragment = new Server();
break;
case 3:
objFragment = new Account();
break;
case 4:
objFragment = new Account();
break;
case 5:
objFragment = new Account();
break;
case 6:
objFragment = new About();
break;
}
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container,objFragment)
.commit();
}
Every time I click the back button, the program always closes. I already tried to use:
getActionBar().setDisplayHomeAsUpEnabled(true);
and this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Can anyone help me to fix my program?
EDIT: Concerning cross-activity-navigation, you should not use startActivityForResult() in that way. Try to use startActivity() instead. If you start an activity for a result, the calling activity waits for the onActivityResult() callback and should not be finished.
In the manifest file you should declare the fragment's activity as parent activity of ServerStatus to enable back navigation. You should not need NavUtils.
If you want to enable fragment navigation within an activity, you have to add your fragment transactions to the backstack:
getFragmentManager().beginTransaction().addToBackStack(null).replace(...).commit();
Then you have to call getFragmentManager().popBackStack() in onOptionsItemSelected() to enable back navigation for the action bar:
#override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
getFragmentManager().popBackStack();
return true;
}
return super.onOptionsItemSelected(item);
}
You may have to override onBackPressed() to enable back navigation for the back button:
#override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() >= 1) {
getFragmentManager().popBackStack(); // return to previous fragment
}
else {
super.onBackPressed(); // Exit application when no fragment is on the backstack
}
}