Creating Fragments Using Android Studio

Creating Fragments Using Android Studio

Think of a fragment as a modular application section that has it on inputs and life cycle.You can refer it as a sub-activity.
This subactivity can be reused in another application.This article will show you how to extend Fragment class and using support library so that the application will be compatible with other devices running an Android version below 1.6.

Create new Android Project

Open Android Studio,go to File ,New Android Project.


Enter Android Project name.
Create new Android Project

Minimum required SDK for this project is Android 3.0 or higher. I selected 4.0
Minimum required SDK


Create Empty Activity.
android-studio-empty-project


Enter Activity Name


Your final created Object should look like this.

android-project-structure

Create Main Activity Layout
We are going to create two Buttons, fragment and add them to LinearLayout. Go to res>layout>activity_main.xml.  Open activity_main.xml and paste below code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.acer.fragmentexample.MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fragment No 1"
        android:onClick="selectFragment"
        />
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fragment No 2"
        android:onClick="selectFragment"
        />
    <fragment
        android:name="com.example.acer.fragmentexample.FragmentOne"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_location"
/>
</LinearLayout>


In design, View You should see this.

Creating Fragments

Open com.example.acer.fragmentexample  package and create a New Fragment.Specify Blank Fragment.


Click Fragment(Blank) and Then Specify Fragment name as OneFragment.


This will automatically create Fragment Layout and Fragment Java files.OneFragment.java  file


package com.example.acer.fragmentexample;


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

/**
 * A simple {@link Fragment} subclass.
 */
public class OneFragment extends Fragment {

    public OneFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_one, container, false);
    }

}


   Fragment_one.xml
<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="com.example.acer.fragmentexample.OneFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello...This is Fragment one" />

</FrameLayout>


TwoFragment.java
package com.example.acer.fragmentexample;

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

/**
 * A simple {@link Fragment} subclass.
 */
public class TwoFragment extends Fragment {

    public TwoFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_two, container, false);
    }

}


   fragment_two.xml
<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="com.example.acer.fragmentexample.TwoFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello..this is Fragment two" />

</FrameLayout>


Coding the MainActivity.

We will implement selectFragment method in the mainactivity that is responsible for interchanging between the two fragments when we click the two buttons.


package com.example.acer.fragmentexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.FragmentManager;

public class MainActivity extends AppCompatActivity {

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

    public void selectFragment(View view) {
        Fragment fragment;
        if (view == findViewById(R.id.button1)) {
            fragment = new OneFragment();
        } else {
            fragment = new TwoFragment();

        }

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment_location, fragment);
        ft.commit();
    }}
Running Application

How application is now complete. Run it and you should see below window.   Downloadthe whole application code here.




COMMENTS

Name

android android Apps android emulators android.app antivirus arrays blogger bluestack Break Statement in Java constructors download downloader drivers emulators fragment java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql node.js php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Creating Fragments Using Android Studio
Creating Fragments Using Android Studio
Creating Fragments Using Android Studio
https://2.bp.blogspot.com/-xwvtZEEFfLc/WnMjzishljI/AAAAAAAAKD8/O2ob9BLtNLEc7hWjvl67B1vqQN6GsIaOwCLcBGAs/s320/android-emulator.PNG
https://2.bp.blogspot.com/-xwvtZEEFfLc/WnMjzishljI/AAAAAAAAKD8/O2ob9BLtNLEc7hWjvl67B1vqQN6GsIaOwCLcBGAs/s72-c/android-emulator.PNG
Java Codes
https://www.download-all.net/2018/02/creating-fragments-using-android-studio.html
https://www.download-all.net/
https://www.download-all.net/
https://www.download-all.net/2018/02/creating-fragments-using-android-studio.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy