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.
Related
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
I have one activity with ListView and 2nd activity which have ViewPager. Now I want in one activity if status true then go to 2nd activity `
Intent n = new Intent(one, two);
startActivity(n);`
But here problem is it giving opening animation is any i can do within that 1st activity
any way i can avoid that animation and it look like it is same activity
or i have to redo full code and this inside 2 fragments ? so have one fragment activity and then but one activity code inside fragment and two activity inside fragment which will have view pager.
Paraphrasing you, your are trying to call activity B from activity A, based on a result, which is an item picked from list item, however you wanna avoid any sort of screen transitions (animations) when moving from one another.
I may be wrong but my google search didn`t revealed any way to avoid transition between activities.What your could actually do is to refactor your code using fragments and sort them out within the parent activity view group in a way that fragment B maximizes itself or pops up over fragment A pausing it.
Brs.,
I have an Activity having multiple fragments of tabs, on pressing "Running total " button present at the bottom, the UI of that particular tab hides and another layout which displays a table,is shown. I want to display the main UI of the tab and hide that table layout on pressing device's back button. I can't use BackStack as I'm using horizonal naviagtion(tabs). How to achieve this?
You could simply override the onBackPressed() in your activity and write the logic there to handle what you want to do.
If you going to manipulate fragment during onBackPressed, it could be done by overriding onBackPressed in the Activity itself. But this would mess up the Fragment-Activity communication. It's better to have onBackPressed in the Fragment also.
A simple google search will give you multiple links on how to have onBackPressed in Fragment.
Follow the below link for how to implement this: http://vinsol.com/blog/2014/10/01/handling-back-button-press-inside-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
I have created a fragment inside an Activity, and now I want to open another application/Activity inside this fragment, Result that I want is, both the activities should be seen on the display (it should not open in another window). Please let me know how can I achieve this.?
Nope you cannot achieve this, you cannot start an activity inside a fragment nor you can show a fragment inside a fragment. For showing multiple layouts in a single screen or activity you need to adjust your activity's layout file and arrange different fragments accordingly ...