Change toolbar from fragment - java

I am writing an app using jetpack recommended architecture, NavigationUI, and the navigation graph. So I have one main activity with a Toolbar, a BottomNavigationView and the NavHostFragment.
Everything worked nicely until now: I need to change the Toolbar to use a CollapsingToolbarLayout and hide the BottomNavigationView in one of my fragment.
I tried to add a navigation listener (as described here) to hide my Toolbar and BottomNavigationView, and in my fragment, I inflate the new Toolbar and call setSupportActionBar() on the main activity.
// in MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
if(destination.getId() == R.id.detailFragment){
bottomBar.setVisibility(View.GONE);
topBar.setVisibility(View.GONE);
}else{
bottomBar.setVisibility(View.VISIBLE);
topBar.setVisibility(View.VISIBLE);
}
});
// ...
}
public void changeToolbar(Toolbar toolbar){
getSupportActionBar().hide();
setSupportActionBar(toolbar);
}
// in DetailFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// ...
navController = NavHostFragment.findNavController(this);
AppBarConfiguration.Builder builder = new Builder(
R.id.accuracyFragment,
R.id.dataFragment,
R.id.magnetFragment,
R.id.settingsFragment);
AppBarConfiguration config = builder.build();
NavigationUI.setupWithNavController(toolbarLayout, toolbar, navController);
((MainActivity)getActivity()).changeToolbar(toolbar);
// ...
}
It almost works correctly, but:
when I navigate up or go to another fragment, the BottomNavigationView is not correctly displayed. It seems to be pushed down by the Toolbar.
the transition is ugly: the toolbar is visibly changing, I can see it disappearing before being changed
So the question is: is there another way to change/hide the navigation elements from the fragment? If not, should I create a new activity?

It's been a wild ride, but I finally found a solution. For the issue number 1, this is due to the way Android manages the fitsSystemWindows property propagation. For this to work correctly, I made a few changes to my layouts. I created a custom FitSystemWindowLinearLayout, which is simply a class extending the standard LinearLayout and overriding onApplyWindowInsets like this:
#Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
int childCount = getChildCount();
for (int index = 0; index < childCount; ++index) {
getChildAt(index).dispatchApplyWindowInsets(insets);
}
return insets;
}
My main activity now looks like this:
+-- CoordinatorLayout, fitsSystemWindows=false
+-- FitSystemWindowLinearLayout, fitsSystemWindows="false"
+-- Toolbar
+-- NavHostFragment, fitsSystemWindows="false"
+-- BottomNavigationView, fitsSystemWindows="false"
For the second issue, namely the transition being ugly, I mitigated that by adding a shared element to the transition.
All in all, I think it's easier to use a new activity for this kind of things, the NavigationUI falls a bit short for now.
Here are some resources that helped me:
https://medium.com/androiddevelopers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec
Android fitsSystemWindows not working when replacing fragments
Collapsing Toolbar problems with Status Bar and Bottom Bar. Fitssystemwindows="true" not working
fitsSystemWindows effect gone for fragments added via FragmentTransaction
https://www.reddit.com/r/androiddev/comments/aryxvu/fitssystemwindows_misbehaves_with_navigation/
-https://medium.com/androiddevelopers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec

Related

Lag when switching tabs in BottomNavigationView

