I have decided my layout on the basis of JSON response and make a layout at run time without any xml . I just take a Activity and call a api that give me json array on the basis of which i make my layout. I have no problem with that but now i want to attach a navigation drawer with this activity . I have used navigation drawer in my previous Activity by making them fragment but in this case i don't know how to convert my activity to fragment because in navigation drawer we need fragment not activity.
i just do not know how to convert this Dynamic ui to fragment
View rootView = inflater.inflate(R.layout.`dashboard`,container, false);
what should i write in place of dashboard because i have no xml for that particular activity ..
I did it this way:
public class BaseFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ScrollView scrollView = new ScrollView(getActivity());
View view = //view from JSON
scrollView.addView(view);
return scrollView;
}
}
in navigation drawer handler
getFragmentManager().beginTransaction().replace(R.id.content_frame, new BaseFragment()).commit();
Related
How to Show activity element or view in it's fragment and also access from fragment without move from fragment to activity and also tell vice-versa of this scenario.
Here is My Problem:-
I am using Search Edit Box in Activity and I want to use this Search Box into my fragment but i don't want make any search box in fragment, I just want to show this Activity Search Box in Fragment When Ever User type in search box then by using textWatcher track the text and show the result of search box in fragment not in Activity.
Below is the image of UI that i want make it.
You can access activity elements and methods from fragment this way:
Activity template
public class ActivityMain extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content)
}
public void updateView(){
// update your views & vars here
}
}
Fragment template
public class BlankFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}
public void updateActivityView(){
if (getActivity() != null){
((ActivityMain) getActivity()).updateView();
}
}
For vice versa you can access fragment items from activity this way:
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
fragment.specific_function_name();
Where R.id.example_fragment is most likely the FrameLayout id inside your xml layout
I am developing an application where i need to call two fragments on an activity replacing another fragment,one of the fragment will contain a Form and another fragment will contain another Form ,i don't have any idea how to do this,so any suggestion will be cordially appreciated,thanks in advance....
I want to replace Form on Click button. by R&D i come to know at this point i need to use to fragment with two layout each layout will contain Forms.
Main Activity.
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Fragment 1.
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment1, container, false);
}
Fragment 2.
public class Fragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment2, container, false);
}
}
You can try like this
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.add(Home.newInstance(),"First");
ft.add(Second.newInstance(),"Second");
ft.commit();
So here you are added multiple fragment into the stack
If you are trying to replace Fragment1 with Fragment2, you replace the fragment.
https://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#replace(int,%20android.support.v4.app.Fragment)
I have two fragments, fragment_one (fragmentOne.class) and fragment_two (fragmentTwo.class). These fragments are being displayed inside ActivityMain.class.
When fragment_one is being displayed I want to set Image A as the background for ActivityMain.class.
When fragment_two is being displayed I want to set Image B as the background for ActivityMain.class
I can swap and set the background of ActivityMain.class when I am not using fragments (i use buttons as a test)....But, when I modify this for fragments I cannot get it to work.
Listed here you will see my ActivityMain.class
public class ActivityMain extends FragmentActivity {
//set variables for use of pageradapter and swipe screen
MyPageAdapter adapter;
ViewPager pager;
Context context = this;
LinearLayout swipeHomeScreen;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_activity_nav);
//Implement the adapater for the swipe screen on launch page and circle indicator
adapter = new MyPageAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
swipeHomeScreen = (LinearLayout) findViewById(R.id.swipeHomeScreen);
}
}
Here I have listed the code of one of my fragments where I am trying to share/pass the set the background image of ActivityMain.class...
public class ActivityNavSwipeTwo extends Fragment {
ActivityMain main;
public void onAttach(Activity activity) {
main = (ActivityMain) activity;
};
public static ActivityNavSwipeTwo newInstance() {
ActivityNavSwipeTwo fragment = new ActivityNavSwipeTwo();
return fragment;
}
public ActivityNavSwipeTwo() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_two, null);
main.swipeHomeScreen.setBackgroundResource(R.drawable.Image2);
return root;
}
}
My activity inflates the fragments fine, but the background image does not change.
How could I implement this for target api 23??
Thanks for your support.
Implement viewpager.onpagechangedlistener in your activity. Then add view.setonpageselecterlistener(this) in oncreated method. You can control selected fragment in implemented on pageselected method
Definitely you have any parent layout(Relative/Linear) for your activity. So you have to do following:
Create id of parent layout if not created.
access from fragment and change the background color of that parent layout.
Write this in Fragment's onCreateView() method.
relativeLayout = (RelativeLayout)getActivity().findViewById(R.id.relativelayout);
relativeLayout.setBackground(background);
Let say the target application is built from 3 fragments which are all in the same activity public class MainActivity extends android.support.v4.app.FragmentActivity implements ActionBar.TabListener. Starting fragment is public class ButtonSectionFragment extends Fragment where there is a Button:
public class ButtonSectionFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.btn, container, false);
Button mybutton = (Button) rootView.findViewById(R.id.mybutton);
mybutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
????????????????????
}
});
}
There are ?? in the onClick method, I will get to that. And there is another fragment like this:
public static class TextSectionFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tv, container, false);
TextView textv = (TextView) rootView.findViewById(R.id.texty);
}
Both of the fragments are using different layouts so this is why rootView is being used right in front of findViewById.
The outcome I would like to achieve is: by Button from 1st fragment click set the TextView from 2nd fragment to Hello. What should I put in the ??? part to make it all work?
You need to use interface as callback to the activity then set the textview in fragment.
Two framgents should never communicate directly.
http://developer.android.com/training/basics/fragments/communicating.html.
Framgent1 -->Activity -->Fragment2.
You can comunicate value from fragment2 to activity first then from activity to fragment2. Then set text in fragment2
You just have to use getActivity().findViewById() instead of getView().findViewById()
final TextView tv = (TextView) getActivity().findViewById(R.id.texty);
if (tv != null)
tv.setText("Hello");
Fragments are just branches inside the common layout tree of activity. All fragment views of a common activity can be accessed through Activity.findViewByXXX(). The only complication is that fragments can be dynamically added, removed, replaced, etc. So you to be sure that the needed fragment is already inflated into the layout hierarchy. You can make initialization of the UI in onViewCreated() of the other fragment. That guarantees you the layout has been loaded already.
Fragment frag1 = new ButtonSectionFragment ();
Fragment frag2 = new TextSectionFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(layout, frag1);
ft.add(layout, frag2);
ft.commit();
View frag1RootView = frag1.getView();
View frag2RootView = frag2.getView();
Button btn = (Button)frag1RootView.findViewById(id);
TextView tv = (TextView)frag2RootView.findViewById(id);
untested but... I think that would do it...
EDIT: You should get the root views onActivityCreated(); or it'll throw you a null...
In continuation to Raghunandan's answer, you could check similar implementation in the link.
update TextView in fragment A when clicking button in fragment B
Do not forget to get a reference of the textview of the TextSectionFragment into the MainActivity.
I made a program that uses FragmentTabs for implementing ActionBar Tabs.
I was wondering how it is possible to add a widget to a inflated Fragments in Tabs. It's more complicated than normal because the fragment is returning only an onCreateView to the TabListener, so how would I add in functioning widgets? I don't need specific code, just the logic please!
Here is a sample Fragment being inflated when a Tab is clicked (inside of which I need widgets):
public class IdFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.id_layout, container, false);
}
}