FragmentTransaction.add() issue while setting parameters for ContainerView and Fragment Class

21 Views Asked by At

I have tried to import android.support.v4.app.Fragment, but it doesn't allowing me to import that.

This is my MainActivity enter image description here

package com.example.saveload3;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public class MainActivity extends AppCompatActivity {

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

        Button addtypeButton = findViewById(R.id.addTypeButton);
        LinearLayout typeContainerLayout = findViewById(R.id.frame12);
        
        addtypeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                TypeFragment TFragment = new TypeFragment();
                fragmentTransaction.add(typeContainerLayout,TFragment).commit();
            }
        });
    }
}

This is Fragment Class (TypeFragment) enter image description here

package com.example.saveload3;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.fragment.app.Fragment;


public class TypeFragment extends Fragment {

This is gradle enter image description here

plugins {
    id("com.android.application")
}

android {
    namespace = "com.example.saveload3"
    compileSdk = 33

    defaultConfig {
        applicationId = "com.example.saveload3"
        minSdk = 24
        targetSdk = 33
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

dependencies {


 implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.9.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    /*implementation("com.android.support:support-fragment:28.0.0")*/
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}

while I'm trying to implement "com.android.supoort", its show an error enter image description here

I'm searching for a solution to add fragments in a ContainerView

0

There are 0 best solutions below