My custom dialog layout has 2 edit text fields.
<EditText
android:id="#+id/e2"
android:inputType="none"
android:longClickable="false"
android:selectAllOnFocus="true"
android:textIsSelectable="true" />
<EditText
android:id="#+id/e2"
android:inputType="none"
android:longClickable="false"
android:selectAllOnFocus="true"
android:textIsSelectable="true" />
I implement my class using OnFocusChangeListener.
My idea is when I click the text field, one custom popup dialog appears to select my pre-defined value.
It work correctly but I have a minor problem.
If I click the editext 1 => dialog apear (I select the button then it dismisses) => click editext 2 => dialig appear (I select the button then it dismisses).
However, if If I click the editext 1 => dialog appears (I select the button then it dismisses), then I click on editext 1 immediately again (even many times I try), my dialog does not show up (In expectation, it should appear whenever I click).
My Class
public class user_input extends Activity implements OnFocusChangeListener {
#Override
public void onFocusChange(View v, boolean hasFocus) {
final EditText edt = (EditText) findViewById(v.getId());
if (hasFocus) {
final Dialog dialog = new Dialog(user_input.this);
dialog.setContentView(R.layout.input_dialog);
dialog.setTitle("SELECT A NUMBER: ");
dialog.show();
btn1 = (Button) dialog.findViewById(R.id.val1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
edt.setText("1");
dialog.cancel();
}
});
btn2 = (Button) dialog.findViewById(R.id.val2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
edt.setText("2");
dialog.cancel();
}
});
}
}
How to show it as my expectation? Thank you very much in advanced.
Related
I am new to Android Studio programming. My program has a recyclerview with 3 Textviews, and one image. I would like an alert dialog to appear based on where the user clicks on the screen, for instance if the user clicks on the image the dialog says, "You clicked on image". If the user clicks on the text, it would say "You clicked on the text".
My onclick listener is as follows: itemView.setOnClickListener(this);
#Override
public void onClick(View view) {
int index = getAdapterPosition();
if (playerNames.equals(index)) {
showDialogPlayerNames();
}
if (numberPlayers.equals(index)){
showDialogPlayerNumber();
}
if (ScorePlayers.equals(index)){
showDialogPlayerScore();
}
if (picturePlayers.equals(index)) {
showDialogPlayerImage();
}
}
}
Here is my alert dialog code:
void showDialogPlayerNames() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Click Information");
alertDialog.setIcon(R.drawable.ic_baseline_info_24);
alertDialog.setMessage("You clicked Player Name");
alertDialog.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
alertDialog.create().show();
}
Thank you
You need to add parameters in your methods
showDialogPlayerNames("Something text here");
void showDialogPlayerNames(String myText) {
alertDialog.setMessage(myText);
}
so I have an app with three different buttons(parent buttons) on pressing any of them, the custom alert dialog with 9 different buttons is displayed but the functions of these 9 buttons within the alert dialog are different depending on which of the three parent buttons called it. On pressing any of the 9 buttons, I want the app to perform a particular function and then close the alertdialog. Now the problem is that I can easily call the alert dialog by calling the method showcustomdialog(); that I created but I can't dismiss it using alertdialog.dismiss(); inside the OnClickListener of the parent button since the method has a void result type. I've tried using if-else statements but it doesn't work. How can I achieve what is required?
The method:
private void showCustomDialog() {
ViewGroup viewGroup = findViewById(android.R.id.content);
View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_main2, viewGroup, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
}
I am calling the meathod and using it as follows:
parentbutton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showCustomDialog();
alertbutton1.getId();
alertbutton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView1.setText("500");
function();
//I want to dismiss the alertdialog here.
}
});
alertbutton2.getId();
alertbutton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView1.setText("1000");
function();
//I want to dismiss the alertdialog here.
}
});
and so on.
I figured out a solution for you using AlertDialog.
parentbutton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createDialog();
}
});
Then the method MUST be a private or public AlertDialog:
private AlertDialog createDialog() {
LayoutInflater inflater = (getActivity()).getLayoutInflater(); // for fragment or getLayoutInflater(); for activity
View v = inflater.inflate(R.layout.dialog_add_new_list, null);
Button okButton = v.findViewById(R.id.confirm_button);
Button cancelButton = v.findViewById(R.id.cancel_button);
final AlertDialog dialog = new AlertDialog.Builder(getActivity()) // for fragment or AlertDialog.Builder ( this ) for activity
.setView(v)
.show();
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View p1) {
dialog.dismiss();
}
});
return dialog;
}
P.S. Replace the ids I used to create this sample with yours.
I'm creating an Android app and i need to add text on a picture.
I need to do a snapchat-like to add my text : I managed to display an editText when you click on the picture but then i have to tap again on the EditText to display the keyboard.
But I need only one click to display the EditText AND the Keyboard.
Thanks for your help.
ImageView iv = (ImageView)findViewById(R.id.imageView1);
iv.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
EditText et=(EditText)findViewById(R.id.editText1);
et.setVisibility(View.VISIBLE);
}
});
in case someone is in the same problem, here's the answer :
ImageView iv = (ImageView)findViewById(R.id.imageView1);
iv.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
EditText et=(EditText)findViewById(R.id.editText1);
et.setVisibility(View.VISIBLE);
et.setFocusableInTouchMode(true);
imm.showSoftInput(et, 0);
}
});
And for the XML file set the EditText like this :
android:visibility="invisible"
I have two menu buttons in separate fragments as shown in the codes below. Strangely, when I press the button ("Home") in the first fragment (and of course, the button in the second fragment), it fires the onOptionsItemSelected in the 2nd fragment.
If I have onOptionsItemsSelected in both fragments, the one in the first fragment is fired for the buttons in both fragments, and the menu id appears as 1 for both buttons.
What can I do to make different buttons do different things? (by firing different events or by generating different menu id's.)
public static class MenuFragment extends SherlockFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add("Home").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
/**
* Second fragment with a menu.
*/
public static class Menu2Fragment extends SherlockFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add("Filter").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) // This gets fired when Menu 1 is selected
{
int id = item.getItemId();
Toast.makeText(getActivity(), "Option " + id+ " selected", Toast.LENGTH_SHORT).show();
Double clicks are being captured as single click :(
In fxml file:
<Button fx:id="A_button" onMouseClicked="#buttonAClicked">
In the controller
private void buttonAClicked(MouseEvent mouseEvent) {
if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
if (mouseEvent.getClickCount() == 2) {
System.out.println("Double clicked A_button");
}
if (mouseEvent.getClickCount() == 1) {
System.out.println("Single clicked A_button");
}
}
}
Unfortunately I am finding that double click is not being caught - only single clicks.
In the debugger, click count is 1.
Update:
Since I cant figure out why it is not working for me on JavaFX 2.2.3-b05, I did a workaround and removed the need for double-click. I added a "Load" button to the UI. Now user must single click AND press the load button.
It was fixed in JavaFX 2.2, see http://javafx-jira.kenai.com/browse/RT-19346
Note, that on double click you will receive two events:
mouse click with getClickCount() == 1
mouse click with getClickCount() == 2
E.g. if you run code below and double click on button output would be:
clicks: 1
clicks: 2
Code (tested with 2.2.4):
public class DoubleClicks extends Application {
#Override public void start(Stage stage) {
Button btn = new Button();
btn.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println("clicks: " + event.getClickCount());
}
});
stage.setScene(new Scene(new Group(btn), 300, 250));
stage.setTitle(VersionInfo.getRuntimeVersion());
stage.show();
}
public static void main(String[] args) { launch(); }
}