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;
Related
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.
I have one TextView in my application and want to change the Background color of the same TextView .When i click 1st time it would be red , click on same 2nd time it would be green and 3rd time click it would be blue color background by problematically.
textType = (TextView)findViewById(R.id.textRNG);
textType.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Drawable d = textType.getBackground();
Log.e("textType "," click !!! ");
if(d.getConstantState() == getResources().getDrawable(R.drawable.red_circle_shape).getConstantState())
{
textType.setBackgroundResource(R.drawable.green_circle_shape);
}
if(d.getConstantState() == getResources().getDrawable(R.drawable.green_circle_shape).getConstantState())
{
textType.setBackgroundResource(R.drawable.blue_circle_shape);
}
if(d.getConstantState() == getResources().getDrawable(R.drawable.blue_circle_shape).getConstantState())
{
textType.setBackgroundResource(R.drawable.red_circle_shape);
}
}
});
This cod is not working.Thanks to appropriate.
Create a global variable x initialize it with 0. Then code like this:
textType = (TextView)findViewById(R.id.textRNG);
textType.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if(x<4)
{
x= x+1;
}
else{
x = 1;
}
if(x==1)
{
// red color
}
else if(x==2)
{
// blue color
}
else if(x==3)
{
// green color
}
}
});
Use the below code,
textType = (TextView)findViewById(R.id.textRNG);
textType.setOnClickListener(new View.OnClickListener() {
private int mCounter = 0;
#Override
public void onClick(View v)
{
if (mCounter == 0)
v.setBackgroundResource(R.drawable.red_circle_shape);
else
if (mCounter == 1)
v.setBackgroundResource(R.drawable.green_circle_shape);
else
v.setBackgroundResource(R.drawable.blue_circle_shape);
mCounter++;
}
});
Try this;
Define a global Variable int type.
Inside your onClick(), increment the int Variable
use switch or if statement to change the color when the variable increases e.g.
If(variable == 1)
// change color to Blue
else if (variable == 2)
// change color to Yellow
Hi Use the following code to Change the Color. Paste these lines inside your textView Onclick. Variable count is a global variable.
if (count == 0)
txtView.setTextColor(colorcode1);
else if (count == 1)
txtView.setTextColor(colorcode2);
else
txtView.setTextColor(colorcode3);
count++;
if (count > 2)
count = 0;
Try below code
TextView textType = (TextView)findViewById(R.id.textRNG);
textType.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(current)
{
case 1:
tv.setBackgroundColor(Color.parseColor("#00ff00"));
current = 2;
break;
case 2:
tv.setBackgroundColor(Color.parseColor("#0000ff"));
current = 3;
break;
case 3:
tv.setBackgroundColor(Color.parseColor("#ff0000"));
current = 1;
break;
default:
break;
}
}
});
I have next and previous button for changing the image. When activity launch, image comes from previous activity. I use bundle object for getting image on my current activity. Actually 2 images use for pass it on bundle(image_a_inner and image_a_outer). One image overlap on second image and set on custom view. Now i want to when any image comes from bundle then i press next button or previous button then according to position image will be change. For example, images like A_Z alphabet. When i press on D image then it display on my activity using bundle and when i press next button then E image will be shown or when i press previous button then C image will be shown. Below is my code.
private DrawingView mDrawingView;
Bundle extras = getIntent().getExtras();
int imageRes1 = extras.getInt("picture1");
int imageRes2 = extras.getInt("picture2");
mDrawingView = (DrawingView) findViewById(R.id.drawing_view);
mDrawingView.setShape(imageRes1, imageRes2);
btn_next = (Button) findViewById(R.id.btn_next);
// btn_next.setOnClickListener(this);
btn_next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
initializeMP();
playsound.start();
if(imageRes1==R.drawable.img_a_inner||imageRes2==R.drawable.img_a){
mDrawingView.setShape(R.drawable.img_b_inner, R.drawable.img_b);
index++;
}
else if(imageRes1==R.drawable.img_b_inner||imageRes2==R.drawable.img_b){
mDrawingView.setShape(R.drawable.img_c_inner, R.drawable.img_c);
index++;
}
else if(imageRes1==R.drawable.img_c_inner||imageRes2==R.drawable.img_c){
mDrawingView.setShape(R.drawable.img_d_inner, R.drawable.img_d);
index++;
}
else if(imageRes1==R.drawable.img_d_inner||imageRes2==R.drawable.img_d){
mDrawingView.setShape(R.drawable.img_e_inner, R.drawable.img_e);
index++;
}
}
});
btn_prev = (Button) findViewById(R.id.btn_prev);
// btn_prev.setOnClickListener(this);
btn_prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
initializeMP();
playsound.start();
if(imageRes1==R.drawable.img_b_inner||imageRes2==R.drawable.img_b){
mDrawingView.setShape(R.drawable.img_a_inner, R.drawable.img_a);
index--;
}
else if(imageRes1==R.drawable.img_c_inner||imageRes2==R.drawable.img_c){
mDrawingView.setShape(R.drawable.img_b_inner, R.drawable.img_b);
index--;
}
else if(imageRes1==R.drawable.img_d_inner||imageRes2==R.drawable.img_d){
mDrawingView.setShape(R.drawable.img_c_inner, R.drawable.img_c);
index--;
}
});
}
If you have lists for all images in advance, declare them as arrays.
Otherwise, you can pass them using bundle with getIntArray() and putIntArray().
Now, you have lists of images like this,
// These are can be declared as member or static variables.
int[] innerPictures = {R.drawable.image_a_inner, R.drawable.image_b_inner, ...}
int[] pictures = {R.drawable.image_a, R.drawable.image_b, ...}
or
int[] innerPictures = extras.getIntArray("innerPictures");
int[] pictures = extras.getIntArray("pictures");
And you need the index of image to be displayed at the first time, it can be also passed as a extra
int displayingIndex = extra.getInt("pictureIndex"); // it has to be member variable to use inside of listener
So code is like below,
private DrawingView mDrawingView;
Bundle extras = getIntent().getExtras();
int[] innerPictures = ...
int[] pictures = ...
displayingIndex = extra.getInt("pictureIndex");
mDrawingView = (DrawingView) findViewById(R.id.drawing_view);
mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
btn_next = (Button) findViewById(R.id.btn_next);
btn_next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
initializeMP();
playsound.start();
if (displayingIndex + 1 == innerPictures.length) return;
displayingIndex++;
mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
}
});
btn_prev = (Button) findViewById(R.id.btn_prev);
btn_prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
initializeMP();
playsound.start();
if (displayingIndex == 0) return;
displayingIndex--;
mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
});
}
Sorry for bad indentation.
In this application i am displaying 6 images randomly from an array containing drawables(named vmarray[12] ) and after 1 minute(using runnable postdelayed) switching is performed to another layout containing a single button with id BUTTON1 and setting the button background to one of the image from the earlier displayed images. I am using LAYOUTINFLATER for switching between layouts
On that button (Button1) onclick i want to do some work, but the PROBLEM IS when i implement onclicklistener and onclick method, the app crashes.
IF i remove the implements onclicklistener everything runs fine. The BUTTON1 is located perfectely through findviewbyid and its background correctly sets to an image. BUT Implementing onclicklister is crashing the app.
here is the code..
int[] vmarray= {R.drawable.vm1bulb, R.drawable.vm2chair, R.drawable.vm3comb,
R.drawable.vm4cycle,R.drawable.vm5dairy,R.drawable.vm6fan,R.drawable.vm7mobile,
R.drawable.vm8pen,R.drawable.vm9shoes,R.drawable.vm10toothbrush,
R.drawable.vm11bangle, R.drawable.vm12watch};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
setContentView(R.layout.cacrvisualmem);
firstview =(LinearLayout)this.findViewById(R.id.firsthumlayout);
secondview = (LinearLayout) inflater.inflate(R.layout.cacrvisualmempart1, null);
for(int i=0;i<6;i++)
{
final int a = i;
button_var[a] = (Button)findViewById(idArray[a]);
}
// IN FIRST LAYOUT, THERE ARE 6 BUTTONS AND THEIR BACKGROUND IS RANDOMLY SET
// DRAWABLES FROM ARRAY "vmarray". the following code is for that
Random randomGenerator = new Random();
Random rand6 = new Random();
while (numbers.size() < 6) {
random1 = randomGenerator.nextInt(12);
random2 = rand6.nextInt(6);
if (!numbers.contains(random1) && (!numbers2.contains(random2)))
{
numbers.add(random1);
numbers2.add(random2);
b[random2].setBackgroundResource(vmarray[random1]);
count++;
}//if ends
}//while ends
Handler changeview = new Handler();
Runnable r1 = new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
// NOW, here, after 1 minute, second view will be inflated and button1
// background is set to one the image from the array
setContentView(secondview);
b11=(Button)findViewById(R.id.button1);
queryrandvar=queryrand.nextInt(12);
switch(queryrandvar)
{
case 0:
b11.setBackgroundResource(vmarray[0]);
queryval=0;
break;
case 1:
b11.setBackgroundResource(vmarray[1]);
queryval=1;
break;
case 2:
b11.setBackgroundResource(vmarray[2]);
queryval=2;
break;
case 3:
b11.setBackgroundResource(vmarray[3]);
queryval=3;
break;
case 4:
b11.setBackgroundResource(vmarray[4]);
queryval=4;
break;
case 5:
b11.setBackgroundResource(vmarray[5]);
queryval= 5;
break;
case 6:
b11.setBackgroundResource(vmarray[6]);
queryval= 6;
break;
case 7:
b11.setBackgroundResource(vmarray[7]);
queryval= 7;
break;
case 8:
b11.setBackgroundResource(vmarray[8]);
queryval= 8;
break;
case 9:
b11.setBackgroundResource(vmarray[9]);
queryval= 9;
break;
case 10:
b11.setBackgroundResource(vmarray[10]);
queryval= 10;
break;
case 11:
b11.setBackgroundResource(vmarray[11]);
queryval= 11;
break;
}//switch ends
}//run ends
};//runnable ends
b11.setOnClickListener(null);
changeview.postDelayed(r1,10000);
} //on create method ends here
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.button1:
{
b11.setBackgroundResource(vmarray[1]);
break;
}
}//switch ends
}//onclick ends
i got it.inside runnable after switch case i have put the following and button onclick is working.RUNING FINE WITH NO APP CRASH
b11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
b11.setBackgroundResource(vmarray[0]); //SOME RANDOM IMAGE FOR CHECKING
}
});
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;