How to Implement pop-up menu layer and PopupWindow on the left of android popwindow

Source: Internet
Author: User

PopupWindow can achieve the floating layer effect. The main methods are: You can customize the view, use the LayoutInflator method; you can display the animation when it appears or exits; you can specify the display position.

In order to present multiple functions of PopupWindow and implement them with simple code, a menu function pops up on the left of the click button is compiled, when the animation effect is displayed and exited, And the pop-up layer automatically disappears when you click another area, as shown below:
Source code:
1. PopwindowOnLeftActivity. java

Copy codeThe Code is as follows: package com. pop. main;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. MotionEvent;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. View. OnTouchListener;
Import android. widget. Button;
Import android. widget. PopupWindow;
Public class PopwindowOnLeftActivity extends Activity {
// Declare the reference of the PopupWindow object
Private PopupWindow popupWindow;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Click the button to bring up the menu
Button pop = (Button) findViewById (R. id. popBtn );
Pop. setOnClickListener (popClick );
}
// Click the display method in the left-side menu.
OnClickListener popClick = new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
GetPopupWindow ();
// Here is the position display mode, in the lower left corner of the button
PopupWindow. showAsDropDown (v );
// Try other effects here, such as popupWindow. showAsDropDown (v,
// (ScreenWidth-dialgoWidth)/2, 0 );
// PopupWindow. showAtLocation (findViewById (R. id. layout ),
// Gravity. CENTER, 0, 0 );
}
};
/**
* Create a PopupWindow
*/
Protected void initPopuptWindow (){
// TODO Auto-generated method stub
// Obtain the pop. xml view of the custom Layout File
View popupWindow_view = getLayoutInflater (). inflate (R. layout. pop, null,
False );
// Create a PopupWindow instance. 200,150 is the width and height respectively.
PopupWindow = new PopupWindow (popupWindow_view, 200,150, true );
// Set the animation effect
PopupWindow. setAnimationStyle (R. style. AnimationFade );
// Click somewhere else to disappear
PopupWindow_view.setOnTouchListener (new OnTouchListener (){
@ Override
Public boolean onTouch (View v, MotionEvent event ){
// TODO Auto-generated method stub
If (popupWindow! = Null & popupWindow. isShowing ()){
PopupWindow. dismiss ();
PopupWindow = null;
}
Return false;
}
});
// Controls in the pop. xml View
Button open = (Button) popupWindow_view.findViewById (R. id. open );
Button save = (Button) popupWindow_view.findViewById (R. id. save );
Button close = (Button) popupWindow_view.findViewById (R. id. close );
// Events triggered by controls in the pop. xml View
// Open
Open. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Perform the following operations:
System. out. println ("open operation ");
// The dialog box disappears.
PopupWindow. dismiss ();
}
});
// Save
Save. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Perform the following operations:
System. out. println ("save operation ");
PopupWindow. dismiss ();
}
});
// Close
Close. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Perform the following operations:
System. out. println ("close operation ");
PopupWindow. dismiss ();
}
});
}
/***
* Obtain a PopupWindow instance
*/
Private void getPopupWindow (){
If (null! = PopupWindow ){
PopupWindow. dismiss ();
Return;
} Else {
InitPopuptWindow ();
}
}
}

Main Interface
2. main. xml Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<Button android: id = "@ + id/popBtn"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/pop_left"/>
</LinearLayout>

Layout of the pop-up layer
3. pop. xml Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "@ android: color/darker_gray">
<Button android: id = "@ + id/open"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/btn"
Android: text = "@ string/open"/>
<Button android: id = "@ + id/save"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/btn"
Android: text = "@ string/save"/>
<Button android: id = "@ + id/close"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/btn"
Android: text = "@ string/close"/>
</LinearLayout>

Style File under value
4. style Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "AnimationFade">
<! -- PopupWindow pop-up results left and right -->
<Item name = "android: windowEnterAnimation"> @ anim/in_lefttoright </item>
<Item name = "android: javaswexitanimation"> @ anim/out_righttoleft </item>
</Style>
</Resources>

String file under value
5. string. xml
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, PopwindowOnLeftActivity! </String>
<String name = "app_name"> PopwindowOnLeft </string>
<String name = "pop_left"> left menu </string>
<String name = "open"> open </string>
<String name = "save"> save </string>
<String name = "close"> close </string>
</Resources>

Files under the anim directory
Animated file from left to right when it appears
6. in_lefttoright.xml Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android">
<! -- Define the animation from left to right -->
<Translate
Android: fromXDelta = "-100%"
Android: toXDelta = "0"
Android: duration = "500"/>
</Set>

Animation that disappears from right to left when exiting
7. out_righttoleft.xml Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android">
<! -- Define exit animation from right to left -->
<Translate
Android: fromXDelta = "0"
Android: toXDelta = "-100%"
Android: duration = "500"/>
</Set>

Related Article

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.