How to dynamically add text over the image? - java

I have to put the text over the image, I want to get the following final result:
I list all images from my database and add them dynamically to my Flexbox.
But I need to do this by code, dynamically.

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");

You can use Canvas to draw the Image and draw the text at the bottom o canvas. Check Developer Google

Related

How do you change hint text color in Android Studio?

I cannot get the hint text color to change in my Android application on the log in page.
<AutoCompleteTextView
android:id="#+id/email"
android:textColor="#color/white"
android:backgroundTint="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_email"
android:textColorHint="#android:color/white"
android:inputType="textEmailAddress"
android:maxLines="1" />
I use the textColorHint method as found in many other examples online but I do not know what is wrong with what I am doing.
Use attribute android:textColorHint="YOUR_COLOR" to set hints text color.
In your AutoCompleteTextView, you are using WHITE color for textColor, backgroundTint and textColorHint. Make sure your AutoCompleteTextView background color is different from WHITE.
Here is an working example for AutoCompleteTextView. I have used RED color for textColor, GREEN color backgroundTint and BLUE color for textColorHint.
<AutoCompleteTextView
android:id="#+id/email"
android:textColor="#FF0000"
android:backgroundTint="#00FF00"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Write something..."
android:textColorHint="#0000FF"
android:inputType="textEmailAddress"
android:maxLines="1" />
OUTPUT
Hope this will help you~
Just add the text in the layout's EditText android:textColorHint = "#android:color/ then press ctrl+space and choice your color.

How can I specify that this ImageView have to be shown smaller?

I am absolutly new in Android development and I am findig the following problem working on my first app.
My problem is related to changing the space occupied by an image into my layout.
So I have a situation like this: I don't put the image directly into my XML layout file but I do it programatically into my activity code calling this method:
ImageView difficultyContainerImageView1 = (ImageView) rootView.findViewById(R.id.difficultyContainer);
difficultyContainerImageView1.setImageDrawable(new BitmapDrawable(getResources(), ImgUtility.createRankingImg(context, 3)));
So the ImgUtility.createRankingImg(context, 3) return a Bitmap object that is setted into the ImgeView of my layout having difficultyContainer as ID.
It works fine and the image is showed into my layout, this one:
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:scaleType="fitXY" />
<TextView
android:id="#android:id/text1"
style="#style/pastaTitleTextStyle" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
style="#style/HeaderTextStyle"
android:text="Difficoltà:" />
<ImageView
android:id="#+id/difficultyContainer"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:scaleType="fitXY" />
</LinearLayout>
</LinearLayout>
The image is correctly showed into this ImageView:
<ImageView
android:id="#+id/difficultyContainer"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:scaleType="fitXY" />
The only problem is that doing in this way the image horizontally occupy all the space of the view, this is what I obtained (the image is the last one that contains the chef hats):
It should depends by the fact that I am using android:layout_width="fill_parent" to say that the ImageView having id=difficultyContainer have to horizontally occupy all the spac of its container (the main LinearLayout).
So I want know how can I set a percentual width\height for this ImageView.
What is the best way to specify a percentage so my image will be shown smaller in my app? What is the best solution to do it?
Are you trying to ask how to set the size of the imageView programmatically?
If so use:
ImageView imageView = findViewById(R.id.difficultyContainer);
imageView.getLayoutParams().width = 120;
The same for height, just replace width with height.
This value of '120' being in dp, which means the size of the image will adjust depending on the screen size of the output device, meaning you don't need to utilise percentages like you would in html/css
Just add padding to your ImageView
<ImageView
android:id="#+id/difficultyContainer"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:padding="8dp"
android:scaleType="fitXY" />

Android adding image to button

I am trying to add an image to a button(image button or normal button) but not in a specific res/drawable folder.
I want this image as a ImageView object or drawable object etc, but I couldn't.
You can set an icon for Button by adding following attribute in XML for Button like this:
android:drawableLeft="#drawable/button_icon"
If you want to know how to do it programmatically, follow this article:
How to programmatically set drawableLeft on Android button?
Or just add 'background':
<Button
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/qrcode"/>
But i think it's better if you use ImageButton, see this example:
<ImageButton
android:id="#+id/searchImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/your_image_drawable" />
Use ImageButton and add src in your background:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/yourImageNameHere"

Set bitmap to an image src

im trying to set a bitmap to image something like:
android:src="img"
but only from code , and i get the img from the server.
but when im doing the setImageBitmap() method
it displays it like its in the background,like i would do this:
android:background="img"
so whatever isnt the image in the imageview becomes black and i dont want black background, this is the bad image i get:
Bad Image
and the image i want to get is:
Good Image
how can i set the image from bitmap in my code to be like src in xml ?
ty alot!
here is my xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:id="#+id/profile_image"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
app:civ_border_width="5dp"
app:civ_border_color="#c5eaf8"
android:elevation="10dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:layout_centerInParent="true"/>
<ProgressBar
android:id="#+id/progress_bar_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:visibility="gone"/>
</RelativeLayout>
my bitmap is working good with all the images except images like this one its filling their background with black..
The problem is that you're setting a transparent image and the background is filled black.
Check this answer to a similar question: https://stackoverflow.com/a/20402227/1281775
Another way is to create another ImageView below the current one with a desired background color.
The black portion you view in your image is transparent. Either change your image to non-transparent background or change the background color.
imageView.setBackgroundColor(color);
imageView.setImageBitmap(bitmap);

How to Show Image View in android?

I am sorry I am very beginner in java/android, maybe this question is too bad.
I want to display my icon in splash screen, but if I use ic_launcher it's size is too small. (192x192)
When I generate ic_launcher from new image asset it generates an image with size 512x512 in "app\src\main\ic_launcher-web.png"
Can I display that image?
I think I can change #mipmap/ic_launcher to some path/image location. So, I don't need to copy my image to drawable/mipmap directory.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#mipmap/ic_launcher" />
I have been looking for it and did not find it.
Thanks for help
Save your original image in res/drawable/youimage.png. Then call it from
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/yourimage.png" />

Categories

Resources