I have got a cool image (fancy with rounded corners). I want to implement that image in my android app. When clicked on it, I want it to darken a bit (like a button when pressed) and launch a new activity.
How can I do that and what should I use (ImageView, ImageButton or just Button) ?
You'll require two version of your cool image call it as active.png (clicked state i.e darken a bit) and inactive.png (normal state).
in the drawable folder create a Selector file for them as follows
Like : /drawable/cool_button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/active"/>
<item android:drawable="#drawable/inactive"/>
</selector>
and then user the ImageButton like this:
<ImageButton
android:id="#+id/coolImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/cool_button_selector"
android:background="#null" />
Added: android:background="#null" to <ImageButton>
Hope this helps :)
You can use any of the above depending on what your requirements are. For button like behavior, you can use an ImageButton, Here's the instructions to change the image onTouch
change button image in android
You can similarly change the image of ImageView.
ImageView img = (ImageView) getViewById(id-here);
img.setImageResource(R.drawable.my_image);
you can use a normal Imageview with an onClickListener, the imageview's drawable will be a state list drawable xml file that comes with its own built in event listeners which can be linked to separate image assets.
Related
I have no idea how to import my Photoshop shape that I made for my buttons, into android, without it bringing along the background. Even if i set the Background in photoshop to Transparent and save it as a PNG, the background still takes up space when I use it in android as an android:background for my Button. Please Help!
Also, a quick side note: If i make the buttons the appropriate px size that is equivalent to the right mdpi, hdpi, and xdpi, will it still be an issue if it doesn't look proportional in the GraphicalLayout of Eclipse? Like will it adjust itself on the screen?
Ok ok ok!
What you need to do, is to create a CUSTOM BUTTON instead of actually just using a background.
First, you need 3 png's:
1) When the button is idle.
2) When the button is pressed.
3) When the button is highlighted.
The Highlighted and idle could use the same image if you dont need such effect.
Create a resource in your drawable carpet. I usually start with MDPI then move on to the bigger screensizes, but this will give you the main idea.
The resource would be your entire button. It goes like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused"
android:state_focused="true" />
<item android:drawable="#drawable/button_idle" />
</selector>
Let's name it "custom_button.xml"
The Selector is THE BUTTON.
You will need to add the 3 resources explained above "button_pressed" is what the button should look when pressed. And so on... the names are self explanatory.
Now in your layout page you could use 2 ways to create your button.
1) If the button is an ImageButton:
<ImageButton
android:src="#drawable/custom_button"
android:background="#null"
...
You have to eliminate the background in this case.
2) If the button is a Normal button with text:
<ImageButton
android:background="#drawable/custom_button"
...
In this case you can add padding to make it contain text.
And yes, you would have to work with photoshop for the actual size of the button. The button can strech or crop when it grows but the results would be not as good and clean as if you nail the right size from photoshop.
Can someone please guide me how exactly can i create a circular buttons and textview in android apps, I am still a new into android development. Please help
You cannot create a real circular view (button or textview) in android - all views are rectangular. That said, you can imitate the look of a circular button by giving it a circular background. There are several ways of doing this. One simple way would be to create a PNG image of a circle, save it in your drawable folder and then set it as a background for your button:
<Button android:background="#drawable/circle_bg" .../>
Another way would be to use custom shapes to draw a circle:
<shape android:shape="oval" ...>
<solid android:color="#FFFFFF"/>
</shape>
Save that as an XML file in your drawable as circle.xml and then reference it in your layout:
<Button android:layout_width="40dp" layout_height="40dp"
android:background="#drawable/circle" ... />
This will create a circular button with 20dp radius.
Of course, you may want to combine this with state selectors to get the highlighted states on touch or focus.
There are loads of examples on the internet. Just search.
As some1 mentioned, to make your custom buttons you've to define Styles:
Custom circle button
About TextView: What do you want to do? Change font? Change colour? Change Size?
You've XML attributes for TextViews if you're doing this by XML. Except to change font, that it was implemented in 4.1.
android:text="TextView"
android:textColor="#288BA2"
android:textSize="30sp"
Is it possible to add an image(view) on top of a button (which as a background image)?
I'm porting an iOS app to Android and it wasn't a problem on iOS, but I'm wondering if it is the right approach on Android because of layouts.
Edit :
To clarify, check this screen shot :
http://a4.mzstatic.com/us/r1000/062/Purple/v4/7c/4b/cd/7c4bcd53-ba55-94d7-a26c-ce1bfe040003/mza_2736801523527387264.320x480-75.jpg
I need to do the bottom left button "carte" (card in french)
I need :
a button with a background image
an image displayed on top of the button which is loaded from internet (a card , there is a lot of different and news cards are added daily, in the screnshot it s "MIDI PASS" )
a text localised on the button , so I cant use Imagebutton class.
It is not quite clear what you want to achieve, but the following may be helpful for you:
Use an ImageButton
Set the Image as the background of the button either in XML (using android:background) or in code (using setBackgroundResource)
Update:
Looking at your updated requirements, it would be better to use a custom component to achieve what you want.
Your question is somewhat unclear but from what i understood, following may work for you:
<ImageButton android:id="#+id/imgButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bg_image"
android:src="#drawable/top_image"/>
Hope it will be helpful.
UPDATE:
If Your background is common, then you can set the bitmap using following code:
((ImageButton)findViewById(R.id.imgButton)).setImageBitmap(bmp);
Here, you will need to get the bitmap of the card image in bmp variable.
You can also use an ImageView and implement the onClickListener.
Yes it is possible.
Use an ImageButton then....
set your android:src="#drawable/foreground Image"
set your android:background="#drawable/background Image"
So if you wanted a an apple for the background image and a 3-d word "apple" for your foreground image.
You could try something like this:
First, you create a selector for the button in the res/drawable/ folder (let's call it selector_button.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/image_resource_for_button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/image_resource_for_button_pressed"
android:state_focused="true" />
<item android:drawable="#drawable/image_resource_for_button_normal" />
</selector>
Here you can define as and android:drawable not just #drawable's, but #color's or #layout's, too. If you want a more complex layout, you should define one with the background image of the button and another image on top of it using a RelativeLayout for example.
In order to do this, you have to have image_resource_for_button_pressed.png (for pressed and focused state) and image_resource_for_button_normal.png (for normal state) in your res/drawable/ folder.
After that, you create a button, like this:
<Button
android:id="#+id/aButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector_button"
android:text="Hardcoded string" />
This approach helps you maintain code readability, since you just extracted the changing of the image resource into an .xml file.
Now problem starts with just setting the color of a shape.
I created one rectangle shape in drawable and set it to ImageView like this
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="0.15"
android:layout_marginRight="10dp"
android:gravity="center_vertical">
<ImageView
android:id="#+id/iv_priority"
android:src="#drawable/case_priority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
EDIT
My case_priority.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#009600" />
<size android:width="30dp"
android:height="30dp" />
</shape>
now I need to change the color of this shape. Actually this color is coming from web service, and I need to change it.
So My Question is: How can we change color of this shape programmatically. and set it to imageview.
I have gone through few examples in stackoverflow but I am not able to change it to image view.Please Help!!!!
Thanks in advance.
EDIT:
May be I was not clear in my question, So Just a refinement to question . Actually I have a ImageView where I set a source which is actually a drawable. By default its green as in my code shown above. Now web service returns the color of this image view which needs to be changed accordingly , so I can't change it from xml during runtime so I need to change it from java file. Since I am using shape and image view, .setBackground() won't help much here. So What can be the probable solution for this. Thanks for answer and sorry for inconvenience.
Thanx for all the answers.It helped me getting to answer.
In My layout I changed android:src="" to android:background="#drawable/case_priority"
And in my java file I n=included this code:->
Resources res = getResources();
final Drawable drawable = res.getDrawable(R.drawable.case_priority);
drawable.setColorFilter(Color.YELLOW, Mode.SRC_ATOP);
ImageView img = (ImageView)findViewById(R.id.iv_priority);
img.setBackgroundDrawable(drawable);
that's it. Hope it will someone.
Have a look here
http://developer.android.com/guide/topics/graphics/2d-graphics.html#shape-drawable.
Instead of creating the Shape in code you can use inflate to inflate your XML if you want.
IimageView.setBackgroundColor("#152");
add this to image view
android:background="#152" // or some other color code
Edit 1 :
to do it with code do this
ImageView.setBackgroundColor(Color.parseColor("red")); // Or any color code like #000 , #987
Edit 2:
ImageView Imgs = (ImageView) findViewById(R.id.iv_priority);
Imgs.setBackgroundColor(Color.parseColor("ColorCode"));
All solutions suggest to use the ImageView's background. I think this is not the desired solution. The question already reflects what an intuitive approach would be: Applying the shape to the src attribute. In order to then change the shape's color programmatically you would do:
GradientDrawable drawable = (GradientDrawable) priorityImageView.getDrawable();
drawable.setColor(Color.RED);
given that the shape is assigned as in the original question:
<ImageView
android:id="#+id/iv_priority"
android:src="#drawable/case_priority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
This also brings the advantage that you can set the ImageView's background separately from its content.
As Lars Blumberg's answer I also did not want to change the background, so I applied the following to code to my Image, since I did not have a GradientDrawable:
Drawable drawable = imageView.getDrawable();
drawable.setColorFilter(Color.CYAN, PorterDuff.Mode.MULTIPLY);
I'm trying to get a button to look like the one at the link I've posted below.
It's a transparent button with image at the top and text on the bottom. When a click occurs, the whole bounding box gets highlighted.
This is how the button looks like when pressed: http://i44.tinypic.com/24nle9e.png
Not sure if this effect is achieved with a Button or an ImageButton. Any ideas? Thanks.
dor506's answer is basically correct, but I'd use a ImageButton instead of a Button, and set it's background to transparent (android:background="#00000000" - the 0 alpha is the key bit here) before applying your Drawable via android:src="#drawable/your_drawable_id".
In case this helps, here is an example of a suitable Drawable:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/button_hi" /> <!-- pressed -->
<item android:drawable="#drawable/button_lo" /> <!-- default -->
</selector>
where 'button_hi' / 'button_lo' are the two images you are switching as a result of a click.
I don't think you can achieve this result with button/imagebutton properties.
here's a simple solution:
you can create two images, one for the button when it isn't clicked, and one for the button
when it is clicked (with the orange color behind..)
then you should create a selector. it is an xml which handle click/non click/focus image behaviour.
finally give the button the background of the selector
<Button android:layout_width="wrap_content" android:background="#drawable/selector"
android:layout_height="wrap_content" />
By the looks of it, it appears the image you're showing is an ImageButton with the image being a PNG image with transparent sections throughout it. If you want to keep the button selected you should be able to mess with the button parameters:
android:state_selected = "true"
Try that.