Solution 1 :
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.
Problem :
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);
}
}
Comments
Comment posted by android.jlelse.eu/…
Is this what you need?
Comment posted by Poison_Frog
No, i dont want a splash screen, i want a loading screen to display over a fragment while the fragment loads in the background…ill edit my post to make it clear
Comment posted by Yaron Grushka
When you say loading screen, you mean like a progress-bar?
Comment posted by Poison_Frog
Yes a progress bar.
Comment posted by Poison_Frog
Nope…its not working…atleast not like it should….If i dont set the visibility it to GONE then it stays and if i Set the visibility to GONE then it dosent appear at all.
Comment posted by Yaron Grushka
Visibility should be set to