My idea was to have a simple layout for a normal notification and a dialog control for the pop up with animation. Since the pop up needs to be at the point on which the user clicks, I wanted to write a custom DialogueView extending the Dialog class, within which I determine the anchor point.
This is what I have done so far:
Design of the layout without the pop up:
pre_session_view_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:verizon="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/preSessionLayout">
<TextView
android:id="#id/verizon_sd_caption"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/verizon_sd_empty_view"
android:layout_toRightOf="#id/verizon_sd_icon"
android:gravity="center|center_horizontal"
android:paddingLeft="5dp"
android:paddingRight="10dp"
android:textColor="#android:color/white"
android:textSize="18sp"></TextView>
<demo.notification.verizon.com.notificationdemo.ResizableImageView
android:id="#id/verizon_sd_icon"
android:layout_width="36dp"
android:layout_height="34dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:scaleType="centerInside" />
<!--android:src="#drawable/_fw_prenotify" -->
<ImageView
android:id="#id/verizon_sd_empty_view"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="invisible"
/>
</RelativeLayout>
The design of the overlay:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="240dp"
android:layout_height="110dp"
android:orientation="vertical"
android:id="#+id/overlayLayout">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/notification_bg_green"/>
</RelativeLayout>
PressionView.java: ( Java file for showing the normal and pop up notification)
I am only giving the code snippet for showing the pop up as an overlay.This function gets called when the user clicks on the bee icon.
private void showOverLay(){
final ConfirmBox dialog = new ConfirmBox(this.bannerThumb);
LayoutInflater inflater = LayoutInflater.from(ctx);
dialog.onCreateView(inflater,this.relLayout,null);
}
Finally the custom Dialogue is as follows:
public class ConfirmBox extends DialogFragment {
private View source;
public ConfirmBox() {
}
public ConfirmBox(View source) {
this.source = source;
}
public static ConfirmBox newInstance(View source) {
return new ConfirmBox(source);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_FRAME, R.style.AppTheme);
}
#Override
public void onStart() {
super.onStart();
// Less dimmed background; see http://stackoverflow.com/q/13822842/56285
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.dimAmount = 0.2f; // dim only a little bit
window.setAttributes(params);
// Transparent background; see http://stackoverflow.com/q/15007272/56285
// (Needed to make dialog's alpha shadow look good)
window.setBackgroundDrawableResource(android.R.color.transparent);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Put your dialog layout in R.layout.view_confirm_box
View view = inflater.inflate(R.layout.overlay_view, container, false);
// Initialise what you need; set e.g. button texts and listeners, etc.
// ...
setDialogPosition();
return view;
}
/**
* Try to position this dialog next to "source" view
*/
private void setDialogPosition() {
if (source == null) {
return; // Leave the dialog in default position
}
// Find out location of source component on screen
// see http://stackoverflow.com/a/6798093/56285
int[] location = new int[2];
source.getLocationOnScreen(location);
int sourceX = location[0];
int sourceY = location[1];
Window window = getDialog().getWindow();
// set "origin" to top left corner
window.setGravity(Gravity.TOP| Gravity.LEFT);
WindowManager.LayoutParams params = window.getAttributes();
// Just an example; edit to suit your needs.
params.x = sourceX - dpToPx(110); // about half of confirm button size left of source view
params.y = sourceY - dpToPx(80); // above source view
window.setAttributes(params);
}
public int dpToPx(float valueInDp) {
DisplayMetrics metrics = getActivity().getResources().getDisplayMetrics();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
}
}
I can show the normal notification. But for the pop up , the approach which I have taken,I get a null pointer exception in the showOverLay() method.
More specifically on the line:
Window window = getDialog().getWindow();
within the getDialoguePosition() of the custom Dialogueclass.
Can someone kindly help me on this? If reqd I can share the full code for the PresessionView.java file as well.
I think I am making some mistake in calling the custom Dialogue class.
Thanks.
You show the dialog fragment incorrect.
Please use dialog.show(...). Also please ensure that you're bannerThumb, ctx, and relLayout are not null.
private void showOverLay(){
final ConfirmBox dialog = new ConfirmBox(this.bannerThumb);
LayoutInflater inflater = LayoutInflater.from(ctx); //Ensure ctx is not NULL
dialog.show(getSupportFragmentManager(), "yourtitle");
}
Related
I have a DialogFragment which consists of three parts, from up to down: the title, the central view which displays all the contents, and the bottom pane which holds the PositiveButton "OK":
public Dialog onCreateDialog(Bundle savedInstanceState)
{
FragmentActivity act = getActivity();
LayoutInflater inflater = act.getLayoutInflater();
AlertDialog.Builder builder = new AlertDialog.Builder(act);
// TITLE:
TextView title = (TextView) inflater.inflate(R.layout.dialog_title, null);
title.setText(R.string.updates);
builder.setCustomTitle(title);
// CENTRAL VIEW:
View view = inflater.inflate(R.layout.dialog_updates, null);
// ... customize it ...
builder.setView(view);
// POSITIVE BUTTON:
builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
// something
}
});
}
The stuff that's shown by the central view is downloaded from the web. Initially, when a user pops up the dialog, the View shows just the "Downloading..." message:
When we get an answer, we create a ScrollView and keep adding vertically scrollable Panes to it like so:
(image above shows three such panes added so far)
The result is that the height of the dialog keeps changing, which is visually unpleasant.
So I really want to keep the height of the whole Dialog constant, let's say pinned to 3/4 of the height of the screen. Let's do it then:
public void onResume()
{
super.onResume();
Window window = getDialog().getWindow();
Context context = getContext();
if( window!=null && context!=null )
{
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
final float height= metrics.heightPixels;
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = (int)(0.75f*height);
window.setAttributes(params);
}
}
Result:
This does kind of work, as you can see though - it works by enlarging the lower pane with the 'OK' button, rather than the central View.
How to fix this?
EDIT: here's my dialog_title.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:gravity="center"
android:padding="10dp"/>
One workaround for this issue is to use ConstrainedLayout for your whole dialog like this:
fragment_dialog 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="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="Updates"
android:textSize="20sp"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/central_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Downloading"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="#id/positive_action"
app:layout_constraintHeight_percent="0.8"
app:layout_constraintTop_toBottomOf="#id/title" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/positive_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="8dp"
android:text="OK"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
You can change the percentage of your central view with app:layout_constraintHeight_percent="0.8"
DialogFragment class:
public class LoadingDialog extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
FragmentActivity act = getActivity();
LayoutInflater inflater = act.getLayoutInflater();
View view = inflater.inflate(R.layout.fragment_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(act).setView(view);
// POSITIVE BUTTON:
view.findViewById(R.id.positive_action).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//something
}
});
return builder.create();
}
#Override
public void onResume() {
super.onResume();
getDialog().getWindow().setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
}
}
And you will get this result:
I want to try BottomSheetDialog introduced in Android Support Library 23.2 but it doesn't seem to work correctly. Here is what the doc says:
While BottomSheetBehavior captures the persistent bottom sheet case, this release also provides a BottomSheetDialog and
BottomSheetDialogFragment to fill the modal bottom sheets use case.
Simply replace AppCompatDialog or AppCompatDialogFragment with their
bottom sheet equivalents to have your dialog styled as a bottom
sheet."
So I changed my AppCompatDialog to BottomSheetDialog:
package my.package.ui.dialog;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import my.package.R;
public class AccountActionsDialog extends BottomSheetDialog {
public AccountActionsDialog(Context context) {
super(context);
if (context instanceof Activity) {
setOwnerActivity((Activity) context);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutInflater().inflate(R.layout.dialog_account_actions, null));
}
}
Here is my layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:padding="16dp"
android:text="Delete account"
android:textColor="#ffffff" />
</LinearLayout>
Then I use the following code in my Activity:
new AccountActionsDialog(this).show();
My screen becomes dimmed but the content of my dialog is not visible. Any thoughts on what might be missing? It works fine when I use AppCompatDialog instead.
Instead of having a separate class, you can simply create an instance for BottomSheetDialog in your Activity/Fragment like following and you can use it. It is very easier and simpler I think.
val dialog = BottomSheetDialog(this)
val bottomSheet = layoutInflater.inflate(R.layout.bottom_sheet, null)
bottomSheet.buttonSubmit.setOnClickListener { dialog.dismiss() }
dialog.setContentView(bottomSheet)
dialog.show()
This is the layout file of BottomSheetDialog.
<android.support.design.widget.CoordinatorLayout
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="match_parent"
android:soundEffectsEnabled="false">
<FrameLayout
android:id="#+id/design_bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:layout_behavior="#string/bottom_sheet_behavior"
style="?attr/bottomSheetStyle"/>
</android.support.design.widget.CoordinatorLayout>
Your content view is inside the view design_bottom_sheet, it will be positioned center vertically by CoordinatorLayout, and BottomSheetBehavior will offset it.
mParentHeight = parent.getHeight();
mMinOffset = Math.max(0, mParentHeight - child.getHeight());
mMaxOffset = mParentHeight - mPeekHeight;
if (mState == STATE_EXPANDED) {
ViewCompat.offsetTopAndBottom(child, mMinOffset);
} else if (mHideable && mState == STATE_HIDDEN) {
ViewCompat.offsetTopAndBottom(child, mParentHeight);
} else if (mState == STATE_COLLAPSED) {
ViewCompat.offsetTopAndBottom(child, mMaxOffset);
}
It intented to positon design_bottom_sheet at mMaxOffset, but actually the initial getTop of the child view is not 0, but (mParentHeight - childHeight) / 2, so you view if offset more than the desired offset.
Find the view design_bottom_sheet and set its gravity to Gravity.TOP | Gravity.CENTER_HORIZONTAL will fix it. But, if the childHeight is less than mPeekHeight, there will be blank area below you content view.
However, if peekHeight > childHeight, the mMaxOffset will less than mMinOffset, which will cause weird behavior.
Maybe the code should be changed to
mMaxOffset = Math.max((mParentHeight - mPeekHeight), mMinOffset);
insted of
mMaxOffset = mParentHeight - child.getHeight();
Here's the issue on code.google.com https://code.google.com/p/android/issues/detail?id=201793
An issue some users are seeing boils down to the FrameLayout that wraps our content view being centered vertically. The BottomSheetBehavior only works if this view is top aligned. I haven't figured out what causes the FrameLayout to become centered vertically yet, but here's a possible workaround:
View contentView = ...
// You may have to measure your content view first.
dialog.setContentView(contentView);
// Change this to a percentage or a constant, whatever you want to do.
// The default is 1024 - any views smaller than this will be pulled off
// the bottom of the screen.
float peekHeight = contentView.getMeasuredHeight();
View parent = (View)contentView.getParent();
BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
behavior.setPeekHeight(peekHeight);
CoordinatorLayout.LayoutParams layoutParams =
(CoordinatorLayout.LayoutParams)parent.getLayoutParams();
layoutParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
I was expriencing the same issue, dimmed background and content not visible. Here is how I managed to workaround it by setting the content view in setupDialog() hidden method.
public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
private TextView mOffsetText;
private TextView mStateText;
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
setStateText(newState);
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
setOffsetText(slideOffset);
}
};
private LinearLayoutManager mLinearLayoutManager;
private ApplicationAdapter mAdapter;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return super.onCreateDialog(savedInstanceState);
}
#Override
public void onViewCreated(View contentView, #Nullable Bundle savedInstanceState) {
super.onViewCreated(contentView, savedInstanceState);
}
#Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.bottom_sheet_dialog_content_view, null);
dialog.setContentView(contentView);
mBottomSheetBehavior = BottomSheetBehavior.from(((View) contentView.getParent()));
if (mBottomSheetBehavior != null) {
mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
}
mOffsetText = (TextView) contentView.findViewById(R.id.offsetText);
mStateText = (TextView) contentView.findViewById(R.id.stateText);
}
}
And the layout:
<?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">
<TextView
android:id="#+id/offsetText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/stateText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
It started to work when I set fixed height for my TextView (200dp), although for some height values it still behaves incorrectly. Obviously it's an issue of support lib. There are already few reports related to BottomSheetDialog in the bug tracker:
https://code.google.com/p/android/issues/detail?id=201793&sort=-opened&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened
https://code.google.com/p/android/issues/detail?id=201826
I have problem with DialogFragmnt's Width and Height. Here is my class representing DialogFragmetn:
public class RecipeAddDialogFragment extends DialogFragment {
private ArrayList<RecipeDialogItem> recipeDialogItems;
private RecipeAddDialogAdapter recipeDialogAdapter;
private String recipeUniqueId;
private CoordinatorLayout coordinatorLayout;
private RecipeAddDialogFragment recipeDialogFragment;
#Override
public void onStart() {
super.onStart();
if (getDialog() == null) {
return;
}
int dialogWidth = 600;
int dialogHeight = 300;
getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
getDialog().setTitle(getString(R.string.recipe_dialog_title));
}
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme_DialogFragment);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_fragment, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
recipeDialogItems = new ArrayList<>();
RecyclerView dialogRecyclerView = (RecyclerView) view.findViewById(
R.id.dialog_recycler_view);
recipeDialogAdapter = new RecipeAddDialogAdapter(getContext(), recipeDialogItems,
R.layout.recipe_dialog_item);
recipeDialogAdapter.setRuidClRdf(recipeUniqueId, coordinatorLayout, recipeDialogFragment);
dialogRecyclerView.setHasFixedSize(true);
dialogRecyclerView.setAdapter(recipeDialogAdapter);
dialogRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
fillRecipeDialogArray();
}
private void fillRecipeDialogArray() {
String name = getString(R.string.add_to_favourites);
int icon = R.drawable.ic_heart_48dp;;
RecipeDialogItem dialogItem = new RecipeDialogItem();
dialogItem.setRowIcon(icon);
dialogItem.setRowOption(name);
recipeDialogItems.add(dialogItem);
recipeDialogAdapter.notifyDataSetChanged();
}
public void setReferences(String recipeUniqueId, CoordinatorLayout coordinatorLayout,
RecipeAddDialogFragment recipeDialogFragment) {
this.recipeUniqueId = recipeUniqueId;
this.coordinatorLayout = coordinatorLayout;
this.recipeDialogFragment = recipeDialogFragment;
}
}
Here is .xml which I infalte in this DialogFragment:
<?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:gravity="center|left"
android:padding="16dp"
android:orientation="horizontal"
android:clickable="true"
android:background="?android:attr/selectableItemBackground">
<!-- Option Icon -->
<ImageView
android:id="#+id/recipe_dialog_option_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:tint="#color/primary" />
<!-- Text Option -->
<TextView
android:id="#+id/recipe_dialog_option_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#color/primary_text" />
</LinearLayout>
The problem is that when I set it's size to 600 x 300 it is displayed fine in my 1280x720 device, but for example when my friend displays it on 1920x1080 resolution dialog is wrapped and only title is shown. List is wrapped and is not entire shown. Any idea how can I automaticly set it's size to fit every display and show entire dialog which is wrapped to it's content?
Edit
I have figured out to adjust the width of the DialogFragment to it's content like this:
getDialog().getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
getDialog().setTitle(getString(R.string.recipe_dialog_title));
However height is not working properly :/
In DialogFragment.onResume():
int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height = getResources().getDimensionPixelSize(R.dimen.popup_height);
getDialog().getWindow().setLayout(width, height);
In the layout for the dialog:
android:layout_width="match_parent"
android:layout_height="match_parent"
Can take whole screen with:
getDialog().getWindow().setLayout(
getResources().getDisplayMetrics().widthPixels,
getResources().getDisplayMetrics().heightPixels
);
Hope that helps
Found the idea here How to set DialogFragment's width and height?
Below is the Gmail No Connection alert. How would I go about achieveing this in my app?
Note: this is a question regarding the UI implementation, and not determining whether there is a connection.
EDIT::
Is the bottom notification an anctivity themed as a dialog and placed at the bottom or is it some sort of AlertDialog?
maybe that example can help you https://github.com/DanielRasta/Material-Design-Alert-Box-
EDIT 2 :
Material Design have Snackbar class too
Edit 1 :
Well, the idea is to call dialog activity in front. So to do that :
1 - Create alertbox_activity.xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#8A8A8A">
<TextView
android:id="#+id/alertbox_message"
android:layout_width="match_parent"
android:paddingTop="10dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:background="#8A8A8A"
android:textSize="20sp"
android:typeface="sans"
android:gravity="center" />
</LinearLayout>
2 - Create an activity called AlertBoxActivity.java and paste this code
public class AlertBoxActivity extends Activity {
// ===========================================================
// Constants
// ===========================================================
private static final int EXIT_DELAY= 3500;
public static final String KEY_BUNDLE_MESSAGE_TEXT = "AlertBoxActivity::message_text";
public static final String KEY_BUNDLE_PARENT_WIDTH = "AlertBoxActivity::parent_width";
private static final String ERROR_GEN = "Oops.. Something went wrong.";
private Runnable Exit(){
return new Runnable(){
#Override
public void run() {
finish();
}
};
}
// ===========================================================
// Methods for/from super class
// ===========================================================
#Override
public void onCreate(Bundle pBundle) {
super.onCreate(pBundle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.alertbox_activity);
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
TextView textViewMessage = (TextView) findViewById(R.id.alertbox_message);
String strNotificationToShow = ERROR_GEN;
int myParentWidth = getWindow().getAttributes().width;
try {
Bundle extras = getIntent().getExtras();
if(extras != null) {
strNotificationToShow = extras.get(KEY_BUNDLE_MESSAGE_TEXT).toString();
myParentWidth = extras.getInt(KEY_BUNDLE_PARENT_WIDTH);
}
else{
myParentWidth = getWindow().getAttributes().width;
}
}
catch(Exception e) {
e.printStackTrace();
strNotificationToShow = ERROR_GEN;
myParentWidth = getWindow().getAttributes().width;
}
finally{
textViewMessage.setText(strNotificationToShow);
}
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = myParentWidth;
params.gravity = Gravity.BOTTOM;
getWindow().setAttributes(params);
Window window = getWindow();
window.setAttributes((android.view.WindowManager.LayoutParams) params);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
new Handler().postDelayed(Exit(), EXIT_DELAY);
}
}
3 - Add these lines to your manifest.xml
<activity
android:name="com.package.AlertBoxActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Dialog">
<intent-filter>
<action android:name="android.intent.action.ALERT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
4 - Call it
Intent i = new Intent("android.intent.action.ALERT");
i.putExtra(AlertBoxActivity.KEY_BUNDLE_MESSAGE_TEXT, "YOUR ALERT GOES HERE!");
i.putExtra(AlertBoxActivity.KEY_BUNDLE_PARENT_WIDTH, getWindow().getAttributes().width);
startActivity(i);
Done.
I am not sure if this is how Google implement their dialog but here is my Go around. I created a class that will be styled as a dialog in the manifest. So when I need to show the alert I simply start the Intent and its diaplayed. In the Activity I have a PostDelayed Handler taht will wait for 3.5 seconds and end the activity to give a toast like animation (similar to Gmail :-) )
Find Code Below
static final int EXIT_DELAY= 3500;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.layout_pesabox_alert);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.x = 5;
params.height = 150;
params.width = 700;
params.y = 5;
params.gravity = Gravity.BOTTOM;
this.getWindow().setAttributes(params);
Window window = this.getWindow();
window.setAttributes((android.view.WindowManager.LayoutParams) params);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
new Handler().postDelayed(Exit(), EXIT_DELAY);
}
private Runnable Exit(){
return new Runnable(){
#Override
public void run() {
finish();
}
};
}
The Layout
<?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="horizontal" >
<TextView
android:id="#+id/detailsText"
android:layout_width="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:layout_height="wrap_content"
android:drawableLeft="#android:drawable/ic_dialog_alert"
android:drawablePadding="25dp"
android:text="#string/connection"
android:textColor="#ffffff"
android:textSize="25sp"
android:typeface="sans" />
</LinearLayout>
A really old post but if anyone else is looking for google like "Bottom Toast like Notification". Refer to Snackbar Material Design. Its also available in the support library. How to use Snackbar with backward compatibility is pretty well explained in this blog.
I am using a XML-layout which I am prompting as the dialog box.
Designing of XML-layout is well formatted with enough required height and width..
But when I open it as the dialog box its width is getting disturbed so how to set height and width of dialog box through coding.
I even had referred this previous STACK OVERFLOW QUESTION
Here is the code:
// Layout Inflater Code..
editDialog = new Dialog(this);
layoutEdit = LayoutInflater.from(this).inflate(R.layout.createlayout, null);
//layoutEdit.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
editDialog.setContentView(layoutEdit);
// Called the Dialogbox to inflate
updateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
editDialog.show();
}
});
// XML File Code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bd"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="false"
android:text="Enter Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/whtie"
android:typeface="monospace" />
<EditText
android:id="#+id/txtname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
</EditText>
</LinearLayout>
</ScrollView>
Try this...
1.Dialog snippet:
private void CustomDialog(String msg) {
final Dialog dialog = new Dialog(YourActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LinearLayout.LayoutParams dialogParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 300);//set height(300) and width(match_parent) here, ie (width,height)
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dislogView = inflater
.inflate(R.layout.my_custom_popup, null);
dialog.setContentView(dislogView, dialogParams);
TextView popupMsg = (TextView) dialog.findViewById(R.id.popupMsg);
Button popupOk = (Button) dialog.findViewById(R.id.popupOk);
popupMsg.setText(msg);
popupOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
2.Then call CustomDialog(Str) where you want to prompt in your activity.
CustomDialog("This is customized popup dialog!");
You better use an activity that looks like a dialog (I feel it will be better in your case). Here is an example code:
public class DialogActivity extends Activity {
/**
* Initialization of the Activity after it is first created. Must at least
* call {#link android.app.Activity#setContentView setContentView()} to
* describe what is to be displayed in the screen.
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
// Be sure to call the super class.
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
// See assets/res/any/layout/dialog_activity.xml for this
// view layout definition, which is being set here as
// the content of our screen.
setContentView(R.layout.dialog_activity);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
android.R.drawable.ic_dialog_alert);
}
}
This code is from api demos
View layout = inflater.inflate(R.layout.view, NULL);
layout.setMinimumWidth(200);
layout.setMinimumHeight(200);
dialog.setContentView(layout);
Try
dialog.getWindow().setLayout(height, width);