Alert Dialog with EditText and positive negative button - android

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

Related

SetScrollY doesn't work in fragment

I'm doing a calendar app, I wish to scroll my scrollview in the fragment to a specific Y-position as presenting the current time, I tried this code in an ACTIVITY, it succeeded, but it is not working in FRAGMENT. Can somebody please help me on this? Thanks.
This is the xml for fragement :
<FrameLayout 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"
tools:context="utar.fyp2.DayFragment">
<!-- TODO: Update blank fragment layout -->
<ScrollView
android:id="#+id/daySV"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/RLforall"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/datePickerLL"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
</RelativeLayout>
<LinearLayout
android:id="#+id/timeLL"
android:layout_width="90dp"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:orientation="vertical">
</LinearLayout>
<RelativeLayout
android:id="#+id/contentRL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="90dp"
android:layout_marginTop="60dp"/>
<Button
android:id="#+id/btntest"
android:text="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</ScrollView>
This is the code:
public class DayFragment extends Fragment {
//declare element
private LinearLayout timeLL;
private RelativeLayout contentRL;
private RelativeLayout RLforall;
private ScrollView daySV;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
// TODO: Rename and change types of parameters
private Integer mParam1;
private OnFragmentInteractionListener mListener;
public DayFragment() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static DayFragment newInstance(int param1) {
DayFragment fragment = new DayFragment();
Bundle args = new Bundle();
args.putInt(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getInt(ARG_PARAM1);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_day,container,false);
timeLL = v.findViewById(R.id.timeLL);
contentRL = v.findViewById(R.id.contentRL);
GradientDrawable gradientDrawableForTime = new GradientDrawable();
gradientDrawableForTime.setStroke(1, Color.BLACK);
gradientDrawableForTime.setColor(Color.rgb(190,238,53));
GradientDrawable gradientDrawableForContent = new GradientDrawable();
gradientDrawableForContent.setStroke(1,Color.BLACK);
LinearLayout.LayoutParams matchParentLP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
for (int i=0;i<24;i++)
{
//for time at left side
TextView tv = new TextView(getActivity());
tv.setText(""+i+":00");
tv.setHeight(120);
tv.setBackground(gradientDrawableForTime);
//tv.setBackgroundColor(Color.argb(55,55,5));
tv.setGravity(Gravity.CENTER);
timeLL.addView(tv);
//for line at right side
TextView tv2 = new TextView(getActivity());
tv2.setBackground(gradientDrawableForContent);
tv2.setLayoutParams(matchParentLP);
tv2.setY(i*120);
//tv2.setZ(1);
tv2.setHeight((int)(tv2.getY()+120));
tv2.setOnClickListener(new View.OnClickListener() {
//String eventid;
#Override
public void onClick(View view) {
//Toast.makeText(DayListActivity.this, key ,Toast.LENGTH_LONG).show();
Intent intent = new Intent(getActivity(), AddEventActivity.class);
startActivity(intent);
}
});
contentRL.addView(tv2);
}
RLforall = (RelativeLayout)v.findViewById(R.id.RLforall);
TextView line = new TextView(getActivity());
line.setHeight(5);
GradientDrawable gradientDrawableForLine = new GradientDrawable();
gradientDrawableForLine.setStroke(3,Color.BLACK);
line.setBackground(gradientDrawableForLine);
Calendar currentCalendar = Calendar.getInstance();
int h = currentCalendar.get(Calendar.HOUR_OF_DAY);
Double m = currentCalendar.get(Calendar.MINUTE)/60.0;
line.setY((int)(120 + ((h+m)*120)));
line.setLayoutParams(matchParentLP);
RLforall.addView(line);
**//this is where the setScrollY doesn't work**
daySV = v.findViewById(R.id.daySV);
daySV.setScrollY((int)(120 + ((h+m)*120)));
Toast.makeText(getActivity(),"donescoll",Toast.LENGTH_SHORT).show();
//daySV.scrollTo(0,(int)(120 + ((h+m)*120)));
//ignore this line below 1st
Button button = v.findViewById(R.id.btntest);
//button.setText(mParam1.toString());
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onButtonPressed(25);
}
});
return v;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(int data) {
if (mListener != null) {
mListener.onFragmentInteraction(data);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(int data);
}
}

How to access the HashMap values in Fragment?

I have an activity in which I am having an edittext in which i am entering something and storing that value in HashMap where the data inserted in edittext is the key and static data is the value. Now I am passing that to DialogFragment but I am not able to access the key in DialogFragment. How to access the key in DialogFragment to access the value associated with that key
Here is the code:
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="grepthorsoft.com.hash.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/tt"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bt"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
HashMap<String, String> d;
EditText et;
String s;
FragmentManager fm;
Button bt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
d = new HashMap<>();
et = findViewById(R.id.tt);
fm = getFragmentManager();
bt = findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
s = et.getText().toString();
d.put(s, "2");
MyDialog myDialog = new MyDialog(d);
myDialog.show(fm, "test");
}
});
}
}
MyDialog.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="grepthorsoft.com.hash.MyDialog">
</RelativeLayout>
MyDialog.java
public class MyDialog extends DialogFragment{
Button yes,no;
HashMap<String,String> d;
public MyDialog(){}
public MyDialog( HashMap<String,String> d)
{
this.d=d;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.activity_my_dialog,null);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
You can do something like this in your dialog fragment ...
for ( String key : d.keySet() ) {
System.out.println("value" + d.get(key ));
}
d.keySet() fetches all the keys from the hashmap d
You are doing this in wrong way.. you should pass any data to DialogFragment via DialogFragment.setArguments() method. For example:
in MainActivity class ->
MyDialog myDialog = new MyDialog();
Bundle extras = new Bundle();
extras.putSerializable("HashMap",d);
myDialog.setArguments(extras);
myDialog.show(fm, "test");
in myDialog class keep only default constructor and remove your constructor and then add this code to onActivityCreated method
Bundle bundle = this.getArguments();
if(bundle != null) {
d = (HashMap<String, String>) bundle.getSerializable("HashMap");
}
for more information you can check here
You should pass the value in your onclick function using setArguments like this..
MyDialog myDialog = new MyDialog(d);
Bundle bundle = new Bundle();
bundle.putSerializable("hashmap",mMap);
myDialog.setArguments(bundle);
myDialog.show(fm, "test");
And then you will get those value using getArguments in yoyr MyDialog fragment
paste this code in your onCreateView
Bundle bundle = this.getArguments();
if(bundle.getSerializable("hashmap") != null)
mMap = (HashMap<String, String>)bundle.getSerializable("hashmap");
//get your hashmap value
Iterator iterator = mMap.keySet().iterator();
while(iterator.hasNext()) {
String key=(String)iterator.next();
String value=(String)mMap.get(key);
}
hasNext() Returns true if the iteration has more elements.
next() Returns the next element in the iteration.
you can access hashmap declare in activity class into the dialog by making hashmap public in the activity and in dialog fragment class access object like this ((MainActivity)getActivity()).d;
and in dialog fragment, you can iterate map like this
void printMap() {
Iterator it = ((MainActivity)getActivity()).d.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
}

Android - ListView inside Dialog

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();
}

OnCreateView not called in a dialogfragment from a fragment

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.

Display a ListFragment inside an AlertDialog [duplicate]

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.

Categories

Resources