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);
}
Related
I have some code which I run in normal activities in method onFocusChanges, it is important to run it just there as the code requires the activity to be loaded first as it get images width and heights from the view:
in MainActivity
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
SizeModifier sizeModifier = SizeModifier.getInstance(this);
sizeModifier.adjust
}
so all this is working just fine, the problem is this main view contains a fragment changes according to button press like that
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.main_view_fragment,fragment).commit();
the fragment of course has activity and xml, I want now when the fragment open to run the previous code when focus changes but the problem is fragments does not have onFocusChange, I tried all onStart onResume onCreateActivity
but non seems to work as I want, of course the method is called but all images widths are returned 0,
so is there away to be sure that images will return correct width or some alternative to onFocusChanges
Ok, finally I found what could do the purpose,
simply for fragments we add this simple code to the onCreateView after inflating the view like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater .inflate(R.layout.activity_courses_fragment, container, false);
// >>>> this is the thread which will run when every thing is ready
rootView.post(new Runnable() {
#Override
public void run() {
// >>> this method now can get width with no problems
SizeModifier.getInstance(getContext()).adjust(rootView);
}
});
return rootView;
}
special thanks to #Staffan for this idea as solution for another question
I am using fragments to design my screen.When I navigate back to another fragment (from the back stack), the onCreateView(...) method gets called each time even if the fragment has already been created.How to avoid that the method onCreateView(...) gets called each time and make sure it's called only once (when it's created the first time)?
You can cache your inflated view to the local field if your want. For example:
public class ExampleFragment extends Fragment {
private View fragmentView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
if (fragmentView == null) {
fragmentView = inflater.inflate(R.layout.you_super_view_id, container);
}
return fragmentView;
}
}
But practically, it's ok that pager is reinflating views because it keeps only part of all fragments in memory at the time. So, I think the best idea is to let it work as it should
I'm creating an application with a CustomPagerAdapter that can be controlled by ActionBar tabs or horizontal swipe. When you select a tab, a fragment corresponding to that tab is displayed on the screen. When the app is created and when any tab is selected, the adjacent tabs, fragments are loaded into memory. I do not want this to happen. I would like it so that when a tab is selected only that selected tab's fragment is loaded into memory. Is there a way to do this?
Edit: The code I'm currently having trouble with is as follows:
public class fragA extende Fragment
{
private VideoView videoViewA;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_A, container, false);
videoViewA = (VideoView) rootView.findViewById(R.id.videoViewA);
return rootView;
}
#Override
public void setUserVisibleHint(final boolean isVisibleToUser)
{
super.setUserVisibleHint(isVisibleToUser)
if (isVisibleToUser)
{
videoViewA.setVideoURI(LINK);
videoViewA.start();
}
else
{
videoViewA.stopPlayback();
}
}
}
The error I'm receiving is at the videoViewA.setVideoURI(LINK); line. Mind you, the link is actually there, but for privacy reasons I cannot post it.
Edit 2: It's ajava.lang.NullPointerException.
Edit 3: Sorry, but I'm doing this all the hard way. The code now reflects what I have actually written.
Try loading your videos within setUserVisbleHint(), which gets fired by the FragmentPageAdapter upon showing the fragment.
http://developer.android.com/reference/android/support/v4/app/Fragment.html#setUserVisibleHint(boolean)
If that doesn't work for you, you can also try to do check onHiddenChanged(boolean hidden).
http://developer.android.com/reference/android/app/Fragment.html#onHiddenChanged(boolean)
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;
}
For example, I have a fragment that contains two seekbar, one loaded of the layout XML and another loaded in runtime.
public class FragmentEqualizer extends Fragment {
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b)
{
View view = inflater.inflate(R.layout.fragment_equalizer, container, false);
myLinearLayout = (LinearLayout) view.findByViewId(R.id.linearLayout);
SeekBar bar = new SeekBar(view.get.Context());
bar.setLayoutParams(layoutParams);
bar.setMax(100);
bar.setProgress(50);
myLinearLayout.addview(bar);
return view;
}
}
When i start the application and change the progress bar of each seekbar and after I changed the screen orientation of my device, the state of progress bar of view loaded keeps equal while the state of progress bar of wiew loaded in runtime get default like if i don't have changed.
My problem is seemed with this setRetainInstance not retaining the instance
Can somebody help me???
tks
i added a ID to seekbar and it works.
bar.setId(1).