Customizing dialog-----Activity

Source: Internet
Author: User

Android comes with the dialog style generally does not meet their own requirements, that requires dialog customization.

In fact, the activity can also exist in the form of dialog, we only need to set its theme property in the manifest file:

<activity
Android:name= ". Editactivity "
Android:theme= "@android: Style/theme.dialog"
>
</activity>

The general Activity form of the dialog, touch its outside will be dismiss, then, how can we prevent it from being dismiss away?

To achieve this, you actually need only one code:

Setfinishontouchoutside (False), which is a bit like Popupwindow's setoutsidetouchable (false);

To press the return key and not disappear, simply mask the return key or override the Backpress method

@Override
public boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back) {
return true;
}
Return Super.onkeydown (KeyCode, event);
}


@Override
public void onbackpressed () {
By default, the finish () method is called

Super.onbackpressed ();
}

This is how the activity was supposed to be implemented.

public void onbackpressed () {

if (!mfragments.popbackstackimmediate ()) {
Finish ();
}
}

This is not enough, the activity in the form of dialog display, the width will be relatively small, which requires us to give the width:

WindowManager manager = Getwindowmanager ();
Displaymetrics displaymetrics = new Displaymetrics ();
Display display = Manager.getdefaultdisplay ();
Display.getmetrics (Displaymetrics);
int width = displaymetrics.widthpixels;
int height = displaymetrics.heightpixels;
Windowmanager.layoutparams params = GetWindow (). GetAttributes ();
Params.width = (int) (width*0.8);
GetWindow (). SetAttributes (params);

So we set the width of the activity.


Customizing dialog-----Activity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.