How to android Popup Window match sreen size width - android

I have implemented popup window in my Activity. Problem is that i want set width of popup window match screen size, Now i have space left on both sides. Here is how it looks now
Here is my popup code:
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate the view from a predefined XML layout
View layout = inflater.inflate(R.layout.activity_pop_up, null);
ImageView baner3img = (ImageView ) layout.findViewById(R.id.baner3img);
Picasso.with(MainActivity.this).load(hrefScroll.get(2))
.into(baner3img);
// create PopupWindow
pw = new PopupWindow(layout, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT,true);
ImageView close = (ImageView) layout.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
findViewById(R.id.relativeLayoutZaFragment).post(new Runnable() {
public void run() {
pw.dismiss();
}
});
}
});
findViewById(R.id.relativeLayoutZaFragment).post(new Runnable() {
public void run() {
pw.setWindowLayoutMode(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
pw.setHeight(1);
pw.setWidth(1);
pw.showAtLocation(findViewById(R.id.relativeLayoutZaFragment), Gravity.CENTER, 1, 1);
new Handler().postDelayed(new Runnable(){
public void run() {
pw.dismiss();
}
}, 5 *1000);
}
});
}
});
activity_pop_up.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/popup"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bolt.automagazin.PopUp">
<ImageView
android:id="#+id/baner3img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
app:srcCompat="#drawable/autojedan" />
<ImageView
android:id="#+id/close"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/baner3img"
android:layout_marginEnd="15dp"
android:visibility="visible"
app:srcCompat="#drawable/closeee" />
</RelativeLayout>

Instead of:
pw = new PopupWindow(layout, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT,true);
use: LinearLayout.LayoutParams.MATCH_PARENT and set it to the PopupWindow
// Creating the PopupWindow
pw = new PopupWindow(MainActivity.this);
pw.setContentView(layout);
pw.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
pw.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
EDIT
Also set android:scaleType="fitXY" to the imageview to fill it's space
<ImageView
android:id="#+id/baner3img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:background="#android:color/transparent"
android:padding="0dp"
android:scaleType="fitXY"
android:src="#mipmap/ic_launcher" />

I found solution setting image scale to XY

Related

Unable to handle Popup Window in android

I am trying to implement the following screen.Here on clicking attachment icon ,popup window is displayed :
I am trying to implement the screen using the below mentioned code :
1.popupwindow_attachment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/padding10"
android:background="#color/while_color">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/gallery"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="#dimen/padding10"
android:drawableTop="#drawable/gallery"
android:gravity="center"
android:text="Gallery" />
<TextView
android:id="#+id/photos"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="#dimen/padding10"
android:drawableTop="#drawable/photos"
android:gravity="center"
android:text="Photos" />
<TextView
android:id="#+id/videos"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="#dimen/padding10"
android:drawableTop="#drawable/videos"
android:gravity="center"
android:text="Gallery" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/audio"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="#dimen/padding10"
android:drawableTop="#drawable/audio"
android:gravity="center"
android:text="Audio" />
<TextView
android:id="#+id/location"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="#dimen/padding10"
android:drawableTop="#drawable/location"
android:gravity="center"
android:text="Location" />
<TextView
android:id="#+id/contacts"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="#dimen/padding10"
android:drawableTop="#drawable/contacts"
android:gravity="center"
android:text="Contacts" />
</LinearLayout>
</LinearLayout>
2.Code implementing the Popup window
private void initializePopUpWindow() {
//inflate the popupwindow_attachment.xml
LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element);
LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup);
popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT,true);
//Displaying the popup at a specific location
popupWindow.showAtLocation(layout,Gravity.CENTER,0,0);
}
I am getting the following screen after using this code:
As you can see here ,popup window is not below the toolbar .Please help me to fix the issue.
Edited Code:
private void initializePopUpWindow() {
//inflate the popupwindow_attachment.xml
LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element);
LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup);
popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
//Displaying the popup at a specific location
// popupWindow.showAtLocation(layout, Gravity.TOP, 0, 150);
popupWindow.showAsDropDown(toolbar,0,0);
//Close the popup when touch outside
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
// popupWindow.dismiss();
}
After using the above code ,popup window is below the toolbar which i want but it not closed after clicking outside it.No functionality is working in the screen.Screen is just hanged badly.
Edited Working Code :
1.onCreate() method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_chat);
//Toolbar
toolbar = (Toolbar) findViewById(R.id.toolbarSingleChat);
toolbar.setNavigationIcon(R.drawable.back); // Setting Navigation Icon in the Toolbar
setSupportActionBar(toolbar);
LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element);
LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup);
popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
//Close the popup when touch outside
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
2.onoptionsItemSelected()
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_viewContacts:
return true;
case R.id.action_media:
return true;
case R.id.action_search:
return true;
case R.id.action_block:
return true;
case R.id.action_email_chat:
return true;
case R.id.action_clear_chat:
return true;
case R.id.action_attach:
initializePopUpWindow();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
3.initializePopUpWindow() method:
private void initializePopUpWindow() {
popupWindow.showAsDropDown(toolbar, 0, 0);
}
Now it is working for me.
If you want your popup at top then you need to set Gravity.TOP not Gravity.CENTER
Replace
popupWindow.showAtLocation(layout,Gravity.CENTER,0,0);
with
popupWindow.showAtLocation(layout,Gravity.TOP,0,0);
Update
If you want the popup below toolbar then you should do something like
popupWindow.showAtLocation(layout,Gravity.TOP,0,100);// where 100 is the height of toolbar
You can do somthing like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.text);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showpopUp();
}
});
}
public void showpopUp()
{
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.showAsDropDown(textView, 0, 0);
}

