I'm trying to create my custom dialog that does two things:
a) is a dialog with two buttons and a title (simple enough)
b) can handle fragment transactions (not so simple)
Firstly, I created a typical Dialog implementation:
public class MyDialog extends DialogFragment {
...
static GeofenceMapDialog newInstance(String str){
...
//Bundle stuff
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fragment_mydialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view)
.setTitle(R.string.geofence_title)
.setPositiveButton(R.string.geofence_ready_button, (dialog, id) -> {
//Do stuff
})
.setNegativeButton(R.string.geofence_cancel_button, (dialog, id) -> {
});
return builder.create();
}
}
Okay. However, with this implementation I have no idea how to use FragmentTransactions.
Adding getChildFragmentManager inside onCreateDialog gives me an exception that there is no view. Moving everything into onCreateView like this:
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mydialog, null);
addStuff.setOnClickListener(v -> getChildFragmentManager().beginTransaction().add(R.id.timeSettings, new TimePickerFragment()).commit());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view)
.setTitle(R.string.title)
.setPositiveButton(R.string.button, (dialog, id) -> {
//do stuff
})
.setNegativeButton(R.string.geofence_cancel_button, (dialog, id) -> {
});
builder.create();
return view;
}
Solves the problem of fragment transactions. However my dialog functionalities now don't work.
Any ideas how to use both? I tried to do that, however I can't inflate view after onCreateDialog is finished...
EDIT:
Here is the stacktrace, when I move everything to onCreateDialog and do fragment transactions.
03-17 09:18:35.693 3639-3639/com.example.mydialog.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.mydialog.debug, PID: 3639
java.lang.IllegalStateException: Fragment does not have a view
at android.support.v4.app.Fragment$1.findViewById(Fragment.java:1746)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:937)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
To get this to work, I have mimiced the alert dialog layout builder.
public class MyDialogFragment extends DialogFragment {
public MyDialogFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_my_dialog, container, false);
view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getChildFragmentManager().beginTransaction().add(R.id.timeSettings, new OtherFragment()).commit();
}
});
//setup negative and positive buttons just like the other button
view.findViewById(R.id.button_ok).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
dismiss()
}
});
return view;
}
}
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
new MyDialogFragment().show(getFragmentManager(),"dialog");
}
}
}
fragment_my_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:id="#+id/contentPanel"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:minHeight="48dp">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/message"
style="#android:style/TextAppearance.Material.Subhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="24dp"
android:paddingTop="24dp"
android:paddingEnd="24dp"
android:text="Message To User Here"/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/timeSettings"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="#+id/buttonPanel"
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="locale"
android:orientation="horizontal"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<Button
android:id="#+id/button"
style="?android:attr/buttonBarNegativeButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Stuff"/>
<!-- Change this View to Space someday when minimum API > 14 -->
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="invisible"/>
<Button
android:id="#+id/button_cancel"
style="?android:attr/buttonBarNegativeButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
<Button
android:id="#+id/button_ok"
style="?android:attr/buttonBarPositiveButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"/>
</LinearLayout>
</LinearLayout>
Before Button Press:
After Button Press with fragment added to dialog:
Related
Somehow the scroll view is not working . The message alert dialog box is showing is really big and so I need to implement vertical scrollbar. I tried to get data from previous asked question but it isn't solving my issue please help.
I need to show the alert dialog on button click event.
benefits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog ad = new AlertDialog.Builder(Panchgavya.this).create();
ad.setCancelable(false); // This blocks the 'BACK' button
ad.setMessage(getString(R.string.benefits));
ad.setTitle("Benefits");
ad.setButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
TextView textView = (TextView) ad.findViewById(android.R.id.message);
textView.setScroller(new Scroller(Panchgavya.this));
textView.setVerticalScrollBarEnabled(true);
textView.setMovementMethod(new ScrollingMovementMethod());
}
});
My XML File Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".Design.Panchgavya"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/Green"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Panchgavya"
android:textColor="#color/white"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:orientation="vertical">
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:text="#string/panchgavya"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="#+id/panchgavya_tv_cow_dung"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cow Dung"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_cow_urine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cow Urine"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_cow_milk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cow Milk"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_ghee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Ghee"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_dahi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Dahi"
android:textStyle="bold"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<Button
android:id="#+id/panchgavya_btn_benefits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Benefits"
android:background="#color/Green"
android:textColor="#color/white"
/>
</LinearLayout>
</LinearLayout>
You can create custom layout for your dialog and set any property easily.
For your requirement, Crate a layout for your required Dialog. Put android:scrollbars = "vertical" in your textView inside your layout. And textview.setMovementMethod(new ScrollingMovementMethod());.
You can set custom layout on your by following method.
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Textview
android:id="#+id/txtDescription"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/margin_16dp"
android:paddingRight="#dimen/margin_16dp"
android:paddingBottom="#dimen/margin_16dp"
android:paddingTop="#dimen/margin_10dp"
android:text="#string/dummy_text_"
android:textSize="#dimen/font_14dp"
android:textColor="#color/colorPrimary"
android:layout_below="#+id/imgClose"/>
<ImageView
android:id="#+id/imgClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_close"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/margin_10dp"
android:layout_marginTop="#dimen/margin_10dp" />
</RelativeLayout>
</ScrollView>
// in your java file put below code
private void showPopup() {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_terms_services,
null);
dialogBuilder.setView(dialogView);
TextView txtDescription = dialogView.findViewById(R.id.txtDescription);
ImageView imgClose = dialogView.findViewById(R.id.imgClose);
txtDescription.setText(message);
final AlertDialog b = dialogBuilder.create();
b.show();
imgClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
b.dismiss();
}
});
}
// after than call that showPopup() methos on button click.Hope it works for you
class extending DialogFragment can have UI elements as per your requirement.
public class OrderDetailFragment extends DialogFragment{
#Override
public void onStart() {
super.onStart();
Dialog d = getDialog();
if (d!=null){
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
d.getWindow().setLayout(width, height);
}
}
public static OrderDetailFragment getInstance(GeneralListDataPojo dataList){
OrderDetailFragment orderDetailFragment=new OrderDetailFragment();
Bundle bundle = new Bundle();
bundle.putParcelable(DATA, dataList);
orderDetailFragment.setArguments(bundle);
return orderDetailFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root_view = inflater.inflate(R.layout.fragment_order_detail, container, false);
GeneralListDataPojo mDataList = getArguments().getParcelable(DATA);//My class which holds data implemented Parcelable
//population of data from mDataList
return root_view;
}
void closeDialog(){
this.dismiss();
}
}
fragment_order_detail is having my requirement specific element's (ScrollView/LinearLayouts/Buttons etc)
GeneralListDataPojo is the class which holds data implements Parcelable for transferring data between components
Now you can invoke this DialogFragment from your Fragment like this. (Im invoking from a Fragment. Change the FragmentManager retrieval accordingly if you are using from Activity)
OrderDetailFragment orderDetailFragment=OrderDetailFragment.getInstance((GeneralListDataPojo) responseObj);
FragmentManager fragmentManager=getFragmentManager();
orderDetailFragment.show(fragmentManager,"OrderDetailFragment");
Create The Custom Dialog layout Using Scroll View in your own custom Layout
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog= new Dialog(getApplicationContext());
dialog.setContentView(R.layout.activity_dialog);
Button click= dialog.findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
i am trying to show fragment from a custom alertdilog's button click event, alert dialog is in fragment's adapter, i wanted to show fragment from alertdialog button click but i am unable to do so.
alertdialog code:
holder.commentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder=new AlertDialog.Builder(context);
final AlertDialog alertDialog=builder.create();
View viewTmp_comment=LayoutInflater.from(context).inflate(R.layout.comment_show_layout,null,false);
RecyclerView recyclerView_commentShow=(RecyclerView)viewTmp_comment.findViewById(R.id.recyclerViewId_commentShow);
TextView textViewId_commentShow_layout=(TextView)viewTmp_comment.findViewById(R.id.textViewId_commentShow_layout);
ImageButton commentSendButtonId_Post=(ImageButton)viewTmp_comment.findViewById(R.id.commentSendButtonId_Post);
ImageButton imageUpload_CommentButton=(ImageButton)viewTmp_comment.findViewById(R.id.imageUploadButtonId_comment);
ImageButton smileyUpload_CommentButton=(ImageButton)viewTmp_comment.findViewById(R.id.smileyButtonId_comment);
editTextId_CommentPost=(EditText) viewTmp_comment.findViewById(R.id.editTextId_CommentPost);
// RelativeLayout smileyLoadLayoutId_CommentAdapter=(RelativeLayout)
final CommentShowAdapter commentShowAdapter=new CommentShowAdapter(context,newsFeedClassArrayList.get(position).getCommentShowClassArrayList(),"NewsFeed");
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
recyclerView_commentShow.setLayoutManager(linearLayoutManager);
if(newsFeedClassArrayList.get(position).getCommentShowClassArrayList()!=null) {
recyclerView_commentShow.setAdapter(commentShowAdapter);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context, linearLayoutManager.getOrientation());
recyclerView_commentShow.addItemDecoration(dividerItemDecoration);
commentShowAdapter.notifyDataSetChanged();
recyclerView_commentShow.onChildDetachedFromWindow(null);
}else {
builder.setMessage("No comments yet");
}
//comment show alert dismiss
textViewId_commentShow_layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
if(alertDialog.isShowing()){
alertDialog.dismiss();
}
}catch (Exception e){}
}
});
**//here i am trying to lauch fragment**
smileyUpload_CommentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ServiceClass.setSmileyKey("comment");
smileyFragment=new SmileyShowFragment();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
Fragment fragment = fragmentManager.findFragmentById(R.id.smileyLoadLayoutId_CommentAdapter);
if (!(fragment instanceof SmileyShowFragment)) {
fragmentTransaction.replace(R.id.smileyLoadLayoutId_CommentAdapter, smileyFragment, "Smiley").commit();
} else {
fragmentTransaction.remove(smileyFragment).commit();
}
}
});
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alertDialog.getWindow(); lp.copyFrom(window.getAttributes());
lp.width = context.getResources().getDisplayMetrics().widthPixels;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity=Gravity.BOTTOM; window.setAttributes(lp);
alertDialog.setView(viewTmp_comment);
alertDialog.show();
}
});
alert xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ddd"
>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/textViewId_commentShow_layout"
android:text="Comments"
android:textSize="16sp"
android:paddingLeft="20dp"
android:textStyle="bold"
android:paddingTop="10dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/underline"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/recyclerViewId_commentShow"
>
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:weightSum="4"
android:background="#1470a6"
>
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/camera"
android:id="#+id/imageUploadButtonId_comment"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/smileyButtonId_comment"
android:src="#drawable/smiley"
android:layout_gravity="center"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="60dp"
android:id="#+id/editTextId_CommentPost"
android:layout_weight="3"
android:background="#FFFFFF"
android:hint="Your comment"
android:textSize="16sp"
/>
<ImageButton
android:layout_width="30dp"
android:layout_height="match_parent"
android:id="#+id/commentSendButtonId_Post"
android:src="#drawable/send_comment"
android:background="#1470a6"
android:gravity="right"
android:layout_gravity="right"
android:layout_weight="1"
/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/smileyLoadLayoutId_CommentAdapter"
>
</FrameLayout>
</LinearLayout>
how to solve this problem? is it possible to show fragment inside alertdialog.i have searched on google but no similar solution
You should create a separate customized dialog. To do so you can extend DialogFragment and on onCreateDialog() set up your views and proper assignments. Create a layout for your customized dialog fragment class with a fragment inside. You can create an interface on your customized dialog to listen for actions like button clicks or others. Then, onClick of your button use fragment manager to show the fragment within your dialogfragment. Something like below:
//Give it a fragment with a FrameLayout to hold the fragment if you
need to hold more than one or with just add a fragment in your layout
if you do not need a holder.
public class MyCustomizedDialog extends DialogFragment{
private TextView dialogTitle;
private TextView dialogText;
private FrameLayout myFragmentContainer;
private Button myButton;
private CustomDialogListener customDialogListener;
public interface CustomDialogListener{
//set listeners here, i.e. for buttons clicked by user
public void onBtnClick();
}
public setDialogFragmentListener(CustomDialogListener customDialogListener){
this.customDialogListener = customDialogListener;
}
#override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//initialize dialog
setUpMyCustomDialog(dialog);
return dialog;
}
public void setUpMyCustomDialog(Dialog dialog){
dialogTitle = (TextView)dialog.findViewById(R.id.dialog_title_tv);
dialogText = (TextView)dialog.findViewById(R.id.dialog_text_tv);
myFragmentContainer=(FrameLayout)dialog.findViewById(R.id.child_fragment_container);
myButton = (Button)dialog.findViewById(R.id.myButton);
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View view) {
//initialize your fragment here.
}
}
}
}
I create a class extends DialogFragment class, my code is as below, my problem is the dialog hides under status bar(in the system) and the toolbar(in the activity), I refer the question here DialogFragment not floating, acts embeded or as another fragment add the onCreate function and set the style, but the dialog still only hides under toolbar, not as the tutorial said it will float on the activity window.
public class PasswordDialog extends DialogFragment {
......
public static PasswordDialog newInstance(PdfFragment pdfFragment) {
Log.i(sClassTag,"newInstance in PdfFragmentPasswordDialog");
PdfFragmentPasswordDialog passwordDialog = new PdfFragmentPasswordDialog();
passwordDialog.mPdfFragment = pdfFragment;
setInteractionListener(pdfFragment);
return passwordDialog;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Pick a style based on the num.
int style = DialogFragment.STYLE_NORMAL, theme = 0;
setStyle(style, theme);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(sClassTag,"onCreateView in PdfFragmentPasswordDialog");
// Inflate the layout to use as dialog or embedded fragment
mView = inflater.inflate(R.layout.layout_password, container, false);
addButtonListener();
addEdittextListener();
return mView;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log.i(sClassTag,"onCreateView in onCreateDialog");
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
void showPasswordDialog(boolean isFirstTime) {
....
FragmentManager fragmentManager = getActivity().getFragmentManager();
show(fragmentManager, "dialog");
...
}
The layout file is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/ic_img_passwordkey_48"
android:id="#+id/key_icon"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:contentDescription="#string/password_input_hint_message"/>
<Space
android:layout_width="match_parent"
android:layout_height="16dp" />
<EditText
android:imeOptions="flagNoExtractUi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:textColorHint="#color/password_dialogUI_hint_text_color"
android:textColor="#color/password_dialogUI_text_color"
android:textSize="12sp"
android:hint="#string/password_input_hint_message"
android:ems="10"
android:id="#+id/dialogUI_edit_text"
android:textCursorDrawable="#null"
android:fontFamily="sans-serif"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textDirection="locale"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/password_error_warning_message"
android:textColor="#color/password_dialogUI_warning_color"
android:id="#+id/dialogUI_warning_text"
android:textSize="12sp"
android:visibility="invisible"
android:fontFamily="sans-serif"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:layout_marginStart="8dp"/>
<Space
android:layout_width="match_parent"
android:layout_height="8dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp">
<Button
android:text="#string/password_ok_button"
android:textColor="#drawable/layout_disable_text_color"
android:minWidth="64dp"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:id="#+id/password_dialogUI_ok_button"
android:layout_alignParentEnd="true"
style="?android:attr/borderlessButtonStyle"
android:background="#drawable/layout_password_button_background"
android:layout_marginEnd="8dp"/>
<Button
android:text="#string/password_cancel_button"
android:textColor="#drawable/layout_disable_text_color"
android:minWidth="64dp"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:id="#+id/password_dialogUI_cancel_button"
style="?android:attr/borderlessButtonStyle"
android:layout_toStartOf="#+id/password_dialogUI_ok_button"
android:background="#drawable/layout_password_button_background"
android:layout_marginEnd="8dp"/>
In Activity, try to use you use fragment.show(), instead of call your own show().
PasswordDialog fragment = PasswordDialog();
fragment.show(getFragmentManager(), "TAG_HERE");
2. Add android:minWidth and android:minHeight to your xml.
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:minWidth="1000dp"
android:minHeight="1000dp">
See below:
Full Screen DialogFragment in Android
Added:
To resize your DialogFragment, you can set values in onResume.
public void onResume()
{
super.onResume();
Window window = getDialog().getWindow();
// You can define popup_width and popup_height
int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height = getResources().getDimensionPixelSize(R.dimen.popup_height);
window.setLayout(width, height);
window.setGravity(Gravity.CENTER); // Optional
}
See below:
How to set DialogFragment's width and height?
https://stackoverflow.com/a/17916441/850347
here is steps to custom DialogFragment:
1.inflate custom view from xml on method
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().setCanceledOnTouchOutside(true);
View view = inflater.inflate(R.layout.XXX,
container, false);
//TODO:findViewById, etc
return view;
}
2.set your dialog's width an height in onResume(),remrember in onResume()/onStart(),seems didn't work in other method
public void onResume()
{
super.onResume();
Window window = getDialog().getWindow();
window.setLayout(width, height);
window.setGravity(Gravity.CENTER);
//TODO:
}
I have a FragmentActivity which has a button, on clicking the button I am showing a dailog
public class FragMainActivity extends FragmentActivity implements android.view.View.OnClickListener {
Button shoBut;
public String DailogTag="dialog";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shoBut= (Button) findViewById(R.id.button1);
shoBut.setOnClickListener(this);
}
#Override
public void onClick(View v) {
MyDialogFragment newFragment = MyDialogFragment.newInstance();
FragmentManager fm ;
fm=getSupportFragmentManager();
newFragment.show(fm, DailogTag);
}
}
My dialog Class
public class MyDialogFragment extends DialogFragment {
static MyDialogFragment newInstance() {
return new MyDialogFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inView= inflater.inflate(R.layout.dialog_layout, container, false);
return inView;
}
}
this is my dialog_layout xml file
<?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="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingBottom="36dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="36dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Hi, i am the dailog BOX...Boom"
android:textAppearance="?android:textAppearanceMedium" />
</LinearLayout>
I am getting the dailog properly
But I have a Fragment class, ExampleFragment like this
public class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.frag_content, container, false);
}
}
Whose layout is
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Fragment Conttent"
android:textAppearance="?android:textAppearanceMedium" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
When i try to put this fragment in my dailog layout , my application is crashing.
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="You really want to read this text?"
android:textAppearance="?android:textAppearanceMedium" />
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="com.example.addfragment.ExampleFragment"
android:id="#+id/example_fragment"
android:layout_width="match_parent" android:layout_height="match_parent" />
</LinearLayout>
When I put it in my activity_main layout it shows the fragment, but the same does not work in dailogFragment.
I am getting
08-28 15:44:09.969: E/AndroidRuntime(438): android.view.InflateException: Binary XML file line #22: Error inflating class fragment
Anyone have any suggestion for this ? Why cant a fragment be included in dailogFragment layout ?
Why can't we add a fragment in a daiologFragment ?
, why cant a fragment be included in
dailogFragment layout ?
Because nested fragment can't be added to a parent fragment through inflation of a layout file containing them. Below is a note from the docs:
Note: You cannot inflate a layout into a fragment when that layout includes a < fragment >. Nested fragments are only supported when added to a fragment dynamically.
Any have any suggestion for this ?
Add the ExampleFragment in code: in the onCreateView() method of the dialog fragment create a instance of ExampleFragment and using getChildFragmentManager() add it to a container from dialog_layout.
I am using a LinearLayout view with a custom dialog ,when I run the code below, weight is not working correctly as the red view takes the green view's weight and verse vise as image below:
but when I use the same layout in another activity , weight goes correctly as the image below :
ConfirmDialog.java
public class ConfirmDialog extends DialogFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_confirm, null);
return view;
}
}
dialog_confirm.xml
<?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="wrap_content"
android:orientation="horizontal"
android:weightSum="7" >
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:background="#00ff00" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000" />
</LinearLayout>