انت تستخدم اخر اصدار شكرا لك

  • More
  • أخر التعليقات

    Two-Fragments in Android Studio (Java) in one Activity

    This Project has:

    1- One activity (MainActivity) with two buttons (button , button2) and a fragment container (fragmentContainerView).

    2- Two fragments (ChannelsFragment, PlayerFragment), both has nothing inside just they have a different background colors.

     MainActivity: Layout:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">


    <Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:onClick="load_channels"
    android:text="Load Channels"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/fragmentContainerView" />

    <Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="load_player"
    android:text="Load Player"
    app:layout_constraintBottom_toBottomOf="@+id/fragmentContainerView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

    <androidx.fragment.app.FragmentContainerView
    android:id="@+id/fragmentContainerView"
    android:name="mr.safwan.iptv.PlayerFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

     MainActivity: Java:

    package mr.safwan.iptv;

    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentManager;
    import androidx.fragment.app.FragmentTransaction;

    import android.os.Bundle;
    import android.view.View;

    public class MainActivity extends AppCompatActivity {
    Fragment fragment_channels,fragment_player;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    public void load_channels(View view) {
    fragment_channels =new ChannelsFragment();
    FragmentManager pfm=getSupportFragmentManager();
    FragmentTransaction pft=pfm.beginTransaction();
    pft.replace(R.id.fragmentContainerView,fragment_channels);
    pft.commit();
    }

    public void load_player(View view) {
    fragment_player =new PlayerFragment();
    FragmentManager pfm=getSupportFragmentManager();
    FragmentTransaction pft=pfm.beginTransaction();
    pft.replace(R.id.fragmentContainerView,fragment_player);
    pft.commit();
    }
    }

     PlayerFragment: Layout:

    <?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"
    android:background="@color/purple_700"
    tools:context=".PlayerFragment" />

     ChannelsFragment: Layout:

    <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"
    android:background="@color/teal_200"
    tools:context=".ChannelsFragment">


    </FrameLayout>

    Note: for the fragments activities: we leave them as they will be created without any changes.










    No comments :

    Post a Comment