Post Top Ad

Post Top Ad

Saturday 8 July 2017

Fragment


1.activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2"
    tools:context="com.example.kiran.lesson9_fragments.MainActivity">


    <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:name="com.example.kiran.lesson9_fragments.SecondFragment"
        android:id="@+id/fragment1"
        android:layout_weight="1" />

    <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:name="com.example.kiran.lesson9_fragments.FirstFragment"
        android:id="@+id/fragment"
        android:layout_weight="2" />

</LinearLayout>
2.MainActivity.java:
package com.example.kiran.lesson9_fragments;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

3.fragment_first.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="#d9f7ff"
    android:layout_height="match_parent">

</LinearLayout>

4  FragmentFirst.java

package com.example.kiran.lesson9_fragments;

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

/**
 * Created by kiran on 11/1/2016.
 */

public class FirstFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_first,container,false);
        return view;
    }
}


No comments:

Post a Comment