Popup window on a ListView Android - android

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

Related

How to get the text value of a button and load it into another button in Android

I am creating a word game application , I have created a grid view consisting of buttons. When I click a button in the grid view a popup windows opens which contains all the English alphabets.Now when I click any letter in my pop up window , I want that letter to appear in my grid that is the string in my pop window's button must appear in my grid view's button. , how do I do it?
This is my button's code:
button1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View container = layoutInflater.inflate(R.layout.activity_popup,null);
popupWindow = new PopupWindow(container,800,1100,true);
popupWindow.showAtLocation(constraintLayout, Gravity.NO_GRAVITY,150,400);
b1=(Button) findViewById(R.id.b1);
String s1 = b1.getText().toString();
button1.setText(s1);
container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionevent){
popupWindow.dismiss();
return true;
}
});
}
});
My popup window's code:
b1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String s1 = "A";
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}
});
Screenshots of the app
This is my grid layout where the user must enter the letters
When I click any button on the grid this is the pop up window which is displayed.
If I run this , the app is getting stopped.
It might help us if you would share the exact exception that is thrown by your application.
If I unterstand you correctly, then b1 is a button contained in your activity_popup- layout. So you might want to try b1 = (Button) container.findViewById(R.id.b1) instead. If thats not working, post your exception!

POPUP window showing in android

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

How make a layout available to a dialog

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

Android:Animated menu not dissappearing

I am dealing with a view flipper. I have 2 views in my view flipper and in the second view on completing a frame animation an animated popup menu translating from bottom. when I press the back button I could able to flip to first view from second but again when I switch to the second view from first view that popup menu is not disappearing. I used reset() and setfillafter() methods but no result
How to solve this? any Idea?
Here is my code.
final Animation popup = new TranslateAnimation(0, 0, 200, 0);
popup.setDuration(20000);
popup.setFillAfter(true);
hearttap.setOnClickListener(new View.OnClickListener() {
public void onClick(final View view) {
final RelativeLayout popuplayout = (RelativeLayout) findViewById(R.id.popuplayout);
final ImageView ekgimgview4 = (ImageView) findViewById(R.id.ekgimgview4);
ekgimgview4.setVisibility(ImageView.VISIBLE);
ekgimgview4.setBackgroundResource(R.anim.ekgtimer);
AnimationDrawable ekgframeAnimation4 = (AnimationDrawable) ekgimgview4
.getBackground();
if (ekgframeAnimation4.isRunning()) {
findViewById(R.id.ekgimgview4).postDelayed(new Runnable() {
public void run() {
// openOptionsMenu();
popuplayout.startAnimation(popup);
popup.setFillAfter(true);
popup.setStartTime(30000);
ekgimgview4.setVisibility(view.GONE);
}
}, 30000);
final Button ekgbutton = (Button) findViewById(R.id.ekgbutton);
ekgbutton.setOnClickListener(new View.OnClickListener() {
public void onClick( View view) {
RelativeLayout popuplayout = (RelativeLayout) findViewById(R.id.popuplayout);
popuplayout.setVisibility(View.INVISIBLE);
}
});
You need to use the dismiss() function for popups. So use the following code whenever you transition between Views:
popup.dismiss();
I would edit it into your code myself, but the way StackOverflow handles displaying code tags made your code half into HTML code tags and half not.

Showing a List View when a button is clicked: Android

I am trying to implement a drop down list when a button is clicked.
So, I have a text view and a button in a navigation bar(nav.xml) and a corresponding list view. This navigation bar is included in another page( products.xml)
when the button is clicked i get the list view right below the button(which is what i want to acheive) but its my moving all the contents on the current page downwards, even the text view which is placed in nav bar moved downwards.
I am totally new to Android, any sample examples or a way how to achieve it
???
Sounds like you need a Spinner. It's the equivalent of a drop down list for Android. You can find an example here.
So, for our need we need to use ListPopupWindow.
The link to official description:
http://developer.android.com/reference/android/widget/ListPopupWindow.html
Let's dive in the code:
we have our own method:
public void downloadBtnSelected(View anchor) {
final ListPopupWindow lpw = new ListPopupWindow(this);
String[] data = { ".png", ".pdf", ".jpg", ".jpeg" };
PopupAdapter pa = new PopupAdapter(data, this);
lpw.setAdapter(pa);
//setting up an anchor view
lpw.setAnchorView(anchor);
//Setting measure specifications. I'v used this mesure specs to display my
//ListView as wide as my anchor view is
lpw.setHeight(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
lpw.setWidth(anchor.getRight() - anchor.getLeft());
// Background is needed. You can use your own drawable or make a 9patch.
// I'v used a custom btn drawable. looks nice.
lpw.setBackgroundDrawable(this.getResources().getDrawable(
android.R.drawable.btn_default));
// Offset between anchor view and popupWindow
lpw.setVerticalOffset(3);
lpw.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
/// Our action.....
lpw.dismiss();
}
});
lpw.show();
}
and the button with an onClickListener to call this method:
Button btn = new Button(this);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
downloadBtnSelected(v);
}
});
we pass the View v argument as our anchor, in order to let our PopupWindow to know where to display itself. It will be displayed in the bottom-left corner of our anchor view if there is enough room below. If not- it will displa

Categories

Resources