I have an Activity that contains a BottomNavigationView, and this bottomnav helps the activity to display three fragments. These fragments load well, and I use an AsyncTask to do every heavy operation, while in the UI thread, I show a ProgressBar until everything loads.
There is a weird behaviour with my fragment: The first time I load the fragment it takes some time to actually display it, instead of displaying it instantly with a progressbar.
This thing only happens the first time, and only in this fragment.
The fragment code only contains this:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new LoadData(getView(), getContext()).execute();
}
private class LoadData extends AsyncTask<Void, Void, Void> {
private View v;
private Context context;
public LoadData(View v, Context context) {
items = new ArrayList<>();
this.v = v;
this.context = context;
}
#Override
protected Void doInBackground(Void... voids) {
setItems(context); //Heavy operation
adapter = new DashAdapter(items, context);
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
//shows progressbar
progress = v.findViewById(R.id.DFProgress);
progress.setVisibility(View.VISIBLE);
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
setPager();
//sets viewPager and hides progressbar
progress.setVisibility(View.GONE);
}
}
In the gif below, if you look at the bottomnavigationview at the bottom, you can see that it takes time to display the fragment. But after trying to load the fragment a second time, it loads as expected.
How could I make the fragment to load the right way?
I had the same problem. I have two options.
Use postdelay when you call LoadData or
First add all fragments with manually. You manage navigationItemSelected yourself.
Like this:
val firstFragment: Fragment = FirstFragment()
val secondFragment: Fragment = SecondFragment()
val thirdFragment: Fragment = ThirdFragment()
val navView: BottomNavigationView = findViewById(R.id.nav_view)
var active = firstFragment
fm.beginTransaction().add(R.id.nav_host_fragment, thirdFragment, "3").hide(thirdFragment).commit()
fm.beginTransaction().add(R.id.nav_host_fragment, secondFragment, "2").hide(secondFragment).commit()
fm.beginTransaction().add(R.id.nav_host_fragment, firstFragment, "1").commit()
navView.setOnNavigationItemReselectedListener { }
navView.setOnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_first -> {
fm.beginTransaction().hide(active).show(firstFragment).commit()
active = firstFragment
}
R.id.navigation_second -> {
fm.beginTransaction().hide(active).show(secondFragment).commit()
active = secondFragment
}
R.id.navigation_third -> {
fm.beginTransaction().hide(active).show(thirdFragment).commit()
active = thirdFragment
}
}
true
}
And remove these lines in your nav_host_fragment:
app:defaultNavHost="true"
app:navGraph="#navigation/mobile_navigation"
you can use jetpack navigation for simple bottombar navigation
Simple Bottom Navigation with Jetpack Navigation:
Let’s start by including the Jetpack Navigation library in your apps by adding these lines in app’s build.gradle file:
def nav_version = "2.1.0"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
We start by creating a simple bottom navigation flow first. For that, you need to do first add NavHostFragment in your single activity layout file. Add this in the activity_main.xml file inside the FrameLayout tag.
<fragment
android:id="#+id/fragNavHost"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/bottom_nav_graph" />
You will see an error saying “Cannot resolve symbol #navigation/bottom_nav_graph .”
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/bottom_nav_graph.xml"
app:startDestination="#id/homeFragment2">
<fragment
android:id="#+id/homeFragment2"
android:name="com.wajahatkarim3.bottomnavigationdemo.HomeFragment"
android:label="fragment_home"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/searchFragment2"
android:name="com.wajahatkarim3.bottomnavigationdemo.SearchFragment"
android:label="fragment_search"
tools:layout="#layout/fragment_search" />
<fragment
android:id="#+id/notificationsFragment2"
android:name="com.wajahatkarim3.bottomnavigationdemo.NotificationsFragment"
android:label="fragment_notifications"
tools:layout="#layout/fragment_notifications" />
<fragment
android:id="#+id/profileFragment2"
android:name="com.wajahatkarim3.bottomnavigationdemo.ProfileFragment"
android:label="fragment_profile"
tools:layout="#layout/fragment_profile" />
</navigation>
Its time for add some code in our activity class. Open MainActivity.kt file, and create a method setupViews() in it. Call this in onCreate() of the activity. Add these lines in the setupVeiws() method.
fun setupViews()
{
// Finding the Navigation Controller
var navController = findNavController(R.id.fragNavHost)
// Setting Navigation Controller with the BottomNavigationView
bottomNavView.setupWithNavController(navController)
// Setting Up ActionBar with Navigation Controller
// Pass the IDs of top-level destinations in AppBarConfiguration
var appBarConfiguration = AppBarConfiguration(
topLevelDestinationIds = setOf (
R.id.homeFragment,
R.id.searchFragment,
R.id.notificationsFragment,
R.id.profileFragment
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
}
When I tried #Kasım Özdemir 's answer, Each time the activity is launched, There was an initial ripple effect on the first item in bottom navigation view.(because I am using material bottom navigation view, which has a default ripple effect. And also UI was not visible when I tap for the first time, but I think that was because I was using different method than #Kasım Özdemir to change my active fragment when Item is clicked.
I didn't want to start my activity with first item, but with middle item in navigation view, which is "FragmentTwo" in below case. So the ripple effect was quite irrelevant.
So, I just attached the fragment instead of adding it and then hiding it, and now there is no ripple, Here is how code looks in Kotlin...
val firstFragment: Fragment = FragmentOne()
val middleFragment: Fragment = FragmentTwo()
val thirdFragment: Fragment = FragmentThree()
var fragment: Fragment? = null
var bnv: BottomNavigationView? = null
bnv = findViewById(R.id.bottom_navigation)
bnv!!.selectedItemId = R.id.middle_page
//fragments attached other than the fragment linked with middle item
supportFragmentManager.beginTransaction().attach(firstFragment).commit()
supportFragmentManager.beginTransaction().attach(thirdFragment).commit()
supportFragmentManager.beginTransaction().add(R.id.activity_main_container, middleFragment())
.commit()
bnv!!.setOnItemSelectedListener { item ->
when (item.itemId) {
R.id.first_page -> fragment = firstFragment
R.id.middle_page -> fragment = middleFragment
R.id.third_page -> fragment = thirdFragment
}
if (item.isChecked){
false
}
else {
supportFragmentManager.beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out).replace(
R.id.activity_main_container,
fragment!!
).commit()
true
}
}
So, using attach can also work fine instead of adding and then hiding it... for more information you can check this answer
(If you haven't already, Try using Navigation Component for bottom navigation, like this. It is more handy in some case, as it handles default backStack management by itself.)

Collapsing Toolbar only for one Fragment in Navigation View

The Problem
I have a navigation drawer with different fragments. There is a default toolbar every Fragment should use, except of one Fragment which needs a collapsing Toolbar.
My Question
How can I switch between the toolbars for the fragments ?
It seems you want to achieve something like this.
I have made an activity with common toolbar. when switching to the collapsing toolbar fragment I've made the toolbar transparent and fragment's toolbar takes over. The toolbar's color remains the same on switching to other fragments.
This allows you to manage complete collapsing toolbar's layout structure in xml and logic remains in Fragment.
Hope this will help. Refer the gif linked.
Gist for gif
The best solution that I found to easily collapse, lock it(keep it in collapsed mode) and unlock the collapsingToolbar.
private void collapseAppBar() {
// Collapse the AppBarLayout with animation
mAppBarLayout.setExpanded(false, true);
}
private void lockAppBar() {
/* Disable the nestedScrolling to disable expanding the
appBar with dragging the nestedScrollView below it */
ViewCompat.setNestedScrollingEnabled(nestedScrollView, false);
/* But still appBar is expandable with dragging the appBar itself
and below code disables that too
*/
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
#Override
public boolean canDrag(AppBarLayout appBarLayout) {
return false;
}
});
}
private void unLockAppBar() {
ViewCompat.setNestedScrollingEnabled(nestedScrollView, true);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
if (behavior != null) {
behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
#Override
public boolean canDrag(AppBarLayout appBarLayout) {
return true;
}
});
}
}
And I use these functions in this way:
Fragment fragment = null;
Class fragmentClass;
switch (menuItem.getItemId()) {
case R.id.fragment1:
unLockAppBar();
fragmentClass = first_Fragment.class;
break;
case R.id.fragment2:
collapseAppBar();
lockAppBar();
fragmentClass = second_Fragment.class;
break;
case R.id.fragment3:
collapseAppBar();
lockAppBar();
fragmentClass = third_Fragment.class;
break;
You can easily get the Toolbar from your Fragment and then modify or change some property of that Toolbar inside the Fragment.
To get the Toolbar from your Activity you might consider using this.
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
Now you need to make the changes on the Toolbar in the onResume function and then undo the changes each time you return from the Fragment inside onStop function. Otherwise the changes made in the Fragment will be carried on to other fragments as well when switched to other Fragment from the navigation drawer.
But in your case, I would recommend each Fragment should have their Toolbar so that it doesn't conflict with each other and can be modified as you need. And yes, remove the Toolbar from your Activity.
So add the Toolbar in the layout of your Fragment like this.
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"/>
Then find it in the Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
// Modify your Toolbar here.
// ...
// For example.
// toolbar.setBackground(R.color.red);
// Create home button
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
And Override the onOptionsItemSelected function.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
getActivity().onBackPressed();
}
return super.onOptionsItemSelected(item);
}
I'm using Jetpack's Navigation components with single Activity and different Fragments in my app.
Some Fragments are accessible from bottom navigation (and have Toolbar from Activity). Some others are "special" Fragments and have their own Collapsible Toolbar.
To achieve this, I'm hiding Toolbar from Activity in "special" Fragments with this code in Activity:
// Handle toolbar changes in different Fragments
val navController = findNavController(R.id.nav_host_fragment)
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.my_special_fragment_with_collapsible_toolbar -> {
binding.toolbarMain.visibility = View.GONE
}
else -> {
binding.toolbarMain.visibility = View.VISIBLE
}
}
}
The recommend practice is to use toolbars in fragments instead of a common toolbar in activity. That way you can control the looks and behaviour of toolbar in fragment. Refer https://developer.android.com/guide/navigation/navigation-ui#support_app_bar_variations

