Declaratively defining fragments inside ViewPager - android

I have 5 unique pages of xml that are fairly complex. I want to put the 5 pages inside a ViewPager. All of the samples I can find simply put identical contents in each page via code. I want to declaratively define the xml in the viewpager like the xml pasted below. But this does not work - the app stops with this xml.
If I can't declaratively define it, then can I load individual xml pages into the viewpager? I can find no examples that do this.
thanks,
Gary Blakely
<?xml version="1.0" encoding="utf-8"?>
<fragment >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/imageView1"
android:src="#drawable/flashright" android:adjustViewBounds="true" android:scaleType="fitXY">
</ImageView>
</LinearLayout>
</fragment>
<fragment >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/imageView1"
android:src="#drawable/flashleft" android:adjustViewBounds="true" android:scaleType="fitXY">
</ImageView>
</LinearLayout>
</fragment>

If I follow, you want to declare everything in XML and avoid any programmatic initialization from within your Activity containing the ViewPager. The only way I can think of doing this would be to define 3 different Fragment classes which reference 3 different xml layouts. You could then embed them in your above xml, replacing each <Fragment> element with <FragmentX> <FragmentY> <FragmentZ> etc. Personally, I would rather write 3 xml layouts and create a single Fragment implementation that takes a single int argument to designate which layout to load, and then do the small amount of programmatic initialization necessary to make such a solution work. The rationale being that there is less duplicated code.
EDIT:
Another approach that might more fully address your requirements is to replace your <Fragment> tags with <Layout> tags and give them id's. Then in your Activity code, store refs to them, remove them from the View in onCreate() and finally add each of the stored references into programmatically created instances of Fragment which you then add to your ViewPager. Very ugly but thats the only way I know of to declaratively define everything in XML. You'll still have the problem that its not clear from the XML that these elements are nested within a ViewPager so personally I don't see the point. ListView and ListFragment operate with the same kind of implicit association though so its not unprecedented.

Related

Reuse a group of buttons across several fragments

I have a fragment defined by the following xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_example"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_example"/>
</RelativeLayout>
Now I would like to reuse the ImageButton (and other buttons I will add) in another fragment, ideally
without copying its definition
by keeping the definition in xml and not adding it programmatically
I.e. the goal is to overlay the same set of buttons in different fragments.
Is there a way to define all buttons in a separate xml file and load them programmatically on fragment creation?
Yes, You can.
Define all buttons in different xml say layout_buttons.xml
and add them in each fragment layout using
<include layout="#layout/layout_buttons" />

How to use two activites in a single screen in android?

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.

Use XML-design of a listview multiple times

I've written an app and now want to design the UI. The problem is that I have the same data (well, not the same, but the same kind of data since it's an XML readout) and now I wanted to design it. But I don't think I can get the design how I want it programmatically. So I thought I design it once with XML and then copy the layout in the for loop for the other data as well. Is this possible?
If yes, how is it possible? I finished now my XML for one entry and now I want that all the other entries have the same style and layout as the others... Thanks!
There is simple way to achieve this.
There is include tag in xml file which you can use one layout in multiple screen design.
for eg. you had create header for application so no need to use same code in all file, just you can include that portion in each xml file.
here is simple example which may help you.
Create a Re-usable Layout
titlebar.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:background="#color/titlebar_bg">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gafricalogo" />
</FrameLayout>
Use the Tag:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="#color/app_bg"
android:gravity="center_horizontal">
<include layout="#layout/titlebar"/>
<TextView android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:text="#string/hello"
android:padding="10dp" />
...
</LinearLayout>
You should be using an Adapter to define your ListView elements. In which case, you will define a layout for that adapter (using XML) that will apply to every element in your ListView.
Bottom line, use an Adapter, which will set the same XML layout for every element in your List.
(Check item #3)
http://docs.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters

Avoid to rewrite code for activities with the same layout

I'd like to run several Activities on my Application;I'd like each Activity to have a Linear Layout and to show an image as heading; basically I'd like every layout to start like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/Grey"
>
<ImageView android:id="#+id/imageHeader" android:src="#drawable/tf_header" android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="#color/Black" android:scaleType="fitXY"></ImageView>
Is it possible not to reapeat this code for each layout? Could I use themes or styles to avoid it? Thank you for your replies.
Yes thats possible use the <include> tag as described in Layout Tricks.
For your example I would add the image as <include> and have a LinearLayout in each Activity layout xml
One of my apps uses the same xml file for 30 or so classes, i just modify it in code to customize it. That approach may work, just leave every label in the xml blank and set it in the .class

How to share a view across various activities

Is there any way to share the same object of the View across various activities? For example myApp has 4 activities, and every activity shows a Logo at the top of the screen. Now each activity will initiate 4 copies of the same Logo. So is there any way to get around this? And if 3 out of 4 share the same logo?
I can't say that it is completely impossible for you to do that. What I can say with almost certainty is that you should not attempt it or expect anything good to happen if you do manage it. Don't fight the framework, let it work for you. If the duplication is that much of an issue you create an abstract class that your Activities inherit from.
Also, since View's maintain a reference to the Context they were created in. If you did manage to pass a View from one Activity to another you would be creating a memory leak. Since the View would hold a reference to the old Activity via the Context it was created in.
Use seperate layout to make your logo.
Ex: title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/headercon" >
<ImageView
android:id="#+id/headerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="#drawable/header" />
</LinearLayout>
This layout can be included in any other layout by using include tag
Ex:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
...
<include layout="#layout/title" />
...
</RelativeLayout>

Categories

Resources