textView.setText does not update text but also gives no errors - java

I have a PopupWindow that has a TextView in it. I am trying to change the text in this TextView dynamically but can't seem to get it to work. The code executes, but it does nothing and gives no errors, making it hard to understand what's happening (as there is nothing that shows up in the debug logs). It's important to note that this TextView is not in the same layout as the layout given to setContentView - and that menu is called when a button is pushed in the main layout. Does anybody have any idea why this could be happening?
Here is my code:
public void popupView(View view, int viewPointer){//viewPointer is R.id.popup_layout
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(viewPointer, null);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
popupWindow = new PopupWindow(popupView, width, height,true);//add another parameter here (true) to make it so it goes away when clicked outside the menu itself
popupWindow.setElevation(20);
// show the popup window
// which view you pass in doesn't matter
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
}
and
public void menu(View view){
textView.setText("this won't change the text but will execute with zero errors...");
popupView(view, R.layout.popup_layout);
}
and in onCreate:
LayoutInflater layoutInflater = getLayoutInflater();
View text_view = layoutInflater.inflate(R.layout.popup_layout, null);
textView = text_view.findViewById(R.id.textViewID);

Related

How to use the setText() Method in a PopUp Window In Android Studio?

I have an editText box in my activity in which the user enters his/her phone number and then clicks on a Next button. Now, on clicking on the Button, I have inflated a popup view that shows the user his/her entered number and asks for confirmation.
The problem here is that I am not able to access the number from the EditText to display it in the Popup View. Could anyone tell me how could it be done?
Layout Inflator:
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
#SuppressLint("InflateParams") View popupView = inflater.inflate(R.layout.confirm_number_popup, null);
ViewGroup root = (ViewGroup) getWindow().getDecorView().getRootView();
Button btn1 = popupView.findViewById(R.id.btn_edit);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
final PopupWindow popupWindow = new PopupWindow(popupView, width,
height, false);
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
}
When I add this in the inflator, I get a null pointer exception:
TextView txt_phone_confirm =
findViewById(R.id.txt_phone_number_confirm);
EditText txt_Phone = findViewById(R.id.Phone_Number);
txt_phone_confirm.setText(txt_Phone.getText().toString());
On doing this, I get:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText(java.lang.CharSequence)' on a null
object reference
Invoke the find view by id method on the popupView, as this is the view that has your TextView:
TextView txt_phone_confirm = popupView.findViewById(R.id.txt_phone_number_confirm);
This will get you the text view you just inflated.

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
}

How does onClickListener keep track of the view to be removed

I am following a tutorial here - Now instead of adding a textView I have the following method as a replacement:
public void addView(LinearLayout container, String[] spin_array, String hint, int inputType, int tagger) {
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.add_company_fragment_two_dynamic_content, null);
final Spinner spin_dynamic = (Spinner) addView.findViewById(R.id.email_spinner1);
EditText edt_dynamic = (EditText) addView.findViewById(R.id.edittext_email1);
ImageView remove_dynamic = (ImageView) addView.findViewById(R.id.btn_remove);
edt_dynamic.setHint(hint);
setUpSpinners(spin_array, spin_dynamic);
edt_dynamic.setTag(container.getTag() + "edt" + tagger);
edt_dynamic.setInputType(inputType);
edt_dynamic.setTypeface(roboto_condenced_light);
spin_dynamic.setTag(container.getTag() + "spin" + tagger);
idsMap.put(spin_dynamic.getTag().toString(), edt_dynamic.getTag().toString());
// get height from dimens
int height = (int) getResources().getDimension(R.dimen.lin_height);
// set this height
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
// we are only concerned about top margin here.
layoutParams.setMargins(0, (int) getResources().getDimension(R.dimen.topMargin), 0, 0);
container.addView(addView, 1, layoutParams);
remove_dynamic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Remove View (LinearLayout) and also remove the key from idsMap so that we dont get a NullPointer later
((LinearLayout) addView.getParent()).removeView(addView);
idsMap.remove(spin_dynamic.getTag().toString());
}
});
}
The concern here is that I can call the above function umpteen times, to insert a view row in a LinearLayout.
I fail to understand how the addView variable is kept track of, when we try to remove a view from the container:
remove_dynamic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Remove View (LinearLayout) and also remove the key from idsMap so that we dont get a NullPointer later
((LinearLayout) addView.getParent()).removeView(addView);
idsMap.remove(spin_dynamic.getTag().toString());
}
});
How does the onClickListener know the addView instance to be removed here, if I insert four views how does teh onClick exactly know which one to remove?
There are no errors, everything is working as it should. But why is it working?
Following is an image representing this behavior:
you have to use the same viewobject while removing view for example you have added three view in ViewGroup
addView1
addView2
addView3
so when you want to remove fistview i.e. addView1 you have to use that object ((LinearLayout) addView.getParent()).removeView(addView1);
It's working due to this line
((LinearLayout) addView.getParent()).removeView(addView);
onClickListener() does not keep track of anything.

Open PopupWindow in PopupWindow

I want to open a PopUpWindow in another PopUpWindow. I have an ImageButtons in my MainActivity. When I click on it a PopUpWindow appears. I use it as a kind of submenu in my app. In my first PopupWindow is another ImageButton. If I click on it a second PopupWindow should appear and overlay the first one.
Opening the first PopupWindow works just fine. When I click on the button in it to open the second one, the app crashes. How can I make the second PopupWindow work?
Thanks for your help.
I tried it likes this:
final ImageButton btnOpenPopup = (ImageButton) findViewById(R.id.button_name);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater
= (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_fertig, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(btnOpenPopup, Gravity.TOP | Gravity.RIGHT, 0, 0);
Button btn_2 = (Button) popupView.findViewById(R.id.button_2);
btn_2.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater layoutInflater_2
= (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView_2 = layoutInflater.inflate(R.layout.popup_2, null);
final PopupWindow popupWindow_2 = new PopupWindow(
popupView_2,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popupWindow_2.showAtLocation(btn_2, Gravity.TOP | Gravity.RIGHT, 0, 0);
}
}
});
}
I'm also trying to do what you are doing but was not successful however I did figure out a workaround for what you want to do.
Inside your popupView xml layout, you would have to create a framelayout as the parent layout and then put both your popupView and popupView 2 layout within the parent layout. You would then switch on and off the visibility for each of the two layouts when a button is pressed in the popupView.
It actually works quite nicely, the popupView resizes itself appropriately according to the content it holds with smooth animations.

PopUp window to be opened to touches within an Imageview

I have a big image which contains different sections. What I want is different PopUps should open when the user touches different sections of the ImageView. For instance, see this Image below:
In this image, what I want is that when the user clicks 1st square, Popup 1 should open, on square 2 popup should open. How to achieve this please.?
Moreover, I want the ImageView still be zoom and pan enabled. Please help.
Hello you can follow my this post . I think my answer will help you ::
popup window from android options menu not working
For you I have edited the code. You can follow in this manner. If button will be Image and replace button with that :
inside onCreate()
Button btn1=(Button) findViewById(R.id.btn1);
Button btn2=(Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
initiatePopupWindow1();
}
})
btn2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
initiatePopupWindow2();
}
})
outside onCreate()
private PopupWindow pwindo1,pwindo2;
private void initiatePopupWindow1() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopupActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,(ViewGroup)
findViewById(R.id.popup_element));
pwindo1 = new PopupWindow(layout, 350, 350, true);
pwindo1.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
private void initiatePopupWindow2() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopupActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,(ViewGroup)
findViewById(R.id.popup_element));
pwindo2 = new PopupWindow(layout, 350, 350, true);
pwindo2.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
You can use (though it doesn't seem to be the best way of what actually you are trying to achieve but still you can have a single image)
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
and the event.getX() and event.getY() will give you the coordinates where you are clicking
for example
if(event.getX() == 100 and event.getY() == 100)
{ // show your popup}
But there will be a problem if you have multiple images
you have to know at what positions these boxes will be present, and you might face problem if the image is to zoomed

Categories