How to move from Bottom Navigation Bar to activity android - android

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

Related

How to give Backward Slide Animation to Fragments with Bottom Navigation Bar in Android

I have a Bottom Navigation Bar Activity with changing fragments. i have done forward slide animation on fragments using 'setCustomAnimations' function but i want backward slide animations it has the same animation as for forward animation. i am unable to solve it, i have also tried many different solutions but none of them are working. can anyone help me give a solution to this
Here is the Code
public class Homepage extends AppCompatActivity {
private BottomNavigationView mainNav;
private FrameLayout mainFrame;
private HomeFragment homeFragment;
private GamesFragment gamesFragment;
private ExerciseFragment exerciseFragment;
private GalleryFragment galleryFragment;
private MoreFragment moreFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
mainNav = (BottomNavigationView) findViewById(R.id.main_nav);
mainFrame = (FrameLayout) findViewById(R.id.main_frame);
homeFragment = new HomeFragment();
gamesFragment = new GamesFragment();
exerciseFragment = new ExerciseFragment();
galleryFragment = new GalleryFragment();
moreFragment = new MoreFragment();
setFragment(homeFragment);
mainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_home:
setFragment(homeFragment);
return true;
case R.id.nav_games:
setFragment(gamesFragment);
return true;
case R.id.nav_exercise:
setFragment(exerciseFragment);
return true;
case R.id.nav_gallery:
setFragment(galleryFragment);
return true;
case R.id.nav_more:
setFragment(moreFragment);
return true;
default:
return false;
}
}
});
}
private void setFragment(Fragment fragment){
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_right,R.anim.slide_out_left);
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}
}

How to hide/disable TabLayouts when items on navigation drawer is clicked?

I am very new to android development and hence, I am not able to solve my problems via help from other internet resources. Using Youtube videos, I am designing an app that uses both NavigationDrawer and TabLayout, and all the TabLayout items and navigation drawer items use fragments. My problem is that when I clicked on items in navigation drawers, the app still displays TabLayout's fragments instead of displaying the fragment corresponding to the clicked item from `NavigationDrawer. Please help me with this. I have included code and even an image below. Thanks a lot.
The abovementioned image.
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//following gives toolbar and navigation bar can be viewed by sliding L-R
Toolbar toolbar = findViewById(R.id.toolbar_main);
setSupportActionBar(toolbar);
//attach sectionpageadapter to viewpager
SectionPagerAdapter pagerAdapter = new SectionPagerAdapter(getSupportFragmentManager());
ViewPager pager = findViewById(R.id.pager);
pager.setAdapter(pagerAdapter);
//attach viewpager to tablayout
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(pager);
//following to give hamburger sign to navigation drawer
drawer = findViewById(R.id.drawer_layout);
//references to listen to click events on navigation views
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//following for hamburger sign to rotate when slide from L-R
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer,toolbar,
R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
//below is our default fragment which will be shown on create, without any clicks
//if statement so that it wont reload on rotation or resume of the device
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
navigationView.setCheckedItem(R.id.nav_home);
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
break;
case R.id.nav_myaccount:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new AccountFragment()).commit();
break;
case R.id.nav_mymessages:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new MessageFragment()).commit();
break;
case R.id.nav_mysongs:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new MySongFragment()).commit();
break;
case R.id.nav_mystores:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new StoreFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
//following for closing navagation bar instead of leaving activity immediately when you press back button
public void onBackPressed() {
if(drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
}else {
super.onBackPressed();
}
}
private class SectionPagerAdapter extends FragmentPagerAdapter{
public SectionPagerAdapter(FragmentManager fm){
super(fm,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
#Override
public int getCount() {
return 4;
}
#NonNull
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new HomeFragment();
case 1:
return new FragmentArtists();
case 2:
return new FragmentAlbum();
case 3:
return new FragmentPlaylists();
}
return null;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return getResources().getText(R.string.home_tab);
case 1:
return getResources().getText(R.string.artist_tab);
case 2:
return getResources().getText(R.string.album_tab);
case 3:
return getResources().getText(R.string.playlist_tab);
}
return null;
}
}
}
Replace this code!
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_home:
pager.setVisibility(View.VISIBLE);
tabs.setVisibility(View.VISIBLE);
fragmentConterner.setVisibility(View.GONE);
break;
case R.id.nav_myaccount:
pager.setVisibility(View.GONE);
tabs.setVisibility(View.GONE);
fragmentConterner.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new AccountFragment()).commit();
break;
case R.id.nav_mymessages:
pager.setVisibility(View.GONE);
tabs.setVisibility(View.GONE);
fragmentConterner.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new MessageFragment()).commit();
break;
case R.id.nav_mysongs:
pager.setVisibility(View.GONE);
tabs.setVisibility(View.GONE);
fragmentConterner.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new MySongFragment()).commit();
break;
case R.id.nav_mystores:
pager.setVisibility(View.GONE);
tabs.setVisibility(View.GONE);
fragmentConterner.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new StoreFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
I hope it's helpful for you!
you have the provision to set the visibility of the layout elements to visible,invisible and gone. Programatically you can do this by
ViewPager.setVisibility(View.GONE);
TabLayout.setVisibility(View.GONE);
and also you can show the FrameLayout that you are using to inflate the FrameLayout container.
FrameLayout.setVisibility(View.VISIBLE);
And start with having the FrameLayout set to gone in the XML attribute.
android:visibility="gone"
I hope it helps.

