Android Parent Activity Button on top of all Child Activities - android

So here is my problem and Question
I have a Header and Footer layout in a Main.xml and its FragmentActivity is Main.java.
Now this header and footer is common across all activities in my application as shown by Image.
https://www.dropbox.com/s/pgox67k33u0zxct/device-2013-10-23-002417.png
So to solve this problem i used Fragments as Shown in Figure Main.java Fragment A is inside Main.xml . Now this all works fine Fragment A is showing header and footer of Main.xml
Problem starts when i start an activity from Fragment A it totally opens in a New Window full screen . My understanding was if i start an Activity from Fragment A then this Activity will only take place of Fragment A . I hope you understand my question .

Re-using Layouts with <include/>
Define your Header and Footer layout
and use that like
<include layout="#layout/header"/>
and
<include layout="#layout/footer"/>
e.g
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/header" />
Go through Android Document
http://developer.android.com/training/improving-layouts/reusing-layouts.html

Why are you using activities in such case?
It seems that fragments would be more convenient for this type of problem especially since you want that header/footer are used from Parent activity.
so you would have in your xml:
header
framelayout (this would be replaced by fragment within activity)
footer
Each of the fragments have its own lifecycle and parent activity as well.
You can find example of this in Android documentation:
http://developer.android.com/guide/components/fragments.html

Related

What is the best way to add footer to all fragments in Android

I have one Android application(Xamarin.Android) and it has many fragments.
All fragments have the link to website in bottom.
Ideally I'd like to create it as custom fragment with link to website and add to all fragments.
But I don't find any way to add this fragment to XML of every fragment without coding.
I don't want to add the custom fragment to FrameLayout of other fragments in code.
Please let me know if anyone knows best solution for this kind of footer.
Thanks.
You can add the footer to the activity and use fragments for other screens. (Fragment frame layout above the footer in the main activity obviously)
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/footer">
</FrameLayout>
<FrameLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:text="Footer content"/>
</FrameLayout>
You can make a custom layout with your footer view at the bottom of the view and extend it for all your parent layouts.
Here is the simple way that I found.
Create new XML layout for footer
Create custom fragment class for this footer
Add this footer fragment to end of layout as needed as following
That's all!

How to Hide a Fragment included in a BaseActivity layout from a specific Activity

Here is my issue, I am working on the Ads integration for my Mobile Application.
In order to display a banner, I have created a BannerFragment.
I want to display this banner in all of my Activities, so that I have included it in a ActionBarAbstractActivity layout.
This allow me to display my Banner on all of my Activities without working on their XML files.
But, I have one specific Activity where I don't want to display Ads.
That's why I need to hide the Banner in this Activity. How can I access the BannerFragment in this Activity even if it's not referenced in its XML layout ?
I tried to find the Fragment by ID and hide it, but it's not working.
Should I create my proper banner container for this activty and set the visibility to "HIDE" or "GONE" ?
You can change the visibility property of the linear layout itself to hide/show the fragment.
Use an id for the LinearLayout in the XML.
<LinearLayout
android:id="#+id/bannerLayout"
android:layout_width="match_parent"
android:layout_height="50dp">
<fragment
..
..
/>
</LinearLayout>
In your activity,
LinearLayout bannerLayout = (LinearLayout) findViewById(R.id.bannerLayout);
To hide the layout,
bannerLayout.setVisibility(View.INVISIBLE); // Or in this case, View.GONE will also work
To show the layout,
bannerLayout.setVisibility(View.VISIBLE);
Maybe you can use include the xml of fragment instead of hide or gone.
like this
/>

How to create an overlay view within an activity

I have a requirement where i have an activity which shows list of items like facebook feeds and when clicking on a button from one of the list item a dialog has to popup which will show comments for that item.
I was going through the documentation and found out that we have to create a DialogFragment on the fly to achieve this. Please advice if this is the right approach.
You don't actually have to use a Dialog. I think dialogs are more appropriate when you want to show simple views or just an alert/confirmation to the user (normally done with an AlertDialog).
For your situation I guess the best approach would be to have a FrameLayout on your Activity, sibling of your main layout element, and add a Fragment to it when you want to show a popup like that over your main Activity's layout. As long as you put the fragment's view after your activity's root layout element, the fragment will be displayed on top of your main layout, as an overlay. e.g.:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Activity's main layout here -->
</LinearLayout>
<FrameLayout android:id="#+id/overlay_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</merge>
and then on your Activity when you want to display the fragment you do:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.overlay_fragment_container, yourFragment)
.commit();
Hope it helps :) Luck!

Android Tabs with List View

I followed this tutorial to created a tab activity using fragments:
http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/
One of the tabbed activities includes a list view; when you click on an item in the list view, it opens a new activity. But I want the new activity to still show the tabs that are in the tab activity. In other words, I want the tabs to show up on all the activities in the application, but I don't know how to make it persistent.
Would someone be able to point me in the right direction?
To make your application persistent, make use of Inheritance. create listView.xml, tabactivity.xml and embed those in MainActivity.xml as below:
<LinearLayout ... >
<include layout="#layout/listView" /> // your listview.xml file will be called
<include layout="#layout/tabactivity" /> // your tabactivity.xml file will be called
</LinearLayout>
You can add your tabview dynamically on every activity start.