Is it possible to hide the Toolbar and disable the scroll when swiping to specific page?

I would like to be able to hide the Toolbar on swipe to a specific tab and then lock it. Expanding it and then locking would also work for me although i prefer the first.
I have tried doing stuff like in the code below, but it gives rise to some snappy behavior because the page is scrolled of the screen originally. As soon as i set the scroll flags to 0, the whole page snaps back up and then locks the screen with the toolbar expanded which makes sense because with scroll flags set to zero, the page should not be able to scroll off the screen at all so it just snaps back up.
The page I am tending to is a chat page and in order to have a static text input bar at the bottom I really need to disable the scrolling for that page while making it possible for the others.
Can you guys think of some way to accomplish this?
Activity:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group);
appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// add all the needed fragments for the tabs inside a Vector
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(pagerAdapter);
viewPager.setOffscreenPageLimit(4);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.clearOnPageChangeListeners();
viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListenerGroups(tabLayout, this, fab, appBarLayout));
viewPager.setCurrentItem(CURRENT_TAB);
}
public void disableToolbarScrolling()
{
logger.d("disabling scrolling toolbar");
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0); // clear all scroll flags
toolbar.setLayoutParams(params);
}
public void enableToolbarScrolling()
{
logger.d("enabling scrolling toolbar");
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); // clear all scroll flags
toolbar.setLayoutParams(params);
}
And then using a PageChangeListener to handle the event:
public TabLayoutOnPageChangeListenerGroups(TabLayout tabLayout, Activity activity, FloatingActionsMenu fab, AppBarLayout appBarLayout)
{
...
this.appBarLayout = appBarLayout;
...
}
private void enableScrolling()
{
((GroupActivity) mActivity).enableToolbarScrolling();
toolbarState = toolbarState_enabled;
}
private void disableScrolling()
{
((GroupActivity) mActivity).disableToolbarScrolling();
toolbarState = toolbarState_disabled;
}
#Override
public void onPageSelected(int position)
{
if(position == 0)
{
logger.d("pos 0");
disableScrolling();
appBarLayout.setExpanded(false);
}
else
{
logger.d("pos != 0");
enableScrolling();
}
}
EDIT 1:
I have tried setting toolbar visibility to both GONE or INVISIBLE. But they only make the toolbar white, giving a white bar at the top of the screen. Still allowing for the scrolling behaviour.
add this line to your code in onCreate
setSupportActionBar(toolbar); and then add
toolbar.setVisibility(View.GONE);
For hiding the toolbar you can just try :
getSupportActionBar().hide();

