Different toolbar behavior in different fragments... how? - android

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.

Related

One bottom menu for all activities in app

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.

Back Button in ActionBar of App

Generally switching from one activity to another activity hamburger icon is replaced with back arrow. I want to control the functionality of that arrow. I have seen many contents here but most of them were related to hardware's back button. How can I control that?
I am trying the functionality in case of fragments. Also I have Navigation drawer attached with the hamburger icon.
I tried this-
if(id == android.R.id.home){
getSupportFragmentManager().beginTransaction().replace(R.id.main_container, new AmbulanceMap()).commit();
getSupportActionBar().setTitle("Book A Ride");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
but doesnt work as I hoped.
I want my back button to change the fragmentto previous fragment.
I had the same problem once. Just like you, things like checking if Android.R.id.home is clicked didn't work.
But I solved it using that:
Set navigation listener to toolbar:
toolbar.setToolbarNavigationClickListener(v -> onBackPressed());
If it should be within fragment:
Create an public method in activity.
In fragment's onAttach (or later) cast getActivity() to your activity and call method you was defined previously.
Example:
// YourActivity
public void setHomeListener(OnLickListener listener){
toolbar.setToolbarNavigationClickListener(listener);
}
//Fragment's onCreate
((YourActivity)getActivity()).setHomeListener(v -> onBackPressed());
//Fragment's onDestroy
((YourActivity)getActivity()).setHomeListener(null);
And, of course, set home us up enabled to show back arrow.
EDIT
if you don't use labmdas u should use:
(YourActivity)getActivity()).setHomeListener(new OnClickListener() {
#Override
public void onClick(View v) {
YourFragment.this.onBackPressed();
}
});
The back button of the ActionBar is a menuItem so you need to override onOptionsItemSelected like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
//Your back button logic here
break;
}
return true;
}
Also don´t forget to add getSupportActionBar().setDisplayHomeAsUpEnabled(true); after setting the Toolbar

Android how to create fragment with X button

Hey community have a good day. I need to create some ui like that :
I tried : Create a fragment , create a dialog .
Assume that i have an activity A.I do some work in my activity then i start this "New event " ui.
I do some work and click "X" button and wanna continue from where i left in my activity A. So how i can do this . This layout is fragment or dialog ?
Actually we can create new activity and pass intent to it and change back icon like that :
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.X_icon);
}
And these 2 methods :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return(super.onOptionsItemSelected(item));
}
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.nothing,R.anim.slide_out_right);
}
Doing like that if we press back button or press X button previous activity will not recreate.

How can I control the activity's up button from a contained fragment?

I'm building a pretty simple app. At it's core are 2 screens:
1) list-screen: a list of items
2) detail-screen: a detailed view of an item
I used one Activity (which extends AppCompatActivity) with an Action-Bar, a Navigation-Drawer and a main-content part (a FrameLayout).
I used 2 different fragments for the 2 screens:
When opening the app I inflate the list-fragment into the main-content part.
When an item in the list is clicked I inflate the detail-fragment into the main-content part and it all works well.
On the detail-screen I want the Action-Bar to display an up-button that goes back to the list-screen.
Considering the fact that I am using fragments, rather than separate activites, how can I achieve that?
You can enable the up button each time the Detail Fragment loads, and disable it whenever any of the other Fragments load.
First define these methods in your Activity, which you can call from the Fragments in order to show/hide the up button:
public void showUpButton() {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void hideUpButton() {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
You will probably want to just enable the up button in on Resume() of the detail Fragment (MainActivity is just an example, change to the name of your Activity) .....
#Override
public void onResume() {
super.onResume();
MainActivity activity = (MainActivity)getActivity();
if (activity != null) {
activity.showUpButton();
}
}
Then in the other Fragments:
#Override
public void onResume() {
super.onResume();
MainActivity activity = (MainActivity)getActivity();
if (activity != null) {
activity.hideUpButton();
}
}
The next thing is to make the up button actually go back. First ensure that you're adding the Fragment with the up button to the back stack, and then add this to that Fragment.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
((MainActivity)getActivity()).onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Then in the Activity, override onBackPressed() and pop from the back stack if the FragmentManager has any entries:
#Override
public void onBackPressed() {
FragmentManager fragmentManager = getSupportFragmentManager();
if (fragmentManager.getBackStackEntryCount() > 1) {
fragmentManager.popBackStackImmediate();
} else {
super.onBackPressed();
}
}

Is it possible to set different actions for floating action button in differet fragments?

I am working or an app where I have 4 fragments in viewpager and floating action button (fab). When I click on fab there appear 3 subfabs. Each of them start different activities where user can search data from different fragments. Is it possible to set fab so that if I click on it while I'm in first fragment it'll open an activity for searching inside this fragment, onClick inside second - search for second and etc. The issue is that now clicking on sub fab while I'm in some fragment I can search data from another fragment too and it's a bit weird. I understand question is kinda strange, if something is unclear I'll explain further
You can make FAB button public and implement onClick listener in each fragment.
You should override setUserVisibleHint and put your code onResume so getActivity() will not return null. Fab button will have different actions in different fragments.
Here's an example, inside each fragment when you want Fab click listener:
#Override
public void setUserVisibleHint(boolean visible)
{
super.setUserVisibleHint(visible);
if (visible && isResumed())
{
onResume();
}
}
#Override
public void onResume()
{
super.onResume();
if (!getUserVisibleHint())
{
return;
}
MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do what you want
}
});
}
You can check in onclick method of floating action button which fragment is currently opened and calling it.
Call findFragmentById() on FragmentManager and determine which fragment is in your R.id.frameTitle container.

Categories

Resources