Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define a fragment's layout from XML?

Tags:

I'm trying to define a fragment's layout in XML in the same way that I defined the layout of my view.

Is this possible? I tried several things, but none of them seem to work.

My activity layout looks as follows (main.xml):

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
  <fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="full.lommeregner.Lommeregnerrv2Activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lommeregnerv2">
    </fragment>   

</ListView>

Now, since I'm not a big fan of generating layouts through raw Java code, I tried defining my fragment as follows (fragment_lommeregner.xml):

  <fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="full.lommeregner.Lommeregnerrv2Activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lommeregnerv2">
       <!-- my content was located here (some textviews, buttons and so on). -->
    </fragment> 

Am I doing something wrong? How can I define the layout of a fragment through XML?

like image 229
Mathias Lykkegaard Lorenzen Avatar asked Feb 26 '12 14:02

Mathias Lykkegaard Lorenzen


People also ask

What is XML fragment?

A Fragment is a combination of an XML layout file and a java class much like an Activity . Using the support library, fragments are supported back to all relevant Android versions. Fragments encapsulate views and logic so that it is easier to reuse within activities.

How do I attach a fragment to an activity?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

How do I switch fragments?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();


2 Answers

A Fragment works much like an activity, in that you need a Java class file to go with it. you cannot create a Fragment just by creating a fragment layout - you need a class for your fragment:

  1. Create a layout XML and an Activity subclass for your activity
  2. Create a layout XML and a Fragment subclass for your fragment
  3. Tie the two together in your Activity layout XML (or using FragmentTransaction if you want to do it in Java code)

If you haven't already done so, read, re-read and digest everything on this page: https://developer.android.com/guide/components/fragments.html

There's a lot there, but Fragments are an essential part of Android apps now so it's required reading. The good news is that the basics of fragments is pretty simple.

like image 111
Ollie C Avatar answered Sep 20 '22 06:09

Ollie C


Simply add into your <fragment> tag as property:

tools:layout="@layout/your_layout_xml"

And in your main.xml (or <fragment> tag container) into your parent container (in this case ListView):

xmlns:tools="http://schemas.android.com/tools"
like image 24
FireZenk Avatar answered Sep 21 '22 06:09

FireZenk