Can I limit findViewById() to within the scope of an individual fragment? - android

In my application, I have multiple instances of the same fragment, Activity_Fragment, displayed in a LinearLayout. In each fragment, there is an activity_text textview and an edit_button. When the button is pressed in a fragment, it should change the text of the textview in the same fragment. However, when multiple instances of the fragment are added, any edit_button pressed in any fragment will only change the activity_text textview in the first fragment added to the LinearLayout. This is because all fragments are identical, and their child views share the same id's. Each fragment should act independently of one other; any edit_button pressed in a fragment should only affect that fragment.
Is it possible to have the findViewById() method limited to the scope of an individual fragment so that one fragment won't be affected by an event in another fragment, or will I have to somehow change the id of each view in a fragment upon creation?
Here is the Activity Fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
android:id="#+id/activity_fragment">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/activity_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Edit Activity"
android:textSize="18sp"
app:layout_constraintBaseline_toBaselineOf="#+id/checkbox"
app:layout_constraintLeft_toRightOf="#+id/checkbox"
app:layout_constraintRight_toLeftOf="#+id/edit_button"/>
<Button
android:id="#+id/edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:onClick="editActivity"
android:text="Edit Activity"
app:layout_constraintBaseline_toBaselineOf="#+id/checkbox"
app:layout_constraintRight_toRightOf="parent"/>
Here is my MainActivity.java with the ActivityFragment class. The onClick method for the edit_button is at the very bottom:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class ActivityFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_fragment, container,
false);
}
}
public void addActivity(View view) {
ActivityFragment fragment1 = new ActivityFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, fragment1).commit();
}
//This is the editButton method
public void editActivity(View view) {
TextView activityText = (TextView) findViewById(R.id.activity_text);
activityText.setText("success");
}
}

Your problem can be solved by limiting the scope that findViewById() scans. If you want to look within a particular fragment, you'll probably want to call myFragment.getView().findViewById() (rather than just using findViewById() from your activity).
So how can you do this? Well, it's not easy with the way you have things set up. Since you're using the android:onClick attribute to set your click listener, you have to handle things from inside the activity.
So don't use android:onClick.
Instead, inside your Fragment's onCreateView() method, write something like this:
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.your_fragment, container, false);
Button button = (Button) root.findViewById(R.id.edit_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView activityText = (TextView) getView().findViewById(R.id.activity_text);
activityText.setText("success");
}
});
return root;
}

you can use view.findViewById(R.id.activity_text) instead of the method from the activity.

Related

Error occurs in when click on a button in a tabbed activity in android

I use four tabs in my android application. An image button is in one tab activity and when I click that button I need to go to another activity which is not a tab activity. I tried a lot to do this, but once I click on the tab my app has stopped work. Please if someone could help me to solve this issue. I really appreciate your help.
This is FirstFragment.java file. I use image button in this activity and after click that button need to go to another activity.
package com.example.user.application001.fragmentspkg;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import com.example.user.application001.Invoice;
import com.example.user.application001.R;
public class FirstFragment extends Fragment {
public FirstFragment(){}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_second, container, false);
ImageButton fabImageButton = (ImageButton) view.findViewById(R.id.fab_image_button);
fabImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Users.class);
startActivity(intent);
}
});
return view;
}
}
This is the XML file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/fab_image_button"
android:layout_width="#dimen/fab_button_diameter"
android:layout_height="#dimen/fab_button_diameter"
android:layout_marginBottom="20dp"
android:layout_marginEnd="36dp"
android:layout_marginRight="36dp"
android:background="#drawable/fab_shape"
android:src="#drawable/plusbtn"
android:tint="#android:color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
you are using layout of fragment_second in fragment_first change it
use this
View view = inflater.inflate(R.layout.fragment_first, container, false);
instead of this
View view = inflater.inflate(R.layout.fragment_second, container, false);

Fragment button affecting other instances of the same fragment

