How to set View to ActionBar icon? not by drawable - android

I'm trying to use CircleImageView as ActionBar icon (like chatting activity in WhatsApp). I've tried this approach:
CircleImageView actionBarIcon_Profile = new CircleImageView(this);
actionBarIcon_Profile.setImageDrawable(getResources().getDrawable(R.drawable.my_picture));
actionBarIcon_Profile.setLayoutParams(new LayoutParams(48, 48));
actionBarIcon_Profile.setId(R.id.actionBarIcon_Profile);
getActionBar().setIcon(R.id.actionBarIcon_Profile);
ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="actionBarIcon_Profile"/>
</resources>
It seems like ActionBar setIcon() only accepts drawables as input. I could have setup a circular shape xml and put it in my drawable folder, but even that I have to change the background of that xml file to fit my need. Any suggestions?

link - http://javatechig.com/android/actionbar-with-custom-view-example-in-android
see if this link helps you

Related

Android Image Asset is not drawn

I am trying to implement a splash screen for my application, following the usual method; defining a new theme inside the styles XML file that places a drawable as windowBackground and reverting to the main theme in the Activity's onCreate().
However, I am having problems when showing the logo I developed in Photoshop: I exported it as a 144x144 PNG file, created an Image Asset with it (replacing the standard ic_launcher drawable) but when I try to visualize it inside an XML file, this image is displayed instead
Screenshot
Any ideas? I've also tried to create a vector asset from the .psd but nothing is shown either...
https://romannurik.github.io/AndroidAssetStudio/
use this link to generate your icon. It will work.
Change the code and so on
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/your_background"/>
<item android:drawable="#drawable/your_image">
</item>
</layer-list>

Android - ResourcesNotFoundException when trying to set Action Bar icon by resource id

I am changing my Action Bar icon to CircleImageView that shows profile picture:
CircleImageView actionBarIcon_Profile = new CircleImageView(this);
actionBarIcon_Profile.setImageDrawable(getResources().getDrawable(R.drawable.my_picture));
actionBarIcon_Profile.setLayoutParams(new LayoutParams(48, 48));
actionBarIcon_Profile.setId(R.id.actionBarIcon_Profile);
getActionBar().setIcon(R.id.actionBarIcon_Profile);
The ids are kept in ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="actionBarIcon_Profile"/>
</resources>
I get ResourcesNotFoundException. Any help will be appreciated.
setIcon is expecting to receive an id of a resource. That id actionBarIcon_Profile is just an unique id you're creating that has no resource associated with it. That's why you're getting ResourcesNotFoundException.
You can set a drawable to your actionbar's setIcon method. Not an imageview. You need to create a circular drawable instead of a circular imageView.

Change the Style of an Image View on Pressed State in Android

I have an ImageView in my application. When user Clicks the imageView I am supposed to show a border around the ImageView. This Border style I have it as a style called "myStyle" in my styles.xml.
I need to show this style only when the user clicks the image view. How can I do this?
Well, i will advice you to get the border style info into an xml file and save it at the drawable folder - let's call it border_pressed.xml.
And then at the drawable folder create a file called, let's say, imagview_state.xml and put in it the next code -
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/border_pressed" android:state_pressed="true"></item>
</selector>
and at the layout file - where the ImageView is- set it's background like that -
android:background="#drawable/imagview_state"

Default RGB color code for activity backgrounds

I am adding pictures to my Android project and the background color of those pictures are the darkest of the darkest color (RGB: 0, 0, 0) but I can see the background color of the images is different from the background color of the android activities.
What is the default RGB color code for the activity backgrounds or is there a method to get it?
(Yes, I know that I could change the background color of the activities to the desired color instead, but that would require that I change all my activities' background color)
As Warpzit recommended I looked into styles and the following solved my problem:
Create an XML file in "values" folder with the following code inside:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="BackgroundStyle" parent="#android:style/Animation.Activity">
<item name="android:background">#000000</item>
</style>
</resources>
In each layout XML file add the following inside the LinearLayout tag:
style="#style/BackgroundStyle"

Is it possible to have a Button with a background image with text on top?

I need button that has a replicated background pattern and normal button text on top - how to specify this in layout XML?
Override android:background on the Button in question. Or, for reuse you could declare your own style, overriding android:background from the base button style:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyButtonStyle" parent="Widget.Button">
<item name="android:background">#drawable/fancy_button</item>
</style>
</resources>
In either case, your fancy_button drawable would be an image, or more likely a StateListDrawable.
For the Button instances you wish to apply this to, you'd do:
<Button style="#style/MyButtonStyle" />.
Consider using ImageButton instead of using regular one.

Categories

Resources