I need to use my AppCompat Activity as a Dialog.For this I tried so my solution that answered in StackOverflow. But nothing worked.Please answer me. I am getting activity as dialog. But it shows very narrow both in height & width.
I used the following Theme:
<style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="windowNoTitle">true</item>
<item name="android:background">#android:color/transparent</item>
</style>
You can use DialogFragment and customize the layout accordingly.
public class CustomDialogFrag extends DialogFragment{
static FragmentManager fragmentManager;
public static CustomDialogFrag showDialog(FragmentManager fm){
CustomDialogFrag customDialogFrag=new CustomDialogFrag();
fragmentManager=fm;
return customDialogFrag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialogfrag_layout, null);
alertDialogBuilder.setView(view);
setupUI(view);
alertDialogBuilder.setTitle("Notification Message");
alertDialogBuilder.setIcon(R.drawable.notificationicon);
alertDialogBuilder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
void setupUI(View view){
TextView textViewOne=(TextView)view.findViewById(R.id.txtEventAlias);
TextView textViewTwo=(TextView)view.findViewById(R.id.txtTime);
TextView textViewThree=(TextView)view.findViewById(R.id.txtLogMessage);
textViewOne.setText("Text 1");
textViewTwo.setText("Text 2");
textViewThree.setText("Text 3");
}
}
And the dialogfrag_layout.xml will be
<?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="match_parent"
android:padding="#dimen/margin_10"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtEventAlias"
android:text="Sample"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/textSizeMedium"
android:padding="#dimen/margin_10"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtTime"
android:text="Sample"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/textSizeMedium"
android:padding="#dimen/margin_10"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtLogMessage"
android:text="Sample"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/textSizeMedium"
android:padding="#dimen/margin_10"
/>
</LinearLayout>
For invoking this Dialog from a Fragment:
DialogFragment dialogFragment=CustomDialogFrag.showDialog(getFragmentManager());
dialogFragment.show(getActivity().getFragmentManager(), "tag");
In your activity's onCreate put the following lines:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your);
// Make the window's width full sized
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
Window window = getWindow();
layoutParams.copyFrom(window.getAttributes());
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
}
Tested and works. You can set to both width and height to WRAP_CONTENT if needed.
Related
I am trying to create a custom Dialog, and is working just fine, but the Dialog is filling the whole screen height. I've done some unsuccessful research on the internet but I don't have a lot of time for this task, if anyone knows why is this happening I appreciate it hard.
here is the onCreateDialog():
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder= new AlertDialog.Builder(getContext());
LayoutInflater inflater= (LayoutInflater) builder.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog__warning, mContainer, false);
txtWarning = (TextView)view.findViewById(R.id.txtWarning);
if(messageWarning.length()>0)
{
txtWarning.setText(messageWarning);
}
btnOkDialog = (Button) view.findViewById(R.id.btnOkDialog);
btnOkDialog.setOnClickListener(MyListener);
btnCancelDialog = (Button) view.findViewById(R.id.btnCancelDialog);
btnCancelDialog.setOnClickListener(MyListener);
builder.setView(view);
return builder.create();
}
and the xml of dialog_warning:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#b1b0b0"
tools:context="ipat.johanbayona.gca.ipat.NewEvidence.Dialog_Warning">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="25sp"
android:id="#+id/txtWarning"
android:text="Mensaje error" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="#+id/btnOkDialog"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/btnCancelDialog"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#b1b0b0"
tools:context="ipat.johanbayona.gca.ipat.NewEvidence.Dialog_Warning">
Change "layout_height" to wrap_content. You fixed height and width, that may be the problem depending on your screen size.
try creating your own theme for the dialog, something like this
<style name="AppDialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
</style>
and set this theme passing to the dialog's super constructor with R.style.AppDialog
upd.
it's not the builder's constructor. you should create your own class that extends the AlertDialog, override it's onCreate and the super constructor and your class should be something like this
public class YourDialog extends Dialog {
public YourDialog(#NonNull Context context) {
super(context, R.style.AppDialog);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog__warning);
txtWarning = (TextView) findViewById(R.id.txtWarning);
if(messageWarning.length()>0)
{
txtWarning.setText(messageWarning);
}
btnOkDialog = (Button) findViewById(R.id.btnOkDialog);
btnOkDialog.setOnClickListener(MyListener);
btnCancelDialog = (Button) findViewById(R.id.btnCancelDialog);
btnCancelDialog.setOnClickListener(MyListener);
}
}
overriding the main constructor, and passing the R.style.AppDialog should do the trick. all you need is to call where you want to show the dialog new YourDialog(context).show();
note that you don't need to call manually the layout inflater to pass your own contentview, just calling the setContentView with resId will automatically inflate the view beneath.
Here's how I called my DialogFragment:
DialogSelectAccount myDiag=new DialogSelectAccount();
myDiag.show(ft,"Diag" );
Here's how (partially) my DialogFragment is created:
public class DialogSelectAccount extends DialogFragment {
public DialogSelectAccount() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_select_account, container, false);
tvMessage = (TextView) rootView.findViewById(R.id.tvMessage);
btnAccountPublic = (Button) rootView.findViewById(R.id.btnAccountPublic);
btnAccountEnterprise = (Button) rootView.findViewById(R.id.btnAccountEnterprise);
tvMessage.setText(message);
btnAccountPublic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Login.setAccountType = 2;
dismiss();
}
});
btnAccountEnterprise.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Login.setAccountType = 1;
dismiss();
}
});
return rootView;
}
and here's the xml for my DialogSelectAccount
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff26b4e9"
android:orientation="vertical" >
<TextView
android:id="#+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#android:color/black"
android:textSize="15dp"
android:textAlignment="center"
android:gravity="center_horizontal"
android:background="#ff26b4e9"
android:autoText="true">
</TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#ff26b4e9"
android:orientation="horizontal" >
<Button
android:id="#+id/btnAccountPublic"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:clickable="true"
android:text="#string/accountPub"
android:textColor="#ffffffff"
android:background = "#drawable/roundedbutton" />
<Button
android:id="#+id/btnAccountEnterprise"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:clickable="true"
android:text="#string/accountEnt"
android:textColor="#ffffffff"
android:background = "#drawable/roundedbutton" />
</LinearLayout>
</LinearLayout>
the problem is there's always an innoying white background displayed, as shown below. How do I remove it?
In the onCreateView() of your DialogFragment, replace
View rootView = inflater.inflate(R.layout.dialog_select_account, container, false);
with
View rootView = inflater.inflate(R.layout.dialog_select_account, container);
Also, add this to onViewCreated():
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme);
and in the outermost LinearLayout of the XML, change
android:layout_width="fill_parent"
android:layout_height="fill_parent"
to
android:layout_height="wrap_content"
android:layout_width="wrap_content"
Try this. This should work.
I suggest you create an alert dialog with your custom UI in onCreateDialog in your DialogFragment instead. Then you can then also easily add a style to it that will remove the white background.
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val view = activity!!.layoutInflater.inflate(R.layout.dialogfragment_my_custom_view, null)
val builder = AlertDialog.Builder(activity!!, R.style.MyDialogTheme)
return builder
.setView(view)
.create()
}
Then you can just create the "MyDialogTheme" like this:
<style name="ProgressDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowBackground">#color/automile_transparent</item>
</style>
You can create style for your dialog:
<style name="DialogStyle" parent="Base.Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
And use it in code by method:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), R.style.DialogStyle);
}
Or you can set FEATURE_NO_TITLE for your dialog only in your code as is shown in the code below:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
Higher android version devices automatically remove title space. But for lower version we have to add some line of code.
It is more effective to add Window.FEATURE_NO_TITLE in onCreateDialog() method. Same as below :
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a dialog window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
Here I think you are trying to hide the title bar. Use this
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
I´m trying to implement a basic custom Alert Dialog.
It should look like this
With the following XML Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/tv_custom_dialog_event"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/actionbar_background"
android:gravity="top"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="100dp"
android:text="Delete Event ?"
android:textColor="#color/white"
android:textSize="20sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DFDFDF" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/btn_custom_dialog_events_cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_action_cancel" />
<ImageButton
android:id="#+id/btn_custom_dialog_events_true"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_action_accept" />
</LinearLayout>
</LinearLayout>
However when I run it on my emulator it looks like this:
Does anybody know why this happens and how I can fix that?
Assuming you have a class that extends Dialog you can do the following:
First define a style in styles.xml with something like this:
<style name="CustomDialogThemeTrasparent" parent="#android:style/Theme.Dialog">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:windowBackground">#color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
Then in the constructor of your custom dialog class you set this theme:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(final Context context)
{
super(context, R.style.CustomDialogThemeTrasparent);
}
To set your custom layout as view you can make a function called setTheme() and then called in the show() of your dialog, even with parameters and a nice done layout you can make a more generic class that you can use to show your custom dialogs all over your app, something like this:
//Function to set the layout when the dialog is instantiated, here we
//set the layout and if you want you can set parameter to
//show/hide controls/views and can show different types of dialogs with the same class but with a unified style
private void setTheme()
{
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = layoutInflater.inflate(R.layout.custom_dialog, null);
this.setContentView(mView);
}
then you can make function(s) to show your dialog(s), like:
public void showDeletionDialog(String pMessage)
{
this.setTheme();
this.show();
}
hope it help you
Try this approach. You can inflate your layout:
public AlertDialog displayLayoutDialog(int layout,final Context context, int theme){
AlertDialog.Builder builder = new AlertDialog.Builder(context, theme);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(layout, null);
builder.setView(view);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog= builder.create();
dialog.show();
Button tb = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
tb.setOnClickListener(new CustomListener(dialog, context));
return dialog;
}
And your Listener where all the validation and anything else that you need happens:
public class CustomListener implements View.OnClickListener {
private final Dialog dialog;
private Context context;
public CustomListener(Dialog dialog, Context context) {
this.dialog = dialog;
this.context = context;
}
#Override
public void onClick(View v) {
Toast.makeText(context, "Custom Layout", Toast.LENGTH_LONG).show();
}
}
Hope it helps!!!
In my application i am using alert dialog with rounded rectangle theme.But it have alertdialog rectangle and my theme.My problem is how to replace alert dialog border like dialog.I want to show this set item with own theme only.
I want output this manner instead of the above theme:
Main Activity:
AlertDialog.Builder alertSeverity = new AlertDialog.Builder(
getActivity(), R.style.Theme_CustomDialog);
alertSeverity.setTitle("Severity Status");
CharSequence[] severityStatus = { "Low-Severity",
"Middle-Severity", "High-Severity" };
alertSeverity.setItems(severityStatus,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
My Theme:
<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#drawable/shapedialogtheme</item>
<item name="android:windowFrame">#null</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#565656" />
<stroke
android:width="5dp"
android:color="#ffff8080" />
<corners android:radius="30dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<size
android:width="150dp"
android:height="150dp"/>
</shape>
Use Dialog instead of AlertDialog.
Create your custom layout which you want to show in dialog and setContent in dialog.
Apply this theme android.R.style.Theme_Translucent_NoTitleBar in dialog it will hide border.
Here is sample code.
Dialog dialog = new Dialog(activity.this, android.R.style.Theme_Translucent_NoTitleBar);
// your layout file
dialog.setContentView(R.layout.dialog);
// for hide title
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//for set title
dialog.setTitle("Custom Dialog");
dialog.show();
Updated:
just tried this in AlertDialog.
AlertDialog.Builder alertSeverity = new AlertDialog.Builder(
getActivity(), android.R.style.Theme_Translucent_NoTitleBar);
try the next solution:
extend from dialog, and set the exact view to use by using setContentView .
the alertDialog is used for some functionalities. it's not that it can do anything you want.
maybe instead of extending you could take the dialog and then use the setContentView.
Use Dialog instead of AlertDialog
Dialog callAlert = new Dialog(LoginActivity.this,R.style.CutomDialog);
callAlert.setContentView(R.layout.call);
Style.xml
<style name="CutomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">#style/Animations.DialogAnimation</item>
</style>
call.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp"
android:background="#drawable/call_bg"></RelativeLayout>
call_bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="3dp" android:color="#A20B3F" />
<corners android:bottomRightRadius="4dp" android:bottomLeftRadius="4dp"
android:topLeftRadius="4dp" android:topRightRadius="4dp"/>
Main thing is that you have to make layout backgrpund transparent otherwise you will not able to get output as you want.
You need to design a custom dialog for this purpose :
**dialog.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_view_SaveAs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/save_as"
android:layout_margin="10dp"
android:textSize="25dp" />
<EditText
android:id="#+id/edit_txt_SaveAs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="300dp"
android:maxLines="1"
android:textSize="20dp"
android:maxLength="50"
android:layout_margin="10dp"
android:text="#string/save_as" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp"
android:weightSum="1.0" >
<Button
android:id="#+id/btn_SaveAs"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.5"
android:minWidth="100dp"
android:textSize="20dp"
android:text="#string/save"
android:layout_margin="3dp" />
<Button
android:id="#+id/btn_Cancel"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.5"
android:minWidth="100dp"
android:textSize="20dp"
android:text="#string/cancel"
android:layout_margin="3dp" />
</LinearLayout>
You can then make different class for the particular dialog like this:
public class SaveDialog extends Dialog implements android.view.View.OnClickListener {
private Context context;
private TextView txt_view_SaveAs;
private EditText edit_txt_SaveAs;
private Button btn_SaveAs;
private Button btn_Cancel;
public SaveDialog(Context context) {
super(context);
this.context = context;
// TODO Auto-generated constructor stub
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
setCancelable(true); // Setting the Dialog to be Cancellable on Back Key Press
txt_view_SaveAs = (TextView) findViewById(R.id.txt_view_SaveAs);
edit_txt_SaveAs = (EditText) findViewById(R.id.edit_txt_SaveAs);
btn_SaveAs = (Button) findViewById(R.id.btn_SaveAs);
btn_SaveAs.setOnClickListener(this);
btn_Cancel = (Button) findViewById(R.id.btn_Cancel);
btn_Cancel.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Write code for all the buttons on click methods
}
}
Then you can call the custom dialog in your main class by using the following code :
SaveDialog save_dialog = new SaveDialog(saving_activity);
save_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
save_dialog.show();
You can not remove border from alert dialog.
use this
public class ActivityIndicator extends Dialog implements android.view.View.OnClickListener{
protected static final String TAG = InfoIndicator.class.getName();
ImageView close;
WebView info;
public ActivityIndicator (Context context,String information)
{
super(context, android.R.style.Theme_Translucent_NoTitleBar);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.info);
setCancelable(true);
}
}
and try this below function for show, hide and clear dialog
private static ActivityIndicator activityIndicator;
public static void hideActivityViewer() {
if (activityIndicator != null) {
activityIndicator.dismiss();
}
activityIndicator = null;
}
public static void showActivityViewer(Context context) {
if (activityIndicator == null)
{
activityIndicator = new ActivityIndicator(context);
}
activityIndicator.show();
}
public static void clearDialogs()
{
activityIndicator = null;
}
you can use popwindow for more style by yourself
Is it possible to have just an image popup/come-up in an Android application? It's similar to an overriding the normal view of an AlertDialog so that it contains just an image and nothing else.
SOLUTION: I was able to find an answer thanks to #blessenm's help. Masking an activity as a dialog seems to be the ideal way. The following is the code that I have used. This dialog styled activity can be invoked as needed by the application the same way a new activity would be started
ImageDialog.java
public class ImageDialog extends Activity {
private ImageView mDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_dialog_layout);
mDialog = (ImageView)findViewById(R.id.your_image);
mDialog.setClickable(true);
//finish the activity (dismiss the image dialog) if the user clicks
//anywhere on the image
mDialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
your_dialog_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image_dialog_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:gravity = "center">
<ImageView
android:id="#+id/your_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src = "#drawable/your_image_drawable"/>
</FrameLayout>
It is crucial that you set the following style for the activity to accomplish this:
styles.xml
<style name="myDialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFrame">#null</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowContentOverlay">#null</item>
</style>
The final step is to declare this style for the activity in the manifest as follows:
<activity android:name=".ImageDialog" android:theme="#style/myDialogTheme" />
No xml:
public void showImage() {
Dialog builder = new Dialog(this);
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialogInterface) {
//nothing;
}
});
ImageView imageView = new ImageView(this);
imageView.setImageURI(imageUri);
builder.addContentView(imageView, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
builder.show();
}
If you just want to use a normal dialog something like this should work
Dialog settingsDialog = new Dialog(this);
settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.image_layout
, null));
settingsDialog.show();
image_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="YOUR IMAGE"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="OK" android:onClick="dismissListener"/>
</LinearLayout>
Try the following:
It has image zoom_in/zoom_out as well.
Step 1:
Add compile 'com.github.chrisbanes.photoview:library:1.2.4' to your build.gradle
Step 2:
Add the following xml
custom_fullimage_dialoge.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dp">
<ImageView android:id="#+id/fullimage" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ImageView>
<TextView android:id="#+id/custom_fullimage_placename"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:textColor="#FFF">
</TextView>
</LinearLayout>
Step 3:
private void loadPhoto(ImageView imageView, int width, int height) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//dialog.setContentView(R.layout.custom_fullimage_dialog);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_fullimage_dialog,
(ViewGroup) findViewById(R.id.layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.fullimage);
image.setImageDrawable(imageView.getDrawable());
image.getLayoutParams().height = height;
image.getLayoutParams().width = width;
mAttacher = new PhotoViewAttacher(image);
image.requestLayout();
dialog.setContentView(layout);
dialog.show();
}
Step 4:
user_Image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
loadPhoto(user_Image,width,height);
}
});
You can do it easily by create a Dialog Fragment in Kotlin:
BigImageDialog.kt
class BigImageDialog():DialogFragment() {
private var imageUrl = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
imageUrl = arguments.getString("url")
}
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
val v = inflater!!.inflate(R.layout.dialog_big_image, container, false)
this.dialog.window.requestFeature(Window.FEATURE_NO_TITLE)
Picasso.get().load(imageUrl).into(v.bigImageView)
return v
}
override fun onStart() {
super.onStart()
val dialog = dialog
if (dialog != null) {
dialog.window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
}
}
companion object {
#JvmStatic
fun newInstance(imageUrl: String) =
BigImageDialog().apply {
arguments = Bundle().apply {
putString("url", imageUrl)
}
}
}
}
dialog_big_image.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<ImageView
android:id="#+id/bigImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Opening Dialog:
"smallImageView".setOnClickListener { BigImageDialog.newInstance("image url").show(fragmentManager,"") }
There is a couple ways you can do this. But, if you're looking to have your image appear to be floating above your existing activity, you may want to use an activity with android:theme="#style/Theme.Transparent" defined in the manifest. Then, design your layout to just have a single ImageView positioned in the center of the screen. The user will have to push the back button to get out of this, but it sounds like that's what you want.
If you want it to look like an actual dialog, you can always use a dialog styled activity as well using Theme.Dialog. OR, you could just use a dialog and customize it.
The more flexible and recommended way is use DialogFragment. If you want to support versions before 3.0 you can use compatibility library