Unable to handle Popup Window in android - 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);
}

Related

Pop up bugged doesn't show all the buttons

I'm trying to create a simple Pop up on this project, while the code works on some device, but there seems to be a bug on some devices.
This is the XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/popup_bg"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Silakan pilih yang ingin di rating"
android:textColor="#000" />
<Button
android:id="#+id/btn_atasan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_marginTop="5dp"
android:background="#drawable/button_bg"
android:text="Atasan"
android:textColor="#fff" />
<Button
android:id="#+id/btn_sekawan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#drawable/button_bg"
android:text="Sekawan"
android:textColor="#fff" />
<Button
android:id="#+id/btn_bawahan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#drawable/button_bg"
android:text="Bawahan"
android:textColor="#fff" />
</LinearLayout>
This is the java code on the fragment
private void popUpRatingType() {
//instantiate the popup.xml layout file
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.popup_rate_karyawan, null);
//casting buttons
btnAtasan = customView.findViewById(R.id.btn_atasan);
btnBawahan = customView.findViewById(R.id.btn_bawahan);
btnSekawan = customView.findViewById(R.id.btn_sekawan);
//instantiate popup window
popupWindow = new PopupWindow(customView, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
//display the popup window
popupWindow.showAtLocation(contentFrame, Gravity.CENTER, 0, 0);
Remove popup on outside click
// Removes default background.
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// Closes the popup window when touch outside.
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.update();
}
On some devices it looks okay, like this :
But I don't know how on some devices (this is on Nougat) looks like this :
private void popUpRatingType() {
//instantiate the popup.xml layout file
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.item_temp, null);
//casting buttons
//instantiate popup window
PopupWindow popupWindow = new PopupWindow(customView, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
//display the popup window
popupWindow.showAtLocation(customView, Gravity.CENTER, 0, 0);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// Closes the popup window when touch outside.
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.update();
}

How do I insert layout into Android menu item row?

How do I insert a layout into an Android menu item row?
I don't know how to create a layout like this.
Here's what I want:
Create a custom layout like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="5dp">
<Switch
android:id="#+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="Play with the Switch" />
<TextView
android:id="#+id/switchStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/mySwitch"
android:layout_marginTop="22dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
In your Activity, implement the following code:
public void showPopup(View v) {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.popup_filter_layout, null);
popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
//TODO do sth here on dismiss
}
});
popupWindow.showAsDropDown(v);
}

How to android Popup Window match sreen size width

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

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">
-----

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?

Categories

Resources