Why doesn't this work!?!?
I've tried putting
getActivity().requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
everywhere! I've tried using the supportRequestWindowFeature. And I've also tried WindowCompat and nothing seems to work. It keeps saying put the line before you set the content. I've tried putting it in my main Activity (where the Fragments are instantiated) and then the progress bar won't even show up. Can anyone else think of what I'm doing wrong?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.frag_camera, container, false);
_activity = getActivity();
prefs = getActivity().getSharedPreferences("com.refect.thisorthat", Context.MODE_PRIVATE);
setHasOptionsMenu(true);
getActivity().invalidateOptionsMenu();
mCache = new ImageCache();
I think i figured it out! I was using a supportFragment (from v4) so I switched my fragment to a just a straight android.app.Fragment and now it works!
You are requesting the Window feature but never asking the progress to show.
activity.setSupportProgressBarIndeterminateVisibility(true); should do the trick
Related
I have an Android Activity, from which I want to show a Dialog. It would probably be a custom DialogFragment. Now when the user clicks on specific buttons I want the layout's inside the dialog to change with the data from the previous DialogFragment and so that it would have an ability to also go back to previous Layout.
I dont think there is an easy way to change views inside of the same DialogFragment so what would be the best way to do this?
I have tried doing it in method onViewCreated and when a button is clicked, but nothing happens.
In my activity I call the fragment like this at the moment:
FragmentManager fm = getSupportFragmentManager();
NewDialog newDialog = NewDialog.newInstace(userId, loc, currentId);
newDialog.setNewClickListener(new NewDialog.OnNewClickListener() {
#Override
public void onCancelClicked() {
finishAdd();
}
#Override
public void onAcceptClicked() {
...
}
});
newDialog.show(fm, "new_frag");
And the fragment:
public class NewDeliveryPointDialog extends DialogFragment {
private LayoutInflater inflater;
private ViewGroup container;
public NewDialog(){
}
public static NewDialog newInstace(){
...
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
this.inflater = inflater;
this.container = container;
return inflater.inflate(R.layout.dialog_layout_1, container);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
saveButton.setOnClickListener(v -> {
View.inflate(getContext(), R.layout.dialog_layout_2, container);
view.invalidate();
view.refreshDrawableState();
}
});
}
}
A DialogFragment is not made to have navigation to other fragments within the same dialog.
You basically have these options:
On your button click you close the Dialog and open another Dialog. But this seems odd. If there is so much happening, probably dialogs are not the best shot.
Instead of DialogFragments have another fragment container overlaying the original one (basically what a Dialog fragment does for you). Within the second container you can easily navigate to other fragments and set it to gone when the user finished interaction.
If there are just a few Views in the Dialog, you could consider setting the old ones to gone and the new ones to visible
I think your code didn't work, because container is null. Method onCreateView gives you #Nullable ViewGroup container, which is null for DialogFragment (but non null for Fragment). So when you call View.inflate(getContext(), R.layout.dialog_layout_2, container), it just creates a view in memory and doesn't attach it to container, cause it is null. See LayoutInflater.inflate, cause View.inflate is just a convenience wrapper for this function.
I dont think there is an easy way to change views inside of the same DialogFragment so what would be the best way to do this?
Instead of changing dialog root you can just manipulate child views inside dialog root layout (add, remove them, or change visibility).
Also my advice is to use recommended way to create dialog with custom layout (onCreateDialog + setView), but if you don't want to do that, you can refer view you've created in onCreateView as dialog root.
You can try creating a dialog fragment with an empty shell layout in which you would replace your two different fragments with ChildFragmentManager and regular fragment transactions
passing data between them can be done using the activity's view model since they both live in the same activity.
So add the ShellDialogFragment using the activity's FragmentManager and in the shell fragment class change between NewDialog & NewDeliveryPointDialog on your button click listener with ChildFragmentManager
good morning every one,
I am using swipe tabs in fragments but getting null action bar.
Code snippet is given below:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater
.inflate(R.layout.ride_detail, container, false);
li = getLayoutInflater(savedInstanceState);
context = getActivity();
customizedToast = new CustomizedToast(context, li);
actionBar = ((ActionBarActivity)getActivity()).getActionBar();
I have also tried with:
actionBar = getActivity().getActionBar();
Thanks.
You should use getSupportActionBar instead of getActionBar if you are working with ActionBarActivity. The getActionBar method is available in the ActionBarActivity API since it extends Activity, but should not be used when working with support Activities.
Check you import which action bar you are importing...
Change Activity theme in Android Manifest to Theme.Holo
In need of a good Ui design for my app with tab layout, i searched in google and i found one UI which is using View Pager and Action bar Tabs.
Link is:
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
i downloaded the template & imported to eclipse.i to tried use that template, don't know where to add my activity coding.
i uploaded one of the fragment below, and i tried to get action of a button. but "it shows create a method finViewById".
public class MoviesFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
Button bu=(Button) findViewById(R.id.button1);
return rootView;
}
i don't know much about fragment. I searched in google but i am not able get solution related to this. anyone pls can help me how and where to add my activity coding using above UI Template. Thanks in advance.
try this,
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
Button bu=(Button) rootView.findViewById(R.id.button1);
return rootView;
}
use this
Button bu=(Button)rootView.findViewById(R.id.button1);
I am using several fragments to be dynamically added into activity. Everything works fine, when I press back-button, the fragments go to backstack. And when I resume it, it appears. But everytime on Resume, it is recreating the fragment and call onCreateView. I know it is a normal behavior of the fragment lifecycle.
This is my onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.competitive_programming_exercise, container, false);
return rootView;
}
I want to stop those fragments from recreating. I tried with onSavedInstanstate but nothing is working. How can I accomplish that?
In the Activity's onCreateView set the savedInstanceState to null before calling the super method. You could also remove only the keys "android:viewHierarchyState" and "android:fragments" from the savedInstanceState bundle. Here is code for the simple solution, nulling the state:
#Override
public void onCreate(Bundle savedInstanceState)
{
savedInstanceState = null;
super.onCreate(savedInstanceState);
...
}
Iam using 5 fragments and working for me good as I was facing the same issue before..
public class MyFragmentView1 extends Fragment {
View v;
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
if (v == null)
v = inflater.inflate(R.layout.my_fragment_view_layout,
container, false
);
return v;
}
}
I put the view variable inside class and inflating it as new only if the view instance is null or otherwise use the one created before
You can't stop the fragment from being recreated, unfortunately. The best you can do is to remove the fragment in a transaction, after it has been restored but before it gets displayed.
If you know you are going to remove the fragment immediately you can reduce the performance hit of restoring the fragment by simplifying methods such as onCreateView() to return a dummy view, rather than inflating the whole view hierarchy again.
Unfortunately the tricky part is finding the best place to commit this transaction. According to this article there are not many safe places. Perhaps you can try inside FragmentActivity.onResumeFragments() or possibly Fragment.onResume().
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).