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);
//...
}
Related
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_apple = (Button) findViewById(R.id.button_apple);
Button btn_cherry = (Button) findViewById(R.id.button_cherry);
Button btn_orange = (Button) findViewById(R.id.button_orange);
Button btn_waterLemon = (Button) findViewById(R.id.button_waterlemon);
btn_apple.setOnClickListener(new View.OnClickListener() {
boolean action = false;
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_apple:
if (!action) {
action = true;
btn_apple.setText("1");
}
else {
int i = Integer.parseInt(btn_apple.getText().toString());
btn_apple.setText(String.valueOf(i + 1));
}
break;
case R.id.button_cherry:
action = false;
if (!action) {
action = true;
btn_cherry.setText("1");
}
else {
int i = Integer.parseInt(btn_cherry.getText().toString());
btn_cherry.setText(String.valueOf(i + 1));
}
break;
}
}
});
}
}
I need to make it so that if a user clicks on button_apple and doesn't click on it for 3 seconds, its text becomes so superfluous. And if anyone knows why my text changes in button_apple, but not in button_cherry. Tell me, please.
You're setting the listener on your apple button. The cherry button doesn't have a listener on it.
Reconsider what you're trying to achieve and simplify it.
If the user clicks the apple button you need to do something.
If the user clicks the cherry button you need to do something (maybe something else).
If the user clicks the watermellon button, ... and so on.
// define your listeners
View.OnClickListener appleListener = new View.OnClickcListener() {
#Override
public void onClick(View v) {
// do whatever you need to do when the apple button is clicked
}
};
// same thing for cherry listener, make a listener to handle the click action
View.OnClickListener cherryListener = ...
// register the listeners
btnApple.setOnClickListener(appleListener);
btnCherry.setOnClickListener(cherryListener);
...
EDIT
To make something happen after a set amount of time you have to consider:
the inputs: which objects / variables influence the action
the output: what's supposed to happpen if all inputs are valid
the duration
With android you could use a handler. See the postDelayed method.
long delayMillis = 3000L; // duration after which to run your task
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after the delay in milliseconds
}
}, delayMillis);
All of this stuff has to be inside the click listener with the logic you need to implement.
"If a user clicks on button apple and doesn't click on it for three seconds"
You could do something like
View.OnClickListener appleListener = new View.OnClickcListener() {
AtomicInteger clicks = new AtomicInteger(0);
#Override
public void onClick(View v) {
int numClicks = clicks.incrementAndGet();
if (numClicks == 1) {
long delayMillis = 3000L; // duration after which to run your task
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (clicks.get() == 1) {
// they only clicked once, do whatever you need to do to make the text superfluous
}
// put the number of clicks back to 0
clicks.set(0);
}
}, delayMillis);
}
} else {
// TODO
// it's been clicked more than once
// show a toast if you need to or do something else
}
}
I didn't test this so you'll probably have to modify it a bit but that's the general idea.
I am fairly new to java but in this case, the button is not responding at all when it is clicked, no errors in the logcat are showing up, the ID of the button is correct and no other posts on here helped solve the issue, this is not all the code but hopefully, this will be enough.
public class activity_main extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_layout);
getSupportActionBar().hide();
}
int error_count;
public void on_click() {
Button page_2 = findViewById(R.id.page_2);
page_2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
int mother_edu_input = Integer.parseInt(((EditText) findViewById(R.id.mother_edu_input)).getText().toString());
error_writer("Text_View_Warning_1", mother_edu_input, 4);
if (error_count > 0) {
throw new NullPointerException();
} else {
Intent page_1_button = new Intent(activity_main.this, revision_time.class);
startActivity(page_1_button);
}
} catch (NullPointerException npe) {
}
}
});
}
You set your listener inside a method called on_click() and it doesn't appear like you call this method anywhere.
You should probably call on_click() inside your onCreate() to set the listener when creating your activity.
In my MainActiviy class I want to display image views of smiley faces based on the number of clicks that occur on the buttons jokes, poems and funnystories combined. However my switch statement does not seem to working as no images appear. Also if any of those image views become visible, then they should remain visible even after the user closing the app and reopening it.
I also notice a click count increasing by one when the user opens the app which is not correct. It should increase based on the buttons mentioned previously being clicked.
public class MainActivity extends AppCompatActivity {
SharedPreferencesManager prefManager = SharedPreferencesManager.getInstance(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button jokesButton = findViewById(R.id.button_jokes);
Button poemsButton = findViewById(R.id.button_poems);
Button funnyStoriesButton = findViewById(R.id.button_funny_stories);
ImageView yellowSmileyFace = findViewById(R.id.yellow_happy);
ImageView greenSmileyFace = findViewById(R.id.green_happy);
ImageView redSmileyFace = findViewById(R.id.red_happy);
jokesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prefManager.increaseClickCount();
openContentPage("jokes");
}
});
poemsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prefManager.increaseClickCount();
openContentPage("poems");
}
});
funnyStoriesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prefManager.increaseClickCount();
openContentPage("funnystories");
}
});
TextView clickCountText = findViewById(R.id.click_count);
clickCountText.setText(Integer.toString(prefManager.increaseClickCount()));
switch (prefManager.increaseClickCount()){
case 4 :
yellowSmileyFace.setVisibility(View.VISIBLE);
break;
case 8 :
greenSmileyFace.setVisibility(View.VISIBLE);
break;
case 12 :
redSmileyFace.setVisibility(View.VISIBLE);
break;
default :
yellowSmileyFace.setVisibility(View.INVISIBLE);
greenSmileyFace.setVisibility(View.INVISIBLE);
redsmileyFace.setVisibility(View.INVISIBLE);
}
}
private void openContentPage(String v) {
Intent intentContentPage = new Intent(MainActivity.this, Content.class);
intentContentPage.putExtra("keyPage", v);
startActivity(intentContentPage);
}
}
below is the Shared preferences class
public class SharedPreferencesManager {
private static final String APP_PREFS = "AppPrefsFile";
private static final String NUMBER_OF_CLICKS = "numberOfClicks";
private SharedPreferences sharedPrefs;
private static SharedPreferencesManager instance;
private SharedPreferencesManager(Context context) {
sharedPrefs = context.getApplicationContext().getSharedPreferences(APP_PREFS, MODE_PRIVATE);
}
public static synchronized SharedPreferencesManager getInstance(Context context){
if(instance == null)
instance = new SharedPreferencesManager(context);
return instance;
}
public int increaseClickCount() {
int clickCount = sharedPrefs.getInt(NUMBER_OF_CLICKS, 0);
clickCount++;
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putInt(NUMBER_OF_CLICKS, clickCount);
editor.apply();
return clickCount;
}
}
You need to add a getter for your clicks
public int getClicks(){
return sharedPrefs.getInt(NUMBER_OF_CLICKS, 0);
}
Whenever you want to get your clicks currently you are calling increaseClickCount() which causes your clicks to increment before returning them. That is why it gains clicks every time you open the stage and why your switch isn't working correctly
so add the above getter to your SharedPrefrenceManager and change these two lines
switch (prefManager.increaseClickCount()){
to
switch (prefManager.getClicks()){
clickCountText.setText(Integer.toString(prefManager.increaseClickCount()));
to
clickCountText.setText(Integer.toString(prefManager.getClicks()));
Tell me if that fixes your problem
The reason for counts' increase is you use increaseClickCount() to receive click count.You have to create another method to receive current clickCount. Your switch statement works only when they equal to 4,8 or 12. Maybe you should use if instead.
I also notice a click count increasing by one when the user opens the app which is not correct
It looks to me like this line of code, in MainActivity.onCreate() method will pass a text String of count 1 to clickCountText.
clickCountText.setText(Integer.toString(prefManager.increaseClickCount()));
Also, every time you call SharedPreferencesManager.increaseClickCount, you are assigning a value to clickCount, and whatever was there gets overwritten.
int clickCount = sharedPrefs.getInt(NUMBER_OF_CLICKS, 0);
What is that value?
System.out.println is your friend.
I use this pattern
System.out.println("MyClass, MyMethod, MyVariable:" + myVariable);
I always include the class and method because it can be annoying trying to figure out where println are coming from if you leave several in for debugging purposes and want to get rid of them later.
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
I am new to android development using JAVA, and I'm having an issue with a simple app I created. (Please don't laugh at it!)
All it's supposed to do is display a number in an editable textview; The three buttons on this main activity are a +, -, and reset. Super simple, right? I can't tell what I did wrong, but everytime I run the app to test, and then click on any of the buttons, it exits the app and goes back to the android home screen. Not sure what I did wrong... but here's the code I have so far:
public class Main extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn_Plus;
Button btn_Minus;
Button btn_Reset;
final EditText sCount= (EditText) findViewById(R.id.txtCount);;
btn_Plus=(Button)findViewById(R.id.btnPlus);
btn_Minus=(Button)findViewById(R.id.btnMinus);
btn_Reset=(Button)findViewById(R.id.btnReset);
//One way I tried to work it
btn_Plus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//gets the text in the textview, makes it a string,
//converts it to an int, calculates, then puts it
//back.
String iCounter = sCount.getText().toString();
int iCount = Integer.parseInt(iCounter);
iCount += 1;
Integer.toString(iCount);
sCount.setText(iCount);
}
});
//The second way I tried -- neither way works.
bM.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int iCount = Integer.parseInt(sCount.getText().toString());
iCount -= 1;
if(iCount >0)
iCount = 0;
Integer.toString(iCount);
sCount.setText(iCount);
}
});
bR.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int iCount = 0;
Integer.toString(iCount);
sCount.setText(iCount);
}
});
}
}
Thanks so much!
Use a TextView instead, unless you plan to allow the user to directly enter the number (which it seems like you don't). Rather than do text manipulation to get the number, why don't you just store the number as a member variable, update that when a button is pressed, and change the text accordingly? This is how I would write it:
public class Main extends Activity {
int count = 0;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.txtCount);
findViewById(R.id.btnPlus).setOnClickListener(this);
findViewById(R.id.btnMinus).setOnClickListener(this);
findViewById(R.id.btnReset).setOnClickListener(this);
}
private void updateText() {
String text = Integer.toString(count);
textView.setText(text);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnPlus:
count++;
break;
case R.id.btnMinus:
count--;
break;
case R.id.btnReset:
count = 0;
break;
}
updateText();
}
}