Android HorizontalScrollView Child Width - android

I have an HorizontalScrollView that contain a LinearLayout that contain some custom views. For now, I fixe the size of the children by this line (in the constructor of the child) :
setLayoutParams(new LayoutParams(200, LayoutParams.FILL_PARENT));
What I would like to do, is to set the width of the component to 30% of the height of itself instead of the actual 200px. But I still need the height to "FILL_PARENT".
When I overrided the onMeasure methode, I tryed that :
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
System.out.println(widthMeasureSpec + "/" + heightMeasureSpec);
}
But I obtained the output :
1073742024/1073742586
So it look likes I can't do a lot of thing here, still I don't have a lot of information :)
Any idea how to do the trick ?
I precise that I'm testing on the simulator (don't know if that matter).

You could do something like get the dimensions of the screen
DisplayMetrics display = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics( display );
int screenW = display.widthPixels;
int screenH = display.heightPixels;
and if the height of the component you want to take 0.3 out of, is the height of the entire screen, then do something like so:
setLayoutParams(new LayoutParams((int)(screenH*0.3), LayoutParams.FILL_PARENT));
I might have misunderstood your question, I just woke up :) Let me know if it worked or if you're looking for something else

Related

How do I allow PopupWindows to wrap content over maximum preferred size?

I have an Android application that extensively uses PopupWindows. I've found that when the layouts of the contents of the PopupWindow use WRAP_CONTENT the dialog will only grow to a specific width before it begins truncating the content. I've traced this down to a config.xml dimension:
<dimen name="config_prefDialogWidth">580dp</dimen>
This dimension is used to create a maximum width in the measureHierarchy method of ViewRootImpl when determining the dialog size. The following code is used to access the value:
final DisplayMetrics packageMetrics = res.getDisplayMetrics();
res.getValue(com.android.internal.R.dimen.config_prefDialogWidth, mTmpValue, true);
int baseSize = 0;
if (mTmpValue.type == TypedValue.TYPE_DIMENSION) {
baseSize = (int)mTmpValue.getDimension(packageMetrics);
}
The config_prefDialogWidth seems to have values for different device configurations, for example, the one I listed is for sw600dp. The one for default devices has 320dp as a value.
It would seem that this dimension is tuned for portrait orientation. Since my app forces landscape orientation this width is too small.
How do I override the config_prefDialogWidth dimension?
What you need is to make the width of your PopupWindow(wrap_content) larger than config_prefDialogWidth.
So here is my solution.
Choose one child View from the contentView of your PopupWindow.
A TextView, for example.
Override its onMeasure, add View.MEASURED_STATE_TOO_SMALL if necessary:
TextView textView = new TextView(context) {
#Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// Text breaks into at least 2 lines, maybe the width is too small
if (getLineCount() > 1) {
int measuredWidth = getMeasuredWidth();
final int desiredMaxWidth = (getResources().getDisplayMetrics().widthPixels >> 2) * 3;
// Compare with the desired max width you want, for example: 3/4 * screenWidth
if (measuredWidth < disiredMaxWidth) {
// Tell ViewRootImpl that the width used to measure is too small
measuredWidth |= View.MEASURED_STATE_TOO_SMALL;
// ViewRootImpl will remeasure contentView with screem width.
setMeasuredDimension(measuredWidth, getMeasuredHeight());
}
}
}
};
Generally speaking:
Override onMeasure of one child View
Check whether the measured width is too small
Ask for remeasurement if necessary (Using View.MEASURED_STATE_TOO_SMALL)
You cannot override nor change config_prefDialogWidth's value because this dimension is a internal resource. A workaround is you must create your own class. You can copy PopupWindows from its source code, and modify it.

Is there a method to get the height of the entire 'ScrollView'?

Is there a method in Android to get the height of the entire 'ScrollView'?
I tried 'layout.getMeasuredHeight()' but it gives the height only of the visible region.
You want the height of the ScrollView's child. Try scrollView.getChildAt(0).getMeasuredHeight().
EDIT
You could possibly try having the View measure itself and give it a measurespec that lets it use as much space as it wants. Something like this:
int width = ... // get screen width;
int height = 65536; // arbitrarily large value
final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST);
final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
view.onMeasure(widthMeasureSpec, heightMeasureSpec);
// now get the height
int measuredHeight = view.getMeasuredHeight();
Full disclosure: I don't know if this will work or what effect it may have in your app. There is a side-effect of calling this method, which is that the view's measured dimensions are set. If it's possible, you might try to create a separate, duplicate view and measure that one; or, after you do this measuring hack, call requestLayout() on the view to schedule another measure & layout of the view tree.
A ScrollView always has only one child, so using:
ScrollView.getChildAt(0).getHeight();
should work.

View getHeight() and getWidth() return values too large

