Rossini
Rossini

Reputation: 13

independence between layout and code in Android platform

If Android uses XML layout to separate Java code from application layout, why I have to specify the Java class in Fragments layout, like the code below? In this case, if I alter the class name (Java code), I must alter the XML layout too. It doesn't sound like independence to me.

<?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:baselineAligned="false"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/titles"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="course.examples.fragments.staticlayout.TitlesFragment" />

    <fragment
        android:id="@+id/details"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="2"
        class="course.examples.fragments.staticlayout.QuotesFragment" />

</LinearLayout>

Upvotes: 0

Views: 32

Answers (2)

Zaid Mirza
Zaid Mirza

Reputation: 3719

I will add some more and simple explanations to CommonWare' answer

<fragment
        android:id="@+id/details"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="2"
        class="course.examples.fragments.staticlayout.QuotesFragment" />

using fragment tag inside XML is just like an another View (Button,ImageView.....). In this case you don't have to add fragment to layout in Java code. System will automatically add fragment to your layout and fragment will behave static as you cannot replace it with other fragment at runtime.To change fragment at runtime, you have to add fragment by yourself in Java code into a fragment container like FrameLayout.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007658

why I have to specify the Java class in Fragments layout, like the code below?

The vast majority of the XML elements in a layout resource specify a Java class name. In your example, LinearLayout is a Java class name.

The LayoutInflater logic looks at the element name and treats it as a Java class name, except for a few specialized element names like fragment. Sometimes, that Java class name is a fully-qualified one, for a class from a library (e.g., android.support.constraint.ConstraintLayout) or one of your own (e.g., com.commonsware.cwac.RecyclerViewEx). Where the Java class name is without a package (e.g., LinearLayout), LayoutInflater looks for it in a few well-known Java packages (e.g., android.widget).

In the case of <fragment>, the class name happens to be in the form of a class attribute, perhaps to help the LayoutInflater distinguish between a view and a fragment.

In this case, if I alter the class name (Java code), I must alter the XML layout too.

Correct. This is no different than with most other elements in a layout resource.

If Android uses XML layout to separate Java code from application layout

Android uses layout resources to help with handling different configurations (e.g., portrait/landscape, small/large screen). Secondarily, layout resources help isolate UI configuration from Java code, in part to be easier for tools like IDEs to manipulate.

Upvotes: 1

Related Questions