How to pass dynamically created RelativeLayout to the fragment - android

I have created the class which extends Fragment which creates the RelativeLayout.I want to know how to pass that dynamically created RelativeLayout to the another fragment containing FrameLayout and set that RelativeLayout to that FrameLayout.

Why creating the layout outside the fragment? that's why fragments have onCreaveView & onViewCreated methods in their lifecycle.
I would suggest you to pass a STATE variable (int,string) to the fragment using Bundle,
and on the onCreateView method get the state in build the view according to it.
You can find simple example how to pass bundle and use it inside the fragment here
If you find yourself must pass a view to fragment you can use 3rd class which will hold it. But that's really not the right way...

I haven't try it yet but it might work
class CustomFragment extends Fragment{
RelativeLayout relativeView;
CustomFragment(RelativeLayout view){
relativeView = view;
}
onCreateView(){
//inflate layout and get FrameLayout
frameLayout.addView(relativeView);
}
}
and your RelativeLayout
RelativeLayout r = new RelativeLayout(context);
// custom it as you wish
CustomFragment fragment = new CustomFragment(r);

Related

ListFragment setListAdapter to custom adapter found outside fragment class

I have an activity with a layout, and the layout consists of a fragment that is a ListFragment. Insise my ListFragment derived class, I want to set the ListFragment from an adapter that is located inside the activity whose layout holds the fragment. Is there a way to pass the adapter to the fragment onActivityCreated, or do I need to create a new adapter inside the onActivityCreated method?
MainActivity.java --> adapter instance is here
activity_main.xml --> fragment is located in this viewgroup
MyListFragment.java --> I want to use setListAdapter(myAdapter) in onActivityCreated
If question doesn't make sense let me know. I am new to android and might have to reword.
Create one public method in activity:
public class MainActivity extends Activity{
public YourAdapter getYourAdapter(){
return adapterInstatnce;
}
}
In your ListFragment :
in onActivityCreate()
final YourAdapter yourAdapter = ((MainActivity)getActivity()).getYourAdapter();
You can pass just the adapter "data" from activity to fragment (use newInstance method), and the create the adapter inside the fragment.

Fragment doesn't fill screen properly

I'm instantiating a Fragment inside another Fragment. The inner fragment has its layout defined as match_parent for width and height. And their parent too.
This is the code for main Fragment. CustomFragment which is inside is the inner Fragment. Its a regular fragment with a calendar.
public class CalendarFragment extends Fragment {
...
public void onViewCreated( LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState ){
customFragment = new CustomFragment();
customFragment.setArguments( bundle );
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add( R.id.calendar_container, customFragment );
transaction.commit();
...
}
When this fargment comes to life, the inner fragment which contains the calendar adjusts its height based on content of each calendar cell.
Screenshot. Notice the empty space:
If both fragments have match_parent in its layout why this behavior occurs?
please make sure, your the container for second fragment (if any) is also match_parent
try give fragment different background color (ie. android:color/red) to check if fragment actually already full screen, but have transparent background. and your calendar view is only reside on small proportion of fragment area.
try to replace this line:
transaction.add( R.id.calendar_container, customFragment );
with this line:
transaction.replace(R.id.calendar_container, customFragment);
and add this line after the line of the commit:
getFragmentManager().executePendingTransactions();
i hope it will help you, for me it's doing the work.

Access ViewPager's Parent Fragment View in child fragment of Pager

I want to access a parent Fragment views in a child fragment which is attached with a View Pager. Means Parent Fragment contains a View Pager and View Pager then have a Fragment, and I want to use Parent Fragment Views in current Fragment of Pager.
Parent Fragment---->(Contains)View Pager------>(Contains)Child Fragment. Child Fragment wants to use Parent Fragment multiple views. Please suggest any solution regarding the same.
Thanks in advance.
in your child fragment use:
ParentFragment frag = ((ParentFragment)this.getParentFragment());
and in your parent fragment you can store the references of your required views and use getter setter to access them, another solution is creating a listener interface. follow below link to find out how to implement:
http://developer.android.com/training/basics/fragments/communicating.html
Inside Parent Fragment
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager())
make sure it is getChildFragmentManager()
and inside the child fragment, to access anything from parent fragment
((ParentFragment)this.getParentFragment()).any_thing_inside_parent_fragment;
You can use the below code.
FloatingActionButton fab = getParentFragment().getView().findViewById(R.id.menu_item);
I was trying to get a sibling Fragment using the parent so I tried to use getParentFragment() from the child Fragment and it was always returning null. So instead I just got the fragment from the viewpagerAdapter here's what I did to access a sibling Fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
mViewPager = (ViewPager) container;
BasePagerAdapter bpa = (BasePagerAdapter) mViewPager.getAdapter();
MySiblingFragment f = (MySiblingFragment) bpa.getItem(1); //I know the position of the fragment so I hardcoded it
}
hopes this will help someone.
I get the variable and view from activity using this
YourActivity activity = (YourActivity) getActivity();
activity.mVar = 1;
activity.textView.setText("Number "+activity.mVar);

