I have a view defined as "public class ChessBoard extends View" in this class in one of the code flows I want to popup a Dialog box and then get a result from that dialog box.
I have tried the answer here: How to create a Custom Dialog box in android? but I have no Activity to pass to the constructor.
since your chessboard is a view there should be a getContext()-method (see here: https://developer.android.com/reference/android/view/View.html#getContext() ). you can cast the result to an activity:
Activity activity = (Activity) getContext()
Use this code
///-----------------------------------------------------
dialog_=new Dialog(this);
dialog_.setContentView(R.layout.dialog_submit);//this is path of xml file
dialog_.show();
Button submit_btn=dialog_.findViewById(R.id.but_submit); //button on dialog
Button cancel_btn = dialog_.findViewById(R.id.but_cancel);
final EditText edit_username=dialog_.findViewById(R.id.edit_name);
Related
I have an application in which my requirement is to show Home screen and then if any button is clicked open another activity in Popup(Which means remaining part of first activity will be visible but not click-able) and if any button is clicked in the second activity launch the third activity same as second.
Also I am not looking for setting in manifest: #android:style/Theme.Dialog
In the image you can see a pop up and a parent screen behind that popup.Please help me to solve this. I have to open an activity on top of another activity with some part of previous visible.
Thanks in advance.
just add this tag to your activity tag in your manifest file
android:theme="#android:style/Theme.Dialog"
then the activity will look according to your requirements hope this helps ..
We can set the style of an activity to open it as pop up using:- android:theme="#android:style/Theme.Translucent.NoTitleBar" in Manifest for activity you want to open as Popup and providing margins to layout.
You can use AlertDialog and inflate its custom layout in its setView() method.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog
// layout
builder.setView(inflater.inflate(R.layout.yourLayout, null));
AlertDialog ad = builder.create();
ad.setTitle("Your title");
ad.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
ad.show();
how can I add a new dialog with new layout on the currently running activity without making changes to the layout file of currently running activity. I cannot add a new activity as the old activity should remain active while the new dialog is displayed.
Here is a small snippet to get started on creating a Dialog and applying a layout file to it.
// create an instance of Dialog
Dialog dialog= new Dialog(c, R.style.CustDialog);
//inflate a layout
LayoutInflater inflater = this.getLayoutInflater();
View root = inflater.inflate(R.layout.custom_alert, null);
// set the layout for the Dialog
dialog.setContentView(root);
If you read the Dialog Docs it gives the different methods you can use.
Note the docs say
The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses:
So for this reason you may want to look at AlertDialog which you can find plenty of examples of on SO or the Google.
This answer gives an example of creating a custom Dialog class.
I want the screen set to (Window.FEATURE_NO_TITLE). I know that I can put requestWindowFeature(Window.FEATURE_NO_TITLE); in the OnCreate method but I have another Class let's say Class B which is extended to the Fragment and it has a button in the layout. I want when that button is clicked in clicked in Class B, the Window should be shown without the Title. I tried as below:
AnandSahib anad = new AnandSahib();
anad.requestWindowFeature(Window.FEATURE_NO_TITLE);
I think you cant do it.you should call anad.requestWindowFeature(Window.FEATURE_NO_TITLE); before setContentView().please See this Android Developer site
I am trying to implement a relatively simple DialogFragment that is supposed to contain an image and a OK button and I want to display it on demand from my activity.
I set its layout in onCreateView via inflater.inflate, but I can't figure out how to tell it that the implementation for the OK button event handler is located in my custom DialogFragment class. It seems to try to find it in the activity, which is not what I want. Would calling getDialog().dismiss() be enough to dismiss it?
Here is how I create a dialog in my activity:
ResponseDialog dialog = new ResponseDialog();
dialog.show(getFragmentManager(), "dialog_response_image");
Also, some folks say that my custom DialogFragment should set getDialog().setCanceledOnTouchOutside(true);, but where should I set this. In onActivityCreated?
How can I get access to its view from the activity, if I wish to set the source of the image contained by it?
Also, for some reason, it fills up the entire display, even if I use static width / height. Does anyone know how to fix this? - I managed to fix this by swithching to LinearLayout instead of RelativeLayout in the DialogFragment layout XML...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/background_color"
tools:context=".MainActivity"
android:layout_width="200dp"
android:layout_height="400dp"
android:id="#+id/dialogImageReponse" >
<Button
android:id="#+id/dialogButtonOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/ok"
android:onClick="Ok" />
</RelativeLayout>
I'll try to answer each of your questions:
1.) About the "how to tell it that the implementation for the OK button event handler is located in my custom DialogFragment class" I'm not sure why you want to do that since you haven't told us.
2.) "Would calling getDialog().dismiss() be enough to dismiss it?". If you wish to dismiss the DialogFragment from your Activity then you just need to call dialog.dismiss() ('dialog' here refers to ResponseDialog dialog = new ResponseDialog(); so obviously you can only call dialog.dismiss() once you've created the ResponseDialog object.
3.) Regarding "getDialog().setCanceledOnTouchOutside(true);" Once again you just need to call
dialog.setCanceledOnTouchOutside(true);
immediately after this:
ResponseDialog dialog = new ResponseDialog();
dialog.show(getFragmentManager(), "dialog_response_image");
4.) You can get access to its view by calling:
View v = dialog.getView();
Although if you just wish to set the source of the image to be used contained by it, and I assume you want to use a photo or a picture stored in your phone then you would have to use a parametrized constructor for ResponseDialog like this and as a parameter you would have to pass the URI OR the filepath of the picture that you would like displayed in the dialogfragment :
ResponseDialog dialog = new Response(String filepath);
and then in your custom dialog class which I understand is ResponseDialog you would have to use this filepath (received in the constructor) to create a bitmap and then set the bitmap as the source of the ImageView in that DialogFragment.
I want the user to enter a string into an EditText into a popup. I have taken a look in Android Developers here .
But this kind of popup is not explained. How can I make it?
On the same page you linked to, check out how they say to create a custom dialog.
Create an XML layout for the dialog that has an EditText. Then show it:
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
dialog.show();
Create your own custom dialog by extending Dialog. Your custom dialog class will have an onCreate() callback, in which you can call setContentView to whatever view you want, just like with an Activity.
Simply create a view to look however you'd like and use that one. Then, when you want to use your dialog, simply get an instance, like Dialog myDialog = new MyCustomDialog(getParent(), R.style.some_style); and then myDialog.show();