Android Custom View: MATCH_PARENT with OnDaw don't work - java

I'm new to Android but i have a simple problem.
I have a vertical linear layout, if i place here a normal button, and give it the MATCH_PARENT flag for the width and the height, the button will fill all the space. All ok.
Now i have a my simple custom view (a class that extends View) and i overrided the OnDraw method for draw a cross in the middle of the view. For draw the cross i need to get the width and the height of the control, if in the UI Layout editor of eclipse i set a fixed width and height, it works correcly (i see a preview of the cross directly in Eclipse) but if i extend the View to the borders i got only WARP_CONTENT (With button i get MATCH_PARENT), no problem, i change it in the XML, but LayoutParams width and height will return 0!! (I see the error directly in the preview of eclipse!) Why?
I tried to set manualy the width and the height in the OnWindowFocusChanged method to wait but it give me always 0.
The problem is that the view actualy have the rights dimensions but when i call the getLayoutParams().width it give me 0, so i see a white rectangle in the right position (white background of the view), but the cross is drawed on the upper-left corner!
Sorry for my bad english.
Thank you.
Mauro.

i think u need to set this parameter for your view.
android:layout_alignTop
or something related to this.i guess

Related

Why are the layouts I designed for different screen widths not working in Android?

I am thinking of making designs according to different screen widths for the layout. For example, if the width of the phone is close to 320dp, the layout-w320dp' that I created should work, if the screen width is 411dp, then layout-w411dp should work.
but it doesn't. Although the width of the screen is 392dp, the 320dp view works instead of the default layout.
I don't want to create view for each width
I am thinking of making designs according to different screen widths for the layout. For example, if the width of the phone is close to 320dp, the layout-w320dp' that I created should work, if the screen width is 411dp, then layout-w411dp should work.
but it doesn't. Although the width of the screen is 392dp, the 320dp view works instead of the default layout.
I don't want to create view for each width
When I run it (width of my phone is 392dp) as in the picture below, the view that is 320dp works.
I can make the default one work?

Position of image view java fx

I am currently trying to get an image view containing an image to move from top of the screen downwards. When it reaches the buttom of the screen i'd like to switch direction going upwards again.
My problem is i can't find the exact spot, where i should make the image view change direction. My screen has a very high resolution and therefore i've set my os, windows, to enlarge all components. I can therefore not use my screen size to calculate how many pixels there are from top to bottom.
Therefore i use following code to get height of screen:
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
bounds.getHeight();
My Imageview is initialized at point (0,0) and i can therefore at any given time get the y-koordinate of my top left corner by using imageview.getY()
Therefore i should move it downwards until the value of imageView.getY() + height the image view equals the height of the screen.
But this solution seems to make my image view switch direction a bit before it reaches the bottom of the screen.
For calculating height of image i use the method imageView.getFitHeight();
I suspect imageView.getFitHeight for delivering the height desired by the imageview before it is actually determined by the underlying anchor pane and therefor i am not sure imageView.getFitHeight() actually delivers the height of the imageView. I can't seem to find any other method in imageView which regards height of the imageView.
I don't know how to make the imageview switch direction exactly at the bottom, can anybody help?
Regards Martin

SlidingMenu Width Not Correct

I'm using jfeinstein1's slidingmenu library in my project. I would like to have the sliding menu slide out to about 3/4 of the width of the screen. According to the docs, this should be able to be accomplished using
setBehindWidth()
setBehindOffset()
But at the moment, the menu slides all the way to the right and covers the entire screen. Below is my code:
public void setupMenu() {
setContentView(R.layout.activity_base);
menu = new SlidingMenu(this);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setBehindWidth(100);
menu.setBehindOffset(100);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.sliding_menu);
}
Thanks in advance
I have a menu sliding from the right and simply call:
getSlidingMenu().setMode(SlidingMenu.RIGHT);
getSlidingMenu().setBehindOffset(100);
Hope that helps
The error is, that you choose both possibilities, so finally the last one wins:
menu.setBehindWidth(100);
menu.setBehindOffset(100);
.... and that is setBehindOffset() what causes your menu being moved until only 100 pixels are left. Using setBehindWidth() you would only see 100 pixels of your menu.
So you need only this one:
menu.setBehindWidth(100);
Cheers!
p.s from the documentation
behindOffset - a dimension representing the number of pixels that you want the above view to show when the behind view is showing. Default is 0.
behindWidth - a dimension representing the width of the behind view. Default is the width of the screen (equivalent to behindOffset = 0).
You don't want to use both width and offset. Width is the width of the menu, offset is the width of the non-menu portion of the screen. Only one is meant to be used.

Android - Get the visible area of a view?

Suppose I have an Android View that is being partially masked by another View. For instance, imagine that the masking view has a transparent rectangular "window" in the center of it, and my view is centered beneath the window and possessing dimensions that are larger than the dimensions of the window. In such a case, some rectangular portion of the view will be visible, with the rest being obscured by the mask.
So my question is, is there any straightforward way to retrive the position and dimensions of the visible area without having any access to the masking view itself (so without knowing how big the mask's "window" itself is)?
I tried calling getLocalVisibleRect(), which sounded promising, but that only seems to return the view's layout rectangle and not the rectangle that is actually visible.
This is pretty late to the game, but I suspect that getGlobalVisibleRect does what you want.
I have used getWindowVisibleDisplayFrame method to determine if view is partially visible or not to determine if a soft keyboard is open. You can try it out.
Rect r = new Rect();
// r will be populated with the coordinates of your view
// that area still visible.
rootView.getWindowVisibleDisplayFrame(r);
Does this not work: http://developer.android.com/reference/android/view/View.html#getDrawingRect(android.graphics.Rect)
The documentation says that it does what you want.

Positioning images to fit all size screens

I'm having a problem with positioning images,I need to position a lot images over another larger background image.
An image Like this
I have tried Absolute but it does not keep the position of the image say I wanted to put a clip art image of a board pin over the background image and need it pointing at a sun and when it's clicked I get a popup dialog,
but then when I change the size of the emulator screen the clip art image is not at the same position I wanted it on the background image.
I first tried just putting the clip art on it with a image editor and used onTouch Listener but that didn't work out when I changed the size of the screen with the x and y coordinates. And tried Absolute Layout and that doesn seem to keep the position.
any ideas would help me big time thanks
AbsoluteLayout is deprecated, so it's probably best to use relative layout alongside with dp.
You could use relative layout so you can use layout_below="#id/view1", android:layout_toRightOf="#id/view2", and android:layout_toLeftOf="#id/view3". You can also use android:layout_marginLeft="10dip", android:layout_marginRight="10dip", android:layout_marginTop="10dip", and android:layout_marginBottom="10dip" to move the views left/right and up/down relative to their current positions. There is also ALIGN_PARENT_LEFT, ALIGN_PARENT_RIGHT, ALIGN_PARENT_TOP, and ALIGN_PARENT_BOTTOM. Click here for more properties.
You can manually calculate visible width and height of the image by using its drawable's getIntrinsicHeight() and getIntrinsicWidth() and then set Image's scaleType to FIT_XY (this saves image's ratio, and also makes its size correct, while FIT_CENTER does not).

Categories