ImageView aligned to the right side of parent in Xamarin Android - android

When I run this code, the imageView is aligned to the right. I've tried adding gravity and such, but no succes.
I want the icon image (imageView) to be displayed on the left side, just before "Sound" text.
Any idea? Thank you very much.
protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context)
{
var cell = (SwitchCellView)base.GetCellCore(item, convertView, parent, context);
cell.SetPadding(50, 0, 0, 0);
var imageView = new ImageView(context);
imageView.SetImageResource(Resource.Drawable.icon);
cell.AddView(imageView, 50, 50);
return cell;
}
Result

Solved by writing
cell.AddView(imageView, 0);
... which will add the imageView to index 0 (to the left).

Related

Cannot center text when programmatically creating view from adaptor

I am using an adaptor to generate a view for each of several items. The view needs to show an image with centered text below it. I create a linear layout and add an image view and text view.
To show the problem I have changed the background colors to green for the linear layout and red for the text view...
Obviously the text is showing left aligned instead of centered. Here is the simple code in the adaptor GetView method...
public override View GetView(int position, View convertView, ViewGroup parent)
{
ImageView imageView = new ImageView(_context);
LinearLayout.LayoutParams imageViewParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent);
imageView.LayoutParameters = imageViewParam;
imageView.Id = 1000 + position;
imageView.SetImageResource(Resource.Drawable.Code);
TextView textView = new TextView(_context);
LinearLayout.LayoutParams textViewParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent);
textView.LayoutParameters = textViewParam;
textView.Text = OPTIONS_TEXT[position];
textView.SetBackgroundColor(Color.Red);
textView.SetForegroundGravity(GravityFlags.CenterHorizontal);
LinearLayout linear = new LinearLayout(_context);
linear.LayoutParameters = new GridView.LayoutParams(200, 172);
linear.SetBackgroundColor(Color.Green);
linear.Orientation = Orientation.Vertical;
linear.AddView(imageView);
linear.AddView(textView);
return linear;
}
As far as I can work out, you cannot set the layout_gravity setting when generating the view programmatically and so I tried setting the foreground gravity but has no effect at all.
I have tried everything I can think of but nothing gets that text centered!
Set gravity center to LinearLayout.LayoutParams of TextView i.e.
textViewParam.gravity = Gravity.CENTER;

How to add label below GridView item Android?

Right now I'm working on a launcher app, in which I've made a GridView which fetches all installed apps from the device. But there are only app icons and no app names. And I wanted to put app name below it so I tried to put TextView below my app icon ImageView, but my app crashes. Any ideas how should I fix it? Here's my code: Thank you very much!
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i;
if (convertView == null) {
i = new ImageView(Apps.this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new GridView.LayoutParams(93, 93));
i.setPadding(15, 15, 15, 15);
} else {
i = (ImageView) convertView;
}
ResolveInfo info = mApps.get(position);
i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
return i;
}
Instead of building a view that contain TextView and ImageView, use CompoundDrawable. You only have to implement one TextView and set it's DrawableTop with the required icon. This will do the job.
From the XML file
<TextView
android:id="#+id/my_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableTop="#drawable/my_icon"
android:gravity="center"
/>
Or you can do it programmatically using the following:
myTextView.setCompoundDrawablesWithIntrinsicBounds(null, topDrawable, null, null);
Simply use a TextView as the image container, by using a compound drawable: http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds(int,%20int,%20int,%20int)
So, you'll set 1 View to replace 2 ones, with UI simplification and performance improvements, as side-effects.
A little example:
public View getView(int position, View convertView, ViewGroup parent) {
TextView t;
if (convertView == null) {
t = new TextView(Apps.this);
t.setLayoutParams(new GridView.LayoutParams(93, 93));
t.setPadding(15, 15, 15, 15);
ResolveInfo info = mApps.get(position);
Drawable drw = info.activityInfo.loadIcon(getPackageManager());
t.setCompoundDrawablesWithIntrinsicBounds(null, drw, null, null);
//t.setText("Some Text");
t.setText(info.activityInfo.loadLabel(getPackageManager()).toString());
} else {
t = (TextView) convertView;
}
return t;
}
[EDIT]
Once you return the View (t, the TextView), you can get the drawable by using getCompoundDrawables(): http://developer.android.com/reference/android/widget/TextView.html#getCompoundDrawables()

how programmatically add imageview under other imageview

Hi I have custom adapter which add ti gridview image
adapter code
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams((tools.getWidth(mContext) - 30) / 3, (tools.getWidth(mContext) - 30) / 3));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(static_file[position]);
imageView.setTag(p[position]);
return imageView;
}
In minimap I have image
Help me please how I can programmatically add 2-rd image in 1-st
There is no way simply because your "transparent background" isn't transparent at all. It's just a part of the image, you should remove it as if it was white color, i've made it for, download this image and use it
Use a custom layout,
<RelativeLayout>
<ImageView> containing the image you want
<ImageView> the 2nd image having centerInParent=true
</RelativeLayout>
Refer to this
Custom Adapter for List View

ImageView Scale Type incorrect inside of a GridView

I have a GridView with 4 columns and n-rows. Each cell is simply an ImageView. My custom adapter's getView() code looks like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
} else {
imageView = (ImageView) convertView;
}
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(1, 1, 1, 1);
//imageView.setAdjustViewBounds(true);
if(imageLoader != null) {
String url = randomImage();
imageView.setContentDescription(url);
imageLoader.DisplayImage(url, imageView);
}
return imageView;
}
The imageLoader will pick a random URL and in the background, go fetch it and update the ImageView accordingly. That works fine and dandy. The problem is that the resulting images seem to be using a scale type of FIT_START instead of FIT_XY. I'm explicitly setting this to FIT_XY and even set it again inside of the code that sets the imageView's drawable... still its not FIT_XY and i'm stumped. Ideas?
Edited to remove the call to setAdjustViewBounds().
So I just found the issue. I was using Romain Guy's code for creating a RoundedDrawable from a bitmap -- to get rounded corners, ala iOS, as shown here: curious-creature.org/2012/12/11/… ... when i skip the RoundedDrawable conversion, it works fine. Something in the RoundedDrawable code is what is throwing this whole thing off. I'll just need to find another method to round the corners of the imageview. I am using CENTER_CROP now thanks to #kcoppock ! :)

How to set height of Gallery view cell equals Gallery height?

I have Gallery control with ImageView as cell View. I need to have ImageView size equals the Gallery height. How can I do that? I tried to set height like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
FrameLayout retval = (FrameLayout) convertView;
ImageView imageView = (ImageView) retval.findViewById(R.id.image);
imageView.setLayoutParams(new FrameLayout.LayoutParams(parent.getHeight(), parent.getHeight()));
But first cell has height equals zero but next cells has right value.
Why not just set the imageView to fill the parent container through the params?
#Override
public View getView(int position, View convertView, ViewGroup parent) {
FrameLayout retval = (FrameLayout) convertView;
ImageView imageView = (ImageView) retval.findViewById(R.id.image);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
imageView.setLayoutParams(layoutParams);
That is, unless you want the width of the imageview to be equal to the height of the gallery. Then this would not work
I think you should set layoutParams on cell itself and let image view to scaleXY to the cell.
retval.setLayoutParams(new FrameLayout.LayoutParams(parent.getHeight(), parent.getHeight());
imageView.setScaleType(ScaleType.FIT_XY);

Categories

Resources