I'm trying to resize a UIButton referenced from xib file.
This is what I've tried without success:
RectangleF frame = actionBtn.Frame;
frame.Width = 140;
actionBtn.Frame = frame;
What am I doing wrong?
You can't modify an existing RectangleF. Try this instead
RectangleF frame = actionBtn.Frame;
actionBtn.Frame = new RectangleF(frame.X, frame.Y, 140, frame.Height);
Related
My ImageView is matching screen size on x-axis and is using remaining space on y-axis in my layout. I want to create bitmap into this ImageView with exactly the same size as the ImageView is. How to make it please? Can it be done by some automatic setting, should I call some measure function?
I tried SetAdjustViewBounds() but it didn't work for me.
Creating Bitmap big enough (I don't like much such a memory wasting) and setting SetScaleType(ImageView.ScaleType.Matrix) works, but still when I'm making drawing operations on canvas, I don't know real size of area I should paint into, both canvas and bitmap height are equal to yScreen while imgWeekView height is pretending to be 0, even though it paints whole desired area with gray color.
imgWeekView = new ImageView(context);
//imgWeekView.SetAdjustViewBounds(true);
imgWeekView.SetScaleType(ImageView.ScaleType.Matrix);
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent,1f);
layoutParams.Height = 0;
imgWeekView.LayoutParameters = layoutParams;
Bitmap bitmap = Bitmap.CreateBitmap((int)xScreen, (int)yScreen, Bitmap.Config.Argb8888);
cnvWeekView = new Canvas(bitmap);
imgWeekView.SetImageBitmap(bitmap);
linearLayout.AddView(imgWeekView); //whole activity layout
//Test
cnvWeekView.DrawColor(new Color(128, 128, 128));
Paint paint = new Paint(PaintFlags.AntiAlias);
paint.Color = new Color(255, 255,0);
cnvWeekView.DrawCircle(50, 50, 40, paint);
Finally I found a way how to measure my ImageView and here I will post my answer.
I believed that there should be much easier solution, but maybe there isn't. From this question I took most of the important data:
How to get the width and height of an android.widget.ImageView?
Things look however a little different in my android application and I'm not experienced enough to tell why. I had to change things a little. I had to learn a bit about interfaces and this question helped too.
Implementing the View.IOnTouchListener interface
Here is how I combined things. First I created class that will do the measure.
public class MyPredrawListener : Java.Lang.Object, ViewTreeObserver.IOnPreDrawListener
{
ImageView imageView;
public MyPredrawListener(ImageView img)
{
imageView = img;
}
public bool OnPreDraw()
{
imageView.ViewTreeObserver.RemoveOnPreDrawListener(this);
int finalHeight = imageView.MeasuredHeight;
int finalWidth = imageView.MeasuredWidth;
Bitmap bitmap = Bitmap.CreateBitmap(finalWidth, finalHeight, Bitmap.Config.Argb8888);
imageView.SetImageBitmap(bitmap);
//Test to see result
Canvas cnv = new Canvas(bitmap);
Paint paint = new Paint();
paint.Color = new Color(255, 255, 0);
cnv.DrawColor(new Color(128, 128, 128));
cnv.DrawCircle(finalWidth-50, finalHeight-50, 50, paint);
return true;
}
}
And in code where I create my imageView I set the listener like this.
imgWeekView = new ImageView(context);
MyPredrawListener listener=new MyPredrawListener(imgWeekView);
imgWeekView.ViewTreeObserver.AddOnPreDrawListener(listener);
In OnPreDraw function I put test code to see the result graphically, clearing bitmap to gray color and painting yellow circle to bottom right of a view.
I am trying to customize the position of text in Section element headers in MonoTouch Dialog. I read up on how you are supposed to create you own UILabel to create styled text and then assign that to the HeaderView of the Section object. That part works great.
The problem that I am faced with now is: how can I get a similar text offset as the offset used in the default unstyled Section element (see comparison in attached image). I cannot seem to find a way to get the "Styled Section" text moved away from the left edge of the screen no matter what I do. I tried changing the x coordinate specified in the RectangleF declaration, but whatever I specify appears to be disregarded when the view is rendered.
Here is the backing code for the screenshot:
Root = new RootElement ("Login2Screen");
var labelHeader = new UILabel();
labelHeader = new UILabel(new RectangleF(0, 0, 320, 48));
labelHeader.Text = "Styled
labelHeader.TextColor = UIColor.Blue;
labelHeader.BackgroundColor = UIColor.Clear;
var styledSection = new Section(labelHeader);
styledSection.Add(new EntryElement("Username", string.Empty, string.Empty));
styledSection.Add(new EntryElement("Password", string.Empty, string.Empty));
Root.Add(styledSection);
var defaultStyleSection = new Section("Default Section");
Root.Add (defaultStyleSection);
Add the labelHeader into an UIView and then set the x-coordinate of your UILabel to 10.
var viewLabelHeader = new UIView(new RectangleF(0, 0, 320, 48));
var labelHeader = new UILabel(new RectangleF(10, 0, 320, 48));
labelHeader.Text = "Styled section";
labelHeader.TextColor = UIColor.Blue;
labelHeader.BackgroundColor = UIColor.Clear;
viewLabelHeader.AddSubview (labelHeader);
var styledSection = new Section(viewLabelHeader);
Result:
Haven't read anything about the position of text in Section element, but
labelHeader = new UILabel(new RectangleF(0, 0, 320, 48));
You are positioning it at 0,0? if you make it 10,0 it should move. i.e. move x position to 10
labelHeader = new UILabel(new RectangleF(10, 0, 320, 48));
In an iPhone app, I wrote some code to resize an image taken from the photo album, in order to use it as background for the app.
The code is strongly inspired from what I could find looking on the net. Namely here:
http://forrst.com/posts/UIImage_simple_resize_and_crop_image-sUG#comment-land
It works fine on the simulator. But it looks like no resizing at all is happening on the device. Why is that? Any idea?
Thank you for any tip.
hi Michel.
i dont know more about your code but i am using this code you can try
this you will find your solution.
here you need to pass image and the required width and height for
that image in this function and you will get another image with that
new size.
-(UIImage *)resizeImage:(UIImage *)image3 width:(int)width height:(int)height {
CGImageRef imageRef = [image3 CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
//if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);
CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}
I want to doing a relative layout programmatically and set android:layout_widht=60 android:layout_height=60.When i doing programmatically it is fill all screen? How can i do that?
my code:`
RelativeLayout relativeLayout=new RelativeLayout(getContext());
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel_btn.height = 60;
rel_btn.width = 60;
relativeLayout.setLayoutParams(rel_btn);
this.setBackgroundResource(com.example.R.drawable.line);`
screen:
btn.getLayoutParams().width = width;
btn.getLayoutParams().height = height;
in fact, just use
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
60, 60);
that's OK. the problem maybe the background pic stretch the button
I created programmatically a multi view iphone application.
when adding objects to a view, like a button for example. in position x = 0, y = 0
this results to x = 0, y = 20 in the iphone simulator.
this is the used code :
UIButton *btnGoToStartView = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnGoToStartView.frame = CGRectMake(0, 0, 240, 30);
[btnGoToStartView setTitle:#"Btn Name" forState:UIControlStateNormal];
[btnGoToStartView addTarget:self action:#selector(myBtnAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnGoToStartView];
In the other hand, when I'm placing a button using Interface Builder in position x = 0, y = 0 the result is good.
Till now I didn't found the real reason for this vertical shifting.
Thanks in advance for your hints.
Regards.
It's Ok, I found the solution.
It's a problem of the UIView not the control.
in Interface builder, I opened the MainWindow.xib then I modified the attribute Layout in the inspector window.
I checked [wants full screen].
That's all.