RelativeLayout change height in onGlobalLayout and leave width to fill - android

I have the following ListView-item:
Currently everything works as intended and is placed where it should, except for the width of the TextView (2).
Since I use a RelativeLayout with some wrap_contents, I use a onGlobalLayoutListener so I can access the MeasuredWidths and Heights the moment the View is done loading and rendering. With the TextView2 however I get some weird results when I debug.
The first time onGlobalLayout is called, measuredWidth of the TextView2 is what it should be (375 px). The second time however, it's 48 px (same as the Height) and when I look at the fields of the TextView2 it says: mMeasuredHeight: 375; mMeasuredWidth: 48 :S
My layout is at the bottom of this post. My onGlobalLayoutListener is:
if(view.getViewTreeObserver().isAlive()){
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
// This will be called once the layout is finished, prior to displaying it
// So we can change some widths and heights based on other View-Elements that are filled now
// (We couldn't do this in the XML itself since they weren't filled yet and we didn't knew the sizes yet.)
#Override
public void onGlobalLayout() {
if(holder != null){
...
// Change the height of the ProductName-TextView to match the Image and leave the width as is
int height = h.imageView.getMeasuredHeight();
int width = h.tvName.getMeasuredWidth();
* h.tvName.setLayoutParams(new RelativeLayout.LayoutParams(h.imageView.getMeasuredHeight(), h.tvName.getMeasuredWidth()));
...
}
// Since we don't want onGlobalLayout to continue forever, we remove the Listener here again.
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
The * is a breakpoint. The first time width is 48 and height is 375. The second time width is 48 and height is 48, and if I look at the mMeasuredHeight and mMeasuredWidth fields in my holder.textView2, they are h=375 and w=48 :S
Here is my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:id="#+id/left_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_marginTop="#dimen/default_margin"
android:layout_marginLeft="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#layout/transparent_background"
android:contentDescription="#string/checkbox_content_description"
android:src="#drawable/checkbox_unchecked" />
<Space
android:id="#+id/filler_space_image"
android:layout_width="1dp"
android:layout_height="1dp"
android:visibility="gone" />
</LinearLayout>
<TextView
android:id="#+id/tv_product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/left_ll"
android:layout_toLeftOf="#+id/right_ll"
android:ellipsize="end"
android:singleLine="true"
android:gravity="center_vertical"
android:layout_marginTop="#dimen/default_margin"
android:layout_marginLeft="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin" />
<EditText
android:id="#+id/et_result_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_product_name"
android:layout_toRightOf="#id/left_ll"
android:inputType="number"
android:layout_marginLeft="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:visibility="gone" />
<AutoCompleteTextView
android:id="#+id/actv_result_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/right_ll"
android:layout_toRightOf="#id/et_result_amount"
android:layout_below="#+id/tv_product_name"
android:ellipsize="end"
android:inputType="text"
android:singleLine="true"
android:visibility="gone" />
<TextView
android:id="#+id/tv_tags"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/et_result_amount"
android:layout_toRightOf="#id/left_ll"
android:text="#string/tags"
android:gravity="center"
android:layout_marginLeft="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:visibility="gone" />
<Spinner
android:id="#+id/sp_tags"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/actv_result_name"
android:layout_toRightOf="#id/tv_tags"
android:layout_toLeftOf="#id/right_ll"
android:layout_marginLeft="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:visibility="gone" />
<LinearLayout
android:id="#id/right_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:layout_margin="#dimen/default_margin">
<TextView
android:id="#+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
<Space
android:id="#+id/filler_space_price"
android:layout_width="1dp"
android:layout_height="1dp"
android:visibility="gone" />
<ImageButton
android:id="#+id/btn_tags"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#android:drawable/ic_menu_manage"
android:contentDescription="#string/button_tags_content_description"
android:background="#layout/transparent_background"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>

Ok, it was (after I figured it out) pretty obvious.. Instead of
h.tvName.setLayoutParams(new RelativeLayout.LayoutParams(
h.imageView.getMeasuredHeight(), h.tvName.getMeasuredWidth()));
I now use
h.tvName.setLayoutParams(new RelativeLayout.LayoutParams(
h.tvName.getMeasuredWidth(), hh.imageView.getMeasuredHeight()));
Difference you ask? I used LayoutParams(height, width) instead of LayoutParams(width, height)...

Related

Impossible to change the height of a container in Recycleview

I have had a big trouble since several hours;
I have a recyclerview with several elements in each item. For one of them (lv_container), I can decrease its height but not increase it.
See the screenshot:
the xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/dashboard_part_medical_file_locked_title"
android:textSize="18sp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/row_dashboard_color"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="#+id/dashboard_part_image"
android:layout_width="80dp"
android:layout_height="95dp"
android:layout_alignParentEnd="true"
android:paddingBottom="20dp"
android:paddingEnd="10dp"
android:paddingStart="10dp"
android:paddingTop="20dp"
android:src="#drawable/dossier_medical" />
<RelativeLayout
android:id="#+id/lv_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/dashboard_part_image"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/dashboard_part_image"
android:layout_toStartOf="#+id/dashboard_part_image"
android:background="#android:color/white"
android:padding="10dp">
<ImageView
android:id="#+id/sup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_sup" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_toStartOf="#id/sup"
android:text="#string/dashboard_part_medical_file_inside" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
and the code of the viewholder
DisplayMetrics metrics = pContext.getResources().getDisplayMetrics();
RelativeLayout.LayoutParams lvContainerParams = (RelativeLayout.LayoutParams) lvContainer.getLayoutParams();
lvContainerParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
lvContainerParams.height = (int) (40 * metrics.density + 20 * metrics.density);
lvContainer.setLayoutParams(lvContainerParams);
ViewGroup.LayoutParams mainContainerParams = mainContainer.getLayoutParams();
mainContainerParams.height = (int) (lvContainerParams.height + 20 * metrics.density);
mainContainer.setLayoutParams(mainContainerParams);
ViewGroup.LayoutParams params = itemView.getLayoutParams();
params.height = (int) (mainContainerParams.height + 50 * metrics.density);
itemView.setLayoutParams(params);
When I change the height of lvContainer (the white container), the height of mainContainer is well modified (as you can see with the blue container), but the white container always remains fixed. However, in debug, heights are well calculated.
I tried to call lvContainer.invalidate() and/or lvContainer.requestLayout(), but nothing changed.
What's the error? Why the height of item and mainContainer can be easily modified, but not this of lvContainer?
I need your help
Thanks
Try removing this line
android:layout_alignBottom="#+id/dashboard_part_image"

Align Android seekbar with thumb

I'm trying to align the seekbar to the top of a view, but i can't center it with the thumb.
Is there some kind of "alignCenter" with the RelativeLayout children.
Here's a sample of my xml code :
<RelativeLayout
android:id="#+id/rl_fragment_audio_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/black_transparent"
android:padding="5dp"
android:visibility="gone" >
<TextView
android:id="#+id/tv_fragment_audio_begin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textColor="#color/white" />
<TextView
android:id="#+id/tv_fragment_audio_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textColor="#color/white" />
<Button
android:id="#+id/btn_fragment_audio_play"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:background="#drawable/btn_play"
android:visibility="gone" />
</RelativeLayout>
<SeekBar
android:id="#+id/sb_fragment_audio"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_above="#id/rl_fragment_audio_player" />
For example, Google Play Music does it.
As you said the height of your seekbar might vary, unless you specify it different. Here is how you can make sure it fits all, always.
android:layout_above="#id/rl_fragment_audio_player"
is setting your seekbar bottom above rl_fragment_audio_player top. There is no direct method to specify centerAlignWith but I would remove the space it occupies within it's parent. To know the height of the seekbar you must wait until the global layout has changed. This code should be placed inside your onCreateView
sb = (SeekBar) rootView.findViewById(R.id.sb_fragment_audio);
sb.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener(){
#Override
public void onGlobalLayout() {
sb.getViewTreeObserver().removeOnGlobalLayoutListener(this);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) sb.getLayoutParams();
params.setMargins(0, - sb.getHeight() / 2, 0, - sb.getHeight() / 2);
sb.setLayoutParams(params);
// I suggest you set the seekbar visible after this so that it won't jump
}
});
The last view in the xml will be in the front. Therefore make sure your seekbar is added after the the other views. Like this
<RelativeLayout
android:id="#+id/rl_fragment_audio_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
// your bottom panel
</RelativeLayout>
<View
android:id="#+id/image_above"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/rl_fragment_audio_player" />
<SeekBar
android:id="#+id/sb_fragment_audio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/image_above" />
Have you tried adding a negative margin to the seekbar?
<SeekBar
android:id="#+id/sb_fragment_audio"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginBottom="-15dp"
android:layout_above="#id/rl_fragment_audio_player" />

