I m beginner of android . .
I have an application in which there are various activities on which different text is appearing. In my application i need all the text in this app. I got 60% of the text in the app that is set using setText() method . But still some text is visible but i dont know how they have displayed this text.
SO what i want is that, the methods or any other ways in android that enables us to display the text in our activity.
For more clarification i show u an image of app:
I need to know how these texts are been set . .
It will be helpful if list out methods which are used to set text .
you can display text in a textview, via java or xml, or you can use canvas.drawText().
these are the simple two... there is also the Toast for quick pop-up text, and dialogs which are toasts with buttons... there are several ways you can display text. the image above, is an image of text set to the background of a tab, or the src/background of an imageview...
Mainly you have to ways of showing text:
Using an element that allows showing text: Button, TextView
for these guys, you'll normally use the .setText setter that you probably already know.
You can show an image or use an image as background for a element: ImageView, ImageButton
for setting an image to an object, you'll normally use
public void setImageResource (int resId)
public void setImageBitmap (Bitmap bm)
public void setImageDrawable (Drawable drawable)
The buttons above, mostly probably are set with an image.
For showing different images for different states (on tap, on select, etc) you'll have to define properties like the ones in the example below for the ImageButton:
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
More information at:
http://developer.android.com/reference/android/widget/ImageView.html
http://developer.android.com/reference/android/widget/ImageButton.html
http://developer.android.com/reference/android/widget/TextView.html
Related
I'm wondering if it's possible to build a custom button layout with different appearance depending on state so it acts like a button. For example, a LinearLayout with an ImageView and TextView, with these states.
When the button is in normal state, display image_normal.png and text in red.
When the button is pressed, display image_pressed.png, background red and text in white.
When the button is disabled, display image_disabled.png and text in grey.
Thank you!
Any view can have any background and any view can be made clickable, so there is nothing preventing you from having a LinearLayout with the background you described. It's worth noting that you don't need a LinearLayout to have an image next to text, you can use the fact that TextView supports drawables on any side of the text using the drawable[Left|Top|Right|Bottom] attributes.
If your question is about the syntax of a selector drawable, I would refer you to the documentation. Note that Android evaluates states top to bottom, so choose the order wisely. The last item should be the "default" state when none of the ones above apply. For the example you gave, you would probably have something like
<selector>
<item android:state_pressed="true" android:drawable="..." />
<item android:state_enabled="true" android:drawable="..." />
<item android:drawable="..." /> <!-- disabled -->
</selector>
I thought to use OnTouchListener and tracking down and up events, setting then an alpha value, but I have to add this listener to all ImageButtons (they are really a lot). I wonder if there is a shortcut to achieve this result.
In case that when user click on button and this cause to change the opacity of that button you can do:
In your xml file on button declaration add this line:
android:onClick = "clickMethod"
and in the java file you need to implement the clickMethod,
public void clickMethod(View view)
{
// change opacity
}
so, if you want to do the same process(change button opacity) for each button, so in the xml file
for each button add the line
android:onClick="clickMethod"
If you want to give users better expirince when clicking ImageButton, I recommend you to use selection drawable as the background of your ImageButtons. It gives better user experience, then setting opacity while performing click and it’s really easy to achieve.
First you need to create in your drawable folder file with name f.e. image_button_selection.xml. In which you should define:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/your_focused_background_drawable" />
<item android:state_pressed="true" android:drawable="#drawable/your_pressed_background_drawable " />
<item android:drawable="#android:color/transparent" />
</selector>
You should also put into drawable two png with alpha channel that will be displayed on focused button and pressed button. In this example they should be named respectively your_focused_background_drawable and your_pressed_background_drawable.
When you do this you should in every use of your ImageButton in xml use following statement:
android:background="#drawable/image_button_selection"
I want to add a border to a borderlessbutton. However, if I create my own style with the parent borderlessbutton and I overwrite the background tag I loose the state change animation etc, as this is also defined by the background. I also cannot implement them myself as the native android drawables are not available as public and therefore not accessible. I do not want to have to copy the drawables.
Is it only possible to overwrite ondraw progammatically or is there an xml based solution i am missing?
(btw this is for a periodic table so this should not involve having an xml file for each button as there are about 100 of them)
thanks
stephan
The bulk of this comes from this article.
There is a 4 step process to doing something like this:
Create an XML file that contains the states.
Create an XML file (Or background) for each state
Create the style of the button
Add the button to your layout, and see what it looks like.
The single most difficult thing is the first step, so I'll show that one here. For the other ones, you can visit the site or do your own thing. Essentially, it will look something like this. Basically needs to capture all of the 4 states. This should be saved in the drawable folder, and the name of this is what your application will use for the name of the drawable.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#drawable/loc_for_button_disabled" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/loc_for_button_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/loc_for_button_focused" />
<item
android:state_enabled="true"
android:drawable="#drawable/loc_for_button_enabled" />
</selector>
An easy way would be to place your button within a separate layout, e.g. a LinearLayout and give this layout a background color and a padding of e.g. 1dp. This would render a "border" around the button. Note, that this is quite costly in regard of layouting, so do not use this method when you have lots of buttons.
The correct solution would be to create your one drawables for all states and build a statelist drawable with your drawables and assign this statelist drawable as your button's background. Actually it's not that much work, just have a look at http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
In my android application i want to create three different image of a single image for each state pressed , default and focussed. Is there any tool that can help me to create these images.
I think you misunderstood my problem, i knows that how to use three different image for three different states. I only wants to know that how two create those three images.
here http://code.google.com/p/iosched/source/browse/android/#android%2Fres%2Fdrawable-hdpi
you can see that there are three different images for three differenct states
home_btn_announcements_default.png
home_btn_announcements_pressed.png
home_btn_announcements_selected.png
I wants to know that is there any tool that can create the above three images. I think now my question is clear to everyone.
My Question was that how to add white border for default image , gray border for focussed image. I know that how to change image according to different states. I want to ask you that is there any tool that can put white or gray border around my image.
Use selector save this file in drawable/click.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:drawable="#drawable/notPressedImage ></item>
<item android:state_pressed="true" android:drawable="#drawable/PressedImage" ></item>
</selector>
use click.xml (click) as button src...
I had a simple button set up with a background image defined like
android:background="?attr/button"
where ?attr/button was a reference to a simple 9-patch png. Everything worked fine, text in the button was aligned correctly.
Then I needed to have a different background for a pressed state of the button. So I changed that to
android:background="#drawable/state_button"
where #drawable/state_button is an xml with the following states
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_pressed" /> <!-- focused -->
<item android:drawable="#drawable/button" /> <!-- default -->
</selector>
And after that I can't align the text properly. If I put android:gravity="center_vertical" the text is drawn about 1/4 of the button height from the top.
I double-checked my 9-patch images, everything seems fine with them. And I also tried having regular pngs for the background, it also doesn't change anything.
You should double check the 9 patch drawables you're using. The standard Android buttons include a huge amount of padding at the top and bottom of the buttons, making it look like the text is always centered. You can see this by opening up the 9 patch file, zooming in closely and looking at the difference between the pixels on the left/top and the right/bottom. The left/top sides mark which parts of the image can be stretched to accomodate more text, while the right/bottom sides mark the space that will actually be filled with text. So the difference between the right/bottom side and the left/top will be the padding. It doesn't quite make sense at first, but after playing around with it it's not so bad.
Just in case you aren't familiar with it, a useful tool for editing 9patches is the draw9patch.bat program in your SDK tools folder.
I had the exact same issue however my 9 patch drawables were ok. The cause was still the same though, just i was using custom drawables using the layer-list element.
It seems that when the Button lays out its text it takes into account all of the states in your selector. Once i'd updated all of the states to match each other my text subsequently aligned correctly.