Get Center point in my app android - android

In my app Horizontalscrollview is present. It consist of LinearLayout in it, to that layout i have added number of buttons to it.ViewFlipper is also present in it. As i flip the layout i want to move the horizontalscrollview the respective button should get to center position.
My layout(s) and number of button(s) are same. for 1st layout 1st button should be at center location?
thnx for any help....

Hmm, okay, just to point out, when you get to the first or last position (possibly first + 1, last - 1 as well, depending on the button size), you won't be able to have it in the center, as you can't overscroll with just a HorizontalScrollView. With that in mind, for your general case (you can handle the edge cases however you like -- I'd suggest just leaving it scrolled as far as you can, and giving the selected button some sort of highlight) you should be able to do something like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels;
//get an instance of your LinearLayout, HSV, and ViewFlipper
LinearLayout ll = (LinearLayout)findViewById(R.id.my_linear_layout);
ViewFlipper vf = (ViewFlipper)findViewById(R.id.my_view_flipper);
HorizontalScrollView hsv = (HorizontalScrollView)findViewById(R.id.my_hsv);
//as you've said, there should always be an equal number of Buttons as
//Views in the ViewFlipper. This code assumes that is always true.
View v = ll.getChildAt(vf.getDisplayedChild());
//assuming smoothScrollTo scrolls to the left side of the screen,
//which I think it does, we want to scroll to the Button's center
//point, minus (shifted to the right) by half the screen width
int scrollTo = v.getLeft() + (v.getWidth() - screenWidth) / 2);
//handle overflow for the edge cases
if (scrollTo < 0) scrollTo = 0;
else if (scrollTo > hsv.getWidth()) scrollTo = hsv.getWidth();
hsv.smoothScrollTo(scrollTo);
Untested, and I'm prone to small syntax errors, but the general idea may help. :)

Get the size of Display, and divide by 2, then subtract your button width divided by 2 ?
But it really sounds like you should have the buttons in its own view, then merge the views on top of each other.
<merge>
<HorizontalScrollView .../>
<Button ... />
</merge>

Related

Which layout should be used to show a "footer" depending on the rest of the screen content

First, my apologies for the title. I thought long and hard to choose a more descriptive title but couldn't really find one.
I have a screen with a header (variable length - using sliding action bar), a middle part (scrollview) and a bottom part for showing ads (fixed length). I am able to write the screen using RelativeLayout but what I would really like to do is this:
If the scrollview's content (the middle part) is not long enough to fill the screen (including header), I would like to show the ad at the bottom of the screen
If the scrollview's content + header is longer than the screen, I would like to show the ad BELOW the scroll view's contents, meaning it's not visible unless the user scrolls to the bottom of the fragment.
Imagine the scroll view's content as a short text or an image + a long text. For the first case, the scroll view content will be short and therefore I'd ideally show the ad at the bottom of the screen since I have enough space but if it's image + text, it will be larger than the screen height and as such, I would like to show the ad after the user has scrolled to the bottom.
The reason I want to do this is so that the ad doesn't necessarily take space if there's useful content on the screen (user experience).
Is there anyway to achieve this in android without writing my own custom layout? If so, which view would you recommend?
Many thanks in advance,
I don't think there is such a layout. You can do this programmatically. The ScrollView always has a single child so you can get the content height this way:
int contentHeight = scrollView.getChildAt(0).getHeight();
You can get window height this way
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int windowHeight = size.y;
The height of the header and footer is fixed so you can do the maths decide where to put the footer.
int totalHeight = heaederHeight + contentHeight + footerHeight
if(totalHeight < windowHeight) {
// add the footer to the scrollview
} else {
// add the footer below the scrollview
}
The cons of this approach is that you have to do all the magic manually.
I hope this helped you :)

How to modify a relative layout to fit a specific left, right, top, bottom position on a screen