Use view from fragment xml file

I have an activity with several tabs (using the 'fixed tabs + swipe' style). Each tab layout is defined as a fragment xml file.
Eg, my activity is called ModifyCustActivity. This uses an almost-empty xml file called activity_modify_cust.xml. Each tab on this page is represented by various xml files such as fragment_modify_cust_basic and fragment_modify_cust_address etc etc. Each of these fragment xml files contains EditTexts, Spinners and more.
When the activity starts, I need to be able to access these views from the activity code, as I need to pre-populate them, and get their results once they are edited. However, because these views exist in a fragment xml file, I don't seem to be able to reach them in code. Is there a way to access a view contained in a fragment xml file?
Is there a way to access a view contained in a fragment xml file?
Yes it is, but your fragment should be declared in the XML layout file, which seems to be your case.
For example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...">
<fragment
android:name="com.example.MyFragment"
android:id="#+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
And you would access the fragment like this:
FragmentManager manager = getSupportFragmentManager();
MyFragment fragment = (MyFragment)manager.findFragmentById(R.id.my_fragment);
Then using the fragment instance you could further access your views, for example by calling a public method from the fragment which updates some particular view.
UPDATE:
Suppose you have a TextView that appears in layout of the fragment, and need to update from the activity.
Let this be the fragment class:
public class MyFragment extends Fragment{
private TextView textView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, null, false);
textView = (TextView)view.findViewById(R.id.textView);
return view;
}
public void updateTextView(String text){
textView.setText(text);
}
}
Then you would update the TextView by calling in your activity the updateTextView() method:
fragment.updateTextView("text");
You can reach fragments views from activity. If you want to send a data from fragment to another fragment. Your sender fragment must communicate with activity and your activity can manipulate the view in other fragment
http://developer.android.com/training/basics/fragments/communicating.html

Problem putting a fragment into another fragment

I Tried putting a Fragment into a FrameLayout inside another Fragment via FragmentManager and FragmentTransaction (from android.support.v4.app). The container fragment has a button and a TextView on top and a FrameLayout at the bottom (I create the layout programmatically and i don't want to hurt your eyes with all of that). The CreateView() works just fine and i cann access the FrameLayout at the bottom of the container and add or remove View dynamically as I please via
#Override
public void onClick(View button) {
FrameLayout frame = (FrameLayout)findViewById(DETAIL_CONTENT_FRAME);
ImageView im = new ImageView(this);
im.setImageResource(R.drawable.test);
frame.addView(im);
}
but when I try to add a fragment instead of an ImageView to the frameLayout the code compiles perfectly but the desired fragment doesn't appear after the onClickListener() method is called. I checked the onCreateView() method of the fragment and it returns a proper view...
#Override
public void onClick(View button) {
ServerDialogFragment serverDialog = new ServerDialogFragment();
FragmentTransaction addDialog = getSupportFragmentManager().beginTransaction();
addDialog.add(DETAIL_CONTENT_FRAME, serverDialog);
addDialog.commit();
}
Do you have an answer to this ?
PS: I once tried adding fragments into other fragments and it worked, but they were simple fragments only holding ImageViews.
Fragments inside of other fragments is not supported at this time. See:
Fragment Inside Fragment
Fragments within Fragments
Android: Can you nest Fragments?

Categories

Resources