Cannot set text to TextView in PopupWindow - android

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

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();
}

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);
}

Why is the below code throwing null pointer exception?

So I have the following code for a popup window in an activity:
private void initiatePopupWindow(String popupText, boolean hasEditText){
LayoutInflater inflater = (LayoutInflater) PlayActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout= inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_layout));
final PopupWindow popupWindow= new PopupWindow(layout, 300, 370, true);
findViewById(R.id.play_activity_layout).post(new Runnable() {
#Override
public void run() {
popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
});
TextView textPopup= (TextView) layout.findViewById(R.id.tvPopupText);
textPopup.setText(popupText);
if(hasEditText) {
EditText et = (EditText) findViewById(R.id.etNumber);
MAX_NUMBER_OF_MOVES = Integer.parseInt(et.getText().toString());
}
btnClosePopup= (Button) layout.findViewById(R.id.btnClosePopup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
//finish();
}
});
}
I get the NullPointerException for this row:
MAX_NUMBER_OF_MOVES = Integer.parseInt(et.getText().toString());
I tried to fix this by searching on the site for ideas but still dont know why it isnt proper.
EDIT:
My popup_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="#+id/tvPopupText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="#string/under_construction"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
<Button
android:id="#+id/btnClosePopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/ok"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/etNumber"
android:inputType="number"
android:layout_alignRight="#+id/tvPopupText"/>
</RelativeLayout>
I think you have to use
EditText et = (EditText) layout.findViewById(R.id.etNumber);
see layout. missing.
Note that getText() never returns null. So only findViewById should raise the NullPointerException.
Try to change this:
final View layout= inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_layout));
for this other one:
// container = get it from param of onCreateView
final View layout= inflater.inflate(R.layout.popup_layout, container);
...
EditText et = (EditText) layout.findViewById(R.id.etNumber);
After you've fix the layout.findViewById....
Initialize the EditText to some default value...see the android:text="0" below
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/etNumber"
android:inputType="number"
android:layout_alignRight="#+id/tvPopupText"
android:text="0"
/>
And you should also ALWAYS wrap Integer.parseInt("string_value") in a try/catch block in case string_value is not a valid number:
try {
MAX_NUMBER_OF_MOVES = Integer.parseInt(et.getText().toString());
}
catch (NumberFormatException nfe_) {
MAX_NUMBER_OF_MOVES = 5; // Or whatever your default number should be
}

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?

Categories

Resources