I'm having an issue where I can create a Fragment, its view appears to be created, but it doesn't show up. The fragment itself is created and any code inside runs without issue, but it is just invisible somewhere. The back button also interacts with it just fine (it "closes" it), it just doesn't physically show up on screen (only the main layout is displayed).
From my FragmentActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_view);
// some logic here that sets a button listener that calls openAddNamePane() when pressed
}
public void openAddNamePane(){
AddNamePane addNamePane = new AddNamePane();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.layout_root, addNamePane, "AddnamePane");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
This is the layout for the View that is first displayed (activity_main_view.xml aka my 'root layout'):
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="@color/white"
android:id="@+id/layout_root">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/addPlayerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Player" />
<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonLabel" />
</LinearLayout>
<ListView android:id="@+id/playerListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@color/white"
android:entries="@array/testStringArray" >
</ListView>
</LinearLayout>
This is my fragment class:
public class AddNamePane extends Fragment {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("AddNamePane.onCreateView()");
View view = inflater.inflate(R.layout.add_player_pane_layout, container, false);
return view;
}
// a few other methods here, onAttach(), onStop(), onPause(), etc
// all are empty of logic with just a println to see if they are being called correctly (in which they are)
}
This is the layout for that "AddNamePane" fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="test name here"
android:hint="Name here" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
I am pretty new to using fragments, not too sure if my issue is in code or if it is something to do with the layouts. My goal is to add the "AddNamePane" to the 'root layout' replacing the main view temporarily. What I did to attempt this was to give my root layout an id and use that in the FragmentTransaction. If it matters, the api level I am using is 8 (Android 2.2) thus using things from android.support.v4.app package.
You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.
Build and issue the notification: Create an Intent that starts the Activity . Set the Activity to start in a new, empty task by calling setFlags() with the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK . Create a PendingIntent by calling getActivity() .
You're trying to programmatically replace a LinearLayout
, while also expecting it to have the hard-coded children. This won't work. You should use a FrameLayout
for your FragmentTransaction
that is also a child of your layout_root
.
If you update your post with the actual xml, I can show you exactly how to do this.
I had a similar problem, when fragment wasn't showing up. The problem was that I removed AndroidAnnotations, but forgot to move the code from onViewCreated and onCreate methods to the onCreateView. I found this mistake by comparing the code of two fragments - one, that was showing up and the other that wasn't. So, if you have similar problems, try to check the code of fragments. It may not show any errors at first glance, but will not still work or show up because of those errors.
Particularly, check that you have this method and that you inflate a view:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.your_fragment_layout, container, false);
return v;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With