Android FragmentPreference and ViewPager - android

Hi i have set up a view pager and fragments for each page and that's all working as it should however i can not get a preference screen to show on one of my pages no matter what i try.
My fragment that i'm trying to add a preference screen looks like this
public class Extras extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.extras, null, false);
return V;
}
}
So then i read the documentation on PreferenceFragments here
http://developer.android.com/reference/android/preference/PreferenceFragment.html
But when i add this in my Extras class as the above link shows
public static class Extras extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
I error out just extending PreferenceFragment errors in my view pager i read the below link though that PreferenceFragments aren't part of the compatiblity library which i believe is my issue. So is it possible to add a preference screen in one of my pages in my viewpager or is this something i'm going to have to do with a listview and basically make my own preference screen
Was PreferenceFragment intentionally excluded from the compatibility package?
Thank you for any help with this issue

Never got the answer i was looking for so i ended up going with a listview instead

I posted my solution here http://forum.xda-developers.com/showthread.php?t=1363906
I simply rebuilt the hidden Preference* class via Reflection.

Related

Changing DialogFragment Layouts while dialog is open

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

Content of Fragment not loading every time when using the NavigationDrawer

I have a Navigation Drawer with 6 Fragments. One of them should contain a ListView. I got it to work and I can switch through all the Fragments via the Navigation Drawer. When I press on the one containing the ListView I can see it, just as it should be. However, when I go to the same Fragment (containing the ListView) while I'm in it already, the ListView isn't displayed anymore. This is probably due to the fact, that I set the ListView on the onActivityCreated() method in the Fragment. I'm curious now though, what happens when I press on the same Fragment again. Does the activity stay loaded and the Fragment needs to be inflated again and through this the OnActivityCreated() method isn't called again? And if so, what do I have to do, that the ListView gets displayed again?
My Fragment.class currently looks like this:
public class FahrplanFragment extends Fragment {
private ListView listView;
private Resources res;
private String[] plans;
private ArrayAdapter<String> adapter;
public FahrplanFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_fahrplan, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = (ListView) getActivity().findViewById(R.id.listview_fahrplan);
res = getResources();
plans = res.getStringArray(R.array.plans);
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
android.R.id.text1, plans);
listView.setAdapter(adapter);
}
}
As in the documentation
Called when the fragment's activity has been created and this
fragment's view hierarchy instantiated. It can be used to do final
initialization once these pieces are in place, such as retrieving
views or restoring state. [..] This is called after
onCreateView(LayoutInflater, ViewGroup, Bundle) and before
onViewStateRestored(Bundle).
It is called every time. You can check by logging the event.
Can you post the code of the event handling when click on the navagition drawer button?
EDIT 1
Here I re-created you project to test it and it works. The fragment loads every time with the list view inside. Moreover, it displays the events when they happens and shows clearly that onActivityCreated is called every time the fragment is replaced.
The best suggestion I can give here is to log your code using Log.d() or System.out.println("...") in various steps of the code, or even better enter in debug mode by setting breakpoints and inspect the code while it's running to understand when it enter in some methods and when not.

Appcompat activity required as argument for function used in fragment

I am using an external library in my app. The first argument used in the example is "this". Which refers to an appcompat activity. However I am using this in a fragment shich obviously doesn't extend appcompat activity.
This is the library:
https://github.com/TouchBoarder/weekdays-buttons-bar
I am not sure whether there is anything I can do. Can I extend the fragment to something compatible (right now it just extends fragment).
Below is a screenshot of the issue:
EDIT:
This is what I extend in my MainActivity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
The Fragment that is called innside Main activity is extended like this:
public class AddAlarmFragment extends Fragment {
And I am trying to create the data source in the oncreate method of the fragment like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_add_alarm, container, false);
WeekdaysDataSource wds = new WeekdaysDataSource(, R.id.weekdays_stub)
.start(this);
new WeekdaysDataSource.Callback() {
#Override
public void onWeekdaysItemClicked(int attachId,WeekdaysDataItem item) {
// Do something if today is selected?
Calendar calendar = Calendar.getInstance();
if(item.getCalendarDayId()==calendar.get(Calendar.DAY_OF_WEEK)&&item.isSelected())
Toast.makeText(getActivity(),"Carpe diem",Toast.LENGTH_SHORT).show();
}
#Override
public void onWeekdaysSelected(int attachId,ArrayList<WeekdaysDataItem> items) {
//Filter on the attached id if there is multiple weekdays data sources
if(attachId==R.id.weekdays_stub){
// Do something on week 4?
}
}
};
Thanks in advance for your help.
Have you tried casting getActivity()?
((AppCompatActivity) getActivity(), R.id.weekdays_stub)
Read the Android API, you can see AppCompatActivity does extend FragmentActivity and getActivity() does return a FragmentActivity, not an AppCompatActivity, as expected by your library.
I do find it odd that a AppCompatActivity is even the defined parameter rather than a Context

Get fragment instance within PreferencesFragment

Via a checkbox in my preferences, the view of a fragment must be updated. The preferences are within a PreferenceFragment:
public class SettingsFragment extends PreferenceFragment {...}
This fragment is instantiated in an Activity using:
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
My app has some other fragments, which extend android.support.v4.app.Fragment. Upon the checkbox change in the preferences, I have to update the view in this fragment:
public class OfferingsFragment extends android.support.v4.app.Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
// This view needs to be updated
View view = inflater.inflate(R.layout.offering_tiles, null);
...
}
}
My question is: How can I update this view from within the PreferencesFragment? Apparently, I cannot use getFragmentManager to get an instance of OfferingsFragment because OfferingsFragment is a fragment from support library. Is there a way to access OfferingsFragment from within PreferenceFragment?
You have a few options based on how your app is setup. If both fragments are not active at the same time, the Offerings fragment can just read the value from the SharedPreferences when it creates the view.
If both fragments are active and the view needs to be refreshed, you can either create a callback through the parent activity using an interface, example here. Or an easier route is to use an event bus like Otto or Green Robot

Assign Content to Tabs in a Fragment - Android

Just created a new project in the newest ADT release in Eclipse and found that it will setup up certain environments for you to get things started. I choose Tabs + Swipe.
It has this code I have question on:
public static class DummyFragment extends Fragment {
public DummyFragment () {
}
public static final String ARG_SECTION_NUMBER = "section_number";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
Bundle args = getArguments();
textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
return textView;
}
}
Both tabs refer to this same fragment. All it does is switch the content on the TextView that has the tab position number on it (1,2, or 3).
The more advanced question first: I want two different Fragments that the tab switches to. In the example code, it points to the same fragment. Where does this change take place? and can I see brief code example?
Easier question: I have two pre-defined XML layouts I'd like to set each tab (or Fragment) with. Do I do this in the actual Fragment? And if so, where? setContentView does't seem to be working in the onCreateView method?
Not really sure what this question is asking exactly, but if I understand correctly the TabHost (or whatever you are using to manage the Fragment tabs) is instantiating multiple instances of the DummyFragment and then attaching each to the screen when a tab is clicked. This is all done behind the scenes... all you need to worry about is implementing the Fragment and telling the TabHost when it should be instantiated/displayed.
Fragments don't have a setContentView method. You should inflate your Fragment's layout from xml in onCreateView. For instance with,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout, container, false);
}

Categories

Resources