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.
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
Can anyone help me, how to add ListView inside dialog. I am trying adding ListView inside customDialog but getting errors.. ListView is not appearing.
You can use a DialogFragment to add a custom layout to a Dialog.
Your Fragment would need to extend the DialogFragment (the app.v4 one) class:
BlankFragment.java
public class BlankFragment extends DialogFragment {
private ListView listView;
public BlankFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = view.findViewById(R.id.f_blank_list);
List<String> strings = Arrays.asList("Hello", "World", "Bye");
ArrayAdapter<String> arrayAdapter
= new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, strings);
listView.setAdapter(arrayAdapter);
}
}
Then you can have anything inside your dialog setting it up as a layout:
fragment_blank.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context="com.example.myapplication.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello_blank_fragment" />
<ListView
android:id="#+id/f_blank_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
Finally, you would need to show it in a transaction from your MainActivity:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String BLANK_FRAGMENT_TAG = "FRAGMENT_TAG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadFragment(View view) {
BlankFragment blankFragment = new BlankFragment();
blankFragment.show(getSupportFragmentManager(), BLANK_FRAGMENT_TAG);
}
}
This is the result:
You can find documentation for Dialogs here:
https://developer.android.com/guide/topics/ui/dialogs.html
For a list in the dialog you can use:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_color)
.setItems(R.array.colors_array, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
return builder.create();
}
I am doing a sample project as an excercise and want to display two fragments in the same activity when dealing with tablets(7' and 10').
So what I have so far is this.
As you can see I can display the data of my recyclerview in the left (static) fragment. However the right fragment is empty.
So I have two questions.
1) How to display by default in the right fragment the data of the first row of recyclerview?(ie image and article)
2) How to implement the click listener and update the right fragment?
Here is my code:
MainActivity
public class MainActivity extends AppCompatActivity {
private boolean mTwoPane;
private static final String DETAIL_FRAGMENT_TAG = "DFTAG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(findViewById(R.id.detailed_match_reports)!=null) {
mTwoPane = true;
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.detailed_match_reports, new DetailedActivityFragment(),DETAIL_FRAGMENT_TAG)
.commit();
}else{
mTwoPane = false;
}
}
}
}
layout/activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/match_reports"
android:name="theo.testing.androidservices.fragments.MainActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context="theo.testing.androidservices.activities.MainActivity"
tools:layout="#android:layout/list_content" />
layout-sw600dp/activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
tools:context="theo.testing.androidservices.activities.MainActivity">
<fragment
android:id="#+id/match_reports"
android:name="theo.testing.androidservices.fragments.MainActivityFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="#android:layout/list_content" />
<FrameLayout
android:id="#+id/detailed_match_reports"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4" />
</LinearLayout>
MainActivityFragment
public class MainActivityFragment extends Fragment {
public static final String TAG = "AelApp";
public static ArrayList<MyModel> listItemsList;
RecyclerView myList;
public static MatchReportsAdapter adapter;
public MainActivityFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
updateMatchReport();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
getActivity().setTitle("Match Report");
View rootView = inflater.inflate(R.layout.fragment_main_activity, container, false);
listItemsList = new ArrayList<>();
myList = (RecyclerView)rootView.findViewById(R.id.listview_match_reports);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
myList.setHasFixedSize(true);
myList.setLayoutManager(linearLayoutManager);
adapter = new MatchReportsAdapter(getActivity(), listItemsList);
myList.setAdapter(adapter);
return rootView;
}
public void updateMatchReport(){
Intent i = new Intent(getActivity(), MatchReport.class);
getActivity().startService(i);
}
}
First let me answer the second question. To show a fragment on the right side of the screen add something like this (note how I'm using that id of the frame layout to replace the fragment):
Fragment fragment = new MyRightSideFragment();
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.detailed_match_reports, fragment).commit();
Now, for the first question, you need to implement click listener, you can find an example here.
public class ReactiveAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = (Integer)v.getTag();
showDetail(position);
}
});
}
}
Notice that I'm using the tag of the view to identify the position (so somwhere in your onBindViewHolder code you must set this tag).
public function showDetail(int position) {
... show fragment por position
}
and finally, when your in your OnCreateView or somewhere in your setup code, call showDetail(0).
I have this problem in one Android application. I have a Fragment where, I can click on Button and I can display a Dialog Fragment with some EditText.
So, I have implemented a onFocusChangeListener on one of this EditText, and I can see another DialogFragment with a RecycleListView.
Now I want this: I want to click on one item of this RecycleListView and display this, in the EditText that have generated this event.
So the xml file where is the EditText is this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="800dp"
android:layout_height="fill_parent" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="830dp"
android:layout_height="fill_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText android:id="#+id/textAgent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:hint="Insert agent"
/>
<AutoCompleteTextView android:id="#+id/reaction_autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:layout_toRightOf="#id/labelReaction"
android:hint="select an option"/>
<Spinner
</RelativeLayout>
</ScrollView>
This is the layout of Another DialogFragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="830dp"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_marginTop="#dimen/margin_list_row"/>
</RelativeLayout>
This is the principal Fragment:
public class AlertsDialogFragment extends DialogFragment {
Context mContext;
List<AlertValueSet> listValueSet_Description;
List<AlertValueSet> listValueSet_Status;
List<AlertValueSet> listValueSet_Agent;
List<AlertValueSet> listValueSet_Reaction;
ArrayAdapter<AlertValueSet> adapterAgent;
View v;
public AlertsDialogFragment() {
mContext = getActivity();
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
v = getActivity().getLayoutInflater().inflate(R.layout.alert_insert_dialog, null);
builder.setView(v);
builder.setTitle("Insert Alerts");
builder.setCancelable(false);
//spinner status
EditText textAgent = (EditText) v.findViewById(R.id.textAgent);
textAgent.setOnFocusChangeListener(new AgentClickListener());
return builder.create();
}
public static AlertsDialogFragment newInstance() {
AlertsDialogFragment f= new AlertsDialogFragment();
return f;
}
public class AgentClickListener implements View.OnFocusChangeListener {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
AlertsAgentDialogFragment dialog = AlertsAgentDialogFragment.newInstance(listValueSet_Agent);
dialog.show(getActivity().getFragmentManager(),"");
}
}
}
}
This is the class of another DialogFragment that display the RecycleListView
public class AlertsAgentDialogFragment extends DialogFragment {
Context mContext;
View v;
List<AlertValueSet> list;
private RecyclerView recyclerView;
private AlertInsertAgentAdapter pAdapter;
//private OnRecurrenceTypeListener mListener;
/* public interface OnRecurrenceTypeListener{
void onRecurrenceTypeSelected(String rrule);
}*/
public AlertsAgentDialogFragment() {
mContext = getActivity();
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
v = getActivity().getLayoutInflater().inflate(R.layout.alert_agent_dialog, null);
builder.setView(v);
builder.setTitle("Select agent");
builder.setCancelable(false);
pAdapter = new AlertInsertAgentAdapter(list, new AlertInsertAgentAdapter.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
//recupero l'elemento che l'utente ha selezionato
AlertValueSet alert = list.get(position);
EditText textAgent = (EditText)v.findViewById(R.id.textAgent);
textAgent.setText(alert.getDisplayName());
}
});
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(v.getContext());
recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(pAdapter);
return builder.create();
}
public static AlertsAgentDialogFragment newInstance(List<AlertValueSet> list) {
AlertsAgentDialogFragment f= new AlertsAgentDialogFragment();
f.list=list;
return f;
}
}
This can be handled in way similar to how communication amongst fragments is done.
Create an interface in AlertsAgentDialogFragment class, implement this in underlying activity.
In onItemClick(), call method of interface using object of interface as shown below. Notice onAttach() method below.
public class AlertsAgentDialogFragment extends DialogFragment {
Context mContext;
View v;
List<AlertValueSet> list;
private RecyclerView recyclerView;
private AlertInsertAgentAdapter pAdapter;
private OnRecurrenceTypeListener mListener;
public interface OnRecurrenceTypeListener{
void onRecurrenceTypeSelected(String rrule);
}
public void onAttach(Context context){
if( context instanceOf MainActivity){
mListener = (OnRecurrenceTypeListener) context;
}
}
public AlertsAgentDialogFragment() {
mContext = getActivity();
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
v = getActivity().getLayoutInflater().inflate(R.layout.alert_agent_dialog, null);
builder.setView(v);
builder.setTitle("Select agent");
builder.setCancelable(false);
pAdapter = new AlertInsertAgentAdapter(list, new AlertInsertAgentAdapter.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
//recupero l'elemento che l'utente ha selezionato
AlertValueSet alert = list.get(position);
//EditText textAgent = (EditText)v.findViewById(R.id.textAgent);
//textAgent.setText(alert.getDisplayName());
mListener.onRecurrenceTypeSelected(alert.getDisplayName());
}
});
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(v.getContext());
recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(pAdapter);
return builder.create();
}
public static AlertsAgentDialogFragment newInstance(List<AlertValueSet> list) {
AlertsAgentDialogFragment f= new AlertsAgentDialogFragment();
f.list=list;
return f;
}
}
I am assuming MainActivity is the name of underlying activity, then in MainActivity after implementing above listener, within onRecurrenceTypeListenercreate new AlertDialogsFragment passing this string in new instance.
class MainActivity extends Activity implements OnRecurrenceTypeListener{
public void onRecurrenceTypeSelected(String str){
AlertsAgentDialogFragment aDialog =
AlertsAgentDialogFragment.newIstance(str);
aDialog.show(getActivity().getFragmentManager(), "");
}
}
Now update your editText from onCreateDialog().
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.