Space (View) is not working in ListItem Layout

I've got a ListActivity with a custom Adapter. This adapter uses the following list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/default_margin"
android:contentDescription="#string/checkbox_content_description"
android:src="#drawable/checkbox_unchecked"
android:background="#layout/transparent_background"
android:focusableInTouchMode="false"
android:adjustViewBounds="true"
android:clickable="false"
android:focusable="false" />
<TextView
android:id="#+id/tv_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginTop="#dimen/default_margin"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textIsSelectable="false" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginTop="#dimen/default_margin">
<TextView
android:id="#+id/tv_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textIsSelectable="false" />
</LinearLayout>
<TextView
android:id="#+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginTop="#dimen/default_margin"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textIsSelectable="false" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll2"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_below="#id/ll1">
<Space
android:id="#+id/filler_space"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/default_margin"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" />
<EditText
android:id="#+id/et_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:inputType="number" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:padding="0dp">
<AutoCompleteTextView
android:id="#+id/actv_search_product"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:singleLine="true"
android:ellipsize="end"
android:inputType="text" />
</LinearLayout>
<EditText
android:id="#+id/et_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:inputType="numberDecimal"
android:digits="0123456789.,-" />
</LinearLayout>
</RelativeLayout>
In the Adapter's getView-method I've added the following piece of code:
// Change the width of the Filler-Space to match the Image
// and leave the height as is
Space space = (Space) view.findViewById(R.id.filler_space);
space.setLayoutParams(new LinearLayout.LayoutParams(
imageView.getMeasuredWidth(), space.getMeasuredHeight()));
This gives the following result at first:
When I change the Visibility of my second LinearLayout (ll2) to VISIBLE, it gives the following result:
What I want instead is:
As it seems the Space-View width isn't changed at all.. While I know for fact that the getView methods are successfully called thanks to Log-messages and other things I do in the getView method.
Does anyone know what I did wrong?
PS: When I use View instead of Space I have the same result. Never used Space before, so I thought it might have to do something with that.
Solution thanks to Demand's option 4:
// Since we can't access Measured widths and heights before the View is completely loaded,
// we set up an Observer that will be called once the ListItem's layout is ready
ViewTreeObserver observer = view.getViewTreeObserver();
if(observer.isAlive()){
// In order to access the view in the onGlobalLayout, it needs to be final, so we create a final copy here
final View v = view;
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
// This will be called once the layout is finished, prior to displaying it
#Override
public void onGlobalLayout() {
ImageView imageView = (ImageView) v.findViewById(R.id.image);
Space space = (Space) v.findViewById(R.id.filler_space);
// Change the width of the Filler-Space to match the Image and leave the height as is
space.setLayoutParams(new LinearLayout.LayoutParams(imageView.getMeasuredWidth(), space.getMeasuredHeight()));
}
});
}
You set width and height if your space to wrap_content. It's mean that space will have width as their children, but there is no children and you have width = 0. It's not about space, it's about layout measuring in android.
When you call your code:
Space space = (Space) view.findViewById(R.id.filler_space);
space.setLayoutParams(new LinearLayout.LayoutParams(
imageView.getMeasuredWidth(), space.getMeasuredHeight()));
Your imageView havn't measured yet and return width = 0. It will measured later before drawing. In getView() in adapter you only create views, but measuring, layout and drawing will be later.
You have several ways to fix it:
set width of your space in dp, instead of wrap_content.
using relative layout instead of three linear layouts.
Use TableLayout.
add GlobalTreeObserver and getMeasuredWidth() at right time.
Post your runnable to view's handler to get width after drawing.
I think the best ways is 2 and 3, because 4 and 5 will cause measuring several times.