Android setOnClickListener crashes my app on avd

I am new to android dev, I created a new project with NavigationDrawer activity, I use Android Studio. The problem is when I add a button and create OnClickListener, the app crushes, but without it, it launches fine. Please look at my code below.
I tried adding setContentView(View) but doesn't help
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //comes by default
setContentView(R.layout.fragment_main); //added by me, but doesnt help
//referencing my button
btnTest = (Button)findViewById(R.id.btnTest);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
//my event listeners
//when i highlight the below code everythin works..these block cause the crash
btnTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
I know others have had these issue and solved it, but I'm not able to solve mine, please help, Thank You
setContentView(R.layout.activity_main); //comes by default
setContentView(R.layout.fragment_main); //added by me, but doesnt help
Don't call setContentView twice. The one that "comes by default" is provided for your convenience when the IDE is generating the Activity class, if you don't need it, delete it.
Secondly, you are setting a fragment layout as View for your Activity. So unless R.id.btnTest is included in that layout, btnTest will be null, hence causing a NullPointerException when calling setOnClickListener.
delete this :
setContentView(R.layout.fragment_main);
and here
setContentView(R.layout.activity_main);
make sure you have a layout file for your Activity that is named activity_main.xml or replace that reference with the name of your layout file.
EDIT : I'm guessing you chose the option in AS to generate a placeholder in your Activity and a Fragment to be added to it. You need to handle the button in the Fragment class, and more importantly, you need to add the Fragment to your Activity.
In your Activity (in OnCreate, after the setup of the nav drawer) :
Fragment newFragment = new ExampleFragment(); // replace ExampleFragment by your Fragment's class name
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(CONTENT_VIEW_ID, newFragment).commit(); // CONTENT_VIEW_ID is the id of the View in your Activity that should contain the Fragment.
And then in your Fragment, move this to onActivityCreated :
btnTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
You will need to inflate R.layout.fragment_main in onCreateView in your Fragment, and get a reference to btnTest from the View you inflate.
Without Logcat I would say that your problem is one of the following:
btnTest is not defined in your ActivityLayout (activity_main). If this is the case check your XML.
Is your btnTest inside your Fragment? If so you should put the OnClickListener in your Fragment class instead of your Activity class.
BTW, the second setContentView doesnt make any sense you should use only one.
Hope it helps

