Android popup on activity create - java

I am using the following code to create a popup which shows as the activity loads.
I call loadingPopup(); in my oncreate method.
The popup works the problem is when ever i try to place onclick listener for the dismiss button the whole thing crashes on load. If i were to remove the DissMissButton onclick listener code it works fine. Any help would be appreciated
private void loadingPopup() {
LayoutInflater inflater = this.getLayoutInflater();
final View layout = inflater.inflate(R.layout.popup_welcome, null);
final Button DismissButton = (Button) findViewById(R.id.button_welcomepopup_dismiss);
final PopupWindow Welcome_popupWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
Welcome_popupWindow.setFocusable(false);
Welcome_popupWindow.setTouchable(true);
Welcome_popupWindow.setOutsideTouchable(true);
layout.post(new Runnable() {
public void run() {
Welcome_popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
DismissButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Welcome_popupWindow.dismiss();
}
});
}
});

You call find view on your activity view, while your button is in the popup, so of course the DismissButton is null and app force close.
Change the find view code to this:
final Button DismissButton = (Button) layout.findViewById(R.id.button_welcomepopup_dismiss);

Related

How to finish an activity, onbackpressed when you have a pop up inflated

For the app i'm currently building i inflate a popup on the activity if there is no internet connection, the problem is that if the user presses their phone's back button, the popup disappears but u still get to see the activity without any information.
I would like to find out a way to finish the activity as well, when the back button is pressed.
Heres the code for the popup im currently inflating:
public void connection_error(final Class<?> clss){
if(!Functions.isOnline(getApplicationContext())) {
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_nointernet, null);
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.MATCH_PARENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
popupWindow.showAtLocation(findViewById(R.id.drawer_layout), Gravity.CENTER, 0, 0);
Button btn_retry = popupView.findViewById(R.id.btn_retry);
btn_retry.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Functions.isOnline(getApplicationContext())) {
finish();
popupWindow.dismiss();
launchActivity(clss);
} else {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.label_no_internet), Toast.LENGTH_LONG).show();
}
}
});
}
}
Set a boolean that's set to true if your popup is open. Something like,
boolean popUpOpen = true;
Override the onBackPressed Method:
#Override
public void onBackPressed() {
if(popUpOpen){
super.onBackPressed(); // Closes Popup.
}
super.onBackPressed(); // Normal Behaviour - Finishes Activity if popup is open
}

I want to access values from a popup back to the activity from which the popup has been called

I've an Activity where I've a button which will lead to popup popup window in the same Activity. In that popup, I've multiple fields. And What I all want is to get back those values from popup to the same activity.
I've been stuck with it. Need a help :)
Related Code as follows.
This is the code in onCreate method for calling the popup.
Button button = (Button) findViewById(R.id.button9);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addProductDetails();
}
});
By calling addProductDetails() method popup gets displayed.
So in this method, the code as follows
private String addProductDetails() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.storeproductdetails_layout, (ViewGroup) findViewById(R.id.popup_element1), 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
/* Field Data */
product1Code = (EditText) layout.findViewById(R.id.productCodeee);
quantity1 = (EditText) layout.findViewById(R.id.quantityy);
details1 = (EditText) layout.findViewById(R.id.editText23);
orderValue1 = (EditText) layout.findViewById(R.id.editText36);
productC = String.valueOf(product1Code.getText());
qty = String.valueOf(quantity1.getText());
dts = String.valueOf(details1.getText());
orderVal = String.valueOf(orderValue1.getText());
StringBuffer sb = new StringBuffer("");
sb.append(productC+","+qty+","+dts+","+orderVal);
System.out.println("StringBuffer Value: "+sb.toString());
/* End of the field Data */
Button doneAddingProduct = (Button) layout.findViewById(R.id.doneAddingProduct);
//init your button
Button btnClosePopup = (Button) layout.findViewById(R.id.closePopup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
doneAddingProduct.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Inside onclick of done adding");
Product product = new Product();
product.setProductCode(productC);
product.setQuantity(qty);
product.setDetails(dts);
product.setOrderValue(orderVal);
app.setProduct(product);
Intent intent = new Intent(OrderReturnMgmtSecondActivity.this, OrderReturnMgmtSecondActivity.class);
startActivity(intent);
}
});
//show popup window after you have done initialization of views
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
return sb.toString();
}
Here I was trying to do is two methods in fetching those field data back to the activity.
1) Simply trying to return the field data by concatenating as a string to who ever calls this method.
2) Creating a pojo By the name as Product(which contains the field data) and setting it in application class i.e MyApplication extends Application ( you see that code as app.setProduct(product)) and redirecting it to the same activity and trying to fetch the data from that application.
But still I'm not able to get those field data.
Any help will be appreciated :)
Create an interface
interface Listener {
void onResult(Product product);
}
Pass a listener to the method and call it
addProductDetails(Listener listener){
//...
//when finished:
listener.onResult(product);
//...
}
The caller now has to implement the interface
addProductDetails(new Listener(){
void onResult(Product product){
//do something with product
}
});

