android - access base activity from custom layout - android

I have a custom layout in my fragment , which extends RelativeLayout
public class Footer extends RelativeLayout
How can i use getSupportFragmentManager from this custom object ? When i use this.getContext() it gives Application , not FragmentActivity.
The method getSupportFragmentManager() is undefined for the type Footer (extends RelativeLayout)
Waiting for your help
Thanks

Send Context in Constructor like this
Let your Activity be MyFragmentActivity
MyFragmentActivity activity;
Footer(MyFragmentActivity activity)
{
this.context=context;
}
And From your Activity send Activity name.this
i.e
MyFragmentActivity.this
And now you can use
activity.getSupportFragmentManager()

Related

Calling mainActivity method through Fragment

I have implemented fragment in viewpager and Fragment has some buttons. Also viewpager is in activity_main layout.
I want that when button is clicked then it implement a method which is mentioned in mainActivity.java.
How can I do this?
I am a beginner.
You can get MainActivity method from fragment like below.
Make sure your methid is public if MainActivity and ViewPager are not in same package.
((MainActivity) getActivity()).getMethodOfMain();
You can call getActivity Method from your fragment and cast it to the your respective Activity.
lets take an example : You Have MainActivity class
MainActivity {
public void check() {}
}
And you have fragment : MainFragment
MainFragment{
Activity mainActivity = (MainActivity) getActivity();
mainActivity.check();
}
Thats how you can call the Respective Activity Method.

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.

how to call function from other fragment class

I'm trying to call this function public void arrowClick()
is inside my main fragment public class CounterMain extends Fragment implements View.OnClickListener{
the fragment is extend android.support.v4.app.Fragment
I want to call this function from another Custmoe Dialog fragment
public class CustmoeDialog extends DialogFragment {
I tried ((CounterMain)getActivity()).arrowClick();
but I can't use it it says android.support.v4.app.Fragment cannot be cast to my.example.CounterMain
and the
CounterMain x = new CounterMain(); x.arrowClick();
it makes my app to stop working when I call it
any help?
You can call activity method by this way
((YourActivityClassName)getActivity()).yourPublicMethod();
and from activity you can directly call by this way
FragmentManager fm = getSupportFragmentManager();//if added by xml
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
fragment.yourPublicMethod();
if you added fragment via code and used a tag string when you added your fragment, use findFragmentByTag instead:
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");
First of all, you can not cast from ACTIVITY to FRAGMENT
But to cast from GLOBAL ACTIVITY to your ACTIVITY
Or create INTERFACE and implement it in ACTIVITY
Then cast it in FRAGMENT from ACTIVITY to INTERFACE
And on the question:
See here the right way how to implement
Basic communication between two fragments

How to pass dynamically created RelativeLayout to the fragment

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);

Changing Title from Activities present in tabs in TabLayout in Android

Im trying to change the title from the activities inside the Tab. but the title remains same as the Title given for TabActivity. I searched for the solution but not succeeded yet. is there a way to do it? please help me on this.
Thanks
In an Activity used as child of a TabActivity you can use simply
getParent().setTitle("your title");
the getParent will return the current instance of TabActivuty, than you can change the title
You could use the singleton methodology with the TabActivity, then use This Method to change the title.
MyTabs.java
class MyTabs extends TabActivity {
private static MyTabs theInstance;
public static getInstance() {
return MyTabs.theInstance();
}
public MyTabs() {
MyTabs.theInstance = this;
}
...
}
ActivityInTab.java
...
TabActivity tabActivity = MyTabs.getInstance();
((TextView)tabActivity.getTabHost().getTabWidget().getChildAt(0).findViewById(android.R.id.title)).setText("New");
...
(Obviously this isn't a complete singleton implementation, but it will suffice for what you're doing. Since the activity can't exist without the parent container, it's safe to assume a reference has been set in the constructor when the object was created.)

Categories

Resources