Bottom Navigation Activity with external button

I am using Bottom Navigation Activity from Android Studio
After setting all the needed bottom tabs, I need to add aditional button for one fragment, but outside the tab zone. And that button is only when the second fragment is called.
Something like in image below
I've set new fragment in navigation/mobile_navigation.xml
<fragment
android:id="#+id/navigation_demografski"
android:name="com.home.Fragment1"
android:label="Podaci o osobama"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/navigation_biometrijski"
android:name="com.home.Fragment2"
android:label="Biometrijski podaci - Desna Ruka"
tools:layout="#layout/fragment_dashboard" />
<fragment
android:id="#+id/navigation_biometrijski2"
android:name="com.home.Fragment3"
android:label="Biometrijski podaci - Lijeva Ruka"
tools:layout="#layout/fragment_dashboard2" />
But I don't know how to call the third fragment from the second fragment.
I tried to use fragmet.replace() but it just overlaps one fragment over the other.
Tabbed activity
public class TabbedActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_demografski, R.id.navigation_biometrijski).build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
}
Use FrameLayout as fragment container, then switch fragments inside it. You could do that by setting OnNavigationItemSelectedListener to BottomNavigationView and using
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, selectedFragment).commit();
to switch between the fragments. On mentioned button in second fragment add onClickListener and call previous transaction from Activity with desired fragment as selectedFragment
you can implement by following my code below:
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//loading the default fragment
loadFragment(new HomeFragment());
//getting bottom navigation view and attaching the listener
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomeFragment();
break;
case R.id.navigation_dashboard:
fragment = new DashboardFragment();
break;
case R.id.navigation_notifications:
fragment = new NotificationsFragment();
break;
case R.id.navigation_profile:
fragment = new ProfileFragment();
break;
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}
}

My Bottom Navigation Bar Only Works When I Double Tap To Switch Fragments

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();
}
};

Prevent fragment refreshing with bottom navbar

I have the following bottom navbar code to switch between 3 fragments:
public class MainActivity extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomeFragment();
break;
case R.id.navigation_dashboard:
fragment = new DashboardFragment();
break;
case R.id.navigation_notifications:
fragment = new NotificationsFragment();
break;
}
return loadFragment(fragment);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadFragment(new HomeFragment());
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}
}
In the fragments there are RecyclerViews with lists. Every time I switch between the tabs (between fragments), it looks like the fragment is reloaded, and the lists jump to the top. I want to prevent that reloading so that the user stays on the same place in the list he viewed before switching fragments
The problem is that you are creating a new instance every time. You can cache the instance like:
private Fragment mHomeFragment = new HomeFragment();
private Fragment mDashboardFragment = new DashboardFragment();
private Fragment mNotificationsFragment = new NotificationsFragment();
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = mHomeFragment;
break;
case R.id.navigation_dashboard:
fragment = mDashboardFragment;
break;
case R.id.navigation_notifications:
fragment = mNotificationsFragment;
break;
}
return loadFragment(fragment);
}
As we could see, you are always replace your fragment when clicks on bottom navigation, replace means previous fragment removes and state cleans. The solution is do not create your fragment each time and use attach/detach method for showing actual fragment. Here is already described about these methods.

Categories

Resources