Complex Android UI design guidance needed (fragments)

I am developing an applications that is aimed at Tablets and Google TVs. It will be like many standard Google TV applications with a LeftNavBar and a top Search bar that is common to all application screens. It will look something like the following image:
Main Screen
The RED area will be different for all other screens. It may contain data like following screens mockups:
Activity One loaded into main container
Activity Two loaded into main container
So you can see that completely different sections can be loaded in the main area.
Screen 3 can be loaded as a detailed section when selecting any list item in Screen 2 (say in fragment list) OR it can be loaded as a result of selecting a tab (which will appear in LeftNavBar).
Here is how I am trying to implement it.
Step 1. I Created a main Activity with the following XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#9ccc" >
<!-- Top Bar -->
</LinearLayout>
<FrameLayout
android:id="#+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- main Red Container that will load other Activities -->
</FrameLayout>
</LinearLayout>
mainContainer is the RED container where I want to load the Activities. LeftNavBar will be added to this Activity as its the parent of All.
Step 2 I created ActivityOne & ActivityTwo with two & three Fragments in them respectively (as shown in above second & third image).
*Step 3 I am trying to load the ActivityOne in main page's mainContainer FrameLayout... But I cannot add it.
I tried by adding the ActivityOne to mainContainer as follows:
View v = (new ActivityOne()).getWindow().getDecorView();
FrameLayout mainContainer = (FrameLayout) findViewById(R.id.mainContainer);
mainContainer.addView(v);
but the getWindow() returns null....
Other issue occurs because all the data comes from a remote services .. so please also suggest how would I be able to hold references to all the loaded Activities in mainContainer in a some kind of stack ... so I can just reload the already loaded activity instead of creating its new instance.. This will be used on BACK button press.
OR
Instead of loading an activity into the above RED container, I should create two Activities each with their own Fragments & a LeftNavBar. This might be easier than the aforementioned approach. or this might be the only solution.... however I feel that saving state for BACK buttons might get messy .. but I will try implementing this
What would you do if you had to create this type of application?
How would you design the UI layout for best performance/practice?
Your suggestions in helping me setting this app's layout are much appreciated.
Disclaimer
This is where fragments can get tricky. The problem would be simple if Activity 1 & 2 had identical layouts so that you could simply attach/detach fragments and use the fragment back stack to unwind.
Because you want 2 unique layouts to house your fragments, things are going to be a little more involved. If at all possible I would try to use the same layout so that you can take the easy path.
As another option, you could use two activities as you outline above and send data back and forth with Intents.
That said, if I really had to implement this solution as written, here is what I would do. Note that I am not advocating this solution but myself do not know of a better way of doing things.
The Solution
Create a FragmentActivity whose view would be Main Screen as you've defined above. The layout for the Main Screen would contain:
Left nav bar
Top bar
2 layouts. layout1 and layout2. These would be contained in a parent layout i.e. RelativeLayout or LinearLayout and would contain the necessary FrameLayout elements for your fragments.
Example using your XML (note, tags are a bit brief):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#9ccc" >
<!-- Top Bar -->
</LinearLayout>
<LinearLayout android:id="#+id/layout1">
<FrameLayout android:id="#+id/listFragment" />
<FrameLayout android:id="#+id/contentFragment" />
</LinearLayout>
<LinearLayout android:id="#+id/layout2">
<FrameLayout android:id="#+id/imageFragment" />
<FrameLayout android:id="#+id/boxFragment1" />
<FrameLayout android:id="#+id/boxFragment2" />
<FrameLayout android:id="#+id/boxFragment3" />
</LinearLayout>
</LinearLayout>
The main idea is that you then show/hide layout1 & layout2 i.e. set android:visibility="gone" based on the state of your app.
The disadvantages of this method are:
Using fragment backstack may be impossible, instead you'll have to track where the user is in your UI flow and manage the back button to show/hide layout
You may need to take special care to attach/detach fragments when you show/hide their parent view to reduce resource consumption while the fragments are invisible
The advantages are:
Easy communication between fragments and the base activity since only 1 activity is used
Re: The nested Fragments problem
To get around the 'nested Fragments' problem in our application where (as you correctly note) Fragments cannot add Fragments I hosted a single templating Fragment under the activity whose only purpose was to define a set of place holders for other fragments to anchor to. When adding further Fragments to the activity past this point I used the templating Fragment's view place holder +#ids to identify the "root" or parent view Id for the Fragment being added.
getSupportFragmentManager().beginTransaction().add(#someIdFromTheTemplateFrag, fragment, fragmentTag).commit();
The Fragment I was adding then knew where to anchor itself in the current layout and of course then went about it's merry way of add it's view. This had the effect of attaching a Fragment to another Fragment hence achieving the desired visual 'nesting'...

Categories

Resources