How to use a Drawable in If/else statement for setImageResource? - android

I'm new at androidstudio and want to compare a imageView by the following:
I have 2 imageView, both are using a drawable i named "blank" at the start of the app, using if/else I want to chance those images to another drawable i have, i tried the following:
private ImageView equipament1;
private ImageView equipament2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
public void sentImg() {
if (equipament1.equals(R.drawable.blank)){
equipament1.setImageResource(R.drawable.reactor);
}
else if (equipament2.equals(R.drawable.blank)){
equipament2.setImageResource(R.drawable.reactor);
} else {finish();}
but it doesn't work, the app just replaces the first image and if i click on the button again, nothing happens (this if/else is inside a button).
I want to check if the first image is blank, if it is, the app should replace the blank image with the image "reactor" or, if is not blank, the app should move to the second blank image, and replace it and this go on for more 2 blank spaces.
I'm doing this because I'm building an program similar to LucidChart where you put your equipments in the app.

The problem is that the second time you have already changed the value of the comparator.
If the objective is just to change the images you don't need the if/else.
private ImageView equipament1;
private ImageView equipament2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
public void sentImg() {
equipament1.setImageResource(R.drawable.reactor);
equipament2.setImageResource(R.drawable.reactor);
}

When the user clicks your button, you want to do 2 things. You want to show some images, or you want to call finish().
I would suggest using a boolean as a flag the the state and compare that instead of comparing the ImageView itself. This'll be easier, and make your code easier to read.
I created a flag called firstClick that is set to true by default. When the user clicks your button (button1 in this example), we check against that and show the images. Then we set it to false,
so the next click will call finish().
private ImageView equipament1;
private ImageView equipament2;
// The current state of the Activity
private boolean firstClick = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
// Setting your OnClickListener
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if( firstClick ) {
firstClick = false;
sentImg();
} else {
finish();
}
}
});
}
public void sentImg() {
equipament1.setImageResource(R.drawable.reactor);
equipament2.setImageResource(R.drawable.reactor);
}

Related

How to change 2 images simultaneously upon each click in android Studio 3