How to position layout off the screen

I need to reach the following result:
My RelativeLayout overlay initially should look like that . So only 20% of the layout should be seen.
when Button is pressed i apply Animation so that layout goes up and looks the following
Overlay layout is placed in the parent RelativeLayout and has number of ImageButtons in it.
<RelativeLayout
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_below="#+id/actionbar"
android:layout_height="wrap_content"
android:background="#drawable/backgroundfeedme"
android:clickable="true"
>
-----number of img buttons here --
</RelativeLayout>
I was trying to set up its initial position as follows
rlOverlay = (RelativeLayout) v.findViewById(R.id.overlay);
ViewTreeObserver vto = rlOverlay.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
WindowManager wm = (WindowManager) MainActivity.appContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int height = display.getHeight(); // deprecated, my app is for API 2.1+
rlOverlay.layout(0, (int)(height*0.8), rlOverlay.getMeasuredWidth(), rlOverlay.getMeasuredHeight());
}
});
Unfortunately the code above doesnt affect position of the layout.
I also cant use marginTop, because it squeezes the layout.
So .. how to position my layout so that only 20% of it is visible initially?
Thanks.
i had same requirment. what i did is put two relative layout. in first layout took button and below it second layout with rest of my views and Visiblity = gone.
from code: when i click on Button i made seond layout visiblity = Visible and animate it.
here's code:
<RelativeLayout
android:id="#+id/llShopping"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bar_green" >
<Button
android:id="#+id/btnShoppingListAddItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bar_green"
android:gravity="center"
android:paddingLeft="5dp"
android:text="Add Item"
android:textColor="#android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/relAddItemChild"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnShoppingListAddItem"
android:layout_marginBottom="10dp"
android:visibility="gone" >
<View
android:id="#+id/gl"
android:layout_width="200dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="1dp"
android:background="#android:color/white" />
<EditText
android:id="#+id/edtItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/gl"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:hint="Item Name..." />
<Button
android:id="#+id/btnSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edtItemName"
android:layout_centerHorizontal="true"
android:text="Please pick store" />
<Button
android:id="#+id/btnAddToList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnSpinner"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginTop="15dp"
android:background="#drawable/btn_blue"
android:padding="5dp"
android:text="Add To List"
android:textColor="#android:color/white"
android:textSize="20sp" />
</RelativeLayout>
</RelativeLayout>