Creating an android fragment that opens another fragment based on button activity

I'm trying to create an activity that adds a dynamic fragment at runtime. From that fragment I want to be able to open six other fragments on button click. [Going to use a case to implement this most likely]
Think of it as a windows 8 UI; with 6 buttons, each one opens a new fragment.
Unfortunately I have no idea how to go about this. I can't seem to get the button to pass data back to the main activity. I've also lost quite a bit of my code due to a git mishap. Here's what I recreated.
If you have any tips on coding style, syntax, java, OO- those are all welcome too. I'm coming from a C background. My end goal would be to create a replaceFragment(Frag) method for some easy syntactic sugar later on. Though I couldn't implement that with any success so far.
Another small question with fragments - I'm trying to add them dynamically at run-time - do I need to create all of them at run time? So each one needs a .add [Drink fragment, Menu fragment] or do I just need to do the .replace
SingleFragmentActivity.java
public abstract class SingleFragmentActivity extends FragmentActivity{
protected abstract Fragment createFragment();
FragmentManager fm = getSupportFragmentManager();
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //Lock screen orientation for app
Fragment frag = fm.findFragmentById(R.id.fragment_container);
fm.beginTransaction()
.add(R.id.fragment_container,frag)
.commit();
}
}
Customer_Activity.java
public class Customer_Activity extends SingleFragmentActivity {
public static Context appContext;
#Override
protected Fragment createFragment() {
return new CustomerSelectionFragment();
}
}
CustomerSelectionFragment
public class CustomerSelectionFragment extends Fragment implements OnClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.customer_selection_fragment, container, false);
//Buttons should be placed here?
Button btnDrink = (Button) v.findViewById(R.id.Drink);
btnDrink.setOnClickListener(this);
Button btnMenu = (Button) v.findViewById(R.id.Menu);
btnDrink.setOnClickListener(this);
return v;
}
//implement the onClick method here
public void onClick(View v) {
// Perform action on click
switch(v.getId()) {
case R.id.Drink:
//Not sure how to pass "Create Drink Fragment to activity?
break;
case R.id.Menu:
//Pass Create Menu fragment to activity?
break;
}
}
}
Totally ok with people editing my post for good-faith reasons [clarity, etc].
Any communication between fragments should be done via activity . Here is the link to developers site http://developer.android.com/training/basics/fragments/communicating.html , the tutorial is about communicating between fragments and pretty much explains everything.

Categories