I have a fragment that executes or display a dialog fragment once a button is clicked. This dialog fragment displays a table generated by code, however when I try to click the button on this is displayed
And when I trace it in the LogCat the dialog fragments onCreateView is not called. Can somebody help or explain this to me?. Im not that good in android programming yet and I know i still have a lot to learn.
Here is the code of the fragment that calls the dialog fragment
public class fragment_schedule extends Fragment {
...............
public fragment_schedule(ArrayList<SubjSchedule> subj){
subject = subj;
}
#SuppressWarnings("deprecation")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
..................
showlstbtn = (Button) rootView.findViewById(R.id.button_showlstschd);
showlstbtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.addToBackStack(null);
DialogFragment dialog = ShowLstDialog.newInstance(subject);
dialog.show(ft, "dialog");
}
});
..........
and heres my dialog fragment
public class ShowLstDialog extends DialogFragment {
private static final String TAG = ShowLstDialog.class.getSimpleName();
public static Context mContext;
public static TableLayout tl;
public static TableRow trh;
private static ArrayList<SubjSchedule> subject= new ArrayList<SubjSchedule>();
public static DialogFragment newInstance(ArrayList<SubjSchedule> subj) {
// TODO Auto-generated method stub
DialogFragment f = new DialogFragment();
subject = subj;
Log.v(TAG, subject.size()+"");
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.v(TAG, "OnCreateView: " + subject.size()+"");
View rootView = inflater.inflate(R.layout.fragment_dialog_showlst, container);
//mEditText = (EditText) view.findViewById(R.id.txt_your_name);
Log.v(TAG, "OnCreateView: " + subject.size()+"");
getDialog().setTitle("");
tl = (TableLayout) rootView.findViewById(R.id.tablelayout_schedlst);
trh = new TableRow(getActivity());
TextView[] tvh = new TextView[3];
for(int i=0; i<3; i++){
tvh[i] = new TextView(getActivity());
tvh[i].setBackgroundResource(R.drawable.green2);
tvh[i].setTextColor(getResources().getColor(R.color.LightCyan));
tvh[i].setGravity(Gravity.CENTER);
tvh[i].setPadding(30, 3, 30, 3);
//tvh[i].setLayoutParams(params);
trh.addView(tvh[i]);
}
tvh[0].setText("Subject");
tvh[1].setText("Description");
tvh[2].setText("Instructor");
TableRow[] tr = new TableRow[subject.size()];
for(int i=0; i<subject.size();i++){
tr[i] = new TableRow(getActivity());
TextView[] tv1 = new TextView[3];
for(int k=0; k<3; k++){
tv1[k] = new TextView(getActivity());
tv1[k].setBackgroundResource(R.drawable.btn_default_disabled_holo_dark);
tv1[k].setTextColor(getResources().getColor(R.color.list_background_pressed));
tv1[k].setGravity(Gravity.CENTER);
tv1[k].setPadding(30, 3, 30, 3);
//tvh[i].setLayoutParams(params);
tr[i].addView(tv1[i]);
}
tv1[0].setText(subject.get(i).getcn());
tv1[1].setText(subject.get(i).getd());
tv1[2].setText(subject.get(i).geti());
tl.addView(tr[i]);
}
return rootView;
}
}
and here is the xml file of the dialog fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#color/LightCyan"
android:scrollbars="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TableLayout
android:id="#+id/tablelayout_schedlst"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*" >
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
</RelativeLayout>
thank you so much in advance.
Change this
public static DialogFragment newInstance(ArrayList<SubjSchedule> subj) {
// TODO Auto-generated method stub
DialogFragment f = new DialogFragment();
subject = subj;
Log.v(TAG, subject.size()+"");
return f;
}
to
public static ShowLstDialog newInstance(ArrayList<SubjSchedule> subj) {
// TODO Auto-generated method stub
ShowLstDialog f = new ShowLstDialog();
subject = subj;
Log.v(TAG, subject.size()+"");
return f;
}
And do read
http://developer.android.com/reference/android/app/DialogFragment.html
Instead of (or in addition to) implementing onCreateView(LayoutInflater, ViewGroup, Bundle) to generate the view hierarchy inside of a dialog, you may implement onCreateDialog(Bundle) to create your own custom Dialog object
So you should use:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
to replace:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
Instead of
public static DialogFragment newInstance(...) {
DialogFragment f = new DialogFragment();
return f;
}
write
public static DialogFragment newInstance(...) {
DialogFragment f = new ShowLstDialog();
return f;
}
and also use Bundle and setArguments, getArguments to pass arguments.
Related
I am trying to create a DialogFragment as
public class PlaceFragment extends DialogFragment {
public PlaceFragment() {
}
public static PlaceFragment newInstance() {
PlaceFragment placeFragment = new PlaceFragment();
Bundle args = new Bundle();
args.putDouble("Latitude", 12.3456);
placeFragment.setArguments(args);
return placeFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_place, container, false);
AlertDialog.Builder placePicker = new AlertDialog.Builder(getContext());
placePicker.setView(R.layout.fragment_place)
.setTitle("Enter Latitude and Longitude")
.setPositiveButton("Ok", null);
// return inflater.inflate(R.layout.fragment_place, container);
return v;
}
}
which should be called as:
case R.id.action_geolocate:
FragmentManager fm = getSupportFragmentManager();
PlaceFragment placeFragment = PlaceFragment.newInstance();
placeFragment.show(fm, "");
return true;
But, as you can see, this is not working,as this is obviously showing the layout only, which is inflated. But I am trying to get the dialog with two line of EditText with Ok and Dissmiss button.
If I use a completely manual way, then I am getting the editText, but dont have any idea how to get those value with OkButton, like:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_place, container);
}
You can
Create a class extend DialogFragment
override and implement onCreateDialog to configure how the dialog appears. Here, create a Dialog / AlertDialog, can also populate its layout here.
In some cases, you want to implement onCreateView and handle views/actions there.
Hope this would help.
if I understand right, something like this should work:
public class DialogEnterExit extends DialogFragment {
public interface EnterExitDialogListener {
void onFinishEditDialog(String event, String place, Location location);
}
EnterExitDialogListener mListener;
private static final String TAG = DialogEnterExit.class.getSimpleName();
AppCompatDialog mDialog;
String mPlace;
Location mLocation;
public static DialogEnterExit newInstance(String place, Location location) {
DialogEnterExit f = new DialogEnterExit();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putString("PLACE", place);
args.putParcelable("LOCATION", location);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPlace = getArguments().getString("PLACE");
mLocation = getArguments().getParcelable("LOCATION");
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.layout_enter_exit_dialog,null);
final RadioButton enter = view.findViewById(R.id.radio_enter);
final RadioButton exit = view.findViewById(R.id.radio_exit);
TextView place = view.findViewById(R.id.place_picked);
place.setText(mPlace);
enter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mListener != null) {
mListener.onFinishEditDialog(getString(R.string.dialog_transition_entered), mPlace,
mLocation);
}
mDialog.dismiss();
}
});
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mListener != null) {
mListener.onFinishEditDialog(getString(R.string.dialog_transition_exited), mPlace,
mLocation);
}
mDialog.dismiss();
}
});
builder
.setView(view)
.setTitle(getString(R.string.dialog_title_event))
.setCancelable(true);
mDialog = builder.create();
return mDialog;
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
// Verify that the host activity implements the callback interface
try {
// Instantiate the EditNameDialogListener so we can send events to the
host
mListener = (EnterExitDialogListener) context;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(context.toString()
+ " must implement EnterExitDialogListener" );
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
}
and the view in xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="place:"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/place_picked"
android:hint="place name"
android:layout_weight="3"/>
</LinearLayout>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_enter"
android:text="#string/geofence_transition_entered"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_exit"
android:text="#string/geofence_transition_exited"/>
</LinearLayout>
</LinearLayout>
and to use:
android.location.Location loc = new android.location.Location("");
loc.setLatitude(location.getLatitude());
loc.setLongitude(location.getLongitude());
DialogEnterExit dialogEnterExit = DialogEnterExit.newInstance(location.getName(), loc);
getSupportFragmentManager().beginTransaction()
.add(dialogEnterExit, "enter exit dialog").commitAllowingStateLoss();
i think you should implement
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
instead of
#Override
public View onCreateView(Bundle savedInstanceState) {
and return a dialog instead of view and i think it would be better if you do not leave the tag string in .show(ft,"") empty
I am creating a sliding menu drawer in my application...the problem is when i am sliding the menu it is sliding the heading too wheres i just want the content to move with the slider. how can i prevent header from moving. i am using fragments for my code:
MainActivity :
setContentView(R.layout.layout_home);
ButterKnife.inject(this);
btnMenu.setVisibility(View.VISIBLE);
homeScreenFragment = new HomeScreenFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frameLayout, homeScreenFragment).commit();
// configure the SlidingMenu menu = new SlidingMenu(this);
menu=new SlidingMenu(this);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.menu_frame);
getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame, new HomeNavFragment()).commit();
findViewById(R.id.imgBtnMenu).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
menu.toggle(true);
}
});
HomeScreenFragment:
public final String[] podName = new String[] { "NO Text", "NO Text",
"NO Text", "NO Text" };
public final Integer[] images = { R.drawable.pod_img1, R.drawable.pod_img2,
R.drawable.pod_img3, R.drawable.pod_img1 };
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_homescreen, container,
false);
ButterKnife.inject(this, view);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
addItemsToListView();
}
private void addItemsToListView() {
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < podName.length; i++) {
RowItem item = new RowItem(images[i], podName[i]);
// RowItem item = new RowItem(imageUrl[i], deityName[i]);
rowItems.add(item);
}
setAdapter();
}
private void setAdapter() {
CustomBaseAdapter adapter = new CustomBaseAdapter(getActivity(), rowItems);
podListView.setAdapter(adapter);
}
Navigation Fragment:
public static final String[] nav_title = new String[] { "CLE & Events",
"Pictorial Roster", "Publications", "On-Demand CLE", "Court Info",
"LBA Alerts", "My Profile", "Notification Settings",
"App Support & Feedback", "Share this App", "Developer", " ", " " };
public static final int[] nav_icon = { R.drawable.ic_menu_calender,
R.drawable.ic_menu_roster, R.drawable.ic_menu_publication,
R.drawable.ic_menu_filebox, R.drawable.ic_menu_court,
R.drawable.ic_menu_bubble, R.drawable.ic_menu_profile,
R.drawable.ic_menu_reminder, R.drawable.ic_menu_share,
R.drawable.ic_menu_developer, R.drawable.ic_menu_developer,
R.drawable.ic_menu_developer };
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_homescreen, container,
false);
ButterKnife.inject(this, view);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
setAdapter();
/*addItemsToListView();*/
}
private void setAdapter() {
NavigationMenuAdapter adapter = new NavigationMenuAdapter(getActivity(),nav_title,nav_icon);
podListView.setAdapter(adapter);
}
Xml:
layout_home:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:id="#+id/header_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
layout="#layout/include_header" />
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header_home">
</FrameLayout>
</RelativeLayout>
layout_homescreen
<ListView
android:id="#+id/podListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent" >
</ListView>
</LinearLayout>
menu.xml
Since you want to move your content area with sidepanel + ActionBar should remain static, best way would be to implement v4 widget SlidingPaneLayout (android.support.v4.widget.SlidingPaneLayout).
here is a good example that explains how to implement SlidingPaneLayout
And please go through Developer's site for proper understanding.
Hope it helps..!! :)
I have a DialogFragment with a simple layout. I want to add a fragment (and in the future replace this fragment with another) to a FrameLayout that is inside the DialogFragment's layout. However, the method of adding the new fragment fails with the error:
"No view found for id 0x7f0b004f com.kennel39.diabeteslive_adtdev:id/frameAlertsContainer) for fragment Fragment_AlertsManage {41e7cb68}"
I have checked my xml, attempted different methods and have read a number of similar issues on stackoverflow but I cannot find a solution.
public class DialogManageAlerts extends DialogFragment{
static int patient_id;
public static DialogManageAlerts newInstance(int given_patient_id){
DialogManageAlerts frag = new DialogManageAlerts();
patient_id = given_patient_id;
Bundle bund = new Bundle();
bund.putInt("Patient_id", patient_id);
frag.setArguments(bund);
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mainView = inflater.inflate(R.layout.dialog_alerts_master, container, false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
FragmentManager myFragmentManager = getFragmentManager();
FragmentTransaction myTransact = myFragmentManager.beginTransaction();
Fragment_AlertsManage manageAlertsFragment = new Fragment_AlertsManage();
myTransact.add(R.id.frameAlertsContainer, manageAlertsFragment);
myTransact.commit();
return mainView;
}
layout.dialog_alerts_master:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/white_regular" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#drawable/lower_border_background_white">
<TextView
android:id="#+id/tvManageAlertsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/manage_alerts_title"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_horizontal" />
</LinearLayout>
<FrameLayout
android:id="#+id/frameAlertsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
</LinearLayout>
And the Fragment_AlertsManage class:
public class Fragment_AlertsManage extends Fragment implements OnClickListener {
int patient_id;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_alerts_manage, container, false);
//Get buttons
Button btnAdd = (Button)myView.findViewById(R.id.btnAddAlert);
btnAdd.setOnClickListener(this);
Button btnBack = (Button)myView.findViewById(R.id.btnBack);
btnBack.setOnClickListener(this);
//FILL CONTENT
populateContent(myView);
return myView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnAddAlert: {
//Switch fragment
FragmentManager myFragmentManager = getFragmentManager();
FragmentTransaction myTransact = myFragmentManager.beginTransaction();
Fragment_AlertsAdd addAlertFragment = new Fragment_AlertsAdd();
myTransact.addToBackStack("Previous");
myTransact.replace(this.getView().getId(), addAlertFragment);
myTransact.commit();
break;
}
case R.id.btnBack: {
FragmentManager myFragmentManager = getFragmentManager();
FragmentTransaction myTransact = myFragmentManager.beginTransaction();
myTransact.remove(Fragment_AlertsManage.this);
//Launch Home
Intent homeIntent = new Intent(this.getActivity(), HomeActivity.class);
homeIntent.putExtra("patient_id", patient_id);
startActivity(homeIntent);
getActivity().finish();
break;
}
}
}
public void populateContent(View myView){
try {
ArrayList<Alert> alerts = new RetrieveAlerts(patient_id).execute().get();
ListView list = (ListView) myView.findViewById(R.id.reminder_listview);
AlertsListAdapter alertsAdapter = new AlertsListAdapter(this.getActivity(), alerts);
list.setAdapter(alertsAdapter);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Why am I getting this error, and what can I do to solve it?
The doc on getFragmentManager():
Return the FragmentManager for interacting with fragments associated
with this fragment's activity.
R.id.frameAlertsContainer is in the DialogFragment's layout, not the activity's layout, so it can't find it. Try using getChildFragmentManager() instead. However I can't tell if this will work inside the onCreateView() method since the view isn't associated with the fragment yet. You may need to put it in onStart() or something else.
I had this problem, for resolved, i can the getFragmentManager() in method OnStart() and work perfect.
The issue of many fragments in one activity has been covered here before, but I cannot seem to find a solution for my positioning problem.
I want a horizontal scroll bar on top of the screen and under it a custom ListFragment. The code I am posting below puts up the scroll bar and then the ListFragment overwrites it. When I try to modify the hosting FrameLayout to have two FrameLayouts I get runtime errors.
Simply, How can I position the listFragment under the scrollbar?
public class GoogleNewsMainActivity extends FragmentActivity{
public static Context c;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
c=this;
setContentView(R.layout.activity_fragment);
/* FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
Scroll_Menu_Fragment scrollFragment = new Scroll_Menu_Fragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, scrollFragment)
.commit();
GoogleNewsMainFragment f = new GoogleNewsMainFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, f)
.commit();
} */
}
}
Here is the scroll bar fragment:
public class Scroll_Menu_Fragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.scrollbar_fragment, container, false);
}
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
This is the ListFragment
public class GoogleNewsMainFragment extends ListFragment implements LoaderCallbacks<Cursor> {
private static final String COLUMN_ID = "id";
public final static String NEWSFROMSERVICE = "NEWSFROMSERVICE";
static ImageView thumbnail;
static String mSectionSelected;
private NewsCursor mCursor;
private ListView lv;
private NewsCursorAdapter adapter;
public static final String ID = "id";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
mSectionSelected = ""; // until they select one
getLoaderManager().initLoader(0, null, this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_google_news_main, container, false);
getActivity().setTitle(R.string.news_list_title);
return v;
}
And finally the hosting layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentContainer"
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/scrollFragment"
android:layout_width="fill_parent"
android:layout_weight="10"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
class="com.gilchrist.android.googlenews.Scroll_Menu_Fragment" ></fragment>
<fragment
android:id="#+id/listFragment"
android:layout_width="fill_parent"
android:layout_weight="90"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
class="com.gilchrist.android.googlenews.GoogleNewsMainFragment" ></fragment>
</LinearLayout>
I just updated the activity layout. The problem was caused by the weight values I was using. By going to 90 and 10 I got exactly what I wanted. All of the code above now works.
This question already has answers here:
How to display an existing ListFragment in a DialogFragment
(4 answers)
Closed 7 years ago.
I'm trying to show a ListFragment in an AlertDialog. The ListFragment appears as expected when called from an activity, but it does not appear in the dialog.
Hers is the Alert:
public class ProfileSelectFragment extends DialogFragment {
private static final String DEBUG_TAG = "ProfileSelectFragment";
protected int layout = R.layout.profileselect_dialog;
final Fragment fragment0 = new LoadedProfilesFragment();
private Button loginButton;
private Button createProfileButton;
private Button anonymousButton;
static ProfileSelectFragment newInstance() {
ProfileSelectFragment f = new ProfileSelectFragment();
return f;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE));
View customView = inflater.inflate(layout, null, false);
...
Dialog myDialog = new AlertDialog.Builder(getActivity()).setView(customView)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.d(DEBUG_TAG, "cancel");
}
}).create();
final android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
final android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fragment, fragment0);
transaction.commit();
return myDialog;
}
}
Here is the AlertDialog layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/fragment"
android:layout_width="fill_parent"
android:layout_height="80dip"
android:background="#33CC33">
</FrameLayout>
<Button
android:id="#+id/button1"
style="#android:style/Holo.ButtonBar.AlertDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/showlogin" />
<Button
android:id="#+id/button2"
style="#android:style/Holo.ButtonBar.AlertDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/createprofile" />
<Button
android:id="#+id/button3"
style="#android:style/Holo.ButtonBar.AlertDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/anonymous" />
</LinearLayout>
And here is the ListFragment:
public class LoadedProfilesFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final String DEBUG_TAG = "LoadedProfilesFragment";
private static final int LIST_LOADER = R.loader.loadedprofilesfragloader;
protected int layout = R.layout.log_fragment;
protected int entryLayout = R.layout.list_item;
protected final Uri table = ProfileProvider.URI_LOADEDPROFILEVIEW;
protected SimpleCursorAdapter listAdapter;
protected String[] uiBindFrom = { ProfilesColumns.USERNAME, ProfilesColumns.CREATIONDATE };
protected int[] uiBindTo = { R.id.title, R.id.creationdate };
protected String[] createProjection = { CommonDatabaseHelper._ID, ProfilesColumns.USERNAME,
ProfilesColumns.CREATIONDATE };
public LoadedProfilesFragment() {
super();
}
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLoaderManager().initLoader(LIST_LOADER, null, this);
listAdapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), entryLayout, null, uiBindFrom,
uiBindTo, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(listAdapter);
}
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final View view = inflater.inflate(layout, container, false);
return view;
}
...
}
Not sure if I need to call the ListFragment in a different way since it is an AlertDialog as opposed to an Activity. My plan is to apply styles to make the list fragment and buttons in the alert dialog appear as a continuous list. Thanks in advance.
Use a fragment dialog to display list items.
How to display an existing ListFragment in a DialogFragment. This link should help you.