Android Get the Background Ressource of an ImageButton - java

I need to get the background ressource of some ImageButton, to re-use it later, for set the background i use setBackgroundResource. I don't find any method to get the backgroundressource.
private void AddImage(int img){
ImageButton imgact1 = (ImageButton)findViewById(R.id.imgact1);
ImageButton imgact2 = (ImageButton)findViewById(R.id.imgact2);
ImageButton imgact3 = (ImageButton)findViewById(R.id.imgact3);
imgact3.setBackgroundResource(img);
}
Thanks for your help .

As for any View, you should use getBackground(), which returns a Drawable.

Related

Changing background in other activities with a button

I have a problem. I have 3 activities (MainActivity, DetailsActivity, SettingsActivity) and in SettingsActivity I have a Togglebutton "Nightmode". What I want is, when the button is changed, change background of all three activities on gray color.
public class SettingsActivity extends AppCompatActivity {
//This is SettingsActivity(not Main one)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
TextView SettingsTitle = (TextView) findViewById(R.id.SettingsTitle);
TextView NightText = (TextView) findViewById(R.id.NightmodeText);
ToggleButton toggleNightMode = (ToggleButton) findViewById(R.id.toggleNightmode);
final RelativeLayout NightBG = (RelativeLayout) findViewById(R.id.NightBG);
final LinearLayout DetailsBG = (LinearLayout) findViewById(R.id.mainBG);
final LinearLayout HomeBG = (LinearLayout) findViewById(R.id.HomeBG);
toggleNightMode.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
NightBG.setBackgroundColor(Color.parseColor("#545657"));
HomeBG.setBackgroundColor(Color.parseColor("#545657"));
DetailsBG.setBackgroundColor(Color.parseColor("#545657"));
}
});
NightBG is in the same activity as that java file (SettingsActivity). But HomeBG is in MainActivity and DetailsBG is in the DetailsActivity. Everytime I start the app, and press on that button, app craches. If I delete HomeBG and DetailsBG from this file, it works just fine with changing current layout's color to gray. Please help me.
One easy way to store little settings like this across multiple activities that may not be open/active at the time of the button click would be to use SharedPreferences.
It might be a little overkill for such a simple piece of code but you can always give it a try if you don't find anything else.
Your code could look something like this:
toggleNightMode.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Set the color of this activity
int color = Color.parseColor("#545657")
View view = SettingsActivity.this.getWindow().getDecorView();
view.setBackgroundColor(color);
// Save color preference
SharedPreferences sharedPref = SettingsActivity.this.getSharedPreferences("bgColorFile",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("color", color);
editor.apply();
}
});
And then when you open your activities you place something like this in the onStart() or onCreate() method of your activity:
// Get the color preference
SharedPreferences sharedPref = getSharedPreferences("bgColorFile",Context.MODE_PRIVATE);
int colorValue = sharedPref.getInt("color", 0);
View view = this.getWindow().getDecorView();
view.setBackgroundColor(colorValue);
So what you're actually doing is storing the background color as persistent data and fetching it once you reopen/open the activity that you want to have the color on. The benefit of this method is that whenever you close your app the preferred background color will be remembered. I hope this helps.
Change background for current activity in the same activity. Since DetailsActivity is not running, you can't do that, it gives you null pointer. Is kind of you are trying to eat 3 apples and you have just one. After current activity is started, change background.
Update:
You can do that in current activity and just in current activity:
findViewById(android.R.id.content).setBackground(getColor(R.color.your_color));
Don't try to call this in other activities that are not running.
setBackground()
or
setBackgroundColor()
If your other activities are open, you should send a message to the other activities by using an Intent.
How to send string from one activity to another?
When you receive the Intent you could then set the background of the activity.
If your other activities are not open yet, you will not be able to send an Intent to them. In this case you could have each Activity reference a static value in your main activity that could contain the current background color. You would want to reference that value on the other activities on create functions.
Here is an example on how to reference a variable from another activity.
How do I get a variable in another activity?
This might not be the most pretty way to handle it but it should work.
as Ay Rue said you have 2 options: use static variable for that button, and then in onResume of each activity, check the value of the static variable (true or false). or you can save a private variable nightMode and then pass this value in the intent when you need to move to the other two activities.
don't set the background color if you already set before and have an updated background color.

Android: Picasso enlarging Image on Click?

I was using Picasso for a while now and I'm very satisfied with it. However I'm in need of enlarging an imageview's image which was loaded by Picasso. As in, I simply want to create a zoomImageFromThumb of the image. However the traditional zoomImageFromThumb requires a resource to be present in the project. In my case, Picasso loads it up.. so how do I enlarge an image laoded by Picasso on click of the image view?
My code so far:
ImageBanner= (ImageView) findViewById(R.id.lb_single_image);
Picasso.with(ctx)
.load("www.flickr.com/randomimage.jpg")
.placeholder(R.drawable.loading_placeholder)
.error(R.drawable.error_placeholder)
.into(ImageBanner);
ImageBanner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//how do I enlarge the picasso image?
}
});
Instead of setOnClickListener() you can use PhotoViewAttacher:
ImageBanner= (ImageView) findViewById(R.id.lb_single_image);
Picasso.with(ctx)
.load("www.flickr.com/randomimage.jpg")
.placeholder(R.drawable.loading_placeholder)
.error(R.drawable.error_placeholder)
.into(ImageBanner);
PhotoViewAttacher mAttacher = new PhotoViewAttacher(ImageBanner);
Only one line, and it will works.

Set loading bitmap to spinner in progress dialog

I used a tutorial that showed me how to load bitmaps in an async task and set them to an imageview and in the tutorial before the bitmap is loaded the imageview is set to black with this piece of code
static class DownloadedDrawable extends ColorDrawable {
private final WeakReference<DownloadImageTask> bitmapDownloaderTaskReference;
public DownloadedDrawable(DownloadImageTask bitmapDownloaderTask) {
super(Color.BLACK);
bitmapDownloaderTaskReference =
new WeakReference<DownloadImageTask>(bitmapDownloaderTask);
}
public DownloadImageTask getBitmapDownloaderTask() {
return bitmapDownloaderTaskReference.get();
}
}
How can I change the imageview so that before it is loaded it is the spinner in the progress dialog.
Thanks in advance.
you can try aquery android library for lazy loading image and listview...below code may help you.....
AQuery aq = new AQuery(mContext);
aq.id(R.id.image1).image("http://data.whicdn.com/images/63995806/original.jpg");
You can download library from from this link

Show description when click on image

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;
}
}

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