Note that there are some explanatory texts on larger screens.

plurals
  1. POShow/Hide Fragments and change the visibility attribute programmatically
    primarykey
    data
    text
    <p>This is a 2 part problem. What I'm having is a 3 Fragment layout where the 3'rd Fragment(FragmentC) is added dynamically when the user taps a button found in another fragment. Then, after it's added the 3'rd Fragment has a button to maximize/minimize it.</p> <p><strong>UPDATE:</strong> Scrool at the end for <strong>SOLUTION</strong></p> <p><br> <br> <b>PROBLEM 1:</b></p> <p>I'm trying to change the visibility attribute of a FrameLayout that acts as a container for the 3'rd fragment(R.id.fragment_C).</p> <p>What the code is supposed to do is to generate another fragment that, originally has an XML containing android:visibility = "gone". Then, the Fragment is added when tapping a button and the visibility is suppose to change to VISIBLE.</p> <p>I know this has been covered before, but after 4 hours of trying to make it work I decided to ask what I'm doing wrong.</p> <p><b>PROBLEM 2:</b></p> <p>After the 3'rd fragment is generated I have a minimize/maximize button that's supposed to hide the first 2 Fragments and allow the 3'rd Fragment to fill the screen.</p> <p>The problem is the Views of the first 2 Fragments are not removed when using .setVisibility(View.GONE). This has been covered before as well, but I can't figure out why it doesn't work in my code.</p> <p>The code so far(sorry if it's to verbose but I thought it's better to include all details for you folks):</p> <p><b>main_activity.xml</b></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="0dp" android:paddingRight="0dp" android:orientation="vertical" &gt; &lt;FrameLayout android:id="@+id/fragment_A" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:background="#CCCCCC" &gt; &lt;/FrameLayout&gt; &lt;FrameLayout android:id="@+id/fragment_B" android:layout_width="fill_parent" android:layout_height="300dp" android:layout_below="@id/fragment_A" android:layout_centerHorizontal="true" android:layout_marginTop="15dp" android:background="#B4B4B4" &gt; &lt;/FrameLayout&gt; &lt;FrameLayout android:id="@+id/fragment_C" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@id/fragment_B" android:layout_centerHorizontal="true" android:layout_marginTop="0dp" android:background="#A3A3A3" android:visibility="gone" &gt; &lt;/FrameLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p><b>land/main_activity.xml</b></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:paddingLeft="0dp" android:paddingRight="0dp" &gt; &lt;LinearLayout android:id="@+id/fragments_container" android:layout_width="fill_parent" android:layout_height="200dp" android:baselineAligned="false" &gt; &lt;FrameLayout android:id="@+id/fragment_A" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.5" android:background="#CCCCCC" &gt; &lt;/FrameLayout&gt; &lt;FrameLayout android:id="@id/fragment_B" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.5" android:background="#B4B4B4" &gt; &lt;/FrameLayout&gt; &lt;/LinearLayout&gt; &lt;FrameLayout android:id="@+id/fragment_C" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@id/fragment_container" android:layout_centerHorizontal="true" android:layout_marginTop="0dp" android:background="#A3A3A3" android:visibility="gone" &gt; &lt;/FrameLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p><b>MainActivity.java</b></p> <pre><code>package com.example.android.fragments_proto.activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import com.example.android.fragments_proto.R; import com.example.android.fragments_proto.fragment.GMC_DateSelectionFragment; import com.example.android.fragments_proto.fragment.GMC_ProdUnitSelectionFragment; public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); FragmentManager fm = getSupportFragmentManager(); Fragment fragmentA = fm.findFragmentById(R.id.fragment_A); Fragment fragmentB = fm.findFragmentById(R.id.fragment_B); if (fragmentA == null) { FragmentTransaction ft = fm.beginTransaction(); ft.add(R.id.fragment_A, new FragmentA()); ft.commit(); } if (fragmentB == null) { FragmentTransaction ft = fm.beginTransaction(); ft.add(R.id.fragment_B, new FragmentB()); ft.commit(); } } } </code></pre> <p>Now the XML and .java files for the first Fragment.</p> <p><b>fragment_A.xml</b></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" &gt; &lt;DatePicker android:id="@+id/datePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; </code></pre> <p><b>FragmentA.java</b></p> <pre><code>package com.example.android.fragments_proto.fragment; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.widget.DatePicker; import android.widget.Toast; import com.example.android.fragments_proto.R; public class FragmentA extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_A, container, false); DatePicker datePicker = (DatePicker) view.findViewById(R.id.datePicker1); datePicker.setCalendarViewShown(true); datePicker.setSpinnersShown(false); datePicker.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Activity activity = getActivity(); if (activity != null) { Toast.makeText(activity, "You Touched ME!", Toast.LENGTH_SHORT).show(); } return false; } }); return view; } } </code></pre> <p>Now the XML and .java files for the Fragment that contains the button that when tapped adds the content in R.id.fragment_C</p> <p><b>fragment_B.xml</b></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="0.1" &gt; &lt;ListView android:id="@+id/listView1" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:gravity="center" android:layout_height="wrap_content"&gt; &lt;Button android:id="@+id/button" android:text="@string/btn_fragment" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p><b>FragmentB.java</b></p> <pre><code>package com.example.android.fragments_proto.fragment; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import com.example.android.fragments_proto.R; public class FragmentB extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragmentB, container, false); ListView listView = (ListView) view.findViewById(R.id.listView1); Button button = (Button) view.findViewById(R.id.button); String[] machines = new String[] { "MachineId-001", "MachineId-002", "MachineId-003", "MachineId-004", "MachineId-005", "MachineId-006", "MachineId-007", "MachineId-008"}; listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); listView.setAdapter(new ArrayAdapter&lt;String&gt;(getActivity(), android.R.layout.select_dialog_multichoice, machines)); final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.fragment_C); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Activity activity = getActivity(); if (activity != null) { getFragmentManager().beginTransaction().replace(R.id.fragment_C, new FragmentC()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).addToBackStack(null).commit(); frameLayout.setVisibility(View.VISIBLE); } } }); return view; } } </code></pre> <p>The XML and .java files for the Fragment that's supposed to be added.</p> <p><b>fragment_C.xml</b></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:gravity="center" android:layout_height="wrap_content"&gt; &lt;Button android:id="@+id/maximize_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Maximize Me!" /&gt; &lt;/LinearLayout&gt; &lt;TextView android:id="@+id/text_view" android:textIsSelectable="true" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FF33FF" /&gt; &lt;/LinearLayout&gt; </code></pre> <p><b>FragmentC.java</b></p> <pre><code>package com.example.android.fragments_proto.fragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import com.example.android.fragments_proto.R; public class FragmentC extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_C, container, false); TextView textView = (TextView) view.findViewById(R.id.text_view); final Fragment fragmentA = getFragmentManager().findFragmentById(R.id.fragment_A); final Fragment fragmentB = getFragmentManager().findFragmentById(R.id.fragment_B); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { FragmentTransaction ft = getFragmentManager().beginTransaction(); if (fragmentA.isVisible() &amp;&amp; fragmentB.isVisible()) { ft.hide(fragmentA); ft.hide(fragmentB); fragmentA.getView().setVisibility(View.GONE); fragmentB.getView().setVisibility(View.GONE); button.setText("Minimize Me!"); ft.addToBackStack(null); } else { ft.show(fragmentA); ft.show(fragmentB); fragmentA.getView().setVisibility(View.VISIBLE); fragmentB.getView().setVisibility(View.VISIBLE); button.setText("Maximize Me!"); ft.addToBackStack(null); } ft.commit(); } }); return view; } } </code></pre> <p><br> <br> <br> <strong>Found the problem and a solution thanks to <a href="https://stackoverflow.com/users/1113510/moesio">Moesio</a></strong></p> <p><strong>PROBLEM:</strong></p> <p>My error was that I was trying to find a view (in FragmentB.java) with </p> <p>final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.fragment_C);</p> <p>This line was returning null so when the code reached the point where it was supposed to do a .setVisibility() then the app. would return a nullPointerException.</p> <p>The same happened for FragmentC.java (so my 2 problems were related). The Views were not removed because my findViewById was null!</p> <p><br> <strong>SOLUTION:</strong></p> <p>Just search for your View with <strong>getActivity</strong>.findViewById(R.id.your_view);</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload