Open In App

How to Implement findViewById in Fragments in Android?

Last Updated : 01 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Fragments represent a part of the app’s UI which is reusable. Fragments are having its own layout, and lifecycle but must be hosted inside an activity to be visible. The activity becomes easier to modify if it consists of many fragments. A fragment can have multiple instances inside an activity. findViewById finds the view by the given ID.

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Working with the XML Files

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
  
    <LinearLayout
        android:id="@+id/linearlayout01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#ccc"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
  
        <!-- This fragment code is used to add fragment in activity -->
        <fragment
            android:id="@+id/gfgFragment"
            android:name="com.gfg.javaexample.gfgFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
  
        <!-- This code is used to show some texts -->
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="100dp"
            android:background="@color/white"
            android:gravity="center"
  
            android:text="Welcome To GeeksForGeeks!"
            android:textColor="@color/purple_700"
            android:textSize="42dp" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>


Step 3: Working with the MainActivity File

Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.

Java




import androidx.appcompat.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);
        // set content to main activity
    }
}


Kotlin




import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // set content to main activity
    }
}


Step 4: Creating a New Fragment

Navigate to app > new > fragment > blankFragment > gfgFragment and add the below code to that file.

Kotlin




import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
  
class gfgFragment : Fragment() {
  
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.fragment_gfg, null)
    }
}


Java




import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
  
public class gfgFragment extends Fragment {
  
    public gfgFragment() {
        // Required empty public constructor
    }
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
  
  
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return (ViewGroup) inflater.inflate(R.layout.fragment_gfg, null);
    }
}


Navigate to the app > res > layout > fragment_gfg.xml and add the below code to that file. Below is the code for the fragment_gfg.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<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=".gfgFragment">
  
    <TextView
        android:id="@+id/text1"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_gravity="center"
        android:background="@color/purple_500"
        android:backgroundTint="@color/purple_500"
        android:gravity="center"
        android:padding="20dp"
        android:text="Fragment in GFG"
        android:textColor="@color/white"
        android:textSize="42dp" />
</FrameLayout>


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads