how to dismiss a popupwindow when click button? - android

what i want to do
i want to write a button that when i click it will show a popupwindow,and when i click the outside or click the button again the popupwindow will dismiss.
what i do
i use the code like this
mPopupWindow=new PopupWindow();
mPopupWindow.setTouchable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
mPopupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
brush.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPopupWindow.setContentView(brushView);
mPopupWindow.showAsDropDown(v);
}
});
what the problem
when i click the button again it will show Attempted to finish an input event but the input event receiver has already been disposed.
it think it because it tirgger the outside and clicklistener

Please try with below answer:
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
mPopupWindow.setOutsideTouchable(true);

To make the popup dissapear when clicked on the outside use:
myPopupWindow.setOutsideTouchable(true);
To open and close the popup on clicking the button, put this code in your OnClickListener:
if(myPopupWindow.isShowing()) {
myPopupWindow.dismiss();
} else {
mPopupWindow.setContentView(brushView);
mPopupWindow.showAsDropDown(v);
}

Just use
myPopupWindow.setOutsideTouchable(true);

just find a trick
use
mPopupWindow=new PopupWindow(new View(mcontext),WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,true);
i don't know why but it works

Related

How to load a layout inside another one on click and change button function?

I'm trying to do something like this:
When I go to this activity I have what is in black and some objects like EditText boxes.
Once I press the button I want those EditBoxes an other stuff that is up there to stay visible but unable to be edited (that's easy to do from code overriding onClick).
But at the same time I also want to load some layout down inside the same activity (from an xml) and change the button function to act over the objects of the new layout.
Could anyone give me an idea on how to do this two things staying in the same activity?
Update:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
editText1.setEnabled(false);
editText2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
}
});
}
On the first click I want the button to do that. But once it's pressed I want it to do another thing. How could I do that?
(that's easy to do from code overriding onClick).
Actually I would recommend enable or disable which is easier to trace by using
view.setEnabled(bool);
as for the other question I'd recommend adding the layout from the start with setting visibility to GONE and when needed set the visibility to VISIBLE
view.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
Ok, I've realized it was a dumb question, just add a flag an edit it:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!button_pressed) {
editText1.setEnabled(false);
edittext2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
button_pressed=true;
}
else{
create_button.setText("Second click");
create_button.setEnabled(false);
}
}
});
}
}

Android - setOnkeyListen method how does it work with EditText?

I create a dialog window in an activity, then I put the buttons in this dialog window and listened to them. However, I want to listen to android device found in the back button. and dialog.setOnkeyListen method I used for it. I have put the program by executing dialog window EDITTEXT not enter data into the field. So when I add code to the setOnkeyListen I can not enter data into the EditText field. I hope you know what I mean
If you're asking how to set the OnClickListener for a textview here is an example hope this helps:
TextView textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Code you want to execute on the click goes here
}
});

Android Layout button partially appears

So,
I have an activity with a layout, and in this layout I only have one button.
when clicking this button, the activity sets the visibility of the button to invisible, and launches a popup window.
I implemented a simple onDismiss function in this popup, which sets the button to visible
pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
MainActivity.packButton.setVisibility(View.VISIBLE);
}
});
the problem is that sometimes, not very often, after the popup is dismissed, the button is shown, but only the top part of it, something like 1/5 of the button.
I suspected that the button became visible before the popup dismissed completely, and a sort of a clash happened between them, but on the other hand I made some checks and the popup window and the button are able to be shown at the same time without a problem, so a "layout clash" cannot be the reaon, right?
You can add delay and run this method on a handler.
pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
MainActivity.packButton.setVisibility(View.VISIBLE);
}
}, 1000);
};
});
I would suggest making the button variable non static and instead call a method of your activity from your listener and in this method set the button visibility. Having the button as a static variable could mean that although it is non null the button isn't added to the activities view at the point when you call to set is visibility.

How do I select all text when clicking on a EditText view?

so I tried android:selectAllOnFocus and of course I'm using android:hint.
The app loads, requestFocus triggers and the full text is selected.
The problem is that when I click in the EditText the selection is lost.
I've already read:
Select all text inside EditText when it gets focus
For some reason, it worked (on Jelly Bean) only if I posted it with a Handler:
new Handler().post(new Runnable() {
#Override
public void run() {
paidView.selectAll();
}
});
Set the selection in an View.OnClickListener like so:
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setSelection(0, editText.getText().length() - 1);
}
}
Set the selection in an View.OnClickListener like so: (with the 0 and text length opposite to the previously approved response) - this will ensure that when the user starts typing the content will be overridden.
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setSelection(editText.getText().length() - 1, 0);
}
}
I realize this is an old post, but I had this same problem. For me, the reasoning was that I like to move the keyboard out of the way (dismiss it) to get my bearings (or hit buttons to add or remove rows of data) and when i touched back on the EditText I was previously editing, it was annoying to have the keyboard pop back up and the text un-select, forcing me to either work to get the cursor to where I wanted to start deleting, or touch another EditText and then touch back on the original to re-select everything. I just want to have the keyboard pop back up and have the text selected and ready to overwrite whenever I go to that EditText.
There are easy solutions to this:
1) Long tap does this for you on most occasions.
2) If you're already using setSelectAllOnFocus(true), you can just throw a simple clearFocus() and requestFocus() in your onClick listener:
etMyEditText.setSelectAllOnFocus(true);
etMyEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view.clearFocus();
view.requestFocus();
}
});
This way the EditText has everything selected when you tap on it, regardless of soft keyboard status.
Additional bonus:
Add android:windowSoftInputMode="adjustPan" inside your <activity .../> tag in your AndroidManifest.xml file to force the selected EditText to stay in sight when the soft keyboard pops up.
try This
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.selectAll();
//or this.selectAll();
}
To select all the text.
Do the code below after you did findViewById and you are good to go:
edtProductPrice.setSelectAllOnFocus(true);
edtProductPrice.requestFocus();
edtProductPrice.selectAll();

android how to write click event of an ImageView

I have an alarm symbol on my interface. I want it like if anyone clicks on this then he/she can get another interface.
so how to write a click event of Imageview. plz give me answer. Thank you
You can add an OnClickListener to ImageView using setOnClickListener method on your ImageView and write your code in the listener's onClick method.
imageView.setOnClickListener(clickListener);
OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
if (v.equals(imageView)) {
// Write your awesome code here
}
}
};
Faster.
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Your code.
}
});
with lambda expression
img_drawer_open_icon.setOnClickListener(v -> Home.drawerLayout.openDrawer(GravityCompat.START));
Also just do it in some easy steps visually:
1- Click your Image
2- From Attribute, Select OnClick and Type a Name Such as onCkick_btnST
3- In code of Your xml, find and select the above verb
4- using yellow bulb menu, select "Create OnClick Event Handler"
5- Fill your Code in created area.
The benefit of this methode is that U`LL see that event title in structure tab of Android Studio!

Categories

Resources