My relative layout is named "highlight".
public static void selectText(float left, float right, float top, float bottom) {
highlight.getLayoutParams().width =(int) (right-left);
highlight.getLayoutParams().height=(int) (bottom - top);
highlight.setX(left);
highlight.setY(top);
}
This works great for highlighting text as far as setting the top left corner of the highlight box. But, the box expands all the way to the bottom right corner of the screen, no matter how small I make the .width and .height values.
You set your width and height as wrap_content. Your layout will have the size of it's content.
Instead of:
highlight.getLayoutParams().width =(int) (right-left);
highlight.getLayoutParams().height=(int) (bottom - top);
Try:
highlight.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Check this link:
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
I don't know why, but the simple 'get layout params' wasn't working. I needed to make a new layout params entirely.
int width = (int) (right-left);
int height = (int) (bottom - top);
RelativeLayout.LayoutParams rlMainlayoutParams = new RelativeLayout.LayoutParams((int) (right-left), (int) (bottom - top));
highlight.setLayoutParams(rlMainlayoutParams);
highlight.setX(left);
highlight.setY(top);
The above answer doesn't answer my question, perhaps because I was not clear that this relative layout doesn't hold any text, it only highlights certain text already present on the screen (in a PDF document, where I cannot necessarily just extract the text to a PDF document).
After you've modified a view's layout params, call requestLayout() on the view for the changes to take effect.
(Calling setLayoutParams() also implicity calls requestLayout().)

how to set a button layout_marginTop/layout_marginBottom in runtime?

I am using a relative layout with 4 buttons (in the center, one below the other).
My problem is to adjust the gap between all buttons so it will be the same between all buttons.
I want to do it dynamically according to the height of the screen (i use Display class to get the actual height).
what is the best way to do it?
Thanks,
Amigal
You can do this via modifying the LayoutParams of your View
Button b;// your button
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)b.getLayoutParams();
lp.leftMargin=10;//your left margin here
lp.topMargin=10;//your top margin here and do this for other 2 margins if you need to
sometimes you need to call
b.setLayoutParams(lp);
to have the changes applied
also i dont know how you get the screen dimensions, but this works at every API:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

Positioning items relative to the ActionBar in Android

I used to have an Activity that would use no ActionBar. The layout is a RelativeLayout with width and height simply matching the parent. It allowed users to drag around buttons using parts of this code in the onTouch event:
MarginLayoutParams marginParams = new MarginLayoutParams(v.getLayoutParams());
int left = (int) event.getRawX() - (v.getWidth() / 2);
int top = (int) event.getRawY() - (v.getHeight());
marginParams.setMargins(left, top, 0, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
v.setLayoutParams(layoutParams);
All dandy—the button moves just below the finger and is placed correctly at the position on screen. Now, however, I enabled the ActionBar, and suddenly when clicked, the button moves ~30 pixels below the finger. To illustrate this behavior, here's what I used to have (on the left) and what happens right now (on the right):
I assume this is because the ActionBar now displaces everything by its own height. Of course, I do not want this to happen, or at least correct this, so:
How can I shift the placement in setMargins() according to the actual size of the ActionBar without using a hardcoded value?
How can I prevent the ActionBar from shifting everything in the first place? Put differently, how can I ensure my button is placed relative to the ActionBar's bottom line and directly where the finger points?
This is what I want:
(I used an approach is my code and it works. I am going to tell you the same here)
I think setMargin() is not such a clever thing to use here. Instead you can use setX() and setY() methods.
Whenever one switches over from a full screen activity to the one with Action Bar, one runs into such problem.
You will need to adjust for the Action Bar height. Instead of using getLayoutParams(), use getLocationInWindow() or getLocationOnScreen(). Since getRawX() and getRawY() work with screen positions of the view, these APIs should be used.

Center a ListView on its current selection

Does anyone know of a way to center a ListView based on its current selection or selection set with setSelection?
I did see this other StackOverflow question without any answers: Android ListView center selection
Thanks,
Kevin
First, get the height of the ListView using getHeight, which returns the height of the ListView in pixels.
Then, get the height of the row's View using the same method.
Then, use setSelectionFromTop and pass in half of the ListView's height minus half of the row's height.
Something like:
int h1 = mListView.getHeight();
int h2 = v.getHeight();
mListView.setSelectionFromTop(position, h1/2 - h2/2);
Or, instead of doing the math, you might just pick a constant for the offset from the top, but I would think it might be more fragile on different devices since the second argument for setSelectionFromTop appears to be in pixels rather than device independent pixels.
I haven't tested this code, but it should work as long as your rows are all roughly the same height.
You will need to have the scroll view and the view of the item selected. Then you can simply do:
scrollView.smoothScrollTo(0, selectedView.getTop() - (scrollView.getHeight() / 2) + (selectedView.getHeight() / 2), 0);
This will center the scroll view exactly on selectedView
I haven't tried any of this but based on the current selection could you use public void smoothScrollByOffset (int offset) to get the view to scroll to where you want so that your selection is in the middle of the view?

Categories

Resources