Search code examples
javaandroidandroid-actionbartoolbarandroid-toolbar

Why Toolbar is Overriding another activity's fragment?


Im not added toolbar in this fragment but it is overriding from Recyclerview's xml file

Im added toolbar in this specific recyclerview's xml file

Manifest file

<?xml version='1.0' encoding='utf-8'?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sanskrit.mysearcher">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:resizeableActivity="false">

        
        <activity android:name=".MainActivity">
                
             <intent-filter>
                    <action android:name="android.intent.action.MAIN" /> 
                    <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter>
            </activity>
            
        <activity android:name=".DisplayContactActivity">

        </activity>           
            
        <activity android:name=".SqlQrudActivity">
            
        </activity>
        
        <activity android:name=".ShowcaseWordsActivity"
            
            
            >
            
        
        </activity>
        
      
        
        
      <meta-data
          android:name="android.max_aspect"
          android:value="4.0"/>

    </application>
    
</manifest>

Recyclerview xml file

<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"
    
    >

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:title="Search Your Words"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/rvPrograms"
        >
    
    </androidx.appcompat.widget.Toolbar>
    
        
    
    
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvPrograms"
        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_toBottomOf="@+id/toolbar" />
    
</androidx.constraintlayout.widget.ConstraintLayout>

Crud Sql xml file where toolbar is not added by me but showing automatically after app installation

<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_height="match_parent"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:background="@color/white">


    <TextView


        android:id="@+id/insertID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:text="Insert Id"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_marginLeft="10dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"

        app:layout_constraintBottom_toTopOf="@+id/updateID"


    />

    <EditText
        

        android:id="@+id/updateID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:textSize="17sp"
        android:padding="10dp"
        android:layout_margin="10dp"
        android:background="@drawable/custom_input"
        android:inputType="number"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/insertID"
        app:layout_constraintBottom_toTopOf="@+id/insertText"
    />







    <TextView


        android:id="@+id/insertText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="5dp"
        android:text="Insert Name"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/updateID"
        app:layout_constraintBottom_toTopOf="@+id/editName"


    />

    <EditText

        android:id="@+id/editName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" 
        android:textSize="18sp"
        android:textStyle="bold"
        android:padding="10dp"
        android:background="@drawable/custom_input"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/insertText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/insertDescription"
    />



    <TextView


        android:id="@+id/insertDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:text="Description"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editName"
        app:layout_constraintBottom_toTopOf="@+id/editDescription"


    />

    <EditText

        android:id="@+id/editDescription"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_margin="10dp"
        android:hint="Add Description"
        android:textSize="18sp"
        android:textStyle="bold"
        android:gravity="center"
        android:padding="10dp"
        android:background="@drawable/custom_input"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/insertDescription"
        app:layout_constraintEnd_toEndOf="parent"
    />

    <include layout="@layout/fragment_word_recyclerview" />

</androidx.constraintlayout.widget.ConstraintLayout>

Styles.xml file

<resources>

    <!-- Base application theme. -->
        
    <style name="AppTheme" parent="DetailTheme">
        <!-- Customize your theme here. -->
      
        <!--
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">       
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        
    </style>
   -->
    </style>
    
        
        
        
    <style name="DetailTheme" parent="Theme.AppCompat.Light.NoActionBar">
       

        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        
        
        
    </style>
    
    
    
</resources>

Fragment class where only toolbar initialised by me

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_word_recyclerview,container,false);
    
        
        Toolbar toolbar = ((Toolbar) view.findViewById(R.id.toolbar));
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        activity.setSupportActionBar(toolbar);
        
        setHasOptionsMenu(true);

return view;

Solution

  • In the "Crud Sql xml", towards the end - you have:

    <include layout="@layout/fragment_word_recyclerview" />

    You may want to remove this line.

    If you see the second code snippet, this layout has toolbar and a recyclerview. If you include this layout in "Crud Sql xml", you will see the toolbar on top. The recyclerview may not be visible because you may not have added any content into it.