Issue
I am trying to have an text and image on a button appear dynamically - the text needs to appear on the left and the image to appear on the right. To give an example, here is an image I would like to get.
After searching through a few other examples on StackOverflow, tried the following code in my activity but it doesnt seem to be working. Can anyone please suggest what I am doing wrong ?
Code :
btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
Drawable icon= getApplicationContext().getResources().getDrawable( R.drawable.cal);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null );
Solution
I noticed that the size of the image is very important when using it on other objects - when I adjusted the image to a smaller size, my code worked fine....
The same code that I was trying earlier worked for me after resizing the image...
Button btn = new Button(this);
btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
btn.setGravity(Gravity.CENTER | Gravity.LEFT);
Drawable icon= getApplicationContext().getResources().getDrawable(R.drawable.cal);
btn.setBackgroundResource(R.drawable.light_bg);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null);
Thank you all for your help and suggestions !
Answered By - Prashanth
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.