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
Related
I want to customize the popupmenu in android, default popup menu gives more space ,so I'm trying to change the custom layout in popup menu but I cant figure out how.
Note: I want to do this small popup design so I go with default popup menu but i want to customize it.
findViewById(R.id.menuclick).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(Sample1.this, view);
popupMenu.setOnMenuItemClickListener(Sample1.this);
popupMenu.inflate(R.layout.menus_layout);
popupMenu.show();
}
});
To inflate popupMenu from a button onClick, use the following code.
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, v);
popup.getMenuInflater().inflate(R.menu.pop_up, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this, "Some Text" + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});
EDIT
To style the popupMenu, add the following style.
<style name="PopupMenu" parent="#android:style/Widget.PopupMenu">
<item name="android:popupBackground">#ffffff</item>
</style>
I noticed you also want to add icons next to your text. It is possible to add icons in popupMenu. However it is a better approach to use popup Window instead. Here is a sample code:
PopupWindow mypopupWindow;
setPopUpWindow();
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mypopupWindow.showAsDropDown(v,-153,0);
//showAsDropDown(below which view you want to show as dropdown,horizontal position, vertical position)
}
}
});
}
private void setPopUpWindow() {
LayoutInflater inflater = (LayoutInflater)
getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.popup, null);
Start=(RelativeLayout)view.findViewById(R.id.start_btn);
Pause=(RelativeLayout)view.findViewById(R.id.pause_btn);
Stop=(RelativeLayout)view.findViewById(R.id.stop_btn);
mypopupWindow = new PopupWindow(view,300, RelativeLayout.LayoutParams.WRAP_CONTENT, true);
popup Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:background="#drawable/whitedrawable"
android:paddingRight="0dp"
android:layout_marginRight="0dp"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/startimg"
android:id="#+id/startimg"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
/>
<TextView
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="0dp"
android:text="Start"
android:layout_toRightOf="#+id/startimg"
/>
<!-- Continue for other items-->
The whitedrawable can be used to set a background of your choice. You can use 9patch to get the shadow and rounded corners for the background.
To dismiss the popupWindow, use the following code:
mypopupWindow.getContentView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mypopupWindow.dismiss();
}
});
To dismiss using the back button, use:
#Override
public void onBackPressed() {
if(mypopupWindow.isShowing()) {
mypopupWindow.dismiss();
return;
}
super.onBackPressed();
}
My answer will be like an update for this answer (the first answer in this post) focusing at PopupWindow using Kotlin, also using View Binding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val bind = ViewBinding.inflate(inflater, container, false)
val popupInflater =
requireActivity().applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflaterERVICE) as LayoutInflater
val popupBind = YourPopupLayoutBinding.inflate(popupInflater)
popupBind.icon1.setOnClickListener { // do your thing for 1st icon }
popupBind.icon2.setOnClickListener { // do your thing for 2nd icon }
val popupWindow = PopupWindow(
popupBind.root, 126.fromDpToPx.toInt(),
89.fromDpToPx.toInt(), true
).apply { contentView.setOnClickListener { dismiss() } }
// make sure you use number than wrap_content or match_parent,
// because for me it is not showing anything if I set it to wrap_content from ConstraintLayout.LayoutParams.
bind.yourButton.setOnClickListener(popupWindow::showAsDropDown)
return bind.root
}
This code is in Fragment class, that's why I call applicationContext using requireActivity()
Here is the code for layout,
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="126dp"
android:layout_height="89dp"
android:background="#FFFFFF">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/icon1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:paddingHorizontal="10dp"
android:paddingVertical="10dp"
android:text="#string/tokopedia"
android:textColor="#color/dark_grey"
app:drawableStartCompat="#drawable/ic_icon1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/icon2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:paddingHorizontal="10dp"
android:paddingVertical="10dp"
android:text="#string/shopee"
android:textColor="#color/dark_grey"
app:drawableStartCompat="#drawable/ic_icon2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/icon1" />
</androidx.constraintlayout.widget.ConstraintLayout>
Don't forget to add background color in custom layout, also you can styling freely in custom layout.
Hope this can help you all :)
I'm implementing a UI where a bottom sheet will appear above the keyboard with an EditText for the user to enter a value. The problem is the View is being partially overlapped by the keyboard, covering up the bottom of the bottom sheet.
Here is the Bottom Sheet and no keyboard.
Here is the Bottom Sheet with the keyboard showing.
What's the best method to ensure the entire Bottom Sheet is shown?
Thanks.
Just reposting #jblejder from this question Keyboard hides BottomSheetDialogFragment since it worked for me, to make it easier for others to find:
The most convenient way that I found to change this is by creating style:
<style name="DialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize</item>
</style>
And set this in onCreate method of your BottomSheetDialogFragment:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle)
}
This is how it looks on my device:
==== UPDATE ====
As already mentioned in the Comments a few times, you might also need to set the state of the BottomSheetDialog to STATE_EXPANDED like in Nordknight's answer below
dialog = new BottomSheetDialog(getContext(), R.style.BottomSheetDialog);
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = d.findViewById(R.id.design_bottom_sheet);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
},0);
}
});
This might be a redundant answer. Although just pointing out the issue.
If you're using BottomSheetDialogFragment, the only way is to enable the attribute android:windowIsFloating to true. This will enable the whole window to be on top of whatever is trying to take the space behind it.
<style name="BottomSheetDialogThemeNoFloating" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:windowSoftInputMode">adjustResize|stateVisible</item>
</style>
Then in your onCreate() of your dialog, set this style.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// set the window no floating style
setStyle(DialogFragment.STYLE_NORMAL, R.style.AppRoundedBottomSheetDialogThemeNoFloating)
}
This is handy for those who frequently use bottom sheets and may want to deal with EditText and soft keyboard overlapping each other.
Note: The class KeyboardUtil by mikepenz has an issue in which on certain phones, the content view with input field is automatically pushed above keyboard despite giving bottom padding to the whole content view supplied.
dialog = new BottomSheetDialog(getContext(), R.style.BottomSheetDialog);
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = d.findViewById(R.id.design_bottom_sheet);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
},0);
}
});
This code works fine at Fragment's onCreateView method (thanks for ADM)
Some answers seem to do the trick better than others but will need modification when using the new material design components instead of the older support libraries while also using kotlin
Hope this will help someone.
BottomSheetDialog(this, R.style.DialogStyle).apply {
setContentView(layoutInflater.inflate(R.layout.bottom_sheet, null))
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
findViewById<EditText>(R.id.time_et)?.requestFocus()
show()
}
layout/bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:padding="16dp">
<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">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<EditText
android:id="#+id/time_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:minWidth="50dp"
android:text="15" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="min" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#000"
android:text="Save"
android:textColor="#fff" />
</LinearLayout>
</ScrollView>
styes.xml (Split for v-21 for using statusBarColor)
<style name="DialogStyle" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize</item>
</style>
A BottomSheetDialog can be helpful for this. it will open with Softkeyboard open with focus on edit text.But user can still close the Softkeyboard and Dialog will be reset to Bottom. Again focusing will make dialog appear at top of Softkeyboard.
public void showDialog() {
final BottomSheetDialog dialog=new BottomSheetDialog(this);
dialog.setContentView(R.layout.item_dialog);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
}
You can make the BottomSheetDialog expanded over keyboard . But for this you need to call it after SoftKeyboard Open. the Expand code is .
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
I have tested it on DialogInterface.OnShowListener() but its not working . Tested with it 1 second delay its working . But Delay is not the solution . You need to figure out the on which action you should expand the dialog.
final BottomSheetDialog dialog=new BottomSheetDialog(this);
dialog.setContentView(R.layout.item_dialog);
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE|
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
FrameLayout bottomSheet = (FrameLayout) dialog.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
}
},2000);
dialog.show();
An updated answer for those using Material Components theme, and also an improved answer to remove the need to add anything into each dialog's onCreate().
In your main AppTheme style, you can add the attribute bottomSheetDialogTheme to apply the style to all of your BottomSheetDialogFragments:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="bottomSheetDialogTheme">#style/BottomSheetDialogStyle</item>
</style>
So with the above, no need to add anything to your BottomSheetDialogFragment code.
And then, as previous answers, your Dialog style, noting to also match the style to the same Material Components library (or you'll get some weird looking buttons, edittexts etc):
<style name="BottomSheetDialogStyle" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:windowSoftInputMode">adjustResize</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
</style>
Note that I am adding my app theme colors back in here; as you can't have multiple inheritance in Android styles, you may want these colors defining here so any buttons and accents align with the rest of your app.
private fun enterMobileNumberPopUp() {
val dialog = BottomSheetDialog(this,R.style.DialogStyle)
val view = layoutInflater.inflate(R.layout.layout_otp, null)
dialog.setContentView(view)
dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
dialog.show()}
THIS IS THE MOST EASY WAY AND BEST WAY TO HANDLE BOTTOM SHEET DIALOG
YOU CAN CALL THIS IN METHOD
<style name="DialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize</item>
Style reference
This one working
BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.DialogStyle);
View sheetView = getLayoutInflater().inflate(R.layout.dialog_remark, null);
Objects.requireNonNull(dialog.getWindow())
.setSoftInputMode(SOFT_INPUT_STATE_VISIBLE);
dialog.setContentView(sheetView);
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
View bottomSheetInternal = d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheetInternal).setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
dialog.show();
add this style to your styles.xml
<style name="DialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustPan</item>
</style>
add your layout like this
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/scrollview"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
android:fontFamily="#font/montserratmedium"
android:text="Add Remarks"
android:textColor="#android:color/black"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:fontFamily="#font/montserratmedium"
android:text="Branch"
android:textColor="#8B8B8B"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/montserratmedium"
android:text="BLR-CO-SINDHUBHAVAN-384"
android:textColor="#android:color/black"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:fontFamily="#font/montserratmedium"
android:text="Enter Remarks"
android:textColor="#8B8B8B"
android:textSize="18sp" />
<EditText
android:id="#+id/input_remark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="8dp"
android:background="#drawable/remark_inputbg"
android:gravity="start"
android:inputType="textMultiLine"
android:lines="5" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="#drawable/reset_bg"
android:padding="8dp"
android:text="CANCEL" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="#4F4DBB"
android:padding="8dp"
android:text="CANCEL"
android:textColor="#android:color/white" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</ScrollView>
bottomSheetDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
It will work for sure.
Just write the following programmatically
override fun setupDialog(dialog: Dialog, style: Int) {
super.setupDialog(dialog, style)
dialog.window?.setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE or
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
);
}
Kotlin, +viewBinding, +by using the accepted answer's dialog style
val bottomSheet = BottomSheetDialog(this, R.style.BottomSheetDialogStyle)
val binding = [YourSheetBinding].inflate(LayoutInflater.from(YourActivity.this))
bottomSheet.setContentView(binding.root)
bottomSheet.behavior.state = BottomSheetBehavior.STATE_EXPANDED
bottomSheet.show()
override fun onStart() {
super.onStart()
bottomSheetBehavior?.skipCollapsed = true
bottomSheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
}
Putting this in BottomSheet helped without setting styles and without ScrollView
(correct me if I'm wrong or if I'm missing something)
This trick solved me
in manifest put
android:windowSoftInputMode="adjustPan"
in your activity
and
bottomSheetDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
bottomSheetDialog.getBehavior().setState(BottomSheetBehavior.STATE_EXPANDED);
For this, I found an AlertDialog worked best. While it doesn't sit flush against the bottom or side of the screen, it still looks good enough.
First, create the AlertDialog with your view.
val view = LayoutInflater.from(context).inflate(R.layout.alert, null)
dialog = AlertDialog.Builder(context)
.setView(view)
.create()
Next, set the gravity.
dialog.window.attributes.gravity = Gravity.BOTTOM
And finally, show it.
dialog.show()
You can also bind the keyboard to stay with the dialog, by using an onDismissListener.
After showing the AlertDialog, I force up the keyboard.
Call this method, passing in your EditText.
fun showKeyboard(view: View?) {
if (view == null) return;
val imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
And for dismissing within the onDismissListener.
private fun hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
My answer might be useful for someone who is still looking for solution. If keyboard is covering edittext in BottomSheetDialogFragment then in setupDialog() method create instance of a class KeyboardUtil and pass your rootview.
#Override
public void setupDialog(final Dialog dialog, int style) {
super.setupDialog(dialog, style);
View view = View.inflate(getActivity(), R.layout.reopen_dialog_layout, null);
new KeyboardUtil(getActivity(), view);
}
Create a new class
public class KeyboardUtil {
private View decorView;
private View contentView;
//a small helper to allow showing the editText focus
ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
decorView.getWindowVisibleDisplayFrame(r);
//get screen height and calculate the difference with the useable area from the r
int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
int diff = height - r.bottom;
//if it could be a keyboard add the padding to the view
if (diff != 0) {
// if the use-able screen height differs from the total screen height we assume that it shows a keyboard now
//check if the padding is 0 (if yes set the padding for the keyboard)
if (contentView.getPaddingBottom() != diff) {
//set the padding of the contentView for the keyboard
contentView.setPadding(0, 0, 0, diff);
}
} else {
//check if the padding is != 0 (if yes reset the padding)
if (contentView.getPaddingBottom() != 0) {
//reset the padding of the contentView
contentView.setPadding(0, 0, 0, 0);
}
}
}
};
public KeyboardUtil(Activity act, View contentView) {
this.decorView = act.getWindow().getDecorView();
this.contentView = contentView;
//only required on newer android versions. it was working on API level 19
if (Build.VERSION.SDK_INT >= 19) {
decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
}
}
/**
* Helper to hide the keyboard
*
* #param act
*/
public static void hideKeyboard(Activity act) {
if (act != null && act.getCurrentFocus() != null) {
InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(act.getCurrentFocus().getWindowToken(), 0);
}
}
public void enable() {
if (Build.VERSION.SDK_INT >= 19) {
decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
}
}
public void disable() {
if (Build.VERSION.SDK_INT >= 19) {
decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
}
}
}
See https://stackoverflow.com/a/61813321/2914140:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
return dialog
}
But if a layout is not tall enough, you can use https://stackoverflow.com/a/66287187/2914140 instead. It will open BottomSheetDialog almost fullscreen:
<style name="BottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:windowSoftInputMode">adjustResize|stateVisible</item>
</style>
Fiddling with BottomSheetDialogFragmentwasn't really worth it. So I just changed it to a simple DialogFragment and just set its gravity to bottom:
window.setGravity(Gravity.BOTTOM);
Worked like a charm.
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 am wondering how it is possible to get rid of (or change color) titleDivider in Dialog. It is a blue line below dialog title shown on honeycomb+ devices.
I guess this is relevant piece of layout from SDK, but since there is no style attribute I dont know how to style it. If i try with findViewById there is no android.R.id.titleDivider
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:fitsSystemWindows="true">
<TextView android:id="#android:id/title" style="?android:attr/windowTitleStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="#android:dimen/alert_dialog_title_height"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:gravity="center_vertical|left" />
<View android:id="#+id/titleDivider"
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#android:color/holo_blue_light" />
<FrameLayout
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:foreground="?android:attr/windowContentOverlay">
<FrameLayout android:id="#android:id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
I have tried to override dialogTitleDecorLayout which is only reference to dialog_title_holo.xml in my theme.xml, but without success. Error is:
error: Error: No resource found that matches the given name: attr
'dialogTitleDecorLayout'.
To get a reference to titleDivider of AlertDialog to change its color:
int divierId = dialog.getContext().getResources()
.getIdentifier("android:id/titleDivider", null, null);
View divider = dialog.findViewById(divierId);
divider.setBackgroundColor(getResources().getColor(R.color.creamcolor));
You need to implement
myDialog = builder.create();
myDialog.setOnShowListener(new OnShowListenerMultiple());
//----------------------------
//Function to change the color of title and divider of AlertDialog
public static class OnShowListenerMultiple implements DialogInterface.OnShowListener {
#Override
public void onShow( DialogInterface dialog ) {
if( !(dialog instanceof Dialog) )
return;
Dialog d = ((Dialog) dialog);
final Resources resources = d.getContext().getResources();
final int color = AppUtility.getColor( resources, R.color.defaultColor );
try {
int titleId = resources.getIdentifier( "android:id/alertTitle", null, null );
TextView titleView = d.findViewById( titleId );
titleView.setTextColor( color );
}
catch( Exception e ) {
Log.e( "XXXXXX", "alertTitle could not change color" );
}
try {
int divierId = resources.getIdentifier( "android:id/titleDivider", null, null );
View divider = d.findViewById( divierId );
divider.setBackgroundColor( color );
}
catch( Exception e ) {
Log.e( "XXXXXX", "titleDivider could not change color" );
}
}
}
I solved the issue by using DialogFragment.STYLE_NO_TITLE theme and then faking title bar in dialog layout.
Here is how I resolved that (thanks to http://joerg-richter.fuyosoft.com/?p=181 ):
MyDialogBuilder.class
public class MyDialogBuilder extends android.app.AlertDialog.Builder {
public MyDialogBuilder(Context context) {
super(context);
}
#NonNull
#Override
public android.app.AlertDialog create() {
final android.app.AlertDialog alertDialog = super.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
int titleDividerId = getContext().getResources()
.getIdentifier("titleDivider", "id", "android");
View titleDivider = alertDialog.findViewById(titleDividerId);
if (titleDivider != null) {
titleDivider.setBackgroundColor(getContext().getResources()
.getColor(R.color.alert_dialog_divider));
}
}
});
return alertDialog;
}
}
use
<View android:id="#+id/titleDivider"
android:layout_width="match_parent"
android:layout_height="2dip"
android:background=#CC3232 />
Before write dialog.show(), write:
int divierId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = dialog.findViewById(divierId);
if(divider!=null){
divider.setBackgroundColor(getResources().getColor(R.color.transparent));}
In colors.xml:
<color name="transparent">#00000000</color>
If you don't want to use Default style, don't use AlertDialog. You could go with Activity(with your custom layout) with Dialog Theme.
<activity android:theme="#android:style/Theme.Dialog">
This one is tested on some 4.x devices:
TextView title = (TextView)getWindow().getDecorView().findViewById(android.R.id.title);
((ViewGroup)title.getParent()).getChildAt(1).setVisibility(View.GONE);
Your idea was correct. However, dialogTitleDecorLayout you were looking for is a private resource, so you can't access it in a normal way. But you still can access it using * syntax:
<item name="*android:dialogTitleDecorLayout">#layout/dialog_title</item>
Adding this to my own style and simply copying dialog_title.xml to my app and changing it slightly solved the problem in my case.
Do you watchthis and there is a pcecial library for that, you can watch it there. And the last link will solve you problem
you can make a custom dialog like this:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
Button okay = (Button) dialog.findViewById(R.id.button1);
okay.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// do your work
}
});
Set a custom title in layout don't use android
dialog.setTitle();
and your custom_dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="40sp"
android:text="Hello"/>
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:text="OK" />
</RelativeLayout>
"Removing the blue line" if I guess correctly means dropping the border between the title of the dialog and it's body. That border come from the Holo theme, so it's not possible to drop it without using your custom layout.
Create a file named custom-dialog.xml with the following content (it's just an example..modify it as you want):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/general_dialog_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/dialogTopImage"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.12"
android:padding="10dp" />
<LinearLayout
android:id="#+id/dialogLine"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#drawable/green_btn"
android:orientation="vertical" />
<TextView
android:id="#+id/dialogText"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.32"
android:padding="5dp"
android:text=""
/>
<LinearLayout
android:id="#+id/general_dialog_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_weight="0.11"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/dialogButton"
android:layout_width="100dp"
android:textSize="8pt"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#drawable/green_btn"
android:gravity="center"
android:text="Ok" />
</LinearLayout>
As you see I'm using resources and stuff that won't be in your project, but you can remove them safely. The result in my case is more or less the following one, with an image at top that I'll programatically set in the code.
To create the dialog then use something like:
private Dialog createAndShowCustomDialog(String message, Boolean positive, Drawable d, View.OnClickListener cl, String text1) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.general_dialog_layout);
// BIND
ImageView image = (ImageView) dialog.findViewById(R.id.dialogTopImage);
TextView text = (TextView) dialog.findViewById(R.id.dialogText);
Button button = (Button) dialog.findViewById(R.id.dialogButton);
LinearLayout line = (LinearLayout) dialog.findViewById(R.id.dialogLine);
// SET WIDTH AND HEIGHT
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = (int) (displaymetrics.widthPixels * 0.85);
int height = (int) (displaymetrics.heightPixels * 0.60);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = width;
dialog.getWindow().setLayout(width, height);
// SET TEXTS
text.setText(message);
button.setText(text1);
// SET IMAGE
if (d == null) {
image.setImageDrawable(getResources().getDrawable(R.drawable.font_error_red));
} else {
image.setImageDrawable(d);
}
// SET ACTION
if (cl == null) {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
} else {
button.setOnClickListener(cl);
}
// SHOW
dialog.show();
return dialog;
}
These is no way hiding it by control brotha.. I've had the same problem. only thing you can do is create your own CustomDialog
Here is a sample App
Download and have look at the design pattern, then it will be easy
Here is one Tutorial About making Custom Dialog
Important part is after creating the DialogObject don't set the Title by setTitle()
create TextView inside your CustomLayout and call it from findViewByID() and set your title
In colors.xml:
<color name="transparent">#00000000</color>
In dialog:
int divierId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider",null, null);
View divider = d.findViewById(divierId);
divider.setBackgroundColor(getResources().getColor(R.color.transparent));
In order to hide the default blue line completely (assuming you're in DialogFragment):
Dialog dialog = getDialog();
if (dialog != null) {
final int dividerId = dialog.getContext().getResources()
.getIdentifier("android:id/titleDivider", null, null);
View divider = dialog.findViewById(dividerId);
if (divider != null) {
divider.setBackground(null);
}
}