Problems with TextView - android

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

Related

Change fragment background on create view dynamically

I want to change fragment background on load dynamically, Below code do that for me, But I have an issue on run-time and that is background changes happen after showing the fragment with default background.
for example when I want to swipe to load another fragment, On transition fragment appears with default background setting and after loading fragment and waiting a little moment that codes work at first time load.
I want to know where I'm wrong
#Override
public void setUserVisibleHint(boolean visible){
super.setUserVisibleHint(visible);
View view=RegisterFragment.this.getView();
if (view!=null)
{
RelativeLayout layout= (RelativeLayout) view.findViewById(R.id.back_layout);
layout.setBackgroundResource(Application.color);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_register, container, false);
view.findViewById(R.id.back_layout).setBackgroundResource(Application.color);
return view;
}
You could override below method which will help you to update UI run-time while loading fragment:
#Override
public void setMenuVisibility(boolean menuVisible) {
layout.setBackgroundResource(Application.color);
}

How to come back to previous rootview of fragment

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)

Android fragment :should back to fresh page

I have two fragment A and B.
Fragment A : have 1 view, i scroll view bottom. after move fragment B, touch back at fragment B move fragment A, i want to see view have scroll top(same refresh layout fragment A. when onCreate).
My view : NewHeader, PullToRevealListView, button search and FrameLayout is parent
Use this function to inflate your first fragment i.e call this function in onCreateView of fragment A
public View createPersistentView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState, int layout) {
if (_rootView == null) {
_rootView = inflater.inflate(layout, null);
} else {
((ViewGroup) _rootView.getParent()).removeView(_rootView);
}
return _rootView;
}

How to set data to layout inflated by a fragment

I have a FragmentActivity which sets a main layout with 3 tabs. Each tab has it's own fragment which inflates is own layout into main layout.
For example, one of the three Fragments:
public class Fragment1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.fragment1, container, false);
}
}
When I add all the fragments and connect them with tabs everything works. When I click on tab1, layout of the Fragment1 shows, when I click on Tab2, layout of the Fragment2 shows.
The problem is that when I want to change something in that inflated layout (for example execute setText to textView in fragment1 layout), I receive a NullPointerException in that line and application stops.
This doesn't work:
TextView test = (TextView) findViewById(R.id.fragmentOneText1);
test.setText("Test text!");
How to set data to inflated layout?
You have to inflate your fragment specific layout first by using LayoutInflater and map the control in your Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.R.layout.fragment1, container, false);
TextView text = (TextView) fragmentView.findViewById(R.id.fragmentOneText1);
mTextNoResult=(TextView)fragmentView.findViewById(R.id.text_noresult);
return fragmentView;
}

Set new layout in fragment

I'm trying to change the layout of a fragment during runtime under a particular condition.
The initial layout in inflated within the onCreateView():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.cancel_video, null);
}
Then sometime later within the fragment code I would like to replace the initial layout with some other layout.
I've tried a few things so far; this is the latest that I have:
private void Something(){
if(checkLicenseStatus(licenseStatus, statusMessage)){
View vv = View.inflate(getActivity(), R.layout.play_video, null);
//more code
}
}
How can I accomplish this?
You cannot replace the fragment's layout once it is inflated. If you need conditional layouts, then you either have to redesign your layout and break it down into even smaller elemens like Fragments. Alternatively you can group all the layout elements into sub containers (like LinearLayout), then wrap them all in RelativeLayout, position them so they overlay each other and then toggle the visibility of these LinearLayouts with setVisibility() when and as needed.
Yes, I have done this in following way. When I need to set a new layout(xml), the following code snippet should be executed.
private View mainView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup containerObject, Bundle savedInstanceState){
super.onCreateView(inflater, containerObject, savedInstanceState);
mainView = inflater.inflate(R.layout.mylayout, null);
return mainView;
}
private void setViewLayout(int id){
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mainView = inflater.inflate(id, null);
ViewGroup rootView = (ViewGroup) getView();
rootView.removeAllViews();
rootView.addView(mainView);
}
Whenever I need to change the layout I just call the following method
setViewLayout(R.id.new_layout);
Use a FragmentTransaction through the FragmentManger
FragmentManager fm = getFragmentManager();
if (fm != null) {
// Perform the FragmentTransaction to load in the list tab content.
// Using FragmentTransaction#replace will destroy any Fragments
// currently inside R.id.fragment_content and add the new Fragment
// in its place.
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, new YourFragment());
ft.commit();
}
The code for the class YourFragment is just a LayoutInflater so that it returns a view
public class YourFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_fragment, container, false);
return view;
}
}
I will change whole layout. You can change only a specific portion of your layout.
Add a root FrameLayout in your_layout.xml, it contains nothing.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fl_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Set your content in the java code.
ViewGroup flContent = findViewById(R.id.fl_content);
private void setLayout(int layoutId) {
flContent.removeAllViews();
View view = getLayoutInflater().inflate(layoutId, flContent, false);
flContent.addView(view);
}
You can change layoutId free at some point.
If you have some listeners, you must set again.

Categories

Resources