public class MainActivity extends AppCompatActivity {
public void fade(View view) {
ImageView homerImageView = (ImageView) findViewById(R.id.homerImageView);
ImageView bartImageView = (ImageView) findViewById(R.id.bartImageView) ;
bartImageView.animate().alpha(1).setDuration(5000);
homerImageView.animate().alpha(0).setDuration(5000);
for ()
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
In my app have totally 2 images upon click one fade out to 0 alpha and second comes back from 0 alpha to 1 alpha when I run the app and click on the image change to other but the only thing I want that it keeps doing that upon each click on the image like if I click on image 1 it changes to image 2 but upon clicking on image to it do not change to image 1 any solution very simple will be helpfull thanks.
Try like this. Just a hint may not suits what exactly you want
In your image onClick method:
switch (v.getId()){
case R.id.id_one:
imgOne.animate().alpha(0).setDuration(5000);
callHandler(imgTwo);
break;
case R.id.id_two:
imgTwo.animate().alpha(0).setDuration(5000);
callHandler(imgOne);
break;
}
Handler function is like,
private void callHandler(final ImageView imgView) {
new Handler(Looper.myLooper()).postDelayed(new Runnable() {
#Override
public void run() {
imgOne.setVisibility(View.GONE);
imgTwo.setVisibility(View.GONE);
imgView.setVisibility(View.VISIBLE);
imgView.animate().alpha(1).setDuration(5000);
}
},5000);
}

ImageButton change: the new image is plotted on top of original image

I am trying to change the image of an ImageButton when the button is pressed. Here is my code:
in my activity_main.xml I have:
<ImageButton
android:id="#+id/my_btn"
app:srcCompat="#drawable/btn_icon1" />
In my MainActivity.java I have:
int myBtnState = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
myButton = (ImageButton) findViewById(R.id.my_btn);
myButton.setOnClickListener(this);
...
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.my_btn:
changeIcon();
break;
...
private void changeIcon() {
if(myBtnState == 0) {
myButton.setBackgroundResource(R.drawable.btn_icon1);
myBtnState = 1;
} else {
myButton.setBackgroundResource(R.drawable.btn_icon2);
myBtnState = 0;
}
}
when I run this code when I press the button, it looks like the first time btn_icon1 is drawn on top of itself 2 times as it has a semi-transparent background I see it gets darker. And next time I press the button btn_icon2 is drawn on top of btn_icon1. I expected that using this code the icons to be changed, not drawn on top of each other. Is there anything I am doing incorrectly here?
I found the answer:
I was setting the background of the image when I did:
myButton.setBackgroundResource(R.drawable.btn_icon1);
Instead, I should do:
myButton.setImageResource(R.drawable.btn_icon1);

how to display image upon clicking the button and slide the image option by swiping in the particular direction in android studio?

I have created a float button in Android Studio
After I click the image, the add floating button should be disabled and the 3 images should be displayed. One images have to selected from the images displayed horizontally just like in the image below. If i have to select the image in the right then i have to the right to position the image in the center and select the image
And after choosing the appropriate image it should be back to displaying floating button like in the first image.
Here, I have provided an example outline of what you are trying to do. You will have to add a lot more code, but this will get you started.
public class MainActivity extends AppCompatActivity {
private boolean cupImageClicked = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//initialize and set up imageViews and button.
//listener for button.
mFloatingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mFloatingButton.setVisibility(View.GONE);
imageView_1.setVisibility(View.VISIBLE);
imageView_2.setVisibility(View.VISIBLE);
imageView_3.setVisibility(View.VISIBLE);
}
});
//example click listener for an image.
cupImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (cupImageClicked) {
//this will execute when cupImage is already in the center
//gather whatever info you need from that clicked image.
imageView_1.setVisibility(View.INVISIBLE);
imageView_2.setVisibility(View.INVISIBLE);
imageView_3.setVisibility(View.INVISIBLE);
mFloatingButton.setVisibility(View.VISIBLE;
} else {
//here you will need to move the cupImage to the center and
// move the center imageView to the left.
cupImageClicked = true;
}
}
});
}

<ToggleButton> onClickListener - Create/Delete button + add content to button