I call getHeight and getWidth in a custom view in the onDraw() method. getHeight() returns 1,073,742,549 and getWidth() returns 1,073,742,304. However, when I look at the display metrics for the screen's height and width, I get 800 and 480 respectively. What is getHeight and getWidth returning?
I'm getting the dimensions of my view so I can choose the dimensions of a bitmap that'll go in the view.
Thanks.
Doesn't explicitly solve you're problem, but curiously:
Width:
1,073,742,304 == 0x400001E0
480 == 0x000001E0
Height:
1,073,742,549 == 0x400002D5
725 == 0x000002D5
Any chance this custom view is roughly 725 x 480? I have no idea where that extra 0x40000000 came from, but it looks like it could be almost right except for 1 bit :)
getHeight() and getWidth() return the size in pixels see http://developer.android.com/reference/android/view/View.html#getHeight%28%29
The extra size is likely to be coming from the View.MeasureSpec constant EXACTLY see http://developer.android.com/reference/android/view/View.MeasureSpec.html#getSize%28int%29
Maybe, you did something like this in your custom view.
final int desiredHSpec = MeasureSpec.makeMeasureSpec(pixelHeight, MeasureSpec.MODE_CONSTANT);
final int desiredWSpec = MeasureSpec.makeMeasureSpec(pixelWidth, MeasureSpec.MODE_CONSTANT);
setMeasuredDimension(desiredWSpec, desiredHSpec);
like this Android:Why after override onMeasure() in a custom view, the view's text can't show in RalativeLayout?
If you change it into
setMeasuredDimension(width, height);
, you will get simple small width, height on your onSizeChanged() and onLayout().
I also had this problem. The way I solved it was overriding the onMeasure method. This way the view always has the same size. For example, If you want the view to have the size and width of the screen you should do something like this:
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
WindowManager wm = (WindowManager)this.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
//display.getWidth() and display.getHeight() return the screen dimensions
this.setMeasuredDimension(display.getWidth(), display.getHeight());
}

Custom Viewgroup works inside LinearLayout not RelativeLayout

I wanted to create a custom LinearLayout (and later a custom ImageButton) that could take percentage values for both dimensions of size based on its parent's size regardless of the parent type (Relative or Linear). I was following this post: How to size an Android view based on its parent's dimensions, and it was very helpful, but I have a problem that those answers don't address.
When I place my Custom LinearLayout inside another LinearLayout, everything works as expected. My Custom LinearLayout covers the expected space (80% of the parent's width in the example below).
However if I place it inside a RelativeLayout, my screen always shows empty, I am not sure why this happens.
Here is my class:
public class ButtonPanel extends LinearLayout {
public ButtonPanel(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int newWidth = (int) Math.ceil(parentWidth * 0.8);
this.setMeasuredDimension(newWidth, parentHeight);
this.setLayoutParams(new RelativeLayout.LayoutParams(newWidth,parentHeight));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
And here is my testing layout for the activity.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.android.tests.views.ButtonPanel
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/inner_panel"
android:gravity="center_horizontal"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</com.android.tests.views.ButtonPanel>
</RelativeLayout>
In my activity all I do is set the Content View to the above layout.
(Incidentally, does anybody now how I could get the type of the parent dynamically for setting the new LayoutParameters? Above you'll see the parent type (RelativeLayout) hard-coded into the Custom View onMeasure function)
Thanks in advance for any help!
Is this exposed to be a problem?
this.setLayoutParams(new RelativeLayout.LayoutParams(newWidth,parentHeight)); // <-- a RelativeLayout params?
In the onMeasure function you could use something like this to know what class is the parent of the view.
this.getParent().getClass().getName()
This should also work
a instanceof B
or
B.class.isAssignableFrom(a.getClass())
When using "instanceof", you need to know the class of "B" at compile time. When using "isAssignableFrom" it can be dynamic and change during runtime.
If you are not compfortable with string comparison, you could also use enums.
Turns out my two inquiries in this post were more related than expected.
I realized that by setting my view's LayoutParams to a completely new instance, I was overwriting the layout positioning information needed by the Relative Layout to position my view.
By 'zeroing out' that information, my view has the right dimensions, but the layout doesn't know where to place it, so it simply doesn't.
The following code for the new onMeasure shows how just directly modifying the height and width of the LayoutParams already attached to my view I avoid both overwriting the layout position information and having to create new LayoutParams based on the parent's type.
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int specWidth = MeasureSpec.getSize(widthMeasureSpec);
int specHeight = MeasureSpec.getSize(heightMeasureSpec);
int newWidth = (int) Math.ceil(parentWidth * 0.8);
int newHeight = (int) Math.ceil(parentHeight * 0.8);
this.setMeasuredDimension(newWidth, newHeight);
this.getLayoutParams().height = newHeight;
this.getLayoutParams().width = newWidth;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
Now, I'll be honest and say that this code is still not bug-free. Bringing the activity to the foreground and background multiple times constantly reduces the size of this custom view. The 0.8 reduction factor gets applied over and over each time the activity is brought up (I suspect the setting of the LayoutParams has to do with it, it might actually be unnecessary, but I haven't has time to test).
BUT, this still answered the question concerning this post, namely, why was my view not appearing at all despite having the right dimensions.

how to add different buttons dynamically

I have an array of Buttons (different sizes etc) which are configured from and xml file (written by me). I want to add those buttons on the bottom of the screen and when the row of buttons ends, just start a new row and add buttons until the array ends. I want to mention that I do not set the size of the buttons in the xml file so I don't know the size from the beginning. Another problem is that after or before I add the button to the layout programatically with layout.addView(button) the method button.getWidth() returns 0 because the UI elements are not drawn in the UI yet. I also overrided the onLayout() method but still wasn't able to redraw the buttons.
If you have any ideas, please help.
Thanks
Well you could make the button take the same size using weight property. In that case, decide how many buttons you need in a row and use a for loop to do it accordingly. If you need the width you could use something like
Add this to your onCreate
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
You should be able to get the width and height
over here.
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
This is how I did it. I overrided the onMeasure() method of the layout containing the buttons.
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int specSize = MeasureSpec.getSize(widthMeasureSpec);
this.width = specSize;
specSize = MeasureSpec.getSize(heightMeasureSpec);
this.height = specSize;
//now you can use the sizes before the layout is drawn
this.webview.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,this.height - this.menuBarHeight) );
//don't forget for the parent method!
//but at the end, after the measures are done
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

Categories

Resources