Show description when click on image - java

Sorry for my English!
Need help!
I make some program, who show random image after button click.
How i can make description for each image after user click on it ?
Program show random image, and how i can make description for every image in new window when user click on it ?

Well you can use the setOnClickListener method on your button to do what you want.
final TextView label = (TextView)findViewById(R.id.some_text_label);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
label.setText("Hello, World!");
}
}

Here's a tip.
Use a list or hashmap and create a custom class to hold image and image data. (Changes by a commentor)
Use Math.Random();//Random the id numbers
Stuff to learn:
Research button events and view flipper events
Here's an example:
My idea for using weakhashmap and
for improvements I recommend saving location instead of the bitmap(wastes memory).
private WeakHashMap<int, Image> whp = new WeakHashMap<Int, Image>
private int position = 0;
public void onCreate{
//Do download information or store images from sd card
//Place it in image class
loop amount of images{
image Image = new image(Bitmap_Image, Image_Description);//load image
whp.add(position, Image);//Note the amount of positions the image is field
position++;
}
//To get bitmap use "whp.get(position).image" <- This code returns a bitmap
setOnClickListener//Create event to listen on clicked image
{
int image_random = new Random().nextInt(position);
String data = whp.get(image_random).note_item;//Image Description
}
}
class image{
Bitmap image;//Actual Image
String note_item;//Image description
public void image(Bitmap intake_image, String intake_description){
this.image = intake_image;
this.note_item = intake_description;
}
}

Related

how do I drawline on canvas with a button press

The Title says it all. drawline works outside the setOnClick Listener, but not inside, so I can set it to run with a button press.
//Bitmap Object creation
bitmap = Bitmap.createBitmap(600,800,Bitmap.Config.RGB_565);
//Create Canvas object and set to bitmap variable
canvas = new Canvas(bitmap);
canvas.drawColor(Color.BLACK);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(50);
imageView.setImageBitmap(bitmap);
up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
paint.setColor(Color.BLUE);
canvas.drawLine(100,100,100,200,paint);
}
});
Something else I want to look into is drawing multiple lines, and making sure each one lines up to the tip of the last one drawn, similar to this:
This will work for multiple buttons, but I only need it for one button right now. I hope this is sufficient information, please let me know if more is needed

Bitmap not updating/ showing old values

