Add text to image in android programmatically
What you can instead do is to put a TextView in overlay to a ImageView using a RelativeLayout :)
Adding a text and image on a button programmatically in android
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 !
How do I write text over a picture in Android and save it?
You have to implement a canvas that allows the user to draw on it and then set the background of that canvas to that particular image. This is just a guess but its somewhere there abouts.
How to write Text on ImageView in android coding?
I believe the easiest workaround without overriding anything would be to have a TextView
and set its background with a drawable
resource.
For instance:
TextView t = (TextView)findViewById(R.id.my_text_view);
// setting gravity to "center"
t.setGravity(Gravity.CENTER);
t.setBackgroundResource(R.drawable.my_drawable);
t.setText("FOO");
Add custom text to image and save android
Quality of bitmap(image) is maintained specially with this:
public boolean compress (Bitmap.CompressFormat format, int quality, OutputStream stream)
here :
quality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting
how to put text on image bitmap at bottom?
Check the below code:-
private void addStampToImage(Bitmap originalBitmap) {
int extraHeight = (int) (originalBitmap.getHeight() * 0.15);
Bitmap newBitmap = Bitmap.createBitmap(originalBitmap.getWidth(),
originalBitmap.getHeight() + extraHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawColor(Color.BLUE);
canvas.drawBitmap(originalBitmap, 0, 0, null);
Resources resources = getResources();
float scale = resources.getDisplayMetrics().density;
String text = "Maulik";
Paint pText = new Paint();
pText.setColor(Color.WHITE);
setTextSizeForWidth(pText,(int) (originalBitmap.getHeight() * 0.10),text);
Rect bounds = new Rect();
pText.getTextBounds(text, 0, text.length(), bounds);
int x= ((newBitmap.getWidth()-(int)pText.measureText(text))/2);
int h=(extraHeight+bounds.height())/2;
int y=(originalBitmap.getHeight()+h);
canvas.drawText(text, x, y, pText);
imageView.setImageBitmap(newBitmap);
}
private void setTextSizeForWidth(Paint paint, float desiredHeight,
String text) {
// Pick a reasonably large value for the test. Larger values produce
// more accurate results, but may cause problems with hardware
// acceleration. But there are workarounds for that, too; refer to
// http://stackoverflow.com/questions/6253528/font-size-too-large-to-fit-in-cache
final float testTextSize = 48f;
// Get the bounds of the text, using our testTextSize.
paint.setTextSize(testTextSize);
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
// Calculate the desired size as a proportion of our testTextSize.
float desiredTextSize = testTextSize * desiredHeight / bounds.height();
// Set the paint for that size.
paint.setTextSize(desiredTextSize);
}
Edit:-
Instead of above addStampToImage method you can also use your updated addStampToImage method like below:-
private void addStampToImage(Bitmap originalBitmap) {
int extraHeight = (int) (originalBitmap.getHeight() * 0.15);
Bitmap newBitmap = Bitmap.createBitmap(originalBitmap.getWidth(),
originalBitmap.getHeight() + extraHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawColor(Color.BLUE);
canvas.drawBitmap(originalBitmap, 0, 0, null);
Resources resources = getResources();
float scale = resources.getDisplayMetrics().density;
String text = "Maulik";
Paint pText = new Paint();
pText.setColor(Color.WHITE);
setTextSizeForWidth(pText,(int) (originalBitmap.getHeight() * 0.10),text);
Rect bounds = new Rect();
pText.getTextBounds(text, 0, text.length(), bounds);
Rect textHeightWidth = new Rect();
pText.getTextBounds(text, 0, text.length(), textHeightWidth);
canvas.drawText(text, (canvas.getWidth() / 2) - (textHeightWidth.width() / 2),
originalBitmap.getHeight() + (extraHeight / 2) + (textHeightWidth.height() / 2),
pText);
imageView.setImageBitmap(newBitmap);
}
How to dynamically add text over the image?
You can use this:
<RelativeLayout>
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myImageSouce" />
<TextView
android:id="@+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/myImageView"
android:layout_alignTop="@+id/myImageView"
android:layout_alignRight="@+id/myImageView"
android:layout_alignBottom="@+id/myImageView"
android:layout_margin="1dp"
android:gravity="center"
android:text="Default text"
android:textColor="#000000" />
And dynamically change Text in this image using:
TextView myAwesomeTextView = (TextView)findViewById(R.id.myImageViewText);
myAwesomeTextView.setText("New Text inside Image");
Related Topics
Clear Cache in Android Application Programmatically
How to Get the Height of Recyclerview Item in "Onbindviewholder"
How to Change Listview Height Dynamically in Android
Changing Background Color of the Layout on a Button Click in Android
How to Get Device Id in Flutter of Both Android and Ios
Textview With Too Long Strings
React-Native: Module Appregistry Is Not a Registered Callable Module
How to Center Items of a Recyclerview
Super.Onbackpressed() and Finish() Exits the App
How to Create a Video from an Array of Images in Android
How to Get Data from Edit Text in a Recyclerview
Charging Animation for Android
How to Open Fragment from Recyclerview Adapter
How to Reduce an Image File Size Before Uploading to a Server
How to Know Location Area Code and Cell Id in Android Phone
Open Facebook Page in Facebook App (If Installed) on Android