Resizing views on orientation change

I'm new to android development(v 4.0, API 14) and stuck in implementing orientation change.
I've added the following line in manifest file
android:configChanges="orientation|keyboardHidden|screenSize">
But it looks like screenSize parameter is not working so I have to manually write these two methods:
public void ChangetoLandscape() {
ed.setTranslationX(300.0f);
btnindex.setTranslationX(300.0f);
btngainer.setTranslationX(300.0f);
btnloser.setTranslationX(300.0f);
lsym.setTranslationX(200.0f);
ltable.setTranslationX(300.0f);
tv1.setTranslationX(290.0f);
tv2.setTranslationX(300.0f);
tv4.setTranslationX(300.0f);
mySimpleXYPlot.setScaleX(3.0f);
mySimpleXYPlot.setScaleY(0.8f);
mySimpleXYPlot.setPlotMarginLeft(50.0f);
mySimpleXYPlot.setTranslationX(60.0f);
mySimpleXYPlot.setTranslationY(-70.0f);
sv1.setTranslationY(-80.0f);
}
public void ChangetoPortrait() {
//NOTE THAT IF I DON'T WRITE BELOW CODE,
//SWITCHING TO PORTRAIT MODE AFTER LANDSCAPE MODE RUINS THE WHOLE LAYOUT.
ed.setTranslationX(-1.0f);
btnindex.setTranslationX(-1.0f);
btngainer.setTranslationX(-1.0f);
btnloser.setTranslationX(-1.0f);
lsym.setTranslationX(-1.0f);
ltable.setTranslationX(-1.0f);
tv1.setTranslationX(-1.0f);
tv2.setTranslationX(-1.0f);
tv4.setTranslationX(-1.0f);
mySimpleXYPlot.setScaleX(1.0f);
mySimpleXYPlot.setScaleY(1.0f);
mySimpleXYPlot.setPlotMarginLeft(1.0f);
mySimpleXYPlot.setTranslationX(-1.0f);
mySimpleXYPlot.setTranslationY(-1.0f);
sv1.setTranslationY(-1.0f);
}
.
I'm using RelativeLayout, so I've to manually translate each view to specified position on orientation change.
main.xml
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/MainLayout">"
<EditText
android:id="#+id/txt1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:hint="Search"
android:layout_marginLeft="220dp"
android:selectAllOnFocus="true"
android:cursorVisible="false" >
</EditText>
<Button
android:id="#+id/btnindex"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="Index"
android:layout_marginLeft="140dp"
android:layout_below="#id/txt1"
android:textSize="12dp"/>
<Button
android:id="#+id/btngainer"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="Gainer"
android:layout_below="#id/txt1"
android:layout_toRightOf="#id/btnindex"
android:textSize="12dp"/>
<Button
android:id="#+id/btnloser"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="Loser"
android:layout_below="#id/txt1"
android:layout_toRightOf="#id/btngainer"
android:textSize="12dp"/>
<TextView
android:id="#+id/tv1"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="100dp"
android:textStyle="bold"
android:gravity="center"
android:textSize="10dp"
android:textColor="#00ff00"
android:text="SYMBOL"/> <!-- 2 similar textviews -->
<ListView
android:id="#+id/ltable"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="70dp"
android:layout_alignParentRight="true"
android:layout_width="match_parent"
android:layout_below="#id/txt1">
</ListView>
<com.androidplot.xy.XYPlot
android:id="#+id/mySimpleXYPlot"
android:layout_width="140dp"
android:layout_height="200dp"
android:layout_alignTop="#+id/btnindex"
title="Index"/>
<ScrollView android:id="#+id/sv1"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginTop="250dp">
<LinearLayout android:id="#+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="vertical">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tv1"
android:text="Scrollview Item 1"/> <!-- 5 similar textviews -->
</LinearLayout>
</ScrollView>
</RelativeLayout>
.
But as showed in below screenshot, in landscape mode, graph which is plotted using android plot is not getting properly displayed. The reason behind this is I've used setScaleX() method bcoz setWidth() is not available. I don't want graph to look stretched. Instead it's width should be increased.
ScrollView is getting displayed properly in portrait mode but in landscape mode, it is not visivle according to it's height which is 100dp.
Edit: After changing android:configChanges="orientation|keyboardHidden|screenSize" to
android:configChanges="orientation|screenSize",
I get this when I switch to landscape mode:
use LayoutParams to place the graph at appropriate location instead of scale() function.

Categories

Resources