prevent softkeyboard from showing up in popup's which contain edittext? - java

i have a pop up which contains edit text..how can i prevent the softkeyboard from showing up on displaying of pop up's because my pop up is moving up as soon as it is displayed.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
i used this line in the activity in which a pop up is displayed on button click. please help...!
android:windowSoftInputMode="stateHidden"
i also used this line in manifest. but of no use.
public class MainActivity extends Activity implements OnClickListener {
AlertDialog dialog;
View checkBoxView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.des_button).setOnClickListener(this);
//checkBoxView = View.inflate(this, R.layout.popup_description, null);
}
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(this);
dialog.getWindow().getAttributes().windowAnimations =
R.style.dialog_animation;
// dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setContentView(R.layout.popup_description);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Button btn_Start = (Button) dialog.findViewById(R.id.ok);
btn_Start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
Button cancel = (Button) dialog.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
#Override
public void onDestroy() {
if (dialog!=null) {
if (dialog.isShowing()) {
dialog.dismiss();
}

Try
android:windowSoftInputMode="stateAlwaysHidden"
in your manifest in that activity. one more thing you can do call the following method after opening that dialog popup.
public static void hideKeyboard()
{
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(editTextInPopup.getWindowToken(), 0);
}

Related

I want to do INVISIBLE on SubActivity with the button on MainActivity

SubActivity cannot INVISIBLE the button in MainActivity I tried to use intent, but I'm not sure.
it SubActivity java code
try to INSIBLE the resetB button in Sub Activity
public class SubActivity extends Activity {
Button resetH, resetH2;
Button resetB, miC;
#SuppressLint("MissingInflatedId")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
ImageButton back = (ImageButton) findViewById(R.id.back);
resetH = (Button) findViewById(R.id.resetH);
resetH2 = (Button) findViewById(R.id.resetH2);
resetB = findViewById(R.id.resetB);
resetH.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
resetB.setVisibility(View.VISIBLE);
}
});
resetH.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
resetB.setVisibility(View.INVISIBLE);
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
I used intent, but I can't move the button

OnClickListener for Multiple ImageButton

I'm super new in Java! Pardon for this question although there are similar ones but I'm completely clueless on fixing my problem.
I'm trying to set an OnClickListener for each ImageButton to open a new activity.
The first ImageButton works but not for the subsequent ones, it is unclickable in AVD.
Would greatly appreciate some help on it!
public class CharacterSelect extends AppCompatActivity {
ImageButton arrowbtnright;
ImageButton contchibtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_select);
arrowbtnright = (ImageButton) findViewById(R.id.arrowbtnright);
contchibtn = (ImageButton) findViewById(R.id.contchibtn);
arrowbtnright.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(CharacterSelect.this, CharacterSelect2.class));
contchibtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(CharacterSelect.this, MiniChallenge1.class));
}
});
}
});
}
You are currently adding the listener of the second button, from inside the listener of the first button, this is probably not what you want to do .
Try putting both at the same level, i.e :
arrowbtnright.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(CharacterSelect.this, CharacterSelect2.class));
}
});
contchibtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(CharacterSelect.this, MiniChallenge1.class));
}
});

void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object referenc [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I have added one button "btn". I have set onclicklistener, inside the button "btn" I have added another button "btnYes" to show display custom dialog when I add these "btnYes" apps get crash.
When I remove the "btnyes" button app is working.
Can we add onclicklistener for two button inside one button for different work?
Java Code
public class MainActivity extends AppCompatActivity {
private Button btn, btnYes, btnNo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.click);
btnYes = findViewById(R.id.yes);
btnNo = findViewById(R.id.no);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder dialogBox = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View myView = inflater.inflate(R.layout.custom_dialogbox, null);
dialogBox.setView(myView);
final AlertDialog mybuilder = dialogBox.create();
mybuilder.setCancelable(false);
btnYes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mybuilder.dismiss();
}
});
}
});
}
}
Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.customdialog.MainActivity$1.onClick(MainActivity.java:33)
If btnYes and btnNo are on the dialog, then you should initialize these buttons using the AlertDialog's View object.
You have to modify your code like following.
public class MainActivity extends AppCompatActivity {
private Button btn, btnYes, btnNo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.click);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder dialogBox = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View myView = inflater.inflate(R.layout.custom_dialogbox, null);
// these button should be initialize here.
btnYes = myView.findViewById(R.id.yes);
btnNo = myView.findViewById(R.id.no);
dialogBox.setView(myView);
final AlertDialog mybuilder = dialogBox.create();
mybuilder.setCancelable(false);
btnYes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mybuilder.dismiss();
}
});
}
});
}
}
Hope it helps you.
if you want to display Dialog with choices yes or no, use this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// action for yes
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// action for no
}
});
AlertDialog alert = builder.create();
alert.show();
But, if you want to use a custom dialog layout, you should do it like this.
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.YOUR_LAYOUT_HERE);
Button btnyes= dialog.findViewById(R.id.btnyes);
btnyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});

Button finish activity after 2 press is WRONG: Android

I want the button to finish the activity. Why do I have to press it TWICE for it to finish, I do not want this. Note, I have to press the button twice before the activity ends, which I do not want.
MainActivity Class:
private Runnable updateOkay= new Runnable() {
public void run() {
if (true) {
Intent i = new Intent(this, WorkTimerNotification.class);
startActivity(i); }
WorkTimerNotification class:
public Button confirmButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_timer_notification);
confirmButton = (Button) findViewById(R.id.confirmOK_button);
confirmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
}
Solved by setting
android:launchMode = "singleInstance"
in AndroidManifest.xml file.

Android Application crash on dialog Button click

Im trying to make a Dialog close when button is pressed.But Every time i press the button the application crashes.
public class CanvasPaint extends Activity implements OnClickListener {
final Button widthbtn = (Button) findViewById(R.id.widthbtn);
final Button widthpopBtn = (Button) findViewById(R.id.widthpopBtn);
final Context context = this;
final Dialog widthDialog = new Dialog(context);
widthbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthpopBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthDialog.dismiss();
}
});
widthDialog.show();
}
});
}
That's some inception code. Why are you setting a click listener inside a click listener? It should be more like
widthbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthDialog.show();
}
});
widthpopBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthDialog.dismiss();
}
});
Your dialog layout is not properly inflated, and therefore, your widthpopBtn will be null. Try inflating the layout like this:
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.your_dialog_layout, null);
Button widthpopBtn = (Button) dialogView.findViewById(R.id.widthpopBtn);

Categories