I have a linear layout that has multiple fields that are to be filled by the user. The fields are to be filled with text or decimal numbers and they are all TextInputLayouts with one date picker. After the user has filled those fields I have a review button which once clicked shows a preview of all the values entered in an image view. I am showing the image by using bitmap and canvas and sending the bitmap as an argument to my material bottom sheet with the following code.
private Bitmap getDailySheetBitmap() {
Bitmap bitmap = Bitmap.createBitmap(completeSheetLinearLayout.getWidth(), completeSheetLinearLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Log.d("Bitmap", "bitmap size: "+bitmap.getByteCount());
completeSheetLinearLayout.draw(canvas);
return bitmap;
}
And the code for sending arg is
private void buildBottomSheet(Bitmap bitmap) {
Bundle args = new Bundle();
args.putParcelable("Bitmap", bitmap);
ViewDailySheet viewDailySheet = new ViewDailySheet();
viewDailySheet.setArguments(args);
viewDailySheet.show(getChildFragmentManager(), "DailySheet");
}
The problem is that after the user reviews the fields and changes are made to those fields and when the user again clicks on the review button, the bitmap with the same old values is shown. I have tried recycling bitmap, invalidating the view, requestLayout() and forceLayout() method. But none of it is helping.
This is the code that puts bitmap into image view.This is in bottom sheet dialog fragment.
if (getArguments() != null) {
bitmap = getArguments().getParcelable("Bitmap");
binding.dailySheetImage.invalidate();
binding.dailySheetImage.setImageBitmap(bitmap);
getArguments().clear();
}
What am I doing wrong or missing?
The bitmap only captures what it thinks has changed. If the text field(not the data, just UI) is not changed it will show the same text field value as before. So I refreshed the whole layout as it would re-draw the entire layout by the following code.
private void refreshView() {
completeSheetLinearLayout.setVisibility(View.GONE);
completeSheetLinearLayout.setVisibility(View.VISIBLE);
}
I put this code just before creating the bitmap.

Firebase to ImageView - How come even when there is no image, my chat will display an image?

I have a standard chat display method for a Firebase chat:
private void displayChat() {
final ListView listOfMessage = findViewById(R.id.list_of_message);
final String DATABASE_URL = "https://firebasestorage.googleapis.com";
Query query = FirebaseDatabase.getInstance().getReference().child("messages").child(chatName).limitToLast(20);
FirebaseListOptions<Message> options = new FirebaseListOptions.Builder<Message>()
.setLayout(R.layout.list_item)
.setQuery(query, Message.class)
.setLifecycleOwner(this)
.build();
FirebaseListAdapter<Message> adapter = new FirebaseListAdapter<Message>(options) {
#Override
protected void populateView(View v, Message model, int position) {
//Get reference to the views of list_item.xml
TextView messageText, messageUser, messageTime;
ImageView img;
messageText = v.findViewById(R.id.message_text);
messageUser = v.findViewById(R.id.message_user);
messageTime = v.findViewById(R.id.message_time);
img = v.findViewById(R.id.imgView);
if (model.getMessageText().contains(DATABASE_URL)) {
messageText.setText("Image sent: ");
Picasso.with(getApplicationContext()).load(model.getMessageText()).into(img);
}
else {
messageText.setText(model.getMessageText());
}
messageUser.setText(model.getMessageUser());
messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", (long)model.getMessageTime().get("timestamp")));
}
};
listOfMessage.setAdapter(adapter);
}
Chat is fully functional. However, when it comes to images, I have DATABASE_URL to determine that what's being received should be an image instead of text, so I load it in with Picasso as above:
if (model.getMessageText().contains(DATABASE_URL)) {
messageText.setText("Image sent: ");
Picasso.with(getApplicationContext()).load(model.getMessageText()).into(img);
}
However, when it is normal text, my:
else {
messageText.setText(model.getMessageText());
}
Will show the text as normal, but still display an image - always the most recent image displayed. I have tried adding img.setVisibility(View.INVISIBLE); into the else statement, as well as equivalent removals from Glide/Picasso but they all end up in every single image being removed and not displayed.
It is happening because You are reusing one type of layout. Every view which will be showed next will reuse the previous one. If You don't invalidate layout's views, it can happen situation where one of the views will contain old data.
This layout is just like a skeleton where You pin some elements. Then someone want to reuse Your skeleton. He gets it with Your stuff. So he has to remove all elements and after that he can pin own stuff. If You bend something in the skeleton, next guy will have to unbend otherwise he will have skeleton with bended element (i.e. setVisibility(INVISIBLE)->setVisibility(VISIBLE)).
As soon as You get the layout, reset values in all views.

How to toggle picture src of ImageButton

I have an image button and want that when the user clicks on it-it changes its src to a different drawable-but i want the background color i defined in xml to remain the same. Here is the code that i have done so far, but doesnt work, because im changing background and not the source-but general concept i will use:
public void onClick(View view) {
if (bgenabled == true) {
holder.ib.setBackground(res.getDrawable(R.drawable.location_deactive));
bgenabled = false;
} else { holder.ib.setBackground(res.getDrawable(R.drawable.location_active));
bgenabled = false;}
Just call the setImageDrawable to replace the current image you are using
with your ImageButton.
ImageButton button;
...
button.setImageDrawable(getResources().getDrawable(R.drawable.new_image));

in android, how would you extract the image from a imagebutton?

I have two imagebutton and I want to switch the image of each other so button a will have button b's image while button b will have button a's image. I tried to do this in my code, but it does not work
Bitmap temmp = a1.getDrawingCache();
a1.setImageBitmap(a2.getDrawingCache());
a2.setImageBitmap(temmp);
follow like this
buttona.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ImageButton ib = (ImageButton)v;
Drawable d11 = ib.getDrawable(); // this is the image u can get from that button
}
You need to enable the drawing cache before calling the getDrawingCache() with setDrawingCacheEnabled(true).

Categories