i have created a button in android and when clicking it would show the popup window..but the code doesnot work like that..it has no errors but not showing popup window...please helpme..here is my code
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout objrl = (RelativeLayout) findViewById(R.id.myrl);
final Button objButton = (Button) findViewById(R.id.mybutton);
objButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
PopupWindow objPopupWindow = new PopupWindow(objrl, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
objPopupWindow.setAnimationStyle(R.drawable.background2);
objPopupWindow.showAtLocation(objButton, Gravity.CENTER_HORIZONTAL, 10, 10);
}
});
}
have you tried this : objPopupWindow.showAsDropDown(popupButton, 0, 0);
or try this http://rajeshandroiddeveloper.blogspot.in/2013/07/android-popupwindow-example-in-listview.html
PopupWindow popupWindowDogs = popupWindowDogs();
called below function where they want ::-
public PopupWindow popupWindowDogs() {
// initialize a pop up window type
PopupWindow popupWindow = new PopupWindow(this);
// the drop down list is a list view
ListView listViewDogs = new ListView(this);
// set our adapter and pass our pop up window contents
listViewDogs.setAdapter(dogsAdapter(popUpContents));
// set the item click listener
listViewDogs.setOnItemClickListener(new DogsDropdownOnItemClickListener());
// some other visual settings
popupWindow.setFocusable(true);
popupWindow.setWidth(250);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
// set the list view as pop up window content
popupWindow.setContentView(listViewDogs);
return popupWindow;
}
I've found some strange stuff in your codes
You've specified WRAP_CONTENT but haven't specified its content at all
Pass a drawable as animation style to the setAnimationStyle method.
In my opinion if you specify a valid animation style and a content view, It should appear.
I think you missed this code inside the OnClickListener
objPopupWindow.setContentView(objrl);
Related
I have created a custom arrayadapter for my listview. It has a player name and the score, I also have a add button on the cell, when that button is clicked I want a popup screen to appear, Here is where the user can add the score of the player. This popup window has 6 buttons "+1", "+2" "+10" ..etc and a done button. When the done button is clicked the score gets updated.
I am handling the add button click event on my customArraryAdapter class, so I have to create the popup here as well. I have searched and tried to do this with no success.
What I tried so far:
I have a View = convertView and a viewHolder = holder The problem is I'm not so sure what to pass as the parameter to create a popup. The code below is myCustomArrayAdapter class. I have also read popups cant handle touch events, but some are saying it can. Since my popup has a lot of buttons maybe this might be a great solution.
This is in #Override
public View getView(final int position, View convertView, ViewGroup parent) Method in CustomArrayAdapter Class
//Handle add button click
holder.add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
addScores(convertView);
//list gets updated
notifyDataSetChanged();
}
});
My addScores method looks like this
private void addScores(View v){
PopupWindow pw;
LayoutInflater inflater = (LayoutInflater)v.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.weight_popup, (ViewGroup)v.findViewById(R.id.linlay_weight_popup));
pw = new PopupWindow(layout, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
pw.setBackgroundDrawable(new BitmapDrawable());
pw.setOutsideTouchable(true);
pw.showAsDropDown(btnSelectWeight);
}
You may pass View that will be displayed in popup the same way you do.
Consider this:
View layout = inflater.inflate(R.layout.weight_popup, (ViewGroup)v.findViewById(R.id.linlay_weight_popup));
Your weight_popup layout should contain 6 buttons, which would have onClick there you update scores.
Something like:
Button btn1 = (Button)layout.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//update your score here.
}
});
//other buttons..
pw = new PopupWindow(layout, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
Is it possible to add a new EditText automatically in a ListView on a Button click?
If so please let me know.
EditText name = new EditText(youractivity);
convertView.addView(name);
Call notifydatasetchanged() but you have to save the field that you add becouse after scrolling into the list you will have in all list that editText and you have to remove where you don't want to appear.
What I recommand you is to make an editext into your cellView hidden and make it visible when you tap on your button.
You could make a custom adapter for your listview and give it a layout containing the edittext with the visibility set to gone. You can then set it to visible onbuttonclick.
I would like to suggest one code its not about listview but see if it could give you some idea,by adding views dynamically in linear layout which is inside scroll view.
public class MainActivity extends Activity {
ScrollView scrollview;
LinearLayout linearLayout;
LinearLayout.LayoutParams layoutParams;
static int i;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollview = (ScrollView)findViewById(R.id.scrollview);
linearLayout = (LinearLayout)findViewById(R.id.linearlayout);
Button button = (Button)findViewById(R.id.button);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView view = new TextView(MainActivity.this);
view.setText(++i+" view");
linearLayout.addView(view, layoutParams);
}
});
}}
In my activity there are 3 buttons. By clicking on the first button, I want a dialog to appear with a graph (in the layout itself it works fine).
btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog = new Dialog(ChartsDuration.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_charts1);
// code to show a graph. Here I have a function that calls drawChartAll(),
// but since the layout is declared outside the dialog it cannot render it to the
// linearlayout, and my graph1 linearlayout will be empty.
dialog.show();
}
});
Tha graph uses data that are queried in functions outside like
public void drawChartAll()
{
//blablabla and this is how I define the layout and render the graph to it:
LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
mChartView = ChartFactory.getBarChartView(ChartsDuration.this, buildBarDataset(titles, values),renderer,Type.DEFAULT);
mChartView.setBackgroundColor(renderer.getBackgroundColor());
layout.addView(mChartView);
}
So without the dialog, I can easily show the graph in the graph1 LinearLayout e.g below the buttons, because they are "on the same levels", but I want to show the graph in a dialog opened by clicking on a button. Because if I were in a dialog I would do this: LinearLayout layout = (LinearLayout)dialog.findViewById(R.id.graph1); But now I cannot do this, since I am outside the dialog.
How do I reach this layout?
Edit:
user113215 I did this:
in the activity:
LayoutInflater inflater = LayoutInflater.from(ChartsDuration.this);
customDialog = (ViewGroup) inflater.inflate(R.layout.dialog_charts1, null);
btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.setContentView(customDialog);
//queries
dialog.show();
}
});
and in drawChartAll:
public void drawChartAll()
{
//code
LinearLayout layout = (LinearLayout) customDialog.findViewById(R.id.graph1);
}
Is this what you mean? This throws me a nullpointer exception to dialog.setContentView(customDialog); line.
If I understand the problem correctly, you're having trouble manipulating things on the layout that's going into the dialog. Instead of calling setContentView(int), inflate the layout yourself and then use setContentView(View).
LayoutInflater inflater = LayoutInflater.from(this);
ViewGroup customDialog = (ViewGroup) inflater.inflate(R.layout.dialog_charts1, null);
// Do chart things here
// Prefix all calls to findViewById with "customDialog."
LinearLayout layout = (LinearLayout) customDialog.findViewById(R.id.graph1);
mChartView = ChartFactory.getBarChartView(ChartsDuration.this, buildBarDataset(titles, values),renderer,Type.DEFAULT);
mChartView.setBackgroundColor(renderer.getBackgroundColor());
layout.addView(mChartView);
// Put the manipulated layout into the dialog
dialog.setContentView(customDialog);
You can use this same trick to take advantage of the AlertDialog.Builder class while still filling the dialog with a custom layout.
Make the dialogvariable a field in your Activity class. You can reach it from anywhere. You can still use dialog.findviewbyid throughout you Activity. Just make sure setcontentView has been called before you call findviewbyid
Edit: I hope when you wrote since I am outside the dialog. you meant you could not access the variable
I have been hunting for the answer to this but most seem to point to getApplicationContext() issues but I am not using getApplicationContext().
Basically I am trying to have a popup window open on start up of a activity but when the activity starts it force closes with the above error.
The code im have writen is:
protected void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout.installguides_menu);
setTitleFromActivityLabel(R.id.title_text);
btn_Back = (Button) findViewById(R.id.btn_Back);
btn_Back.setOnClickListener (btn_Back_onClick);
btn_Ubuntu10Guide = (Button) findViewById(R.id.btn_Ubuntu10Guide);
btn_Ubuntu12Guide = (Button) findViewById(R.id.btn_Ubuntu12Guide);
btn_BacktrackGuide = (Button) findViewById(R.id.btn_BacktrackGuide);
btn_DebianGuide = (Button) findViewById(R.id.btn_DebianGuide);
btn_Ubuntu10Guide .setOnClickListener (btn_Ubuntu10Guide_onClick);
btn_Ubuntu12Guide .setOnClickListener (btn_Ubuntu12Guide_onClick);
btn_BacktrackGuide .setOnClickListener (btn_BacktrackGuide_onClick);
btn_DebianGuide .setOnClickListener (btn_DebianGuide_onClick);
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.donation_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(){
public void onClick(View v) {
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(btn_Ubuntu10Guide, 50, -30);
}
Hope you guys can help, been hunting for the answer for hours so your my last hope!
My first guess is that you are not allowed to show a pop-up inside onCreate. It is for initializing your application and constructing your layout, but you are forcing it to display a pop-up instead.
onStart() is a more suitable place to show it. Override this function and show your pop-up there. If you prefer to do it in onResume(), note that the user will see it not only when they launch the application but also when they come back to it after navigating away to another activity.
EDIT 2: Try this for creating the pop-up in onStart or onCreate:
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(
inflater.inflate(findViewById(R.id.popup_layout), null, false),
100,
100,
true);
// delaying popup until after all application initialization is done
findViewById(R.id.main_page_layout).post(new Runnable() {
public void run() {
pw.showAtLocation(findViewById(R.id.main_page_layout), Gravity.CENTER, 0, 0);
}
});
Add id's to your activity and pop-up layouts using
android:id="#+id/main_page_layout"
and
android:id="#+id/popup_layout"
respectively so that they match the id's you will use in the code above.
I have a popup menu implemented , which shows up on click of a button. This is my onclick method.
public void showOverflow(View view) {
boolean click = true;
Button action = (Button) findViewById(R.id.btbAction);
LayoutInflater inflater = (LayoutInflater) main.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.overflow_layout, null);
final PopupWindow pw = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
pw.setOutsideTouchable(true);
if (click) {
pw.showAsDropDown(action, 0, 0);
click = false;
} else {
pw.dismiss();
click = true;
}
}
The popup window shows up when the button is clicked. Now, the problem is that the window is not dismissed when i touch outside the popup window.
I tried setting this property to the popup window
pw.setOutsideTouchable(true);
Things remain the same. Please help me fix this
You should change the setOutsideTouchable call's parameter to true:
pw.setOutsideTouchable(false);
Controls whether the pop-up will be informed of touch events outside
of its window. This only makes sense for pop-ups that are touchable
but not focusable, which means touches outside of the window will be
delivered to the window behind. The default is false.
If the popup is showing, calling this method will take effect only the
next time the popup is shown or through a manual call to one of the
update() methods.
Parameters: touchable true if the popup should receive outside touch
events, false otherwise
On the other hand, what is the click local variable supposed to do? It is set to true, so it will always force the pw to pop up, whenever the showOverflow method is called, and for no reason it is set to false later, because it's life cycle ends as you leave that method.
Your code should look something like this:
private LayoutInflater inflater;
private Button action;
private PopupWindow pw;
private View popupView;
/*
* (non-Javadoc)
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.overflow_layout, null, false);
action = (Button) findViewById(R.id.action);
action.setOnClickListener(this);
}
public void showOverflow()
{
pw = new PopupWindow(getApplicationContext());
pw.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
pw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
pw.setOutsideTouchable(true);
pw.setContentView(popupView);
pw.showAsDropDown(action, 0, 0);
}
The getApplicationContext() shoud be used in case you are inside an Activity class. Otherwise you should get the Context as a parameter.
change pw.setOutsideTouchable(true); to pw.setOutsideTouchable(false);
I know this is an old question but this is what I have done to fix it
The problem is:
You are creating a new instance of popupwindow everytime you call showOverFlow() thats why after you close the popupwindow another popup window will show
What will you do is initialize popupview in OnCreate
Then call popupwindow.showAsDropDown(view) in showOverFlow() method
And lastly you can check whether is it showing below code
Put this code in your button onclick
if(popupwindow.isShowing()){
popup.dismiss() }
else{
ShowOverflow()}