change automatically select for bottonavigation selected - android

I'm working on this code
private void setupViews(){
frameLayout = (FrameLayout) findViewById(R.id.frame_id);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_id);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new ProfileFragment()).commit();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int navID = menuItem.getItemId();
switch (navID){
case R.id.home:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new HomeFragment()).commit();
break;
case R.id.search:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new SearchFragment()).commit();
break;
case R.id.profile:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new ProfileFragment()).commit();
break;
}
return true;
}
When I launch my app, it's automatically going on first bottom (I have 3 bottoms)
I want to change this to second bottom in navigation view. Please help me

Try this:
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_id);
bottomNavigationView.setSelectedItemId(R.id.search);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new SearchFragment()).commit();

#iamhanniballake
I call setupViews() in main activity
I have 3 botoms
-1-2-3-
when my app running open fragment with a bottomnav view
its automatically selected 3 I want to change it to 2

Related

BottomNavigationView with setOnItemSelectedListener()

I am trying to build app with BottomNavigationView and I set setOnItemSelectedListener() method to bottom navigation so I can do what I want when user select one of the menu in bottom navigation.
everything is good when I don't set setOnItemSelectedListener(), but when I set setOnItemSelectedListener() method then the fragment is not updated automatically when user select the bottom navigation menu.
I consider if that do i have to handle fragment transaction manually when I set this method?
thanks ^^
Yes. You need to manually replace the fragment item on onNavigationItemSelected
Example:
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// By using switch we can easily get
// the selected fragment
// by using there id.
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.algorithm:
selectedFragment = new AlgorithmFragment();
break;
case R.id.course:
selectedFragment = new CourseFragment();
break;
case R.id.profile:
selectedFragment = new ProfileFragment();
break;
}
// It will help to replace the
// one fragment to other.
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, selectedFragment)
.commit();
return true;
}
};
You can find a good tutorial here: https://www.geeksforgeeks.org/bottomnavigationview-inandroid/

Problem with Oncreate view of bottom navigation bar

I have a bottom navigation bar with 4 fragments and 1 activity inside another activity(in which all fragment will be displayed). I want my first fragment to be displayed on the starting of the activity(in which all fragment will be displayed) along with the matching item of bottom navigation bar. My fragment 1 is getting displayed on starting but with wrong item of the bottom navigation bar.
This is what, I am getting on starting. Selected item should be Home(middle)
I have this under OnCreate
btmNav = findViewById(R.id.btmnav);
btmNav.setOnNavigationItemSelectedListener((navListner));
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new RecViewFragment()).commit();
and this outside Oncreate
private BottomNavigationView.OnNavigationItemSelectedListener navListner = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navprofile:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment2()).commit();
break;
case R.id.navmap:
Intent intent = new Intent(MainActivityBuses.this, MapActivity.class);
startActivity(intent);
break;
case R.id.navhome:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment2()).commit();
break;
case R.id.navmybus:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment3()).commit();
break;
case R.id.navinfo:
getSupportFragmentManager().beginTransaction().
replace(R.id.fragment_container,new Fragment4()).commit();
break;
}
return true;
}
i got the answer,
just need to add this
btmNav.getMenu().findItem(R.id.navhome).setChecked(true);
below this
btmNav = findViewById(R.id.btmnav);
btmNav.setOnNavigationItemSelectedListener((navListner));
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new RecViewFragment()).commit();

Android Bottom Navigation Bar in activities

I am developing a car rental app and right now I have 3 fragments in my app (Home - where all the cars are displayed from a recyclerview , Orders - where user's bookings are shown , Profile - user profile). I can switch beetween these 3 fragments from my bottom navigationbar.
Now I created onclick listeners for every car on home page. When I click on a car it sends me to a new activity "activity_car_detail.xml" which contains specific information about the selected car. My question is: how can I make the navigation bar to appear on this Car Detail activity? Should I use fragment instead of activity or what should I do?
This is the code for the Main Activity which contains the bottom navigation bar:
public class MainActivity extends AppCompatActivity {
public static ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_orders:
selectedFragment = new OrdersFragment();
break;
case R.id.nav_profile:
selectedFragment = new ProfileFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
return true;
}
};
}
I suggest using the Navigation component instead of Fragment transactions. Also this will fix your issue and it will remove boilerplate code. Read more about it's documentation here.

Show loading screen while activity is loading in background

I have an app with bottomnavigationview using which I can navigate between five different fragments/pages. When I switch over to a different page, the app waits(i.e loads the page) for 0.5 - 1 second and then displays it. I want that on moving to another page, the app shows a loading screen on the foreground while the page loads in the background, and when it's done loading the loading screen goes away and the loaded page appears? How can I do that? Making the loading screen is not a problem but implementing it in the way I want is.
My MainActivity.java:
public class MainActivity extends AppCompatActivity {
RelativeLayout btmnavl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new Home()).commit();
LinearLayout team = findViewById(R.id.team_lay);
BottomNavigationView bottomNavigationView = findViewById(R.id.btmnav);
bottomNavigationView.setOnNavigationItemSelectedListener(navlistner);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new Live()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navlistner =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.live:
selectedFragment = new Live();
hide();
break;
case R.id.home:
selectedFragment = new Home();
break;
case R.id.schedule:
selectedFragment = new Schedule();
break;
case R.id.feed:
selectedFragment = new Feed();
break;
case R.id.recent:
selectedFragment = new Recent();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
private void hide(){
btmnavl = findViewById(R.id.btmnavl);
btmnavl.setVisibility(View.GONE);
}
}
To add a progress bar which is only visible when the page is loading, add the following to your activity XML:
<RelativeLayout
android:id="#+id/loadingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:elevation="4dp">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:indeterminateTintMode="src_atop"/>
</RelativeLayout>
Then you can fetch it with loadingPanel = (RelativeLayout) findViewById(R.id.loadingPanel);
And set visibility with:
loadingPanel.setVisibility(View.GONE);
loadingPanel.setVisibility(View.VISIBLE);
In your case, you can set it to GONE right before the .commit(), so:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new Live());
loadingPanel.setVisibility(View.GONE);
transaction.commit();
This should work for you I believe, just don't forget to set it back to VISIBLE when you load other fragments, and edit the above XML to fit your specific layout better.

Navigation Drawer back button

I implemented Navigation Drawer in my app. It's just a sample app, auto-generated Navigation Drawer fragments and activities from Android studio. I'm starting an activity from a section list item like this:
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.pocetna);
break;
case 2:
mTitle = getString(R.string.oglasna_ploca);
break;
case 3:
mTitle = getString(R.string.e_novine);
break;
case 4:
mTitle = getString(R.string.portal);
break;
case 5:
mTitle = getString(R.string.raspored);
startActivity(new Intent(this, RasporedWebView.class));
break;
}
}
When I use the back button, could I get back to let's say case 1 or even MainActivity (closing navigation drawer), because when I call the activity, and go back it returns to a blank activitity (or w/e), and then I must click back button once more. I tried searching for solutions, but couldn't find any.
Thanks in advance.
Refer this Docs.
Then add the below code to do Back button in Activity
ActionBar actionBar;
actionBar=getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}

Categories

Resources