I'm looking for your help!
I want to create an app with bottom navigation bar. Of coarse it is possible to add menu bar to every activity in app but i want to find solution where i can make one activity with menu and extend it in other activity.
So i made MenuActivity :
public class MenuActivity extends AppCompatActivity {
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);
return true;
case R.id.navigation_dashboard:
// mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
// mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigationBar);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}}
And after that i create some other activity :
public class testActivity extends MenuActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}}
And here is the problem, when i start my testActivity its create MenuActivity when calling super.onCreate() which contains setContentView(R.layout.activity_menu);
But i want to add only bottom menu to other activity. Can you provide me to answer how can i do this?
Thank you all in advance for any help!
It can be achieved easily with a single Activity and multiple Fragments.
Make an activity which contains only the bottom navigation and a FrameLayout container.
Implement your pages with Fragments and after that, you can load the required fragment in the activity's container with fragment manager
Try using Fragments it will help you get desired results.
Related
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.
There are many questions like this asked but everything I have tried seems to not work.
Essentially I have a main activity that calls different fragments depending on what the user clicks with the home fragment being default.
I would like to have a back button on the title bar, to go back to the previous fragment.
My fragment is called from the main activity like so:
Fragment fragment = null;
fragment = new nextFragment();
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, fragment).addToBackStack(null);
fragmentTransaction.commit();
fragmentTransaction.addToBackStack(null);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
But since ActionBarActivity activity is deprecated I need to extend AppCompatActivity instead of FragmentActivity so I can use actionbar (I'm assuming this is what I need).
However then I am unable to switch to my fragment. So does anyone know how I could implement a back button in my fragment or how to use AppCompatActivity in this situation.
Thanks for any help.
Please try this if you extend AppCompatActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Include these 2 lines ONLY if need to use Toolbar from layout xml as Action Bar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Add back navigation in the title bar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//
//Other works to be done in onCreate.....
//
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
//Title bar back press triggers onBackPressed()
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
//Both navigation bar back press and title bar back press will trigger this method
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0 ) {
getFragmentManager().popBackStack();
}
else {
super.onBackPressed();
}
}
}
To add Back Button in title bar, you must add the following code to your Fragment.
Toolbar toolbar = (Toolbar)view.findViewById(R.id.app_bar);
AppCompatActivity AppCompatActivity = (AppCompatActivity)getActivity();
AppCompatActivity.setSupportActionBar(toolbar);
AppCompatActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
return view;
}
Don't forget to extend your MainActivity to AppCompatActivity.
You must then use this Java code in my Fragment class to react to the user tapping the back/up icon in the action bar.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You've saved the last fragment used by calling
addToBacktack(null).commit()
So the next step forward to call it is by overriding onBackPressed() in the activity hosting the fragment.
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0 ){
getFragmentManager().popBackStack();
}
else {
super.onBackPressed();
}
}
So whenever you call the activity's onBackPressed() from the fragment, the fragment would go back to the last saved fragment.
I have an activity (MyActivity) with two fragments (MyFirstFragment and MySecondFragment). I want the screens correspondent to the two fragments to display the same exact toolbar: just the 'home' button ('🡠') and the top right corner menu, with only one item (let's call it Foo).
The only difference among them should be the behavior when clicking on Foo. (Just to make an example, when I'm on MyFirstFragment and I press Foo I want it to display a Toast, while whem I'm on MySecondFragment I want it to open an AlertDialog).
What do you think could be the best implementation for this?
Can I define the toolbar inside MainActivity.java but then set two different behaviours in each fragment java file? How?
I'll add some code, here's how I'm defining the toolbar inside my MyActivity.java for the moment:
#Override
protected void onCreate(Bundle savedInstanceState) {
// [...]
Toolbar tb = (Toolbar) findViewById(R.id.toolbar_my);
setSupportActionBar(tb);
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
tb.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed(); //goes to the previous fragment
}
});
// [...]
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.foo:
// do something
return true;
default:
return false;
}
}
Yep, just define your Toolbar inside MyActivity.java and then in each fragment constructor set this flag to true. Then override onOptionsItemSelected(MenuItem item) with different behavior in each fragment.
In a Bluetooth-related app (with minSdkVersion="18") I have a single MainActivity.java, displaying one of the following 3 UI Fragments:
MainFragment.java (the top screen)
SettingsFragment.java (settings screen, entered through menu)
ScanningFragment.java (lists nearby Bluetooth devices)
To display an "Up button" and handle the "Back button" I have the following code in place:
public class MainActivity extends Activity
implements BleWrapperUiCallbacks {
// set in onResume() of each fragment
private Fragment mActiveFragment = null;
´ #Override
public void onBackPressed() {
if (!getFragmentManager().popBackStackImmediate())
super.onBackPressed();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_root);
getActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
Fragment fragment = new MainFragment();
getFragmentManager().beginTransaction()
.replace(R.id.root, fragment, "main")
.commit();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getFragmentManager().popBackStackImmediate();
break;
case R.id.action_settings:
Fragment fragment = new SettingsFragment();
getFragmentManager().beginTransaction()
.addToBackStack(null)
.replace(R.id.root, fragment, "settings")
.commit();
break;
}
return super.onOptionsItemSelected(item);
}
This works well, but has a cosmetic problem, that the "Up button" is still displayed when the MainFragment.java is being displayed - as you can see on the left side of the above screenshot.
I have tried calling
getActionBar().setHomeButtonEnabled(false);
when that fragment is being active, but that only disables the "Up button" - without really hiding it.
With help of Little Child (thanks!) here my solution using the FragmentManager.OnBackStackChangedListener:
public class MainActivity extends Activity
implements OnBackStackChangedListener,
BleWrapperUiCallbacks {
#Override
public void onCreate(Bundle savedInstanceState) {
...
getFragmentManager().addOnBackStackChangedListener(this);
}
#Override
public void onBackStackChanged() {
getActionBar().setDisplayHomeAsUpEnabled(
getFragmentManager().getBackStackEntryCount() > 0);
}
You are in luck because in API level 18 there is this:
getActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);
for support library, it is:
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);
So now depending upon what fragment you are in, you can change the icon of "up" button.
Also, try this:
getActionBar().setDisplayHomeAsUpEnabled(false);
Set whether home should be displayed as an "up" affordance.
2021 Working Solution:
(requireActivity() as AppCompatActivity).supportActionBar?.setHomeAsUpIndicator(null)
In one of my Android Application I need to keep the title bar same but the view that is shown in the rest of the screen changes. So, I have taken different Activity for all the views that I need to show and set the title bar in every Activities onCreate method.
Now, the problem is that I have a button in the title bar and need to perform certain action on its click event. Writing the same event handling code in every Activity class is very cumbersome. Is there any other way out that whenever there is a click event on that button of the title bar then we can have the same functionality without writing the same code in all the Activity classes.
Can we use ViewGroup for that? I don't have much idea about ViewGroup. Is that possible with ViewGroup?
If anyone knows the solution then please let me know.
Thanks & Regards
Sunil
If you are sharing view elements and functionality amongst several classes extending Activity, you might want to consider making a common superclass to handle this overlap.
The best solution is to keep a base activity like this.
public class HeaderBaseActivity extends AppCompatActivity{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
mAppPreferences = AppUtil.getAppPreferences(this);
item_patients = menu.findItem(R.id.item_patients);
setBatchCountOnMenu(0);
RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();
mRealm = Realm.getInstance(realmConfig);
mDotor = new Gson().fromJson(mAppPreferences.getString(Constants.SETTINGS_OBJ_DOCTOR, ""), Doctor.class);
mAppPreferences = AppUtil.getAppPreferences(this);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_logout:
/* DialogUtility.showShortToast(this, " Main manu Action Logout");*/
SharedPreferences.Editor Editor = mAppPreferences.edit();
Editor.putBoolean(Constants.SETTINGS_IS_LOGGED_IN, false);
Editor.apply();
clearRealmDB();
Intent loginIntent = new Intent(HeaderBaseActivity.this, LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(loginIntent);
finish();
break;
case R.id.item_patients:
System.out.println("current activity "+getApplicationContext());
Intent mPatientListIntent = new Intent(HeaderBaseActivity.this, PatientSummaryInfoActivity.class);
mPatientListIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mPatientListIntent);
break;
case R.id.action_doctor_profile:
openDialogOfDoctorProfile();
break;
}
return super.onOptionsItemSelected(item);
}
}
Your other activities can extend the above activity like this:
public class MainActivity extends HeaderBaseActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
}
}