This question already has answers here:
How to set a Button gray and unclickable? [duplicate]
(3 answers)
Closed 6 years ago.
So right now i am having trouble making the next button unclickable when it is on the last page of the activity. As of right now it goes back to the first screen. How do i make it so that it know when to grey out the button or make it unclickable when the user gets to the last screen.
Here is my code:
public class ReadingActivity extends Activity implements OnClickListener {
private ViewFlipper viewFlipper;
Button btnNext, btnPrev;
private float lastX;
/** Called when the activity is first created */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reading);
viewFlipper=(ViewFlipper)findViewById(R.id.view_flipper);
btnNext=(Button)findViewById(R.id.btnNext);
btnPrev=(Button)findViewById(R.id.btnPre);
btnNext.setOnClickListener(this);
btnPrev.setOnClickListener(this);
btnNext.setEnabled(true);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.btnNext:
viewFlipper.setInAnimation(this, R.anim.in_from_right);
viewFlipper.setOutAnimation(this, R.anim.out_to_left);
viewFlipper.showNext();
break;
case R.id.btnPre:
viewFlipper.setInAnimation(this, R.anim.in_from_left);
viewFlipper.setOutAnimation(this, R.anim.out_to_right);
viewFlipper.showPrevious();
break;
}
}
}
you can set the OnClickListener to null like so
btnNext.setOnClickListener(null);
I think you just want this method
button.setClickable(false);
To make a button grey and UnClickable
put android:enabled="false" in button tag
And using code button.setEnabled(false);
Related
I am working on FAQ page I don't want to use Expandable list view and stuff.
So I set 2 TextViews(1 for Question and 1 for Answer) and made one clickable.
The above image shows when the first textview mfaq is clicked it sets second one mAns to visible.
The below code works well to Set the mAns textview visible:
public class faq extends AppCompatActivity {
TextView mfaq,mAns;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_faq);
mfaq=findViewById(R.id.faq);
mAns=findViewById(R.id.ans);
mfaq.setOnClickListener(new View.OnClickListener() {
int counter=0; //setting counter to count onclick
#Override
public void onClick(View view) {
++counter; //incrementing counter first click
if(counter==1){
mAns.setVisibility(View.VISIBLE);
}
//this sets mAns visible , but when i click on it again i want it to hide the text view
counter=0; //resetting the counter
}
});
}
}
So I want to set the visibilty to gone when the textview is clicked again(Should function like Click-visible,ClickAgain-Invisible,Repeat).
Note-I am a beginner please try to explain me what the code is doing so I learn more :)
Thanks.
If I understand well you wanna hide/show your textview each time you click on the other text?
mfaq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mAns.getVisibility() == View.VISIBLE){
mAns.setVisibility(View.GONE);
}
else
mAns.setVisibility(View.VISIBLE);
}
});
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
New to Android developing, using Android Studio. The first activity of my app is a simple main menu with a "Start" and a "Quit" button. When i press the "Start" button, the app should take me to the second activity named "EncounterScreen". Instead, the app crashes. enter image description here. I am trying to display the current health of the player object, of class Player. Apparently the problem is with "TextView healthText=findViewById(R.id.healthText);". This is the error: "Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference"
public class EncounterScreen extends AppCompatActivity implements View.OnClickListener {
Player player=new Player();
TextView healthText=findViewById(R.id.healthText);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_encounter_screen);
Player player=new Player();
TextView healthText=findViewById(R.id.healthText);
healthText.setText("Health "+player.getCurrentHP());
Button attackButton=findViewById(R.id.attackBtn);
Button drinkPotButton=findViewById(R.id.drinkPotBtn);
Button runButton=findViewById(R.id.runBtn);
attackButton.setOnClickListener(this);
drinkPotButton.setOnClickListener(this);
runButton.setOnClickListener(this);
}
#SuppressLint("SetTextI18n")
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.drinkPotBtn:
player.drinkPotion(player);
healthText.setText("Health "+player.getCurrentHP());
break;
}
}
Make sure the TextView id that you are trying to find has the correct id. You are using "R.id.healthText"
Does the TextView in your activity_ecounter_screen have a different id?
Player player=new Player();
TextView healthText=findViewById(R.id.healthText);
I believe the issue is with the above line. remove =findViewById(R.id.healthText).
This question already has answers here:
android change image button background
(4 answers)
Closed 6 years ago.
how to change image button with another image button for example :
Play and Pause
You can just change the icon on the button, instead of changing the entire button.
To change the icon programmatically you can just do this:
ImageButton btn = (ImageButton)findViewById(R.id.button);
btn.setImageResource(R.drawable.your_image);
If however, you wish to replace the button with a different button, you can create two buttons in the same location using a RelativeLayout, and then hide or show the buttons, based on what you want.
To do this:
ImageButton playBtn = (ImageButton)findViewById(R.id.play);
ImageButton pauseBtn = (ImageButton)findViewById(R.id.pause);
playBtn.setVisibility(View.GONE);
pauseBtn.setVisibility(View.VISIBLE);
You don't need to change the button, just the text / icon on the button.
Button button = (Button) findViewById(R.id.button_play_pause);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (playing) {
playing = false;
button.setText("PLAY");
} else {
playing = true;
button.setText("PAUSE");
}
}
});
Further reading: https://developer.android.com/reference/android/widget/Button.html
This question already has answers here:
How to make Views with an Invisible attribute 'Visible' after clicking a button
(4 answers)
Closed 7 years ago.
I have four EditText that I set to invisible in the XML and when the button is clicked, I want them to be visible in pairs. For example, when the button is clicked, I want et1 and et2 to be visible, then when the button is clicked again eet1 and eet2 to be visible. And when all of them are visible, i want the TextView to be visible .
public class app extends ActionBarActivity {
EditText et1;
EditText et2;
EditText eet1;
EditText eet2;
TextView sum;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app);
et1 = (EditText)findViewById(R.id.et1);
et2 = (EditText)findViewById(R.id.et2);
eet1 = (EditText)findViewById(R.id.eet1);
eet2 = (EditText)findViewById(R.id.eet2);
sum = (TextView)findViewById(R.id.sum);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick();
}
});
}
public void onClick() {
et1.setVisibility(View.VISIBLE);
eT1.setVisibility(View.VISIBLE);
eet1.setVisibility(View.VISIBLE);
eet2.setVisibility(View.VISIBLE);
}
}
Set up a variable where you store how many times you clicked the button:
public class app extends ActionBarActivity {
int counter;
...
Then in onClick you increment counter and distinguish cases:
public void onClick() {
switch(counter):
case 0:
et1.setVisibility(View.VISIBLE);
...
break;
case 1:
...
break;
case ...
}
counter++;
}
public void onClick() {
if (if (et1.getVisibility() == View.INVISIBLE &&
et2.getVisibility() == View.INVISIBLE)) {
et1.setVisibility(View.VISIBLE);
eT1.setVisibility(View.VISIBLE);
} else if() ///... you Get the idea
}
i know the brainy people wont like my petty questions but im trying to learn
I,m trying to make a pairs game i have been using int so far on my apps but this game needs a different approach ive created the pairs game with ints but confusing code and a floor that pushing same button twice will delete the pair as below ive been trying with tags the code all looks clean as in no errors
public class MainActivity extends Activity {
//added Tag here for the if (pic2.getTag()==(beck));
Tag beck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageButton pic1 = (ImageButton ) findViewById(R.id.imageButton1);
final ImageButton pic2 = (ImageButton ) findViewById(R.id.imageButton2);
pic1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic1.setBackgroundResource(R.drawable.becks);
pic1.setTag(R.drawable.becks);
if (pic2.getTag() == pic1.getTag()){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);}
}});
pic2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic2.setBackgroundResource(R.drawable.becks);
pic2.setTag(R.drawable.becks);
if (pic1.getTag() == pic2.getTag()){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);
}
}});
}}
ive tried since my original post to work out how to do ive shown code for 2 buttons all i want to do is compare and make invisible after the second button is clicked
if (pic1.getTag().equals(pic2.getTag())){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE); }
the .equals crashes the app
pic1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic1.setBackgroundResource(R.drawable.becks);
pic1.setTag(beck);
if (pic2.getTag()==(beck));{
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);}
}});
this works with or without semi but both buttons dissapear when either button clicked
if (pic1.getTag()==(pic2.getTag())){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE); }
this changes the image but the buttons don,t disappear when second image clicked trying to not use ints if possible
this line works with comma to
if (pic1.getTag()==(pic2.getTag()))
if (pic1.getTag()==(pic2.getTag()));
with effect of both button disappear on 1 click of either button dread moving to the else if lol
Also can a Tag be removed if the pair of images compared if false eg
if no match remove the button tag and reset all remaining images to Default image as when i put all 24 buttons on i need a reset method
i,m finding the semi colon at end of if statement has different effects to not having can anyone point correct way when and when not to use semi colons
Use tags for saving your image-id:
pic1.setTag(R.drawable.becks);
pic2.setTag(R.drawable.becks);
You can then check and compare those by calling getTag() on the buttons that have been clicked:
public boolean isMatch(View x, View y) {
return x.getTag() == y.getTag();
}