I have this problem with my ToggleButton.
I want it to create/delete a button upon being toggled, and at the same time add content/functions to the button, like drawable and such.
This is the current code:
public class BillardScoreboardActivity extends Activity {
/** Called when the activity is first created. */
Button minuskegle, minuskugle, pluskugle, pluskegle, plusmidkegle, minusmidkegle, miss;
ToggleButton toggle;
LinearLayout bottomlayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggle = (ToggleButton) findViewById(R.id.bRedGreen);
toggle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
pluskugle = (Button) findViewById(R.id.bBallhole);
minuskugle = (Button) findViewById(R.id.bBallhole);
pluskegle = (Button) findViewById(R.id.bKegle);
minuskegle = (Button) findViewById(R.id.bKegle);
plusmidkegle = (Button) findViewById(R.id.bKeglemid);
minusmidkegle = (Button) findViewById(R.id.bKeglemid);
bottomlayout = (LinearLayout) findViewById(R.id.bottomlayout);
miss = (Button) findViewById(R.id.bMiss);
if(toggle.isChecked())
{
minuskugle.setBackgroundResource(R.drawable.redballinhole);
minuskegle.setBackgroundResource(R.drawable.redkegle);
minusmidkegle.setBackgroundResource(R.drawable.midkegleminus);
miss.setBackgroundResource(R.drawable.missbutton);
miss.setVisibility(View.VISIBLE);
}
else
{
pluskugle.setBackgroundResource(R.drawable.whiteballinhole);
pluskegle.setBackgroundResource(R.drawable.kegleb);
plusmidkegle.setBackgroundResource(R.drawable.midkegleplus);
miss.setVisibility(View.GONE);
}
}
});
}
The current problem is that it can't find the (buttontest) in this part of the code:
else
{
pluskugle.setBackgroundResource(R.drawable.whiteballinhole);
pluskegle.setBackgroundResource(R.drawable.kegleb);
plusmidkegle.setBackgroundResource(R.drawable.midkegleplus);
bottomlayout.removeView(buttontest);
}
And as mentioned earlier, the second problem is to make the button inherit some functions/content.
for bigger version: http://i.imgur.com/KxKvh.png
Btw... Everytime i start up the application, it gives me 2 apps to choose between, whereof only the bottom one works:
I guess the problem is that the togglebutton's initial state is 'checked'. That means when you click it the first time, isChecked() will return false and the else-part of your code will be executed. But at that point, buttontest hasn't been added to bottomlayout yet.
I recommend you to have the button inside the layout by default and call buttontest.setVisibility(View.GONE) when you would like to hide it and buttontest.setVisibility(View.VISIBLE) when it needs to be shown.
As for your second question, just call setBackgroundResource/Drawable to add content (like you're already doing it with the other buttons). If you say you want to add functionality, I assume you intend to do something when the button is clicked? If yes, add a View.OnClickListener.
Hope I could help you.

Why does my animation freeze when returning to activity containing it?

I'm still sort of new with Android, so forgive me if it's an obvious mistake. In this activity I'm using ViewPager to horizontally scroll through three layouts containing an ImageButton that has an animated background depending on its current state. When the button is pressed, it starts a new activity. However, when I hit the back button to go back to the activity containing the animation from the new activity, sometimes the animation freezes or plays back faster than it should. I wrote a method for starting up the animation that I use in onWindowFocusChanged(), and onRestart(). I'm working in Android 2.1 (API 7).
This is my code:
public class CopyOfWorld extends Activity{
MediaPlayer muzak;
Boolean mSwitch = false;
ImageButton holmes;
AnimationDrawable holmesAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.world);
Preferencer pp = (Preferencer)getApplicationContext();
ViewPagerAdapter adapter = new ViewPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
if(pp.getMuzak()){
mSwitch = true;
muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
muzak.setLooping(true);
muzak.start();
}
}
public void clicker(View v){
Intent myIntent = new Intent(CopyOfWorld.this , Subworld.class);
startActivityForResult(myIntent, 0);
}
#Override
public void onWindowFocusChanged(boolean hasFocus){
super.onWindowFocusChanged(hasFocus);
beginRender();
}
#Override
protected void onPause(){
super.onPause();
if(mSwitch){
muzak.release();
}
}
#Override
protected void onRestart(){
super.onRestart();
Preferencer pp = (Preferencer)getApplicationContext();
if(pp.getMuzak()){
muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
muzak.setLooping(true);
muzak.start();
}
beginRender();
}
public void beginRender(){
ImageButton holmes = (ImageButton) findViewById(R.id.subworlder);
StateListDrawable background = (StateListDrawable) holmes.getBackground();
Drawable current = background.getCurrent();
if(current instanceof AnimationDrawable){
holmesAnimation = (AnimationDrawable) current;
holmesAnimation.start();
}
}
}
I've tried calling the method beginRender() under ()onResume, but then the app simply crashes.
Could anyone point me in the right direction?
EDIT:
I've been tweaking the code here and there, unfortunately to no avail. But I did notice a pattern in the behavior of the animation. When I press down on the ImageButton or hold it so that it goes from its default animation to its pressed or focused animation then move my finger away from the button so that it doesn't start up the new activity, it sometimes behaves very much as I described at the beginning of this post (i.e. it's supposed to return to its default animation, but instead plays back at twice the rate, chokes up, or doesn't play at all.)
Currently the xml that contains these ImageButtons defines their backgrounds as the animations and have no source (src). But when I change the background to transparent and the src to the animations, the app crashes.
Any clues?

Categories

Resources