Home » Android » android – Activity to Fragment Without Using replace(R.id.layout)-Exceptionshub

android – Activity to Fragment Without Using replace(R.id.layout)-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

so im trying to add a button in my activity to go to a fragment and i used the correct code but in .replace(R.id.container, f2).commit(); the “container is the id for the layout but how do i make the transaction with out overlapping layouts here is the code for xml
and below it is a picture of the activity code
example: (i have MainActivity i want to add a button that can take me to MyFirstFragment from MainActivity) without replacing the layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/Blue"
    android:id="@+id/container"
    android:tag="container_1"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/stay_connected_with_friends"
        android:textSize="28sp"
        android:textColor="@color/white"
        android:fontFamily="@font/nunito_bold"
        android:layout_marginTop="230dp"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="200dp"
        >

    </TextView>


    <Button
        android:id="@+id/createAccount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@color/Blue"
        android:fontFamily="@font/nunito_bold"
        android:text="@string/sign_up"
        android:textColor="@color/white"
        android:textSize="25sp">

    </Button>

    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="200dp"
        android:layout_marginBottom="172dp"
        android:background="@color/Blue"
        android:fontFamily="@font/nunito_bold"
        android:text="@string/login"
        android:textColor="@color/white"
        android:textSize="25sp">

    </Button>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="175dp"
        android:layout_height="141dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="114dp"
        android:layout_marginBottom="136dp"
        android:contentDescription="@string/logo"
        app:srcCompat="@drawable/logo" />

</RelativeLayout>

enter image description here

How to&Answers: