Pop-Up Layout in Android (Virtual Layout)
Problem:
hey guys, i'm back with an interesting experience of Dynamic Layout for Android. I am not going to discuss the whole story but i'm here to discuss the an interesting feature "Pop-Up Layout in Android",
Solution:
Here in this session, i will show you how to create another layout on the existing layout, Convenient when you want to display an additional information like customize action bar but don't want to launch another activity or via a dialog.
Time for bed programming:
1. Create a new Android Project in Eclipse:
Project name: PopUpsInAndroid
Build Target: Android 4.0.3 (or any above 2.1)
Application name: PopUpsAndroid
Package name: scube.rao.popups
Create Activity: PopUpActivity
Min SDK Version: 8
2. Creating an popup.xml file:
Create a new layout file in the layout folder of resources., and define your any customize layout. for example, i am having a simple button in the layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_popUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button from PopUp Layout" />
</LinearLayout>
3. Edit the main.xml file:
Add a button in your auto-created main.xml file in the layout folder of resources.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_show_popUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here to show Pop-UpLayout" />
</LinearLayout>
4. Edit the PopUpActivity.class file:
Now it's the time for the bed logic and coding. what will we need though-out the code.
- A Point to specify the position of the pop-up layout in the screen
- Creating a Inflate layout instance for the popup.xml and adding it the the main layout. whereas the rest of the code is given below...
public class PopUpActivity extends Activity {
Point point;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn_show = (Button) findViewById(R.id. btn_show_popUp);
btn_show.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
if (point != null)
showPopup(PopUpActivity.this, point);
}
});
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2]; Button button = (Button) findViewById(R.id. btn_show_popUp); button.getLocationOnScreen(location);
point = new Point();
point.x = location[0]; point.y = location[1]; }
private void showPopup(final Activity context, Point p) { int popupWidth = 200; int popupHeight = 150;
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
Looking from suggestion your side.
hey guys, i'm back with an interesting experience of Dynamic Layout for Android. I am not going to discuss the whole story but i'm here to discuss the an interesting feature "Pop-Up Layout in Android",
Solution:
Here in this session, i will show you how to create another layout on the existing layout, Convenient when you want to display an additional information like customize action bar but don't want to launch another activity or via a dialog.
Time for bed programming:
1. Create a new Android Project in Eclipse:
Project name: PopUpsInAndroid
Build Target: Android 4.0.3 (or any above 2.1)
Application name: PopUpsAndroid
Package name: scube.rao.popups
Create Activity: PopUpActivity
Min SDK Version: 8
2. Creating an popup.xml file:
Create a new layout file in the layout folder of resources., and define your any customize layout. for example, i am having a simple button in the layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_popUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button from PopUp Layout" />
</LinearLayout>
3. Edit the main.xml file:
Add a button in your auto-created main.xml file in the layout folder of resources.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_show_popUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here to show Pop-UpLayout" />
</LinearLayout>
4. Edit the PopUpActivity.class file:
Now it's the time for the bed logic and coding. what will we need though-out the code.
- A Point to specify the position of the pop-up layout in the screen
- Creating a Inflate layout instance for the popup.xml and adding it the the main layout. whereas the rest of the code is given below...
public class PopUpActivity extends Activity {
Point point;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn_show = (Button) findViewById(R.id. btn_show_popUp);
btn_show.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
if (point != null)
showPopup(PopUpActivity.this, point);
}
});
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2]; Button button = (Button) findViewById(R.id. btn_show_popUp); button.getLocationOnScreen(location);
point = new Point();
point.x = location[0]; point.y = location[1]; }
private void showPopup(final Activity context, Point p) { int popupWidth = 200; int popupHeight = 150;
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService (Context. LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout. popup_layout, viewGroup);
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
int OFFSET_X = 30;
int OFFSET_Y = 30;
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
Button close = (Button) layout.findViewById(R.id.
btn_popUp );
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popup.dismiss();
}
});
}
}
Pop-Up Layout in Android (Virtual Layout)
Reviewed by Unknown
on
Tuesday, August 21, 2012
Rating:
No comments: