I am new to android development. I have created a popup window which is being called on a button click from the main activity class. I have some Edit text field on my pop window. can i clear all these edit text field on the pop up window through a button click? if yes then how? Thanks in advance!!
Below is what i am trying but it is giving null point exception.
public void initatePopupWindow(View view)
{
try
{
LayoutInflater inflater=(LayoutInflater) ContactdetailsActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout=inflater.inflate(R.layout.popupwindow,(ViewGroup)findViewById(R.id.popuplayout));
pw = new PopupWindow(layout, 500, 500, true);
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
Button b1=(Button)layout.findViewById(R.id.cancel);
b1.setOnClickListener(cancel_button_click_listener);
Button b2=(Button)layout.findViewById(R.id.clear);
b2.setOnClickListener(clear_fields);
}
catch (Exception e)
{
}
}
private OnClickListener clear_fields= new OnClickListener()
{
public void onClick(View v)
{
ViewGroup group= (ViewGroup)findViewById(R.id.popuplayout);
for(int i=0,count=getChildCount(); i<count; ++i)
{
View v1= group.getChildAt(i);
if(v1 instanceof EditText)
{
((EditText)v1).setText("");
}
if(v1 instanceof ViewGroup && (((ViewGroup)v1).getChildCount() > 0))
onClick((ViewGroup)v1);
}
}
};
private OnClickListener cancel_button_click_listener=new OnClickListener()
{
public void onClick(View v)
{
pw.dismiss();
}
};
}
Are you getting your errors on this line?
ViewGroup group= (ViewGroup)findViewById(R.id.popuplayout);
ViewGroup group= (ViewGroup) layout.findViewById(R.id.popuplayout);//layout is your inflated
I think it might be beacuse you need to use the value you called on the layout inflator.
Anyway, I guess you should do instead: Get a reference of all your edit texts and then clear them one by one. Maybe keep an array of edit texts and walk through them and set their texts to "" instead of checking for the type.
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);
In my android app, I create a dialog like this:
private void handleEdit() {
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_gallery, null);
final AlertDialog d = new AlertDialog.Builder(this)
.setView(dialoglayout)
.setTitle(R.string.edit)
.setNegativeButton(R.string.cancel, null)
.create();
CheckBox mainCB = (CheckBox)dialoglayout.findViewById(R.id.main);
CheckBox usedCB = (CheckBox)dialoglayout.findViewById(R.id.used);
mainCB.setChecked(image.getIsMain());
usedCB.setChecked(image.getApproved());
mainCB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (Network.isNetworkAvailable(GalleryScreen.this)) {
new Async_update_image_state(GalleryScreen.this, fish, image, !image.getIsMain(), image.getApproved(), false);
d.dismiss();
} else {
Popup.ShowErrorMessage(GalleryScreen.this, R.string.no_internet, false);
}
}
});
usedCB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (Network.isNetworkAvailable(GalleryScreen.this)) {
new Async_update_image_state(GalleryScreen.this, fish, image, false, !image.getApproved(), true);
d.dismiss();
} else {
Popup.ShowErrorMessage(GalleryScreen.this, R.string.no_internet, false);
}
}
});
d.show();
}
But I get a warning on View dialoglayout = inflater.inflate(R.layout.dialog_gallery, null); underlining the null.
Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)
What does this mean and how can I fix it?
Thanks.
this works for me
View.inflate(getActivity(), R.layout.dialog, null);
without any warnings nor errors.
When inflating a layout for use in a dialog, You can safely pass null here and ignore the warning.
From This Link
The issue here is that AlertDialog.Builder supports a custom view, but
does not provide an implementation of setView() that takes a layout
resource; so you must inflate the XML manually. However, because the
result will go into the dialog, which does not expose its root view
(in fact, it doesn’t exist yet), we do not have access to the eventual
parent of the layout, so we cannot use it for inflation. It turns out,
this is irrelevant, because AlertDialog will erase any LayoutParams on
the layout anyway and replace them with match_parent.
Instead of :
inflater.inflate(R.layout.dialog_gallery, null);
do:
inflater.inflate(R.layout.dialog_gallery, parent, false);
Using a #SuppressLint annotation as suggested by Eugen in the comment above might "suppress" the warning, but it doesn't solve the problem.Using null as an argument for the ViewGroup will cause you problems in the future.
Instead of
inflater.inflate(R.layout.dialog_gallery, null);
try
inflater.inflate(R.layout.dialog_gallery, null, false);
It will not attach view to the parent that is null in your case.
See details on Android Reference
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);
I want to make button unclickable using setClicable() but it's not working. I am using inflater because I need.
This is my code:
mContactList = (LinearLayout) findViewById(R.id.contactList);
LayoutInflater inflater = getLayoutInflater();
for (ListIterator<ContactModel> it = contactList.listIterator(); it.hasNext();){
ContactModel contact = it.next();
View view = inflater.inflate(R.layout.contact_unknown_list_row, null);
view.findViewById(R.id.inviteButton).setTag(contact.getEmail());
view.findViewById(R.id.inviteButton).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String address = (String) v.getTag();
sendInvatoin(address);
if(v.findViewById(R.id.inviteButton).isClickable())
v.findViewById(R.id.inviteButton).setClickable(false);
}
});
mContactList.addView(view);
}
Try using.
button.setEnabled(false);
In your case, you will do something like this:
view.findViewById(R.id.inviteButton).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String address = (String) v.getTag();
sendInvitatoins(address);
Button b = (Button)v;
b.setEnabled(false);
}
});
When using setOnClickListener, unclickable views (= v.setClickable(false)) would become clickable as mentioned in Docs.
... a callback to be invoked when this view is clicked. If this
view is not clickable, it becomes clickable.
Better to use v.setEnabled(false) if you want to set an OnClickListener to the button or any other view...
This will work in case of Imageview as well as the button.
private OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (imageview.isEnabled()){
//I have wrapped all code inside onClick() in this if condition
//Your onClick() code will only execute if the imageview is enabled
//Now we can use setEnabled() instead of setClickable() everywhere
}}
};
Inside onCreate(), you can do setEnabled(false) which will be equivalent to setClickable(false).
We are able to use setEnabled() as tag because it's state remains unaffected on invocation of click (unlike setClickable() whose state changes).
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.