android keep dialogbox open until user click on button [duplicate]

i am having an custom alert view which pop ups on a button click event.all the things are going fine.but the problem is:
if user clicks outside alert dialog it disappear.i want to restrict user for clicking out side.I am giving him the choice of cancel/cross button to close alert dialog.
so how to restrict user clicking outside the alert box?
code:
the code in onCreate for button click where i am calling show dialog:
final Button cdButton = (Button) findViewById(R.id.denonCdImage);
cdButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
showDialog(CD_CATG_ID);
}
});
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder;
Context mContext = this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.categorydialog,(ViewGroup) findViewById(R.id.layout_root));
GridView gridview = (GridView)layout.findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
/** Check the id for the device type for image tobe change */
switch(id) {
case 1 : // for the cd image
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {
Toast.makeText(view.getContext(), "Image selected for CD", 3000).show();
cdImageId = getImageId(position);
int elementId = getApplicationContext().getResources().getIdentifier(cdImageId, "drawable", getPackageName());
cdImageView.setImageResource(elementId);
Log.d("CdImageid", ""+cdImageId);
closeDialog(view);
}
});
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
dialog = builder.create();
break;
default:
dialog = null;
}
/** onclick listner for the close button */
ImageView close = (ImageView) layout.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
dialog.dismiss();
}
});
return dialog;
}
any suggestions?
thanks!
There are two methods concerning this behaviour: setCancelable() and setCanceledOnTouchOutside() as you can see in the reference.

Cannot set EditText? Issue From dialog in a fragment - Strange - Android Java

I've come across an issue which I've never had before. I have a Fragment containing a button. This button (ViewBookingButton) shows a popup dialog. When the dialog opens I would like to set a string in an EditText (AllBooking). Unfortunately it crashes and shows NullPointerExecption. What is going wrong? Here is my code:
ViewBookingButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
AlertDialog.Builder ViewBookingHelpBuilder = new AlertDialog.Builder(getActivity());
ViewBookingHelpBuilder.setTitle("view booking:");
LayoutInflater inflater = getActivity().getLayoutInflater();
View DialogLayout = inflater.inflate(R.layout.view_booking, null);
ViewBookingHelpBuilder.setView(DialogLayout);
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
AllBooking.setText("Hello World");//WHEN I REMOVE THIS THE DIALOG WORKS
ViewBookingHelpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
}
});
AlertDialog helpDialog = ViewBookingHelpBuilder.create();
helpDialog.show();
}
});
Change this
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
to
TextView AllBooking = (TextView)DialogLayout.findViewById(R.id.AllBooking);
TextView belongs to the inflated layout and `findViewById looks for the view in the current view hierarchy.

ImageButton changing an Image of another ImageButton causes unintended cycle

Aftermath of this: Image for ImageButton doesn't change as intended
When ImageButton of "ibChamp" is clicked, it opens up champions.xml. In that layout, there is an Imagebutton called "ibAnnie". What I want that to do when clicked is, to change the image of "ibChamp", and to switch to that layout. What happened here is that after "ibAnnie" is clicked, it switches to "create_build_page.xml", and in that is a changed picture of "ibChamp". But that only happens for a split second before changing back to the original, unchanged picture, "create_build_page.xml" layout. When I click the back button, it goes to the "create_build_page.xml" layout with the changed picture, but the buttons do not work. I would gladly provide any additional information required.
I'm still not entirely clear on what you're trying to achieve... but if my understanding is correct, this should get you closer:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.champions);
ImageButton ibAnnie = (ImageButton) findViewById(R.id.ibAnnie);
ibAnnie.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
RelativeLayout test = (RelativeLayout) findViewById(R.id.layoutChampions);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View createView = layoutInflater.inflate(R.layout.create_build_page, null);
test.addView(createView);
ImageButton img = (ImageButton) view.findViewById(R.id.ibChamp);
img.setImageResource(R.drawable.ic_launcher);
img.setTag(R.drawable.ic_launcher); // so you can retrieve it later
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// this will set the imageView of ibAnnie
ibAnnie.setImageView((Integer)view.getTag());
// this will remove the selection view from your layout
((RelativeLayout) findViewById(R.id.layoutChampions)).removeChild(createView);
}
});
// this opens another Activity... you don't want that, at least not for what you seem to be trying to do.
/*try {
Intent open = new Intent("android.intent.action.CREATE");
startActivity(open);
} catch (Exception e) {
}*/
}
});
}

Categories