I have a button that adds an instance of a fragment, ActivityFragment, when pressed. There is an edit_button in the fragment that, when pressed, changes the activity_text textview in the fragment. However, when multiple instances of ActivityFragment are added, any edit_button pressed in any fragment will only affect the first ActivityFragment added. I believe this is happening because each fragment shares the same id, but would there be any way to get around this so that each edit_button only affects the fragment that it is in, preferably without changing the fragment id upon creation?
Here is the Activity Fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
android:id="#+id/activity_fragment">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/activity_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Edit Activity"
android:textSize="18sp"
app:layout_constraintBaseline_toBaselineOf="#+id/checkbox"
app:layout_constraintLeft_toRightOf="#+id/checkbox"
app:layout_constraintRight_toLeftOf="#+id/edit_button"/>
<Button
android:id="#+id/edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:onClick="editActivity"
android:text="Edit Activity"
app:layout_constraintBaseline_toBaselineOf="#+id/checkbox"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
Here is my MainActivity.java with the ActivityFragment class. The onClick method for the edit_button is at the very bottom:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class ActivityFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_fragment, container,
false);
}
}
public void addActivity(View view) {
ActivityFragment fragment1 = new ActivityFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, fragment1).commit();
}
//This is the editButton method
public void editActivity(View view) {
TextView activityText = (TextView) findViewById(R.id.activity_text);
activityText.setText("success");
}
}
*This is my first time posting a question, sorry if I'm doing anything incorrectly.
This is happening because you're adding lots of fragments to your view. You haven't noticed that because all of them are equals to each other. I think you have to change this:
public void addActivity(View view) {
ActivityFragment fragment1 = new ActivityFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, fragment1).commit();
}
to this:
public void addActivity(View view) {
ActivityFragment fragment1 = new ActivityFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragment1).commit();
}
You have to remember that, when you're adding, all the views are appended and the findViewById will find the first one. And you can remove the static from the Fragment.
By using this :
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, fragment1).commit();
You are stacking up fragments one over the other. Change it to
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragment1).commit();

How to create a fragment of an activity in android studio?

I want to call a new activity when a list item is clicked from the drawer. I searched over the internet and what I got was to call the fragment of that activity. So, I wanted to know how to make a fragment of an activity.
P.S.- Sorry, if this question is silly. I'm still a noob.
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. Here the full explanation from android official developer guide.
Also on android developer guide you can find the code to properly create and use a fragment.
Create a fragment class:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_view, container, false);
}
}
Then you have to add the fragment to the activity layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.android.fragments.HeadlinesFragment"
android:id="#+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.android.fragments.ExampleFragment"
android:id="#+id/example_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
Finally you can add the layout to your activity:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_from_exampleLayout);
}
}
I took those examples from the android developer guide that i put above.
Anyway if i have understood your question this code should work:
list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Intent intent = new Intent(ThisClass.this, ClassToCall.class);
startActivity(intent);
}
});
This will start a new activity when a list item is clicked using an intent, but it doesn't need fragments.

Custom positioning of ListFragment

I have an Activity that loads a FrameLayoutcontaining a SearchView and Button when created. The button's onClick method loads a custom ListFragment on top of the main FrameLayout.
I override the onCreate method of the ListFragment to load my custom ArrayAdapterand populate the list with values from a string array that I manually create for now. All of this works properly but I would like to load the ListFragment at the bottom of the screen.
I have tried changing the gravity of the main FrameLayout to bottom, since it is the parent of the ListFragment but this makes the SearchView and Button drop to the bottom of the screen along with the ListFragment.
In this example, I am loading three rows in the list. How can I move the ListFragment down so that only the first row is visible at the bottom of the screen with the other two rows below it (off the screen)?
My MapActivity is the main Activity
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MapActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_map, container, false);
}
}
// Define the onShowTimesButtonClick listener
public void onShowTimesButtonClick(View view){
// Instantiate timeListFragment
TimeListFragment timeListFragment = new TimeListFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.container, timeListFragment, "timeList");
transaction.commit();
}
}
The layout of the MapActivity
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapActivity"
tools:ignore="MergeRootFrame"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false">
<SearchView
android:layout_width="300dp"
android:layout_height="50dp"
android:id="#+id/searchBar"
android:layout_gravity="start|top"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:background="#656565"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="text" />
<Button
android:id="#+id/timesButton"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="top|center"
android:layout_marginTop="90dp"
android:text="#string/times_button"
android:onClick="onShowTimesButtonClick"/>
</FrameLayout>
TimeListFragment is my custom ListFragment.
import android.app.ListFragment;
import android.os.Bundle;
import android.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout.LayoutParams;
/**
* A simple {#link Fragment} subclass.
*/
public class TimeListFragment extends ListFragment {
public TimeListFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Temporary time data
String[] times = new String[]{"1:00", "0:30", "0:45"};
// Load the time data
setListAdapter(new TimesListArrayAdapter(getActivity(), R.layout.layout_times_list_row, R.id.listText, times));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_times_list, container, false);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.BOTTOM;
container.setLayoutParams(layoutParams);
return view;
}
}
The layout for TimeListFragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:gravity="bottom">
<ListView android:id= "#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data"/>
</LinearLayout>
Here is what I have so far. I can move the ListFragment to the bottom of the screen by setting the gravity to bottom. In the onCreateView method of the TimeListFragment, I was applying the layoutParams to the container; according to the Android documentation, this container is the parent view that the fragment's UI is attached to. In order to apply the layoutParams to the fragment itself, I had to find the fragment view first by calling the findViewById on the inflated view.
The correct code for the onCreateView is shown below
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_times_list, container, false);
View fragmentView = view.findViewById(R.id.timesList);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.BOTTOM;
fragmentView.setLayoutParams(layoutParams);
return view;
}

