How to revert selection from radio button after pressing add button - java

So, I have this class project : Class Project
and I want to set all text and radio button selection to be empty after I click the button "Tambah" (it mean add).
I can do it with the text field and text area using this method :
private void kosongkanText(){
txtIdPelanggan.setText("");
txtNamaPelanggan.setText("");
Talamat.setText("");
txtNotlp.setText("");
but how to do it to radio button?. already try this, and not working :
groupJenisKelamin.setSelected(null, rootPaneCheckingEnabled);

I dont really get what is your question, but if you want to uncheck the radio button that already checked you can use setChecked(false)
for example :
private boolean flagpria = false;
private boolean flagwanita = false;
rdbtnMale.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (rdbtnMale.isChecked()) {
if (!flagmale) {
rdbtnPria.setChecked(true);
rdbtnWanita.setChecked(false);
flagpria = true;
flagwanita = false;
} else {
flagpria = false;
rdbtnPria.setChecked(false);
rdbtnWanita.setChecked(false);
}
}
}
});
rdbtnFemale.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (rdbtnFemale.isChecked()) {
if (!flagfemale) {
rdbtnWanita.setChecked(true);
rdbtnPria.setChecked(false);
flagwanita = true;
flagpria = false;
} else {
flagwanita = false;
rdbtnWanita.setChecked(false);
rdbtnPria.setChecked(false);
}
}
}
});
those code above mean you have 2 radio button if you check "pria" in "wanita" this code will uncheck it

Related

Handaling single and double taps android kotlin

I want to display some toast when my button is clicked once and other toast when my button is clicked twice. Is there any built in library or do I need to implement custom logic?
You can Gesture Detector for detecting double taps.
final GestureDetector mDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener {
#Override
public boolean onDown(MotionEvent e) {
return true;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}
});
For more details you can check this documentation.
Answer given by #TaranmeetSingh is absolutely right but there is a better solution. You can use this library to do it.
How to implement
Add this library -> implementation 'com.github.pedromassango:doubleClick:CURRENT-VERSION'(Version is given above).
Instead of using an onClickListenerObject for onClickListener, you can use DoubleClickListener Check the example below
Button btn = new Button(this);
btn.setOnClickListener( new DoubleClick(new DoubleClickListener() {
#Override
public void onSingleClick(View view) {
// Single tap here.
}
#Override
public void onDoubleClick(View view) {
// Double tap here.
}
});

Adding EventListeners to multiple Checkboxes - Android Studio/Java

I'm trying to add event listeners to three checkboxes in my View. But when I test it on an android tablet, only one of the eventlisteners for the checkboxes works... Please help. (Note: I am very new to programming so a detailed explanation is appreciated. Thanks!)
Edit1: The model class is basically in-charge of generating a REGEX string. The three checkboxes give the user an option to include/exclude numbers/digits/anchors to create the REGEX string. The code below is the controller in charge of adding eventlisteners to the 3 checkboxes, though I am not sure if I am doing it right.
EDIT2: Fixed it by adding final before CheckBox... no clue why it works :S
// a method in my controller
public void checkListener() {
CheckBox dig = (CheckBox) findViewById(R.id.dig);
CheckBox blarg = (CheckBox) findViewById(R.id.blarg);
CheckBox duff = (CheckBox) findViewById(R.id.duff);
dig.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (((CheckBox) view).isChecked()) {
model.setLetter(true); //setting a bool val in my model class
newRegex(); // this method generates a regex string from the model class and outputs it into the View }
else {
model.setLetter(false);
newRegex(); // generates a regex string and updates the view
}
}
blarg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (((CheckBox) view).isChecked()) {
model.setSomething(true); //setting a bool val in my model class
newRegex();
}
else {
model.setSomething(false);
newRegex();
}
}
duff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (((CheckBox) view).isChecked()) {
model.setAlpha(true);
newRegex();
}
else {
model.setAlpha(false); //setting a bool val in my model class
newRegex();
}
}
}

Prevent ProgressDialog from being dismissed onClick event