Cannot set text to TextView in PopupWindow

I created a popup window to show a disclaimer in my app but I can't figure out why i cannot set text to TextView. It is an HTML string so I did this:
private void showPopup(final Activity context) {
final PopupWindow pwindo;
Button btnClosePopup;
try {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
TextView txt = (TextView)findViewById(R.id.txtView);
txt.setText(Html.fromHtml(getString(R.string.tos_text)));
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
popup_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4d4d4d"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="#+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:textColor="#ffffff" />
<Button
android:id="#+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
</LinearLayout>
strings.xml
<string name="tos_text"><![CDATA[
<p>This is a html-formatted string with <b>bold</b> and <i>italic</i> text</p>
<p>This is another paragraph of the same string.</p>
]]>
</string>
It works only if I add android:text="#string/tos_text" in the XML file, but i see the raw html codes. What could be the problem?
Try next:
private void showPopup() {
//if you call this method correctly then you do not need to wrap
// this method by try-catch block which affects performance
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element), false);
final PopupWindow pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//get txt view from "layout" which will be added into popup window
//before it you tried to find view in activity container
TextView txt = (TextView) layout.findViewById(R.id.txtView);
txt.setText(Html.fromHtml(getString(R.string.tos_text)));
//init your button
Button btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
//show popup window after you have done initialization of views
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
EDIT:
To add scrollable content you need to change approach of initialization of popup window:
final PopupWindow pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
where you need to change width of window to ViewGroup.LayoutParams.MATCH_PARENT.
Then you need to wrap your text view by ScrollView which has root child - LinearLayout:
popup_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4d4d4d"
android:orientation="vertical"
android:padding="10sp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txtView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:textColor="#ffffff"/>
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close"/>
</LinearLayout>
ScrollView has field with weight:
android:layout_weight="1"
I have added this to avoid hiding of button when text will fill all of visible content.
EDIT 2:
Also you can fix height of scroll view, if you do not want to fill all the screen:
-----
<ScrollView
android:layout_width="match_parent"
android:layout_height="150dp">
-----

How to create three dot button on the top right of an item on an android listview?

Like this
If you click the button a menu will drop down. I use ListView to implement, but I don't know how to attach the three-dot-button and add a pop-up menu.
Here is my list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="4dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:background="#drawable/card_background" >
<TextView
android:id="#+id/tvMain"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"
android:textColor="#android:color/primary_text_light" />
</LinearLayout>
</FrameLayout>
And the screenshot of my app:
https://www.dropbox.com/s/h2ko2ctfn5ohmf9/Screenshot_2013-09-10-15-43-57.png
It's just a ImageView with the correct PNG (you'll have to draw the image yourself on photoshop, paint, GIMP or any other graphical editor) and use a PopupWindow to make the popup.
here is a simple example on how to do a PopupWindow (got it from here http://android-er.blogspot.de/2012/03/example-of-using-popupwindow.html)
#Override
public void onClick(View view) {
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(view, 0, -0);
}

1px line at left side of popup [duplicate]

I have a problem when creating a popup for my app. I'm trying to show a popup that fills the total size of my screen. The problem is that i'm getting 1px line at the left side that isn't filled.
my popup xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/popup"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#drawable/popup_round_corners"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="#string/zm_main_uvod" />
<Button
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:text="Ok" />
</LinearLayout>
</LinearLAyout>
and my function to show popup:
private void showPopup(final Activity context) {
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout)findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.startpopup, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height=display.getHeight();
popup.setWidth(width+5);
popup.setHeight(height+5);
popup.setFocusable(true);
popup.showAtLocation(layout, Gravity.FILL, 0, 0);
Button close = (Button) layout.findViewById(R.id.close);
close.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
}
});
}
I tried with gravity.center and try setting offsets to -30,-30 but nothing happend. Any ideas?

PopupWindow with Gallery child

I'm trying to use a PopupWindow with a Gallery inside, but when inflating the popup, I get the following error:
ERROR/AndroidRuntime(31817): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
Does it mean there's no way to use a Gallery in a PopupWindow?
Thanks
btn_open_popup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) HomeActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popup_container = inflater.inflate(R.layout.popup_container,null, false);
final PopupWindow pw = new PopupWindow(popup_container, width, height, true);
pw.showAtLocation(findViewById(R.id.home), Gravity.CENTER, 0,0);
ImageView btn_close_popup = (ImageView) popup_container.findViewById(R.id.btn_close_popup);
btn_close_popup.setAlpha(120);
btn_close_popup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pw.dismiss();
}
});
}
});
popup_container.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="295dp"
android:layout_height="307dp" android:background="#drawable/bg_popup">
<ImageView android:id="#+id/btn_close_popup"
android:layout_width="25dp" android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true" android:src="#983742" />
<Gallery android:id="#+id/popup_gallery"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/popup_contact" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/popup_welcome" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/popup_useraccount" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/popup_geoloc" />
</Gallery>
</RelativeLayout>
This doesn't necessarily answer your question, but why don't you use a dialog?. It's prettier and easier to use.
Edit 1: Yes you should be able to use a gallery in a popup window, I'm not sure why you can't get it to work.

Categories

Resources