Fragment doesn't work

I have main layout like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_showdetails"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_marginLeft="110dip"
android:layout_marginTop="15dip"
android:text="Show Details" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/my_parent_layout"
>
</LinearLayout>
</LinearLayout>
am trying to call another activity in the my_parent_layout using this class
package com.appnetics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
public class FragmenttestActivity extends FragmentActivity {
Button LogIn = null ;
final FragmentManager fragmentManager = getSupportFragmentManager() ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LogIn = (Button) findViewById(R.id.btn_showdetails);
LogInEvent() ;
}
public void LogInEvent(){
LogIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
details fragment = new details();
fragmentTransaction.add(R.id.my_parent_layout, fragment);
fragmentTransaction.commit();
}
});
}
}
the details layout is :
<?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:orientation="vertical" >
<TextView
android:id="#+id/date_time_label"
android:layout_centerVertical="true"
android:layout_marginLeft="200dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32dp"
android:textStyle="bold"
android:text="time_label"
/>
<TextView
android:id="#+id/last_view_time"
android:layout_toRightOf="#+id/date_time_label"
android:layout_alignBaseline="#+id/date_time_label"
android:layout_marginLeft="8dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32dp"
/>
</LinearLayout>
and the layout code is
/* $Id: $
*/
package com.appnetics;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* DateTime
*/
public class details extends Fragment {
private static String getDateTimeString(Date time) {
return new SimpleDateFormat("d MMM yyyy HH:mm:ss")
.format(time);
}
private final String time = getDateTimeString(new Date());
/** #see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) */
#Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle b)
{
View view = inflater.inflate(
R.layout.details,
container,
false); //!!! this is important
((TextView) view.findViewById(R.id.last_view_time))
.setText(time);
return view;
}
}
the problem is that when click the button, nothing appear , any idea what's wrong in my code
Lets start!
On your details XML:
android:layout_toRightOf="#+id/date_time_label"
android:layout_alignBaseline="#+id/date_time_label"
this two tags are for relative layouts, the parent of the layouts must be of relative type in order to work.
android:layout_centerVertical="true"
This tag is also incorrect, if you want to center the text in the textview you can use two ways or wrap content in textView and set center in its parent or use:
android:gravity="right"
android:width="fill_parent"
This will be the detail_fragment.xml
<?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:orientation="vertical" >
<TextView
android:id="#+id/date_time_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32dp"
android:textStyle="bold"
android:text="time_label"
/>
<TextView
android:id="#+id/last_view_time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="view time"
android:textSize="32dp" />
</LinearLayout>
On your main.xml you declare the schema lots of times! should be declared once on your root layout.
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
So the main.xml will be...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/add_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<LinearLayout
android:id="#+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
The fragment is an element that is declared in its class and apply its xml.
public class DetailsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, container, false);
}
}
And in the main activity add it to the holder layout:
public class FragmentSimpleTestActivity extends FragmentActivity {
private Button addFragment;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final FragmentManager fragmentManager = getSupportFragmentManager();
addFragment = (Button) findViewById(R.id.add_fragment);
addFragment.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
DetailsFragment fragment = new DetailsFragment();
fragmentTransaction.add(R.id.fragment_holder,fragment, "MY_FRAG");
fragmentTransaction.commit();
}
});
}
}
Its important that fragmentTransaction.add has parameters
1: the id of the view to hold the fragment.
2: the fragment object.
3: a tag that is used to find the fragments in the stack.
and thats all :)
Enjoy your fragments! you can also set animations when adding or removing it.
onCreateView()
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you
must return a View from this method that is the root of your
fragment's layout. You can return null if the fragment does not
provide a UI.
Call OnCreateView in your fragment class
return inflater.inflate(R.layout.fragmentlayout, container, false);
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;
}

Categories

Resources