android change image button background - android

I cannot seem to change the background image of my image button. Heres the code i'm currently trying to use:
ImageButton imgButton = (ImageButton) findViewById(R.id.showSportsButton);
imgButton.setBackgroundResource(R.drawable.tab2_selected);
However this seem to be placing the new image on top of the old image leaving me with 2 images overlapping each other.
Does anyone know why this is??

For solving this question you should implement
imgButton.setImageResource(R.drawable. tab2_selected);

Use this method:
imgButon.setBackground(getActivity().getDrawable(R.drawable.your_icon));

in xml file, <Button> write: android:backgroud="#drawable/your_file"

Make sure you are on same activity. If you are changing background of different activity first create the constructor then use object to change button.
Activity obj= new activity();
obj.imgButton.setBackgroundResource(R.drawable.tab2_selected);
and also check that oncreate() method has void return type if you are using only findviewbyid.

Related

Change image Floating Action Button Android

I used this library https://github.com/futuresimple/android-floating-action-button. How can I change the image of the main button? I want to change the button image right after selecting one of the smaller buttons.
From https://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html
As this class descends from ImageView, you can control the icon which
is displayed via setImageDrawable(Drawable).
Or you can use setImageResource():
fab.setImageResource(R.drawable.ic_shopping_cart_white);
you can use this in your .XML :
android:src="#drawable/icon" // change backgroung icon
app:backgroundTint="#color/icons" // change background color
use this in code:
mFloatingActionButton.setImageResource(R.drawable/icon2);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_btn);
// State 1 -on
fab.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.fab_on));
// State 2 - off
fab.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.fab_off));
In Kotlin, this will be:
#RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun pauseTimer() {
floatingActionButton.setImageDrawable(getDrawable(R.drawable.ic_baseline_play_arrow))
I faced same issue recently, I tried with following option
fab:fab_icon="#drawable/icon"
and
android:src="#drawable/icon" // change backgroung icon
even tried programmatically
fab_menu_btn.setImageResource();
Nothing worked.
Solution: In app's build.gradle file replace
compile 'com.getbase:floatingactionbutton:1.10.0'
to
compile 'com.github.toanvc:floatingactionmenu:0.8.9'
In .xml file Use:
<toan.android.floatingactionmenu.FloatingActionsMenu
</toan.android.floatingactionmenu.FloatingActionsMenu>
In activity File:
floatingActionsMenu.setIcon(getResources().getDrawable(R.mipmap.icon));
In the Material design version:
build.gradle (app)
defaultConfig {
vectorDrawables.useSupportLibrary = true // For srcCompat
}
dependencies {
implementation 'com.google.android.material:material:<version>'
}
XML (for set icon)
app:srcCompat="#drawable/ic_google"
app:tint="#color/colorGoogle"
More documentation: https://material.io/develop/android/components/floating-action-button/
I had the same problem and I managed to create my own solution. Maybe some else founds it also useful. I have posted the complete answer to another question (How to set an icon to getbase FloatingActionsMenu) but this part posted here is relevant to the question in dynamically changing the main menu button picture/image when one of the sub buttons is chosen. In this case, you need o combine the answer from the "linked question" and the answer below.
In order to change the icon on the menu button when you choose a floatingActionButton it can be implemented like this:
Create menu button in xml file, create floating button(s) on .java file (programmatically) set menu button (color button, color pressed button and image). Then simply add all buttons to the menu button. You can also deactivate the animation of the menu button by simply commenting out the code in FloatingActionsMenu class.
Then every time that you create a button, sample:
final FloatingActionButton actionA = new FloatingActionButton(getBaseContext());
actionA.setTitle("Familie");
actionA.setIcon(R.drawable.world_map);
actionA.setSize(FloatingActionButton.SIZE_MINI);
actionA.setColorNormalResId(R.color.red);
actionA.setColorPressedResId(R.color.black_semi_transparent);
actionA.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
menuMultipleActions.setMenuButton(R.drawable.icon, R.color.red_transparent, R.color.black_semi_transparent);
Toast.makeText(MainMapView.this, "Action Description", Toast.LENGTH_SHORT).show();
((FloatingActionsMenu) findViewById(R.id.multiple_actions)).collapse();
return;
}
});
See the answer posted on the link on how to configure the classes and define the menu button and floating button(s).
So the important part to notice here is:
menuMultipleActions.setMenuButton(R.drawable.icon, R.color.red_transparent, R.color.black_semi_transparent);
This method you need to add in the FloatingActionsMenu class. Simply you call the method after on every floatingActionButton you want to update the image.
More information you can find on the link that I have posted. So when you click one of the floatingActionButton(s).
For the moment the color on the menu button is not updating correctly but I am working on it if I find a solution I will update the answer here as well. Hope this helps, happy coding.
Unfortunately with this library you can't change the icon from the menu (see issues from this library from more info)
That is why I dropped this library to use a way more flexible one! It is originally a fork but it is now more advanced.
Here is the link
Add the property
fab:fab_icon="#drawable/fab_expand"
in the xml where you initialized the floating action menu.

How to change existing TextView style in action

