Is it possible to show multiple drawables (with margins in between them) inside an ImageView without using a frame layout + any unnecessary nesting? How can following be achieved?
Each drawable to appear as normal without any resizing
Only drawableA and drawableB to have 10dp margin on the end/right
XML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/myImgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/myTxtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Java
Resources r = getContext().getResources();
int tenDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, r.getDisplayMetrics());
ImageView customIV = content.findViewById(R.id.myImgView);
Drawable drawableA = getResources().getDrawable(R.drawable.ic_happyface);
Drawable drawableB = getResources().getDrawable(R.drawable.ic_neutralface);
Drawable drawableC = getResources().getDrawable(R.drawable.ic_sadface);
customIV.addView(?);
An ImageView only houses a single Drawable. Multiple ImageViews are needed to display multiple Drawables.
You could replace the root LinearLayout with a RelativeLayout to avoid nesting.
Related
I'm trying to take a thumbnail image of a video the user can view in the app and display it in an ImageView. However, when I try to display the thumbnail, it looks like the whole ImageView disappears and no image appears at all.
I tried using a test photo that wasn't a thumbnail, and it definitely displayed in the ImageView, so I don't think it's a problem with the XML, but it could be. I've googled for hours and don't understand where I'm messing up. My code comes from Stack Overflow already.
uri = Uri.parse("android.resource://"+ getActivity().getPackageName() +"/"+R.raw.test);
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(uri.getPath(), Thumbnails.MICRO_KIND);
//Create ImageView to hold the thumbnail
ImageView imageView = view.findViewById(R.id.mediaPreview);
imageView.setImageBitmap(thumb);
XML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/startup_dialog"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="25"
tools:context=".ui.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/getting_started"
android:gravity="start"
android:layout_gravity="start"/>
<ImageView
android:id="#+id/mediaPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="10dp"
/>
The image would be displaying but it is not showing because you may have a image of greater pixel try changing your image pixel or try to add another image instead.
How to set image1 overlap image2
I want to set image1(by Drawable) overlap image2(by bitmap)
example
In the example above
icon play is image1 and picture is image2
I found slove is only image1 and image2 is by drawable
and If image2 is by bitmap ,I have no idea to set it
ps. sorry my english is not good.
file.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageView
android:id="#+id/image_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bacground_pic"
/>
<ImageView
android:id="#+id/image_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="trues"
/>
</RelativeLayout>
secondly you have to set Bitmap to second image with this
ImageView image_1=(ImageView) findviewbyid(R.id.image_1);
image_1.setImageBitmap(bitmap);
On ImageView object you can set a background image and as well as the ImageResource. Another way of doing this is RelativeLayout, place two ImageView overlapping each other.
ImageView having extra properties to set extra bitmap or icon or drawable other than normal Background in View properties,
So for your case, you can set Play icon as src(which will come on top of background and center) and Bitmap you can set as Background
<ImageView
android:id="#+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/msg_media_play_small_icon" >
</ImageView>
and to set the Background Image, don't use imageView.setImageBitmap
use imageView.setBackground
imageview.setBackground(new BitmapDrawable(context.getResources(), bitmap));
I am facing a seemingly simple issue but after many hours on the case now I cannot figure it out. I am trying to create a ListView with rows that contains an ImageView which re-sizes based on the ImageView's image. This works through subclassing the ImageView and giving it a height ratio to follow since I know the image size before loading it:
Custom subclass of ImageView onMeasure method:
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mHeightRatio > 0.0) {
// set the image views size
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) (width * mHeightRatio);
setMeasuredDimension(width, height);
}
else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
In the adapter I set the ratio of the ImageView:
holder.couponImage.setHeightRatio(359.0/1080.0);
The Problem is that I also want an overlay on the ImageView in form of a RelativeLayout with a semi-transparent background and containing a TextView. The issue is that when the ImageView Rezises the overlaying RelativeLayout doesn't, the white in the image below is the RelativeLayout:
The row XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<se.bdms.babydirect.views.DynamicHeightImageView
android:id="#+id/couponImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="#drawable/coupon_01"/>
<RelativeLayout
android:id="#+id/relRedeemedOverlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/almostWhiteTransparent"
android:gravity="center"
>
<se.bdms.babydirect.views.TextViewMega
android:id="#+id/lblRedeemed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextField"
android:textColor="#color/headlineGray"
android:layout_centerInParent="true"
android:textSize="24sp"/>
</RelativeLayout>
</RelativeLayout>
I have tried multiple solutions to fix this among them: this and this.
I have succeeded in making the RelativeLayout the same size as the ImageView by subclassing that in the same way the ImageView is subclassed but then then TextView is still not centered vertically and stays at the top like nothing happened.
I would like to let the RelativeLayout know that the row height has changed and fill the row and then the TextView centers itself based on the new height.
Does anyone have a clue what to do about this?
Any help is greatly appreciated!
I think the problem is just that you need to tell the overlay that it is the same height as the image. You'll be able to do that because the parent is already a RelativeLayout. I'm showing aligning the child RelativeLayout to the ImageView, but you could also use alignParentTop="true" and alignParentBottom="true". Also, I think best is if the parent view has height wrap_content, and both children are match_parent.
Once your overlay is the full height of the item, your centerInParent should work to center it within the overlay (which it already is).
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<se.bdms.babydirect.views.DynamicHeightImageView
android:id="#+id/couponImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="#drawable/coupon_01"/>
<RelativeLayout
android:id="#+id/relRedeemedOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#id/couponImage"
android:layout_alignBottom="#id/couponImage"
android:background="#color/almostWhiteTransparent"
android:gravity="center"
>
<se.bdms.babydirect.views.TextViewMega
android:id="#+id/lblRedeemed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextField"
android:textColor="#color/headlineGray"
android:layout_centerInParent="true"
android:textSize="24sp"/>
</RelativeLayout>
</RelativeLayout>
Let me know whether that works.
I'm trying to set an imageview's resource like so:
Code:
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.bg);
Yet I see nothing on my screen.
I can set
imageView = (ImageView) findViewById(R.id.bgView);
And it works, but in my instance I can't use this because I need apply the imageview to an arraylist of imageviews.
Is there any other way to construct my imageview or am I doing something wrong?
Edit:
My xml looks like:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/entire_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
tools:context="com.example.guilyan.Game" >
<ImageView
android:id="#+id/bgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="#string/Descript"
android:scaleType="fitXY"
android:src="#+drawable/testpicks" />
</FrameLayout>
I think u should add the dynamically created image view to your layout.just try this.Suppose if your layout id is frameLayout then
frameLayout.addView(imageView);
Using of
new ImageView(getApplicationContext()); not correct.
You need use activity context, not application.
And of course after initialization of view you need attach it to parent.
I have an app which counts up from 1 to 100, showing the count-up on the screen.
I have a custom font. The problem is that the numbers keep jumping left and right as it counts up. I draw the numbers by drawing on a canvas, using graphics.Paint.
How can I set the font to be fixed and not jumping around?
You will either have to use a fixed width font or put the individual digits in a custom layout with fixed widths.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/digit1"
android:layout_width="0dp"
android:layout_weight="0.5"
android:gravity="center_horizontal|center_vertical" />
<TextView
android:id="#+id/digit2"
android:layout_width="0dp"
android:layout_weight="0.5"
android:gravity="center_horizontal|center_vertical" />
</LinearLayout>
Set the typeface using your custom font like this:
private void setLogoTypeface()
{
AssetManager assetManager = getContext().getAssets();
Typeface tf = Typeface.createFromAsset(assetManager,"GILB.TTF");
logo = (TextView)findViewById(R.id.logo);
logo.setTypeface(tf);
}
Substitute your font name and the textview should render the font.