Fragment not getting replaced on click : <p>I have an <code>Activity</code> which consists of a <code>Fragment</code>. The <code>Fragment</code> has two ViewGroups (LinearLayouts),which I want to use like buttons to change the layout of that very <code>Fragment</code>. </p>

<p>I am facing two problems in this:<br>
 1. The two LinearLayouts don't give any visual touch-feedback (ripple animation) even when <code>clickable="true"</code> and <code>android:background="?attr/selectableItemBackground"</code> is set.<br>
 2. The <code>Fragment</code> doesn't get replaced by a new <code>Fragment</code> even though <code>onClickListener</code> for the two <code>LinearLayout</code> works (checked using a <code>Snackbar</code>).<br>
 3. In the statement <code>fragmentManager.beginTransaction().add(R.id.fragment_linearlayout,sb_qa_defaultFragment).commit();</code>, I read on SO that the first argument of add should not be the id of the <code>fragment</code> itself but the <code>container</code> of the <code>fragment</code>. Is it true ? Because I got the same behaviour setting id of the <code>fragment</code> or its <code>container</code> as the first argument.</p>

<p><strong>MainActivity</strong></p>

<pre><code>public class sb_question extends AppCompatActivity{

    private LinearLayout typeAnswerLL, recordAnswerLL ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_sb_question_new);

        final FragmentManager fragmentManager = getSupportFragmentManager();

        SB_QA_defaultFragment sb_qa_defaultFragment = new SB_QA_defaultFragment();
        final SB_TextAnswerFragment sb_textAnswerFragment = new SB_TextAnswerFragment();


        fragmentManager.beginTransaction().add(R.id.fragment_linearlayout,sb_qa_defaultFragment).commit();

        typeAnswerLL = (LinearLayout) findViewById(R.id.type_answer_linearlayout);
        typeAnswerLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                THIS STATEMENT BELOW DOESN'T WORK AND DOES NOTHING
                fragmentManager.beginTransaction().replace(R.id.fragment_linearlayout, sb_textAnswerFragment).commit();
//                Snackbar snackbar = Snackbar.make(view, "Type Answer", Snackbar.LENGTH_SHORT);
//                snackbar.show();
            }
        });

        recordAnswerLL = (LinearLayout) findViewById(R.id.record_answer_linearlayout);
        recordAnswerLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar snackbar = Snackbar.make(view, "Record Answer", Snackbar.LENGTH_SHORT);
                snackbar.show();
            }
        });

    }  
</code></pre>

<p><strong>Layout of MainActivity</strong>  </p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"&gt;
    &lt;android.support.v7.widget.Toolbar
        android:id="@+id/sb_ques_toolbar"
        android:elevation="4dp"
        android:background="@android:color/background_dark"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"&gt;
        &lt;TextView
            android:layout_gravity="center_horizontal"
            android:textStyle="bold"
            android:textSize="18sp"
            android:id="@+id/sb_ques_toolbar_title"
            android:textColor="@android:color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" /&gt;
    &lt;/android.support.v7.widget.Toolbar&gt;
    &lt;LinearLayout
        android:orientation="vertical"
        android:padding="@dimen/activity_horizontal_margin"
        android:background="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="match_parent"&gt;
        &lt;LinearLayout
            android:orientation="vertical"
            android:layout_weight="0.7"
            android:layout_width="match_parent"
            android:layout_height="0dp"&gt;
            &lt;TextView
                android:padding="@dimen/activity_horizontal_margin"
                android:textColor="@android:color/black"
                android:background="@android:color/white"
                android:id="@+id/sb_ques_text"
                android:minHeight="70dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" /&gt;
        &lt;/LinearLayout&gt;
        &lt;LinearLayout
            android:id="@+id/fragment_linearlayout"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp"&gt;
            &lt;fragment
                android:id="@+id/text_record_fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:name="com.example.yankee.cw.SB_QA_defaultFragment"&gt;
            &lt;/fragment&gt;
        &lt;/LinearLayout&gt;
        &lt;LinearLayout
            android:gravity="center|bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"&gt;
            &lt;Button
                android:padding="@dimen/activity_horizontal_margin"
                android:textAllCaps="true"
                android:text="Save &amp;amp; Exit"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content" /&gt;
            &lt;Button
                android:padding="@dimen/activity_horizontal_margin"
                android:textAllCaps="true"
                android:text="Skip Question"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content" /&gt;
        &lt;/LinearLayout&gt;
    &lt;/LinearLayout&gt;

&lt;/LinearLayout&gt;  
</code></pre>

<p>If it helps, here is the XML Layout of the <code>Fragment</code> I set as default when the <code>Activity</code> is launched <a href="https://gist.github.com/gamebusterz/6486078d40450bb1011df973961008ee" rel="nofollow noreferrer">sb_qa_default_layout.xml</a> and its <a href="https://gist.github.com/gamebusterz/454a11298822404fb5c897ae2d0bc41a" rel="nofollow noreferrer">Java code</a><br>
Same for the <code>Fragment</code> which will replace the default <code>Fragment</code> on clicking the <code>LinearLayout</code> on the default <code>Fragment</code>. <a href="https://gist.github.com/gamebusterz/d6ef5b77d84a58483ecf61351d2481d9" rel="nofollow noreferrer">Layout</a> and its <a href="https://gist.github.com/gamebusterz/fdb6273ffcdb342579774da981d5a2f9" rel="nofollow noreferrer">Java Code</a>  </p>

<p>Thanks in advance.</p>