I have some intent with set of TextViews and a Button. If user clicks the button and there is some error I want to change look of one TextView. E.g. change the border to red and make font bold. I wrote a style for it but I have not found method setStyle on the TextView. After some self study I realized that Android does not support setting the style programmatically. There are some workarounds, when you create the intent source. But my intent already exists, it seems odd to recreate it.
Could you tell me the proper way?
use the workaround and create the TextView again
forget the styles and use java methods to decorate existing TextView
something else
Changing the style of the textview directly does not work as you know. But you can create a second textview with other styles in your layout, which you can show up if needed.
Just add this xml attribute android:visibility="gone" to the second textview, so this second textview is not displayed at first, but available.
When you now want to change the style of your textview, you simple need to swap the two textviews by hidding the first one and showing the second one
textView1.setVisibility(View.GONE);
textView2.setVisibility(View.VISIBLE);
I used these two answers to make it work:
https://stackoverflow.com/a/5488652/1639556
https://stackoverflow.com/a/14195090/1639556
and the code is:
ViewManager parent = (ViewManager) unknown.getParent();
parent.removeView(unknown);
TextView newUnknown = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
newUnknown.setId(unknown.getId());
parent.addView(newUnknown, unknown.getLayoutParams());
unknown = newUnknown;
You can try using setTextAppearance() on the textview. The link is: setTextAppearance
Your style will need TextAppearance.SomeThing.SomeOtherThing as the parent.
Use R.style.YourStyleName as the integer argument.

How to clear ImageView correctly?

For example, in my Activity I have such code (I skip the initialization of variables):
ImageView iview; //some ImageView
Bitmap b; //some Bitmap
iview.setImageBitmap(b);
Question is - how to clear iview resources correctly (with or without destroying view) ? Would ImageView free it's resources (used in native code) after b.recycle()?
I suppose, that ImageView doesn't just free it resources after Activity onStop (or onDestroy).
imgview.setImageResource(0);
or
imgview.setImageDrawable(null);
no you need to unbindDrawables, you can do it by setting iview.setImageDrawable(null);
You can use frequently it works:
imageView.setImageResource(0);
viewToUse.setImageResource(android.R.color.transparent);
I think using setImageResource with a color identifier will give you crashing issues on Android 2.2.1, make sure to test it.
if nothing is working for you try setting the background color of view to layout color.if my layout color is white u can do like this:
edit_countflag.setBackgroundColor(Color.parseColor("#ffffff"));
//then set the image
edit_countflag.setImageResource(R.drawable.flag_id);

Best way to program buttons/images

I am designing something that will have a homepage close to the Google+ android app.
I'm having trouble figuring out the best way to do this? Should I create a button with text and set the background as the image? Should I create an image with the text already programmed in the actual picture or should I program the text and picture to be buttons.
Any suggestions from you guys on past projects?
You can use a Button with text to whatever you like and then place the image for that button above the text (using android:drawableTop)like so:
<Button
android:id="#+id/imageButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Photos"
android:drawableTop="#drawable/button_image" />
replacing buttton_image with your actual image. If you want the image in a different position (i.e. below text etc) use:
android:drawableLeft
android:drawableRight
android:drawableBottom
This would be how I would and do do it...
Since I am a newbie in android development I may be wrong but I suggest why not use a Grid View with each grid item haaving a textview and imageview.
My suggest to layout:
If you want to try something new in Android 4.0 , you can try GridLayout to layout , it can reduce the complicate of the nested layout , check out the blog about GridLayout:
http://android-developers.blogspot.com/2011/11/new-layout-widgets-space-and-gridlayout.html
You should write Buttons this way: http://developer.android.com/resources/articles/layout-tricks-merge.html
I normally use RelativeLayout but this is not important.
Write a class myClass extends RelativeLayout and inflate the XML with
LayoutInflater.from(context).inflate(R.layout.myCustomLayout, this, true);
I think this is best practice by Google.
use a regular button and set the android:drawableTop="#drawable/icon_...." value
Having the text burnt into you image is most certainly not the way to go.
One option for you is a GridView. You could also do this through a combination of Image button and scrollview. In my opinion, GridView is best and involves the least amount of code with most flexibility.
Remember that you can replace the button's background image with a state list drawable.

How to change background image of a ImageButton in android

I am facing a situation in which-
1.First I have to set the background image of a ImageButton to a default image.
2.Now at runtime based on some condition I have to change the background image of that ImageButton to some other image.
I have tried this but it doesn't work and the background image remains to default image and
doesn't change.
btn.invalidateDrawable((Drawable)getResources().getDrawable(R.drawable.info));
btn.setBackgroundDrawable((Drawable)getResources().getDrawable(R.drawable.infonewred));
How can I achieve this goal.
Have you tried btn.setBackgroundResource(R.drawable.infonewred).
I suggest you set the "default" Background directly in your XML-Layout-File via android:background.
If you have to change the Background in Runtime just use btn.setBackgroundResource(R.drawable.imagename);
since you haven't provided xml, i am guessing you are providing src to imageButton.. so to change the source of the image , what you are doing is changing the image background which will not be visible since it is the source which will be visible to you.. so to change source..
do this.
btn.setImageDrawable()
rather than..
btn.setBackgroundDrawable()
I know that's old but i got the solution as i'm also was searching for that, the correct answer is to use setImageResource , so the code will be like that:
btn.invalidateDrawable(getResources().getDrawable(R.drawable.info));
btn.setImageResource(R.drawable.infonewred);
Try the method setImageResource
Ex:
if(someCondition()){
btn.setImageResource(R.drawable.info);
} else {
btn.setImageResource(R.drawable.infonewred);
}
Just try following code to change at runtime and for default view set background property in layout xml:
if(yourCondition)
btn.setBackgroundResource(R.drawable.infonewred);
in activity file you can write
btn.setBackgroundResource(R.drawable.sliderr);
OR
in your xml layout file you can add one extra line to you button xml code
android:src="#drawable/sliderr"

Categories

Resources