Handle Activities OnClick inside Fragment - android

I have this layout that Half of the screen is framelayout and the other half is buttons.
The framelayout has a fragment.
And my buttons on the other half has some commands that will change the views of the fragment.
But my problem is, How would I know if the button is clicked which is inside the activity to tell the fragment that button is clicked.
I cant put the button inside the fragment because the button will be used on other fragment. If there is only a way.

The easiest way is using interfaces. Define an interface inside your fragment, and let the activity implement that interface.
Check this link: https://developer.android.com/training/basics/fragments/communicating.html

Related

Implement bottomSheet behavior in fragment

I am trying to implement bottom sheet behavior to fragment.The two points are need to implents
i)calling one fragment to another when button is clicked.
ii)apply bottomsheet behavior to fragment B,So that When I drag down the fragment can hide/dismiss.
Currently I am having two fragment A,B.I call fragment B in Fragment A by a container When button is clicked .
But when I am trying to implent second point I get error.On FrgmentB I am extend only Fragment not Fragmentdialog. The main reason is I cannot able to acess the background UI.
I have the same problem before, what I did is change the fragment to BottomSheetDialogFragment and made some changes.

Hide activity button in some fragments and show in some fragments

I have a main activity that includes two buttons 'Submit' and 'Pick' and few images. Inside the activity i have two fragments named MyThings and MyPicks. I want to hide activity button 'Pick' in MyThings fragment but want to show in MyPicks fragment. How can i achieve this? Because i don't know how to initialize the button which is in main activity to that fragment so that i can hide and show button in fragments. Please Help
There are several ways to handle this situation.
One of them is that you have two methods in your Activity named
showButton() and hideButton() and in that methods you hide and
show button. In your Fragments you can call these methods in
onCreateView() Method since you can invoke Activity methods from
Fragment.
Another way is you hide or show button in your Activity when
creating and putting Fragment in the container. I prefer the
second way.

How to blur the Parent Activity when a fragment is added?

An Activity which has an image when i click the image
1)A fragment should be added in the middle of the parent activity
2)When the fragment appears the background of parent activity should get blurred/dimmed.
3)And clicking outside the fragment the fragment should disappear again.
(Same effect we get when we click contact image in whatsApp it opens an overlay)
I have can add fragment successfully but the activity remains active.
How to achieve this effect?
I have tried fragmentTransaction.setCustomAnimation(int,int) but it does not work.
Thank you.
You have two options:
Use a DialogFragment setting the layout in the onCreateView() method.
Add a full screen Fragment with a background with alpha (for example #44000000). Add the OnTouchListener to the background view and in the onTouch() method, dismiss the Fragment.
By the way, I would seriously recommend you the first option.

Android: Using fragments

Is it possible to disable an activity elements when it loads a fragments?
I have a program which has an activity and two fragments. I put a container in activity. When I put two buttons in activity and load each fragment by clicking the button, fragment loads on the activity, but when I click in the position of buttons which are under fragment(or in the large screen next to it), they do some actions, however I don't like it. The buttons should not be clickable.
As a simple solution I create a third fragment and put my buttons in it and load it as a default view in the activity.
I was wondering is it possible to do this without using third fragment.
If you do not want clicks to propagate to below layers you can specify android:clickable="true".
In your case define android:clickable="true" in the bottom layout of your fragments layout xml file to stop any clicks to the activity below.
mach's solution is great, but i can suggest a solution that will be helpful if you want to do more actions in the future than just disabling buttons.
You can simply have your activity implement an Interface "OnFragmentLoaded" for example which has a single method onLoaded()
and in your fragment in your onAttach(Activity ac) method you can do the following
((OnFragmentLoaded) ac).onLoaded()
and you activity would implement onLoaded() to do what ever you want

Sliding the top layer of an activity when clicking a button

Please, suggest a solution for the following scenario:
When a user clicks a button, the screen must be slid from right to
left and the next screen must be shown. But the background for the
both screens is the same (it's an image), and it is not slid, it stays
at its place.
What can you suggest? Can I use activities here, or there must be fragments, or anything else?
I found the solution. I created an activity and in the setContentView() function set a layout with a custom ViewPager. In the custom ViewPager the onTouchEvent() and onInterceptTouchEvent() functions are overriden and just return false.
The screens are implemented as Fragments. When a button is clicked, the viewPager's setCurrentItem() function is called and the corresponding screen is shown (the viewPager object is declared static and it can be called from a fragment).
Here is a good example of using ViewPager: http://www.javacodegeeks.com/2013/04/android-tutorial-using-the-viewpager.html
And this is an example of a custom view pager, where sliding is disabled: https://stackoverflow.com/a/13437997/2346046

Categories

Resources