I have two layouts layout1 and layout2.
Initially in onCreateView of my fragment I am using layout1 like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.layout1, container, false);
Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setViewLayout(R.layout.layout2);
}
});
return rootView;
}
After that onclick button I am changing rootView from layout1 two layout2 and its working fine:
This is how I am changing:
private void setViewLayout(int id){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rootView = inflater.inflate(id, null);
ViewGroup viewGroup = (ViewGroup) getView();
viewGroup.removeAllViews();
viewGroup.addView(rootView);
}
To access fields of layout2 I created one class and instantiate it on onClick event of button:
Now I want to go back layout1 from layout2 I did same and view is changing but fields are not working like onClick button of layout1 is not responding now.
PS: I don't want to start fragment again, I just want my previous layout1 instead of new one.
How I can go back so that I can use my previous states of layout1?
the better approach here is to use two separate fragment for each layout and on Click replace the current fragment with second one.
you won't be able to recover your previous layout as you have removed that from the rootview
secondly you are not saving fragment state
This is not how it is done. You need to add fragment in a backstack to maintain the state. Check implement back navigation for fragments from android developer site. Hope it helps.
You can try Replace Fragment. Not setViewLayout(R.layout.layout2)
Related
I am looking for a hint/way how to achieve such a "layout" in android. What I have so far is layout of mainActivity, on which is embedded fragment A. What I wanna do, is place above fragment A next one (Fragment B). For better illustration image is included.
How should I implement such a composition ? Also, after click on "Save" it should process inserted information a fragment should be hidden/removed.
Thanks in advance
You can use DialogFragment. You may refer to this or this tutorial
Create layout (your editview and button), e.g. fragment_register_account.xml
Inflate the layout in a class e.g. RegisterAccountDialog that extend DialogFragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_register_account, container);
mEditText = (EditText) view.findViewById(R.id.txt_your_name);
getDialog().setTitle("Hello");
return view;
}
3.Register onClick to your 'Create new Account' button and show the DialogFragment.
FragmentManager fm = getSupportFragmentManager();
RegisterAccountDialog dialog = new RegisterAccountDialog();
dialog.show(fm, "fragment_edit_name");
A good tutorial here
Ok i have a TabActivity with a FragmentActivity in each tab. In one of those tabs i have a root Fragment which contains a ScrollView with multiples HorizontalScrollView like the picture:
In each HorizontalScrollView i add dinamically a few of objects inflated like this:
To get the effect of Gallery, well the main problem is when I click in each item to replace for another fragment, i add this fragment to backstack, BUT when i press back to return to this fragment all my textview are contains the same text... It's so very strange, and i dont know WHY!? Some ideas?
I put the onCreateView function that i'm using:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
if (view == null) {
this.view = inflater.inflate(R.layout.activity_home, container, false);
} else {
((ViewGroup) view.getParent()).removeView(view);
}
return this.view;
}
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'm trying to add the new Navigation drawer to my app and instead of changing images(as they are shown in the google example) I manage to change layouts inside the fragment.
The problem is that after changing a layout I don't have any access to the buttons and other objects inside the layout.
How can I get access to the buttons and other objects that are shown in the current
inflatered layout, and set listeners?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
switch(pageNum)
{
case 0: rootView = inflater.inflate(R.layout.groups_layout, container, false);
break;
case 1: rootView = inflater.inflate(R.layout.schedule_layout, container, false);
break;
case 2: rootView = inflater.inflate(R.layout.courses_layout, container, false);
break;
}
theView = rootView;
return rootView;
}
}
Thanks in advance.
After changing the layouts, you will still need to reference your buttons in your java class to the buttons in your layout, EVERY time when you change a layout. Then after that setting the click listeners again.
A bit on the tedious side, but that should be the way to do it, judging from the code snippet you've shown.
what you should do is use multiple fragments and switch between them with the drawer. then just give each fragment a different layout, here's a great example
http://manishkpr.webheavens.com/android-navigation-drawer-example-using-fragments/
I am porting Activity-type app to Fragments-type app. I am a bit confused where I am supposed to initiate UI elements of the fragment.
For example, if I initiate a button from Fragment class in FragmentActivity class, I get no error.
btnOne = (Button) findViewById(R.id.btn_one);
But, if I try to initiate onClick listener, then I get error of type java.lang.NullPointerException.
Then again, I cannot find method findViewById in the Fragment class.
Am I really forced to initiate in FragmentActivity and to specify listeners in Fragment?
You initiate your button in the fragment usually. An example would be:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.yourlayout, container, false);
Button yourbutton = (Button) fragmentView.findViewById(R.id.button);
yourbutton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//Do your thing
}
}
return fragmentView;
}