i am targeting an app on android 2.1. i am having a layout which contains three sections. the header, mainbody and the footer. the footer remains same all through the application lifecycle. the footer has four options for the user to select(like four tabs). when the user selects each option in the footer the content has to change in the mainbody. and when the user interacts with the UI in the mainbody, there is a need to change the content of just the mainbody(like activity replacing an activity). and the user selection in the footer has to remain highlighted untill user selects another option in the footer.i alomost have a need like, launching activities within the same tab, but the tabs are placed below. a lot of people have suggested using activitygroup but as it is deprecated how do i go about doing this?. if anybody needs more clarity about question i am ready to provide
If you want support from lower versions like 2.1 and higher I can propose my way. I always use separate XML layout for tray (footer in your case), for example (res/layout/tray.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainMenu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#EEEEEE"
android:gravity="bottom|fill_horizontal"
android:orientation="horizontal"
android:padding="5dp" >
<ImageView
android:id="#+id/addBtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/add" android:layout_weight="0.2"/>
<ImageView
android:id="#+id/catalogBtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/catalog" android:layout_weight="0.2"/>
<ImageView
android:id="#+id/searchBtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/search" android:layout_weight="0.2"/>
<ImageView
android:id="#+id/settingsBtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/settings" android:layout_weight="0.2"/>
<ImageView
android:id="#+id/infoBtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/info" android:layout_weight="0.2"/>
</LinearLayout>
And includes it in every Activity I need:
<include layout="#layout/tray" android:id="#+id/tray" />
After that I can in java code hide/show some buttons in tray by ID, or select some of them with another color ...
To display the footer menu across all activities, you may create a custom layout - the layout of the footer - and include it in every activity, or to be more specific, include it in the layout of every activity with <include />
Also, all your activities should have a parent activity, let it be BaseActivity, where you will provide appropriate actions for you footer menu.
Then you will need just to inherit the BaseActivity and include the footer menu layout, into your current layout, to have the menu available for any Activity you would like.
Another possible approach is using fragments instead of a full activity per content page. Fragments are a lot like activities, except that they need to be embedded in an activity to be displayed and they give you the freedom to change the content of one part of an activity (that is, swapping one fragment for another), meaning you can have another part of the activity remain unchanged - for instance tabs for switching between these fragments. A nice bonus is that reusing that content page in another activity is very easy, and should you choose create a tablet-friendly version you can easily compose more complex views of your existing fragments.
Using a ViewPager together with some type of page indicator, such as the tab indicator here you can have an active fragment and easily switch between them.
Since you are targeting 2.1 you will need to use the android support library to support fragments.
Related
I want to display two different activities in a single screen how can i do that in android?Please if anybody has idea share it.And I don't wanna use fragments.
I want to display a screen which contains some fields and below(at the bottom of the screen) I want another screen with some buttons.
Is this possible in android?
If so, How can i do this ?
You can't have two activities in one screen. You can have only one. So, ultimate solution is Fragments.
An activity is not directly a visual component, so I'm thinking that what you're really asking is how to have a single activity display different views.
There's nothing that says you can't rerun setContentView() with a different layout/view ID. But there's another non-fragments way of doing what your probably want.
You can define more than one full-size (match_parent) view in a layout. What you want to do is set the visibility for one of them to "visible" with android:visibility="visible" and all the others to "gone" with android:visibility="gone".
Then when you want to switch the displayed view, you'll run setVisibility(View.GONE) on the outgoing view and setVisibility(View.VISIBLE) on the incoming. It's important to use GONE and not INVISIBLE or the layouts won't render correctly.
Sample layout file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
<SurfaceView
android:id="#+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<WebView
android:id="#+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</FrameLayout>
Sample Code to switch view:
video.setVisibility(View.VISIBLE);
img.setVisibility(View.GONE);
web.setVisibility(View.GONE);
That said, you probably want to learn how to use fragments since you can handle switching the view along with other state in a single unit of work (a transaction). But the above approach above does work for simple view changes.
We are developing an app that in one point, we need a screen like Honeycomb Gmail application :
http://www.cnx-software.com/wp-content/uploads/2011/02/android_3.0_honeycomb_gmail_app_fragments_700px.png
We are trying to use fragments and includes a listview to show our items.
We did exactly the same thing on this link : http://www.vogella.com/articles/Android/article.html#fragments_tutorial
But in any way we did not able to view different layouts,
what i mean is that, the only thing that we can show on the right fragment a single textview.
but we need a listview there, that we can view a thumbnail,some explanations and this needs to be clickable.
anyone to help?
Should work. I think you want to instantiate a ListFragment that, upon the user selecting a row, instantiates another ListFragment.
Maybe read this article aswell.
Below is an example for a layout xml file that has two fragments next to each other with an even width.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<fragment
class="package.of.fragmentA"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/fragmentA"
/>
<fragment
class="package.of.fragmentB"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/fragmentB"
/>
</LinearLayout>
In the case of the GMail application the class fragmentA would be a ListFragment and fragmentB would be a normal fragment class with a custom layout file.
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'...
I'm trying to embed the fragments API to an application but I can't control the order of fragments being loaded and thus getting some exceptions.
I configure my layout this way:
<fragment class="org.me.myListFragment"
android:id="#+id/frag_title"
android:layout_marginTop="20dp"
android:layout_width="#dimen/titles_size"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<fragment class="org.me.DetailsFragment"
android:id="#+id/details"
android:layout_marginTop="20dp"
android:layout_width="#dimen/titles_size"
android:layout_height="match_parent" android:layout_weight="1"
/>
</FrameLayout>
But the DetailsFragment is being loaded before the list fragment and this causes trubles.
I don't want to break the old code, controlling the order here would be the quickest way to fix it.
Is there a way I can make the second fragment to be initialized only after the first one?
No, you cannot control the order if you use <fragment> elements. If you switch to dynamic fragments (e.g., FragmentTransaction), you control when fragments are created, because you create them.
IMHO, one fragment should not depend upon another fragment in the manner that you describe.
I am creating a custom widget to display a rotating list of items. I have declared the layout in newsview.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/tblLink"
style="#style/newsviewstyle"
android:layout_alignParentBottom="true">
<ImageView
android:id="#+id/newsviewimg"
style="#style/newsviewimgstyle"
android:layout_margin="5dp" />
<TextView
android:id="#+id/newsviewtitle"
android:layout_toRightOf="#id/newsviewimg"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:textColor="#000000" />
<TextView
android:id="#+id/newsviewurl"
android:layout_toRightOf="#id/newsviewimg"
android:layout_below="#id/newsviewtitle"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_width="fill_parent"
android:layout_height="15dp"
android:textColor="#000000" />
</RelativeLayout>
The idea is in my main.xml, I have a ViewFlipper that I would like to use animate the transitions between each item. How could I instantiate "n" unique items declared above, so they can bee added to the ViewFlipper. How would I be able to get access to the layout declared from a class outside the main activity (and also outside the package containing the main application, i.e. a view controller).
As a note, collecting the list of items takes place in a controller outside the main activity of my application. I would like to keep everything as much as possible in the controller for possible reuse later on.
How could I instantiate "n" unique items declared above, so they can bee added to the ViewFlipper.
Use getLayoutInflater().inflate(), the same way you would in a custom Adapter. In fact, on Honeycomb, there is AdapterViewFlipper to handle this very scenario.
How would I be able to get access to the layout declared from a class outside the main activity (and also outside the package containing the main application, i.e. a view controller).
I'm sorry, but this sentence did not parse for me.