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.
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 have been struggling to put this to work. The app resizes well as long as i keep the status bar visible. According to this post it is an android bug, but the issue is closed but not resolved.
From all that i read, playing with Insets should do the trick but i can't put it to work. Not sure even if should go this way.
Following this post i added the below code to OnCreate.
ViewCompat.setOnApplyWindowInsetsListener(getWindow().getDecorView().getRootView(), new OnApplyWindowInsetsListener() {
#Override
public WindowInsetsCompat onApplyWindowInsets(View view, WindowInsetsCompat insets) {
((ViewGroup.MarginLayoutParams) view.getLayoutParams()).topMargin =
insets.getSystemWindowInsetTop();
return insets.consumeSystemWindowInsets();
}
});
It throws the exception:
java.lang.ClassCastException: android.view.WindowManager$LayoutParams
cannot be cast to android.view.ViewGroup$MarginLayoutParams
The post goes on but i am stuck here and have doubts if i am going the right way.
Bellow the testing code i am using if someone wants to try.
Works well until the statusbar is removed by adding <item name="android:windowFullscreen">true</item> to AppTheme.
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
activity_mail.xml:
<!-- this control should be position static -->
<TextView
android:layout_height="50dp"
android:layout_width="fill_parent"
android:background="#color/colorPrimaryDark"
android:text="static window at top"
android:gravity="center"
/>
<!-- the resizable view -->
<android.support.v7.widget.RecyclerView
android:id="#+id/my_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:transcriptMode="normal">
</android.support.v7.widget.RecyclerView>
<!-- use this EditText box to call softKeyboard-->
<android.support.v7.widget.AppCompatEditText
android:id="#+id/cv_bottom_bar_edit"
style="#style/Widget.AppCompat.AutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="18sp"
android:background="#color/colorPrimaryDark"
android:gravity="center"
/>
row.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="wrap_content"
android:background="#F06060"
android:layout_margin="30dp"
android:padding="5dp"
android:textSize="25sp"
android:gravity="center"
>
MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.my_list);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
String[] myDataset = new String[]{"1", "2", "3","4","5","6","7","8","9"};
mAdapter = new MyAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
}
Update:
I moved the windowFullscreen attribute from manifest to AppTheme because Android was not reading it from the manifest.
public class AndroidBug5497Workaround extends AppCompatActivity {
// For more information, see https://issuetracker.google.com/issues/36911528
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
private ViewGroup contentContainer;
private ViewTreeObserver viewTreeObserver;
private Rect contentAreaOfWindowBounds = new Rect();
private LinearLayout.LayoutParams rootViewLayout; //-->change to the view root control type of your view.
private int usableHeightPrevious = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contentContainer = findViewById(android.R.id.content);
rootViewLayout = (LinearLayout.LayoutParams) contentContainer.getLayoutParams();
}
#Override
protected void onPause() {
super.onPause();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.removeOnGlobalLayoutListener(listener);
}
}
#Override
protected void onResume() {
super.onResume();
if (viewTreeObserver == null || !viewTreeObserver.isAlive()) {
viewTreeObserver = contentContainer.getViewTreeObserver();
}
viewTreeObserver.addOnGlobalLayoutListener(listener);
}
#Override
protected void onDestroy() {
super.onDestroy();
contentContainer = null;
viewTreeObserver = null;
}
ViewTreeObserver.OnGlobalLayoutListener listener = new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
contentContainer.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds);
int usableHeightNow = contentAreaOfWindowBounds.height();
if (usableHeightNow != usableHeightPrevious) {
rootViewLayout.height = usableHeightNow;
contentContainer.layout(contentAreaOfWindowBounds.left, contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom);
contentContainer.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
};
}
Got it from facebook/react-native issue on github. Slight simplified for a common activity. Works good with portrait and landscape, from api 21, but it not much more tested than that. Use with care.
Notice this line:
((ViewGroup.MarginLayoutParams) view.getLayoutParams()).topMargin =
insets.getSystemWindowInsetTop();//what's the parent of "view"
According to getLayoutParams says, you should know what's the parent of "view" ,for example, if the parent is FrameLayout, then do like this:
((FrameLayout.LayoutParams) view.getLayoutParams()).topMargin =
insets.getSystemWindowInsetTop();
If the parent is relativelayout or linearlayout, also switch to definite type like above.
Edited: try this:
((WindowManager.LayoutParams) view.getLayoutParams()).topMargin =
insets.getSystemWindowInsetTop();
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");
}
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);
}
}
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);