KIO
Kreative Ideen online
What fragment code looks like

What fragment code looks like

When you create a fragment, Android Studio creates two files for you: Java code for the fragment itself, and XML code for the fragment’s layout. The Java code describes the fragment’s behavior, and the layout describes the fragment’s appearance.

We’ll look at the Java code first.

package com.test.workout;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

//This class extends the Fragment class.
public class SomeFragmentName extends Fragment{
      
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
      //This tells Android which layout the fragment uses 
      return inflater.inflate(R.layout.layout_belonging_tothis_fagment, container, false);
       }
}

The above code creates a basic fragment. As you can see, the code fro a fragment looks very similar to activity code.
To create a fragment, you first need to extend the Fragment class or one of its subclasses. The fragment implements the onCrateView() method, which gets called each time Android needs the fragment’s layout, and it’s where you say which layout the fragment uses. The onCreateView() method is optional, but as you need to implement it whenever you’re creating a fragment with a layout, you’ll need to implement it nearly every time you create a fragment.

The fragment’s onCreateView() method

The onCreateView() ethod returns a View() object that represents the fragment’s user interface. It gets called when Android is ready oto instantiate the interface, and it takes three parameters:

public View onCreateView(LayoutInflater inflater, 
                         ViewGroup vontainer, 
                         Bundle savedInstanceState) {

}

The firs parameter is a LayoutInflator that you can use to inflate the fragment’s layout. Inflating the layout turns your XML views into Java objects.
The second paramters is a ViewGroup.This is the ViewGroup in the activity’s layout that will contain the fragment. The final parameter is a Bundle. This is used if you’ve preeviously saved the fragment’s state, and want to reinstate it.

You specify the fragments’s layout using hte LayoutInflator’s inflate() method:

public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState){
    return inflater.inflate(R.layout.layout_belonging_tofragment, container, false);
}

Fragment layout code looks just like activity layout code

Fragments use layout files to describe their appearance. Fragment layout code looks just like activity layout code, so when you write your own fragment layout code you use any of the views and layouts you’ve already been using to write activity layout code.

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  android:orientation="vertical">

  <TextView
    android:layout_widht="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/some_string"
    android:id="@+id/textTitle" />

</LinearLayout>

Leave a Reply

Your email address will not be published. Required fields are marked *