This has been asked in previous posts, but I've tried many and none of them seem to work. I have a boolean flag that should in theory prevent my dialog from being closed. Here is what I have so far:
boolean start_match = false;
public void WarmupDialog()
{
if(use_warmup == true)
{
final ProgressDialog spinner = new ProgressDialog(this);
spinner.setTitle("Warmup");
spinner.setCancelable(false);
spinner.setCanceledOnTouchOutside(false);
timer = new CountDownTimer(300000, 1000)//5 minutes
{
#Override
public void onFinish()
{
spinner.cancel();
}
#Override
public void onTick(long l)
{
spinner.setMessage(((int)Math.round(l/1000.0)-1)+"secs remaining of warmup");
}
};
spinner.setButton(DialogInterface.BUTTON_POSITIVE, "Start Warmup", (DialogInterface.OnClickListener)null) ;
spinner.show();
Button button = spinner.getButton(DialogInterface.BUTTON_NEUTRAL);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(start_match)
{
spinner.dismiss();
}
else
{
start_match = true;
Button button = spinner.getButton(DialogInterface.BUTTON_POSITIVE);
button.setText("Start Match");
timer.start();
}
}
});
spinner.setOnCancelListener(new DialogInterface.OnCancelListener()
{
#Override
public void onCancel(DialogInterface dialog)
{
timer.cancel();
ChooseServer();
}
});
}
else
{
ChooseServer();
}
}
This open a warm up timer for a tennis match. When the button is pressed for the first time the timer should start. When the button is pressed again the dailog should then cancel. At the moment the dialog is dismissed immediately. Any ideas?
Thanks in advance for any help!
As far as my knowledge about Progress dialogs goes, it should be the normal behavior.
As ProgressDialog inherits from AlertDialog -> Dialog and by default they get dismissed when user press a button on the dialog, regardless of which button it is (positive/negative etc).
Suggestion: intercepting the touch event & detecting the source position of the touch event (i.e on which button user has touched), we should be able to override this behavior.

How attach two Click Listener in a button in android?

I want to attach two click listener in a button and both onclicklistener should be different. Is there any way to do this.? Can't use function to call from first to second. When I use this then I got output from second one only. I want both output.
I am working on some screening task, so whenever a use click on a button it tell me that user clicked on this in Logcat and also button do its normal task.
First is :
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("First Click" , "Clicked on button 1");
}
});
Second is :
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v2) {
if (v2 instanceof Button) {
Log.i("User Clicked a checkbox with value ", " : " + ((Button) v2).getText().toString());
}
}
});
You can't do that. Setting your second OnClickListener removes the first one.
That's why the function is called setOnClickListener() instead of addOnClickListener()
As you say, you can call two functions from the onClick()
Edit:
On your edit, you say that you want to show a message when your button is clicked.
You can add the loggging functionality that you need before doing anything else on the event, simply doing
#Override
public void onClick(View v) {
// Log something
// Your functionality
}
Or you can create a class implementing View.OnClickListener
public class MyOnClickListener implements View.OnClickListener {
#Override
public void onClick(View v) {
// Log something
}
}
And then, on your Activity:
btn.setOnClickListener(new MyOnClickListener() {
#Override
public void onClick(View v) {
super.onClick(v);
// Your functionality
}
});
You can use function in OnClickListener like-
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
run1();
run2();
}
});
private void run1(){
//Your first functionality
}
private void run2(){
//Your second functionality
}
If I understood you correctly, you could do this:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dothings(); //what you are trying to achieve with this button click
showViewInLogCat(v); //to show you the view in the logcat
}
}
where showViewInLogCat() is a function that show you which view was clicked in your logcat:
public void showViewInLogCat(View view) {
if (view instanceof Button) {
Log.i("User Clicked a checkbox with value ", " : " + ((Button) view).getText().toString());
}
//and add the other if
}
You can call this function in every OnClick event on other views
Probably if you want to do something like this..!
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Log.i("First Click" , "Clicked on button 1");
// add a boolean here to check if you want to do the task or not.
doTask = true;
doGeneralTask(doTask); //to show you the view in the logcat
}
}
and in doGeneralTask(doTask) do something like this.
public void doGeneralTask(boolean doTask) {
if (doTask) {
// do whatever generalized tasks you need.
}
}

How to enable some buttons, when all others are disabled?

I have 16 buttons in my game activity. Plus 4 (I will talk about them later), all 4 disabled at this point. Now, whenever a user click on one of those 16 buttons, I set some text to it and then disable it, so it cannot be used again. Is there a way, when all 16 buttons are clicked and disabled, to enable all of those 4 buttons?
Here is a quick way to do it:
public class example{
int activeButtons = 16;
public void onCreate(Bundle savedInstanceState){
//initialize
}
/**
* This is your onClick method
*/
public void click(View v){
v.setEnabled(false);
activeButtons--;
if(activeButtons == 0){
enable();
}
}
/**
* This will enable your four buttons
*/
public void enable(){
//Get references to your buttons here
Button b1 = ...;
Button b2 = ...;
Button b3 = ...;
Button b4 = ...;
b1.setEnabled(true);
b2.setEnabled(true);
b3.setEnabled(true);
b4.setEnabled(true);
}
}
An example of one way to accomplish what you are asking is simply to set aside a case such as this in your code; if I understand what your question is in regards to what has been posted above:
Button buttonOne = null;
//...fourteen buttons
Button otherButtonOne = null;
//...other four buttons
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupButtons();
}
private void setupButtons()
{
buttonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//..Whatever else you do with button.
if(allButtonsAreDisabled())
{
enableOtherFourButtons();
}
}
});
//...
}
private boolean allButtonsAreDisabled()
{
if(!buttonOne.isEnabled()) // && !buttonTwo.isEnabled() && ...
{
return true;
}
else
return false;
}
private void enableOtherFourButtons()
{
otherButtonOne.setEnabled(true);
//...
}

Categories

Resources