I am developing application which requires many (more than 80) screen layouts (each layout having different image views, buttons, textviews and so on) and I have to be able to switch between them (upon button clicks). I am also using SlidingMenu so I would very much like to have only one Activity (I am currently using ActionBarActivity and the SlidingMenu serves as expandable navigation TreeView).
So my question is, what is the best practice to be able to switch between a lot of unique screen layouts within one activity ?
I suggest two ways:
1) Use FrameLayout, and Fragment
2) Use ViewFlipper in main.xml that in this one every child of ViewFliper is one of your 80 views like:
<ViewFlipper ...>
<!-- first view -->
<LinearLayout ...>
</LinearLayout>
<!-- second view -->
<LinearLayout ...>
</LinearLayout>
<!-- third view -->
<LinearLayout ...>
</LinearLayout>
<!-- and so on -->
</ViewFlipper>
Of course you can create 80 separate xml files and include them in ViewFlipper.
Related
I'm going to create a complex/normal design that has an empty screen layout & shimmer layout.
my question is to have all in one layout with multiple <include> files is better than doing two fragments and keep replacing them.
more details:
1- make the data screen shown & empty screen hidden as follows:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/common_toolbar" />
<include layout="#layout/empty_layout"/>
// my data layout
</LinearLayout>
2- create a FrameLayout and keep change the container for different screens with multiple fragments:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
I need to know the best practice as depending on slow rendering of multiple views.
another question, is that included in for shown/hidden views or it depends on shown ones only as I got confused
and need to apply best practices.
if you attend to reuse some views Fragment is the best, otherwise, if you looking best to render I advise you to use constraint layout and section your views with a group so you can hide and show group
take a look to this one How to group multiple views in a ConstraintLayout
I have 3 buttons that is modify ,view, clear and i want it to be used in 20 activities. I want to re use the same as if in user control in android?
Button 1- Modify,
Button 2 - Clear,
Button 3- view.
Is it good to make common view for all?
You can keep your 3 buttons in another layout say layout_button.xml and then include this in ur activity's layout wherever required like :
For ex. this ur activty layout :
<?xml version="1.0" encoding="utf-8"?>
<-- Inflate your other elemnts of ur activity -->
<include
android:layout_width="fill_parent"
android:layout_x="0dp"
android:layout_y="0dp"
layout="#layout/layout_button" />
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'...
Example: http://cdn3.staztic.com/screenshots/cfo-magazine-mobile-1-2.jpg
I've just started learning how to program android apps, and I'm not sure how to go about it. Are they using tab layouts for both the main menu and the submenu right below it? If you could point me in the right direction, it'd be greatly appreciated.
The top menu is a TabHost.
The "submenu" looks like a HorizontalScrollView with Buttons.
The main view is a ListView.
I'd create a LinearLayout to act as the tab content's view something like this...
<LinearLayout>
<!-- Usual width/height attributes and set the orientation as vertical -->
<LinearLayout>
<!-- Usual width/height attributes and set the orientation as horizontal -->
<!-- Place the 'Latest News' and other buttons here -->
</LinearLayout>
<ListView>
<!-- Usual width/height attributes -->
</ListView>
</LinearLayout>
I want to let the user know what page of the app he is on using tabs. So if he is on page one the page one tab will be lit and if he is on page two the page two tab will be lit etc. But i want it so that the tabs don't have any function. They are stay the same across all the pages (execpt what is lit) and do not have touch/click events. Should I use tabs for this or is there a better option? How exactly do I accomplish this with tabs for the better option? Thanks in advance
Probably the best way to do this is to avoid using the tab widgets altogether, and simply roll your own with TextViews arranged in a container like LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<!-- Top-level layout for page -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Tab Bar -->
<LinearLayout
android:id="#+id/tab_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Tab 1 -->
<TextView
android:id="#+id/tab_bar_file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/file_label"/>
<!-- Tab 2 -->
<TextView
android:id="#+id/tab_bar_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/edit_label"/>
<!-- More tabs go here -->
</LinearLayout>
<!-- Page content goes here -->
</LinearLayout>
Some notes about this:
You can set padding on the TextView
to expand the tab size. You can set a
background drawable (including a
color or an image resource) to
change the 'look' of the tab, as
well as style the text.
You can extract the enclosing LinearLayout for the tab bar into a
separate XML file, and then use the
<include> directive to incorporate
it into whatever layouts need to
display the tab.
In your Java code, you simply change
the style/color of whatever tab is
current, to highlight it for the user.