I'm a beginner with android.
What I want to do is, loading multiple image icons, like a folder with images needed to be opened. A user will select one and that image should be displayed in full size.
Here is what I've done till now...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
image = (ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.btnChangeImage);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
image.setImageResource(R.drawable.def);
}
});
}
I mean, here the image will be changed, if I add more images in setImageResource, but not like the way I want them to be. What should I do further? Please help...
Related
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);
}
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;
}
}
});
}
I have an ImageButton and I want that onClick would replace it with another image (flip back and forth) and on a long press, would replace it to another image.
How can I do that?
I don't feel like reading long documentaries for this.
Set onClickListeners for your button then change the drawable. Since you don't have any code, the following is based on a dynamic ImageButton that only outlines how to perform the action you want. I suggest you define your ImageButton in your XML layout first and then use
iBtn = (ImageButton) findViewById(R.id.btnID);
ImageButton iBtn = new ImageButton(this);
iBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
iBtn.setImageDrawable(getResources().getDrawable(R.drawable.img1);
}
});
iBtn.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
iBtn.setImageDrawable(getResources().getDrawable(R.drawable.img2);
return true;
}
});
If you're going to learn Android (or any language or platform really), you should really get comfortable reading the documentation provided, as it will give you answers to many basic questions, such as how to use various methods and classes.
That aside, you need to set both an OnClickListener and an OnLongClickListener for your button. Then inside those listeners, you'll need to set the image using the setImageResource() method. That method requires a drawable image, which you should have saved in your drawable folder (if not, put it there!)
You didn't post your existing code, so here's a generic example.
ImageButton button = (ImageButton) findViewById(R.id.img_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button.setImageResource(R.drawable.pic1);
}
});
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
button.setImageResource(R.drawable.pic2);
return true; // <-- This must be true.
}
});
You could read further about how to use any buttons in the button guide, you'll just be swapping for ImageButton where appropriate.
Add ImageButton to your layout :
<ImageButton
android:id="#+id/img_btn1"
android:src="#drawable/imgc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
and then add this code to your Activity Oncreate() method
ImageButton imageButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.img_btn1);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageButton.setImageResource(R.drawable.imga);
}
});
imageButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
imageButton.setImageResource(R.drawable.imgb);
return true;
}
});
}
change imga , imgb, imgc names according to your images taht are placed in drawable folder
I'm new to android, and I'm facing a problem where I'm trying to make my ImageView, which is playing an animation through startAnimation(), to become Invisible as the user clicks on it. what happens however is that only after the animation is done the Imageview becomes invisible. my assumptions was it's because they both run on the UI thread. surprisingly however, when I changed my event handling from setVisibility to startAnimation() it actually listened to the click, interrupted the animation and restarted it!
To override my problem, which did turn out nice, I made a new Animation object that opposite to the first, which shows the Imageview, hides the Imageview and it worked perfectly, but I'm still curious as to why my Imageview was selectively responsive to different event handling?
This code change visibility only after the animation is done
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
face = (ImageView)findViewById(R.id.face);
final Animation showAnimation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation);
face.startAnimation(showAnimation);
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
face.setVisibility(View.INVISIBLE);
}
});
}
this code changes the animation "while" the original is playing
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
face = (ImageView)findViewById(R.id.face);
final Animation showAnimation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation);
face.startAnimation(showAnimation);
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation hideAnimation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation1);
face.startAnimation(hideAnimation);
}
});
}
You should not call face.setVisibility(...), instaed tt should be:
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setVisibility(View.INVISIBLE);
}
});
I'm new to android and am making a simple app. I'm trying to change the image (in an imageview) on a button click.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
frown = (ImageView)findViewById(R.id.imageView1);
}
public void action(View view)
{
Toast.makeText(getApplicationContext(), buttontest, Toast.LENGTH_SHORT).show();
frown.setImageResource(R.drawable.chimpy);
}
"action" is being called via XML with the "android:onClick"[insert method here]" for my button
The button works fine and I get my toast, but the image stays the same.
Try changing the drawable to something standard e.g. android.R.drawable.btn_default.
Does it chnage Now?
I am sure you are having some issues with R.drawable.chimpy.
You must use .png image and you can go with the following code snippet:
frown.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
frown.setBackgroundDrawable(R.id.chimpy);
}
});
If it is not working, just tell me...!