In my android application, there are number of images in drawable folder. In my layout there are two buttons: Back and Next buttons. On clicking next and back buttons 2 different images get loaded on the same layout(common for all images).
Problem:I am able to load images on Next/Back button's click but after reaching the last image, I want to make my Next button disable and same for the back button.As the user is on the first image the back button should be disabled. Code is as:
public class SequencerActivity extends Activity implements OnClickListener
{
private int imageCounter = 0;
private ImageView imageLoader;
private int[] imageList = {R.drawable.image_wo_lbl_0, R.drawable.image_wo_lbl_1, R.drawable.image_wo_lbl_2, R.drawable.image_wo_lbl_3, R.drawable.image_wo_lbl_4, R.drawable.image_wo_lbl_5,
R.drawable.image_wo_lbl_6, R.drawable.image_wo_lbl_8, R.drawable.image_wo_lbl_9,R.drawable.image_wo_lbl_10, R.drawable.image_wo_lbl_11};
#Override
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.parent_frame);//this one is the common parent layout for all image views
super.onCreate(savedInstanceState);
/*requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);*/
//int image1 = R.drawable.image_w_lbl_0;
imageLoader = (ImageView) findViewById(R.id.imageLoader);
//imageLoader.setImageResource(image1);
ImageButton next = (ImageButton) findViewById(R.id.next);
ImageButton back = (ImageButton) findViewById(R.id.back);
next.setOnClickListener(this);
back.setOnClickListener(this);
//show the default image
this.loadImage(imageList[imageCounter]);
}
#Override
public void onClick(View v)
{
int imagePath = 0;
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.next:
Log.i("Tag","tag");
if(imageCounter < imageList.length)
{
imageCounter++;
imagePath = imageList[imageCounter];
if (imageCounter==(imageList.length)-1)
{
//how to make my next button disable
}
}
break;
case R.id.back:
if(imageCounter > 0)
{
imageCounter--;
imagePath = imageList[imageCounter];
if (imageCounter==0)
{
//how to make my back button disable
}
}
break;
}
this.loadImage(imagePath);
}
private void loadImage(int imagePath)
{
imageLoader.setImageResource(imagePath);
}
}
KOTLIN:
next.isClickable = false
JAVA:
next.setClickable(false);
case R.id.next:
Log.i("Tag","tag");
if(imageCounter < imageList.length)
{
imageCounter++;
imagePath = imageList[imageCounter];
if (imageCounter==(imageList.length)-1)
{
ImageButton next=(ImageButton)findViewBYId(R.id.next);
next.setEnabled(false);
}
}
break;
case R.id.back:
if(imageCounter > 0)
{
imageCounter--;
imagePath = imageList[imageCounter];
if (imageCounter==0)
{
ImageButton back=(ImageButton)findViewBYId(r.id.back);
back.setEnabled(false);
}
}
break;
more preferred solution is,
onclick(){
btn.setEnabled(false);
btn.setClickable(false);
//yourwork
myWork();
}
myWork(){
//your tasks.
btn.setEnabled(true);
btn.setClickable(true);
}
As a link can be ignored easily, I had to repeat this again and again
With kotlin you can disable a button with
button.isEnabled = false
or to disable clicking on it
button.isClickable = false
For Kotlin you can use this
override fun onClick(v: View) {
when (v.id) {
R.id.cancel_button -> {
button.isClickable = false
button.isEnabled = false
}
}
}
just insert two extra images in your drawable folder one for disabled right arrow and one for disabled left arrow.
now try this
case R.id.next:
Log.i("Tag","tag");
if(imageCounter < imageList.length)
{
imageCounter++;
imagePath = imageList[imageCounter];
if (imageCounter==(imageList.length)-1)
{
//disabling right button by changing image from following code
next.setImageDrawable(getResources().getDrawable(R.drawable.right_disabled));
}
}
break;
case R.id.back:
if(imageCounter > 0)
{
imageCounter--;
imagePath = imageList[imageCounter];
if (imageCounter==0)
{
back.setImageDrawable(getResources().getDrawable(R.drawable.left_disabled));
}
}
break;
Related
As I mentioned in the title ,I want to add text to a Textview without replacing the previous text .
In my application I have a TextView and 7 buttons .On every button click I set the text of button to the TextView.
If the button is clicked on first time ,Setting the text to TextView ,and if the same button is clicked 2nd time I am removing that button's text from TextView.
Here What I want to do is for 7 buttons I want to set positions(uniqueness for sun to sat) in TextView and when the respective button is clicked that text is set to the TextView and if the button is clicked 2nd time that specific position of the text should remove .
Here text shouldn't replace the previous text that is important to have and if some button's are selected and again that are deselected means TextView should show the default text as "Never"
I tried to get source from SO but I can't find a clear solution for this .
If anyone helps me to come out from this ,that's much helpful for me .
coding
public class CreateAlarm extends Activity implements View.OnClickListener {
private Button mbtn_Sun, mbtn_Mon, mbtn_Tue, mbtn_Wed, mbtn_Thu, mbtn_Fri, mbtn_Sat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_alarm);
mRepeat = (TextView) findViewById(R.id.mRepeat);
mbtn_Sun = (Button) findViewById(R.id.mbtn_Sun);
mbtn_Mon = (Button) findViewById(R.id.mbtn_Mon);
mbtn_Tue = (Button) findViewById(R.id.mbtn_Tue);
mbtn_Wed = (Button) findViewById(R.id.mbtn_Wed);
mbtn_Thu = (Button) findViewById(R.id.mbtn_Thu);
mbtn_Fri = (Button) findViewById(R.id.mbtn_Fri);
mbtn_Sat = (Button) findViewById(R.id.mbtn_Sat);
mbtn_Sun.setOnClickListener((View.OnClickListener) this);
mbtn_Mon.setOnClickListener((View.OnClickListener) this);
mbtn_Tue.setOnClickListener((View.OnClickListener) this);
mbtn_Wed.setOnClickListener((View.OnClickListener) this);
mbtn_Thu.setOnClickListener((View.OnClickListener) this);
mbtn_Fri.setOnClickListener((View.OnClickListener) this);
mbtn_Sat.setOnClickListener((View.OnClickListener) this);
int hours = mTimePicker.getCurrentHour();
mCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mbtn_Sun:
if (mRepeat.getText().toString().contains("Sun")) {
mRepeat.setText("");
} else
mRepeat.setText("Sun");
break;
case R.id.mbtn_Mon:
if (mRepeat.getText().toString().contains("Mon")) {
mRepeat.setText("");
} else
mRepeat.setText("Mon");
break;
case R.id.mbtn_Tue:
if (mRepeat.getText().toString().contains("Tue")) {
mRepeat.setText("");
} else
mRepeat.setText("Tue");
break;
case R.id.mbtn_Wed:
if (mRepeat.getText().toString().contains("Wed")) {
mRepeat.setText("");
} else
mRepeat.setText("Wed");
break;
case R.id.mbtn_Thu:
if (mRepeat.getText().toString().contains("Thu")) {
mRepeat.setText("");
} else
mRepeat.setText("Thu");
break;
case R.id.mbtn_Fri:
if (mRepeat.getText().toString().contains("Fri")) {
mRepeat.setText("");
} else
mRepeat.setText("Fri");
break;
case R.id.mbtn_Sat:
if (mRepeat.getText().toString().contains("Sat")) {
mRepeat.setText("");
} else
mRepeat.setText("Sat");
break;
default:
mRepeat.setText("Never");
}
}
}
Image :
By default the TextView text is "Never".
You can define a TreeMap as:
private TreeMap<Integer, String> mAlarmDays = new TreeMap<>();
as a field of your class and add/remove the days to/from the TreeMap when the corresponding button is clicked. So the implementation of onClick method will be:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mbtn_Sun:
if (mRepeat.getText().toString().contains("Sun")) {
mAlarmDays.remove(0);
} else
mAlarmDays.put(0, "Sun");
break;
case R.id.mbtn_Mon:
if (mRepeat.getText().toString().contains("Mon")) {
mAlarmDays.remove(1);
} else
mAlarmDays.put(1, "Mon");
break;
case R.id.mbtn_Tue:
if (mRepeat.getText().toString().contains("Tue")) {
mAlarmDays.remove(2);
} else
mAlarmDays.put(2, "Tue");
break;
case R.id.mbtn_Wed:
if (mRepeat.getText().toString().contains("Wed")) {
mAlarmDays.remove(3);
} else
mAlarmDays.put(3, "Wed");
break;
case R.id.mbtn_Thu:
if (mRepeat.getText().toString().contains("Thu")) {
mAlarmDays.remove(4);
} else
mAlarmDays.put(4, "Thu");
break;
case R.id.mbtn_Fri:
if (mRepeat.getText().toString().contains("Fri")) {
mAlarmDays.remove(5);
} else
mAlarmDays.put(5, "Fri");
break;
case R.id.mbtn_Sat:
if (mRepeat.getText().toString().contains("Sat")) {
mAlarmDays.remove(6);
} else
mAlarmDays.put(6, "Sat");
break;
}
StringBuilder repeatDays = new StringBuilder();
if (mAlarmDays.size() == 0) {
repeatDays = new StringBuilder("Never");
} else {
for (String day:mAlarmDays.values()) {
repeatDays.append(day).append(" ");
}
}
mRepeat.setText(repeatDays.toString());
}
You should set each button id first,add this to your xml for each specific button : android:id="sun" and ...
My suggestion is: use a single TextView can make your logic quite complex
Use a horizontal LinearLayout instead, you will have 7 TextView inside with predefine text and position. Just simply show/hide them according to which button is clicked and you don't have to deal with any complex string analize.
I would like to know how to set an if else condition where the next button have to be disabled if none of the buttons are being clicked. Otherwise, they are able to proceed to next question?
private AdvancedQuestion nAdvancedQuestion = new AdvancedQuestion();
private TextView nScoresView;
private TextView nQuestionsView;
private TextView tvTime;
private Button nButtonChoices1;
private Button nButtonChoices2;
private Button nButtonChoices3;
private Button nButtonChoices4;
private String nAnswers;
private int nScores = 0;
private int nQuestionNumbers = 0;
Button btnNextz;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advanced_quiz);
updateQuestions();
nButtonChoices1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (nButtonChoices1.getText() == nAnswers) {
correctSound.start();
nScores = nScores + 1;
nButtonChoices1.setEnabled(false);
nButtonChoices2.setEnabled(false);
nButtonChoices3.setEnabled(false);
nButtonChoices4.setEnabled(false);
nButtonChoices1.getBackground().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
Toast.makeText(advancedQuiz.this, "correct", Toast.LENGTH_SHORT).show();
} else {
wrongSound.start();
Toast.makeText(advancedQuiz.this, "wrong", Toast.LENGTH_SHORT).show();
nButtonChoices1.setEnabled(false);
nButtonChoices2.setEnabled(false);
nButtonChoices3.setEnabled(false);
nButtonChoices4.setEnabled(false);
}
}
});
btnNextz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextSound.start();
updateQuestions();
nButtonChoices1.setEnabled(true);
nButtonChoices2.setEnabled(true);
nButtonChoices3.setEnabled(true);
nButtonChoices4.setEnabled(true);
nButtonChoices1.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
nButtonChoices2.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
nButtonChoices3.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
nButtonChoices4.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
}
private void updateQuestions() {
nQuestionsView.setText(nAdvancedQuestion.getQuestions(nQuestionNumbers));
nButtonChoices1.setText(nAdvancedQuestion.getChoices1(nQuestionNumbers));
nButtonChoices2.setText(nAdvancedQuestion.getChoices2(nQuestionNumbers));
nButtonChoices3.setText(nAdvancedQuestion.getChoices3(nQuestionNumbers));
nButtonChoices4.setText(nAdvancedQuestion.getChoices4(nQuestionNumbers));
nAnswers = nAdvancedQuestion.getCorrectAnswers(nQuestionNumbers);
nQuestionNumbers++;
}
private void updateScore(int points) {
nScoresView.setText("" + nScores);
}
Please note there is 4 possible answers. If none of them are selected, they cannot proceed to the next question until one button is press so they can go to the next question. The updateQuestions() is the part where i believe it will show next question.
This is a simple example on how to disable/enable a button based on an if condition -
int count = 0;
if (count == 0) {
NextButton.setEnabled(false);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.otherButtons:
count++;
NextButton.setEnabled(true);
Toast.makeText(this, "Button Disabled", Toast.LENGTH_LONG).show();
break;
case R.id.nextButton:
//Move the user to the next question
break;
}
}
Also check out this link
You will need to add a button element in the view of the activity you want it to appear on, then add an event listener to it either in the activity code or specify which function to call on click in the activities XML layout file.
See: https://developer.android.com/reference/android/widget/Button.html
change the text of the button once clicked to P then again that same button is clicked the text should change to A then again the same button is clicked the text should change to H then again when the button is pressed the text should change to L in android Studio how c
final Button button = (Button) findViewById(R.id.number);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (curr) {
case 0:
button.setText("P");
curr = curr + 1;
break;
case 1:
button.setText("A");
curr = curr + 1;
break;
case 2:
button.setText("H");
curr = curr + 1;
break;
case 3:
button.setText("L");
curr = 0;
break;
}
}
});
This will work 100%
public class MainActivity extends AppCompatActivity {
private int current = 0;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (current){
case 0:
button.setText("P");
current = 1;
break;
case 1:
button.setText("A");
current = 2;
break;
case 2:
button.setText("H");
current = 3;
break;
case 3:
button.setText("L");
current = 0;
break;
}
}
});
}
}
You're going to need three things for this:
A way to react to the button being clicked
A way to set and get the state of the button
A way to change the text on the button
Thankfully, these are all easy tasks.
To react to a button click, set its OnClickListener
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
updateButtonState();
}
});
To set and get the state of the button, we need a way of representing the state. Since you described a simple state of P -> A -> H -> L we can represent each point as an integer.
final int BUTTON_STATE_P = 0;
final int BUTTON_STATE_A = 1;
final int BUTTON_STATE_H = 2;
final int BUTTON_STATE_L = 3;
We can then simply store the given state into a variable change this variable according to our state logic and then read whatever value the variable is at.
int buttonState = BUTTON_STATE_P;
private void updateButtonState(){
if (buttonState == BUTTON_STATE_P){
buttonState = BUTTON_STATE_A;
}
else if (buttonState == BUTTON_STATE_A){
buttonState = BUTTON_STATE_H;
}
else if (buttonState == BUTTON_STATE_H){
buttonState = BUTTON_STATE_L;
}
}
Then we just need a way of setting the text of the button depending upon what state we're in. To do this we need to know what each state looks like in terms of text.
button.setText(getTextForButtonState(buttonState));
private String getTextForButtonState(int buttonState){
if (buttonState == BUTTON_STATE_P){
return "P";
}
else if (buttonState == BUTTON_STATE_A){
return "A";
}
else if (buttonState == BUTTON_STATE_H){
return "H";
}
else if (buttonState == BUTTON_STATE_L){
return "L";
}
return null;
}
You'll notice that the most important step was step 2. You can make lots of different decisions as to how you might want to handle a state. You might decide that you want to put all of your state code into its own class and then just call methods of that class, as one suggestion.
Hope this helps.
Am beginner to android..I developed an app like if i press prev and next button it will display prevous and next images....
But I had issue in my app..The problem here was, if app execute i set an current image source as IMAGE_IDS[0]... So that if i pressed left arrow app gets force close, actually if i press left arrow it shows last image in the array IMAGE_IDS... Any idea ??? Thank You !
public class MainActivity extends Activity implements OnClickListener{
ImageButton play;
ImageButton ib_left_arrow, ib_right_arrow;
ImageView slidingimage;
Animation rotateimage;
private int[] IMAGE_IDS = { R.drawable.image1, R.drawable.image2,
R.drawable.image3};
int imglength=IMAGE_IDS.length;
int img_position;
int img_minus;
int img_plus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movies);
ib_left_arrow = (ImageButton) findViewById(R.id.iv_left_arrow);
ib_right_arrow = (ImageButton) findViewById(R.id.iv_right_arrow);
ib_left_arrow.setOnClickListener(this);
ib_right_arrow.setOnClickListener(this);
slidingimage = (ImageView) findViewById(R.id.ImageView3_Left);
slidingimage.setImageResource(IMAGE_IDS[0]);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.iv_left_arrow:
img_minus=--img_position;
slidingimage.setImageResource(IMAGE_IDS[img_minus% IMAGE_IDS.length]);
rotateimage = AnimationUtils.loadAnimation(this,R.anim.slide_in_left);
slidingimage.startAnimation(rotateimage);
break;
case R.id.iv_right_arrow:
img_plus=++img_position;
slidingimage.setImageResource(IMAGE_IDS[img_plus% IMAGE_IDS.length]);
rotateimage = AnimationUtils.loadAnimation(this,R.anim.slide_in_right);
slidingimage.startAnimation(rotateimage);
break;
}
}
}
I think the problem is img_plus% IMAGE_IDS.length when setting the image position in the array.
I would try:
#Override
protected void onCreate(Bundle savedInstanceState) {
img_position = 0;
imglength = IMAGE_IDS.length;
slidingimage.setImageResource(IMAGE_IDS[img_position]);
// ...
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.iv_left_arrow:
img_position--;
break;
case R.id.iv_right_arrow:
img_position++;
break;
}
// this is to check if your current position is out of array bounds, you could
// handle here your exit if you want to close the app when pressing left and
// img_position = 0 || img_position < 0
if(img_position < 0) img_position = imglength-1;
if(img_position >= imglength) img_position = imglength -1;
slidingimage.setImageResource(IMAGE_IDS[img_position]);
Its happening because of this line
img_minus=--img_position;
and this calculation
img_plus % IMAGE_IDS.length
If it's at 0 then
img_minus=--img_position;//img_minus = --0 therefore img_minus = -1
//img_plus % IMAGE_IDS.length // -1 % 3 = 2
slidingimage.setImageResource(IMAGE_IDS[2]);//which is the last image.
So if you want to close the Activity if left is pressed then
img_minus=--img_position;
if(img_minus < 0({
finish();
return;
}
imgposition=0;
case R.id.iv_left_arrow:
if(0<img_position){
--img_position;
slidingimage.setImageResource(IMAGE_IDS[img_position]);
rotateimage = AnimationUtils.loadAnimation(this,R.anim.slide_in_left);
slidingimage.startAnimation(rotateimage);}
break;
case R.id.iv_right_arrow:
if(2>=counter)
{
++img_position;
slidingimage.setImageResource(IMAGE_IDS[img_position]);
rotateimage = AnimationUtils.loadAnimation(this,R.anim.slide_in_right);
slidingimage.startAnimation(rotateimage);}
break;
this application requires:
first click will change image1 to image2
second click will change back to old image (image2 to image1)
image1 = (ImageView)findViewById(R.id.imageView1);
image1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
image1.setImageResource(R.drawable.a3_01);
image1.setTag(70);
}
});
this image will set a new tag for the server knows that the picture have been change.
*the code i used is only for the first click and it works. ive just have no idea to make a second click event. can anyone gives me idea of it? much appreciate. thanks.
You could use a boolean to act as a switch for you to flop back and forth with an if statement.
boolean showingFirst = true;
image1 = (ImageView)findViewById(R.id.imageView1);
image1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(showingFirst == true){
image1.setImageResource(R.drawable.a3_02);
showingFirst = false;
}else{
image1.setImageResource(R.drawable.a3_01);
image1.setTag(70);
showingFirst = true;
}
}
});
Put both images into an ImageSwitcher and use the Button clicks to call its showNext() method.
You can use if case in it like
public void onClick(View v) {
if (i == 0) {
Toast.makeText(getApplicationContext(), "First Click", 1000).show();
i++;
} else if (i == 1) {
Toast.makeText(getApplicationContext(), "Second Click", 1000).show();
i = 0;
}
}