i can set image textview rightside using xml attribute like
<TextView
android:id="#+id/txtarrow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/arrow"
android:gravity="right"
android:onClick="VideoClick"
android:padding="5dip"
android:textColor="#feb4e7" >
</TextView>
i want to set
android:drawableRight="#drawable/arrow"
programatically
like
txtarrow1.setBackgroundResource(R.drawable.arrow_blue);
but this one is not working
You can simply use Google to find the Android java docs of TextView.
They have a huge table showing every attribute's its programatical option.
android:drawableRight (#compile time)
setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) (#runtime)
The drawable to be drawn to the right of the text.
Here You want to use
Adding image on specific direction in Text View pro-grammatically
There are 2 method :
1> Through Image Drawable
ex:
Drawable img = getContext().getResources().getDrawable( R.drawable.dogy );
txtarrow1.setCompoundDrawablesWithIntrinsicBounds(
img,// left
null,//top
null,// right
null//bottom);
Remember this method is most use-full in image store in one view (Text View , Button)
2> Through Image Id
txtarrow1.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.ic_doggy,// left
0,//top
0,// right
0//bottom);
Most probably these method use in different images display in multiple view like (GridView)
Must remember difference between these two when Drawable can't show image set null & in ID set 0 (zero)
You can use SpannableString..
SpannableString text = new SpannableString("text");
text.setSpan(new ImageSpan(getActivity(), R.drawable.arrow_blue), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
txtarrow1.setText(text);
Related
just a question wondering when I was working on my app
let say I have image which I want to make it semi-transparent but I don't want to add about image into the app (as in create another one and add it in)
is there a way to do it??
You have to set the alpha value of the image. ( If i understood you correctly)
If you load this Image in to a ImageView, you can set it from xml like this:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_image_in_res"
android:alpha="1"/>
where according to the developers site "alpha property of the view, as a value between 0 (completely transparent) and 1 (completely opaque). " Play with the value to get the desired effect.
If you have your image int the res/drawable folder, you can use it anywhere in your code. In xml almost all elements have the following parameter:
android:background="#drawable/im_your_image"
. You just have to set this in all places, where you want this to be your background, for the parent views background. (e.g. your MainActivitys RelativeLayout that you inflate in the onCreate funciton. You see the image is put only once in the package, but this way you can set it several times as a background.
I want to create metro UI, so I did like in the picture But the problem is the position of the icons, they are in the top of the button, and I don't think it's aesthetic interface. I inserted the icons in the drawable Top in the button. Well how can make the a little bit in the middle.
You have to set the android:paddingTop and android:paddingBottom eqaully. Just don't copy and paste 10dp as the value of the padding. Value depends on the size of your button. Refer to below:
<Button
android:id="#+id/find_teacher"
style="#style/page_button"
android:drawableTop="#drawable/ic_search_light"
android:text="#string/find_teacher"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
This is the output:
I can remember 2 options:
top padding
gravity
You might wan't to check the layout documentation
For EditText that going be filled with RTL text, is there a way to change the gravity of the error drawable (and popup of course) ?
here is an example of regular error drawable
so since the text entered is RTL i would like the pop up to show up at the LEFT side of the EditText
i tried to apply custom drawable , but Drawable doesnt seem to have any setGravity method.
thanks in advance.
It's not possible by normal means, however you can extend the Android TextView class and change the layout that Android uses to inflate the popup. I haven't tried this myself but it could work.
I did a quick look in the source of TextView and found this line
final TextView err = (TextView) inflater.inflate(com.android.internal.R.layout.textview_hint,
null);
Which references to this layout.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/popup_inline_error"
android:textAppearance="?android:attr/textAppearanceSmallInverse"
/>
Adding gravity here could do the trick. A similar approach for the drawable could apply.
It's over a year, but if it helps anyone else, you can use:
myEdit.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_error, 0, 0, 0);
but it will stay until you remove it
myEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
How can i drop an image under a piece of text in a TextView.
For example i want to drop image of bread under the text of bread in my TextView. I really want to show a drawable instead of background color for a part of text in a TextView. Is it possible with ImageSpan?
you can just simply add android:drawablebottom="#drawable/yourimage" to the TextView in xml
or otherwise the equivalent in Java is
public void setCompoundDrawablesWithIntrinsicBounds (Drawable left, Drawable top, Drawable right, Drawable bottom)
usage:
myText.setCompoundDrawablesWithIntrinsicBounds(null,null,null, myNewDrawable);
Just add android:drawablebottom="#drawable/bread" in your textview
If you want it like a watermark, use ImageButton as follows, make sure you use a small icon-like image
<ImageButton
android:layout_width="140dp"
android:layout_height="wrap_content"
android:src="#drawable/bread" />
Write below code and try
In your XML File
<Button
android:layout_width="140dp"
android:layout_height="wrap_content"
android:src="Bread" android:id="#+id/mBtn1" />
In your Java File.
Button mBtn1 = (Button) findViewById(R.id.mBtn1);
Drawable d = getResources().getDrawable(R.drawable.bread);
mBtn1.setCompoundDrawablesWithIntrinsicBounds(null, null, null, d);
it will solve your problem.
I've been working on my android calculator and i can't seem to insert an image instead of text on a button. For example sqrt I don't want to have sqrt written on my button i want the image of the symbol on it, the same goes for x^y and alot of others. I have my custom background of the button working just fine. Thank you for your help in advance :)
EDIT:
Thats not what i wanted i did manage to get my custom button and thats fine. On my button since its a calculator button i have android:text on it saying sqrt. I want to remove that and insert another png on my button showing the symbol of the square root instead of that text. Thank you for quick responses.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/imageName"/>
You can have both text and image (or no text) when using the attribute drawableTop, drawableBottom, drawableLeft and drawableRight.
And for positioning the image how you want, consider using: paddingTop, paddingBottom, paddingLeft and paddingRight.
You can do this by ImageButton:
<ImageButton
android:id="#+id/submitEmailButton"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_gravity="right" android:layout_marginRight="15dip"
android:background="#drawable/ic_submit" />
Here ic_submit is the image you want to show as a button.
I used this tutorial and it worked perfectly for me: http://www.mkyong.com/android/android-imagebutton-selector-example/
I haven't seen your code but basically you should remove the "text" tag from your xml and it should appear the image without text then.
I don't know if that's exactly what you were looking for or not, but I reccomend you to follow the tutorial and for sure it will work for you ;)
There are a number of alternatives you can try to implement to build your SQRT button:
draw the button as a 9-patch drawable so your SQRT symbol won't get distorted;
use a RelativeLayout with a button inside and implement a selector
You can simply set background for the Button and keep its text blank.