How to display Relative layout content from top of the screen - android

I am having following layout in file mylayout.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/yellow"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:id="#+id/Layout5"
android:layout_alignParentTop="true"
android:layout_gravity="top"
android:gravity="top"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Layout6"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="top">
<ImageView
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#color/green"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="5dp"
android:id="#+id/imageView"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_below="#+id/imageView">
<Button
android:id="#+id/btn1"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="#string/text_btn_1"
/>
<Button
android:id="#+id/btn2"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="#string/text_btn_2"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
With this layout when i am doing setContentView(R.layout.mylayout) for my activity, which has transparent theme, my layout is getting displayed in the center of the screen.
I want to change this layout positioning dynamically, so center(default) and bottom(by using following code) display is working.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);
But somehow i am not able to figure out how to display this layout dynamically to the top of the screen.
Any help will be appreciated.

Try removing the rule that you made earlier (setting it to align bottom), and add a new rule:
//your code.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);
//remove rule
relativeParas.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lLayout.setLayoutParams(relativeParas);
Note that removeRule() is available from API 17 onwards.

Try modifying following way:
layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Layout5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#color/yellow"
android:padding="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#color/green"/>
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/imageView"
android:orientation="horizontal">
<Button
android:id="#+id/btn1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="button1"
/>
<Button
android:id="#+id/btn2"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="button2"
/>
</LinearLayout>
</RelativeLayout>
class:
RelativeLayout Layout5 = (RelativeLayout)findViewById(R.id.Layout5);
Layout5.setGravity(Gravity.BOTTOM);

I found the following fix for this issue.
I added filler Liner layout in the end under Relative layout.
To display in center don't do anything.
To display in top use following code. Just move filler layout to bottom.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.filler);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);
To display in bottom use following code. i.e. move layout6 to bottom.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);

Related

Android display half TextView and half ImageView if true else display only TextView

I'm trying to display a text inside a TextView in the left side of the screen and an image inside ImageView in the right side when "true":
<RelativeLayout
android:id="#+id/displayMessageCenter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/displayMessageTop"
android:layout_centerHorizontal="true"
android:background="#android:color/white">
<TextView
android:id="#+id/textMessageText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="#000000"
android:layout_weight="0.5"
android:layout_alignBottom="#+id/dividerId" />
<ImageView
android:id="#+id/messagePicture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignBottom="#+id/dividerId"
android:layout_weight="0.5"
android:gravity="right"/>
<View
android:id="#+id/dividerId"
android:layout_width="400dp"
android:layout_height="1dp"
android:background="#000"
android:layout_above="#id/leftTextGenericDialog"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"/>
<TextView
android:id="#+id/leftTextGenericDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center"
android:layout_centerHorizontal="true"
android:textColor="#FF72CCCC"
android:text="SEND RESPONSE"
android:textSize="40dp"
android:drawableLeft="#drawable/sendicon"
android:layout_above="#+id/closeButton"
android:onClick="sendMessage"/>
<ImageButton
android:id="#+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5sp"
android:layout_gravity="center"
android:background="#drawable/button_close_animation"
android:onClick="closeActivity"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"/>
</RelativeLayout>
and if "false" to hide the image and display just the TextView with the text in the center. The hiding will be something like:
if(true) imageView.setVisibility(View.GONE);
else imageView.setVisibility(View.VISIBLE);
but I don't know how to align the ImageView and the TextView to be easier to hide/show just the ImageView when needed.
I'm new in Android development so please be gentle :)
When using Relative layout on these cases, you can use alignParentLeft=true for the TextView and alignParentRight=true for the ImageView, but if you wanted the TextView to adjust whenever the ImageView is visible or not. Use something like
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="text"
android:layout_weight="0.5"
android:gravity="center_horizontal"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:visibility="gone"
/>
</LinearLayout>
For true condition, you can just set
android:alignParentLeft="true"
and
android:alignParentRight="true"
to your TextView and ImageView corresponsdingly.
For false condition, you can programmatically set rules to your TextView. Just get the TextView using findViewById and set rules like following:
TextView tv = (TextView) findViewById(R.id.textMessageText);
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)tv.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
tv.setLayoutParams(layoutParams);

layout_weight in float and decimal

I change the width of a view dynamically in my code using:
final ImageButton ButtonTwo = (ImageButton)findViewById(R.id.callButton);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ButtonTwo.getLayoutParams();
params.weight = 1.3f;
ButtonTwo.setLayoutParams(params);
The idea is that it matches another view in XML :
<ImageButton
android:layout_height="match_parent"
android:layout_width="0dp"
android:id="#+id/imageButton"
android:src="#drawable/softkeyboard"
android:clickable="true"
android:onClick="softkeyboardButton"
android:layout_gravity="center_horizontal"
android:layout_weight="1.3"
android:background="#drawable/buttonstates"
/>
They're not the same width - the first button is wider than the second one. I know that this is because one is float and one is decimal, but how can I make them both the decimal size, or should I just do trial and error on the float value? My params.weight only accepts float values.
EDIT:
Here's my XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context=".MainActivity">
<ListView
android:id="#+id/ListView_2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1">
</ListView>
<!--Let's set the height of the linearlayout below to 0dp. Apparently it's less work on resources,
as otherwise we would be drawing it twice, because the size is also set dynamically-->
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
>
<!--android:adjustViewBounds="true"-->
<!-- we want the text to begin in the centre of the edittext view
that's what gravity does-->
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/editText"
android:inputType="phone"
android:gravity="center"
android:background="#d9d9d9"
android:layout_weight="4"
/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/callButton"
android:src="#drawable/call"
android:clickable="true"
android:onClick="softkeyboardButton"
android:background="#ffa62b"
android:layout_weight="2"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal" >
<CheckBox
android:layout_height="match_parent"
android:layout_width="0dp"
android:background="#ffa500"
android:text="New CheckBox"
android:id="#+id/checkBox"
android:layout_weight="5"
/>
<ImageButton
android:layout_height="match_parent"
android:layout_width="0dp"
android:id="#+id/imageButton"
android:src="#drawable/softkeyboard"
android:clickable="true"
android:onClick="softkeyboardButton"
android:layout_gravity="center_horizontal"
android:layout_weight="1.3"
android:background="#drawable/buttonstates"
/>
<View android:layout_height="fill_parent"
android:layout_width="2px"
android:background="#90909090"/>
<ImageButton
android:layout_height="match_parent"
android:layout_width="0dp"
android:id="#+id/imageButton2"
android:src="#drawable/settingsicon"
android:background="#drawable/buttonstates"
android:layout_gravity="center_horizontal"
android:layout_weight="1.3" />
</LinearLayout>
</LinearLayout>
Posting this as answer as advised. :)
If the idea is to just have them match the LayoutParams, why not just retrieve the LayoutParams of the ImageButton indicated in the xml, then set it as the LayoutParams of the ImageButton you want dynamically? Like so:
buttonTwo.setLayoutParams(buttonOne.getLayoutParams());
And to quote #Christophe Harris for other users:
I was having terrible trouble with the float - params.weight = 1.3f. The widths were never exactly the same, even though I was using your setLayoutParams = getLayoutParams. Probably something to do with floats never being accurate, I don't know. But then I changed it to params.width= 80, and it was exact.
You need to set layout_width to "0dp" for ButtonTwo for layout_weight to work correctly.
Hope this should work for you!

Adding LinearLayout xml in a ScrollView

I have a xml with a ScrollView and a LinearLayout as a child of ScrollView.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ViewStub
android:id="#+id/social_empty_stub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:inflatedId="#+id/social_empty_stub_inflted"
android:layout="#layout/empty_screen"
android:visibility="gone" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/activity_stream"
android:background="#DCDCDCDC"
android:fadingEdge="none" >
<LinearLayout
android:id="#+id/activity_stream_wrap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
and I have another xml with a LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<!-- List Item on activity stream -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout_Activity_Streams"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp" >
<!-- Avatar -->
<com.example.bb.ShaderImageView
android:id="#+id/imageView_Avatar"
android:layout_width="#dimen/social_avatar_size"
android:layout_height="#dimen/social_avatar_size"
android:layout_marginLeft="5dp"
android:scaleType="fitXY" />
<!-- Content -->
<LinearLayout
android:id="#+id/relativeLayout_Content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="5dp"
android:background="#drawable/news_browser_background_shape"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="#+id/textView_Name"
style="#style/textview_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:maxLines="5"
android:textStyle="bold" />
<TextView
android:id="#+id/textView_Message"
style="#style/textview_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="3dip"
android:maxLines="5" />
<TextView
android:id="#+id/textview_temp_message"
style="#style/textview_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="3dip"
android:maxLines="5"
android:visibility="gone" />
<ViewStub
android:id="#+id/attached_image_stub_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="2dp"
android:inflatedId="#+id/attached_image_stub_inflate"
android:layout="#layout/activity_image_display_layout"
android:padding="5dp"
android:visibility="gone" />
<TextView
android:id="#+id/activity_comment_view"
style="#style/textview_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="3dip"
android:maxLines="5"
android:visibility="gone" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginRight="10dp"
android:layout_marginTop="7dp"
android:gravity="center_vertical">
<ImageView
android:id="#+id/activity_image_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/ImageDesc"
android:layout_marginLeft="5dp"
android:padding="2dp"
android:scaleType="fitXY" />
<!-- Time text view -->
<TextView
android:id="#+id/textView_Time"
style="#style/textview_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/activity_image_type"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:layout_marginRight="3dp" />
<!-- Comment button -->
<Button
android:id="#+id/button_Comment"
style="#style/textview_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/social_activity_browser_comment_button_shape"
android:gravity="center_vertical"
android:padding="2dp" />
<!-- Like button -->
<Button
android:id="#+id/button_Like"
style="#style/textview_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/button_Comment"
android:background="#drawable/social_activity_browser_like_button_shape"
android:gravity="center_vertical"
android:padding="2dp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Now I want to add the LinearLayout in second xml programmatically in ScrollView of first xml. I am using following code
setContentView(R.layout.activitybrowserview);
View view = LayoutInflater.from(context).inflate(R.layout.activitybrowserviewcell, null);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.RelativeLayout_Activity_Streams);
ScrollView sv = (ScrollView) findViewById(R.id.activity_stream);
sv.addView(ll);
I get following error, how can I avoid that. I don't understand the error.
IllegalStateException "Scrollview can host only one direct child"
Scrollview can host only one direct child,the scrollvie has direct child yet(android:id="#+id/activity_stream_wrap"),u should add view with this one.
setContentView(R.layout.activitybrowserview);
View view = LayoutInflater.from(context).inflate(R.layout.activitybrowserviewcell, null);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.RelativeLayout_Activity_Streams);
LinearLayout parentLl = (LinearLayout ) findViewById(R.id.activity_stream_wrap);
parentLl .addView(ll);
In your first XML file you have
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/activity_stream"
android:background="#DCDCDCDC"
android:fadingEdge="none" >
<LinearLayout
android:id="#+id/activity_stream_wrap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
If you remove the <LinearLayout> here, then you can add another LinearLayout instead. You can either remove it directly from the XML file or remove it programatically.
Just add a Linear Layout as a container to your current LinearLayout and the LinearLayout that you want to add pragmatically. I think this should solve your problem. ScrolView Can contain only one child as the exception says the same.
<ScrolView>
<LinearLayout> this will be the container
<linearlayout> the one that is currently in the first xml file
</LinearLayout>
</LinearLayout>
</ScrolView>
Hope this will help.
setContentView(R.layout.activitybrowserview);
View view = LayoutInflater.from(context).inflate(R.layout.activitybrowserviewcell, null);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.RelativeLayout_Activity_Streams);
ScrollView sv = (ScrollView) findViewById(R.id.activity_stream);
sv.removeAllViews();
sv.addView(ll);

Change RelativeLayout from Java in Android

I have this XML RelativeLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeBackGround"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="right"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
<ImageView
android:id="#+id/img1"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/sample" />
<LinearLayout
android:id="#+id/linout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/img1"
android:background="#drawable/bubble_left" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="text2"
android:textSize="10sp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
What I'm trying to to do is to change the gravity of the RelativeLayout from right to left based on the row like so
wrapper = (RelativeLayout) view.findViewById(R.id.relativeBackGround);
if (even) {
wrapper.setGravity(Gravity.LEFT);//this one isn't working
//How can I change the alignment of linout to be on the left of the img1?
} else {
wrapper.setGravity(Gravity.RIGHT);
}
the setGravity isn't working, and I'm not sure how to do the alignment, can you help me with that?
You can set the layout of the LinearLayout with something similar to the following:
LinearLayout ll = (LinearLayout) view.findViewById(R.id.linout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
ll.setLayoutParams(lp);
Then just switch the rule based on whether it's odd or even row.

HorizontalScrollView Not Scolling When Specified Width

I'm trying to have a HorizontalScrollView with 8 buttons which I can code fine. But I want to have the layout double the width of the screen so there are 4 buttons on the screen and the user has to scroll to see more (I'm don't want a "snap" scroll).
To do this I've manually change the width of the HorizontalScrollView to say "770dp" but whenever I specify the width it looks correct but does not scroll. Changing the width back to "wrap_content" and it works fine but does not look correct (5 or 6 buttons on the screen).
My xml code is below. This is just an extract - there are many more layouts/views on the screen.
I will be wanting to programmatically specify the width later the double the phone's screen size didn't want to move on to that until I worked out why the above isn't working.
I have included that code if anyone wants to lend a hand, but it is NOT related to the above non-scrolling problem.
Thanks in advance for any help you can give. Much appreciated.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:orientation="vertical"
tools:context="com.example.testScroll.MainActivity"
tools:ignore="MergeRootFrame" >
<HorizontalScrollView
android:id="#+id/topline_Buttons"
android:layout_width="770dp"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:orientation="horizontal" >
<Button
android:id="#+id/button_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_1" />
<Button
android:id="#+id/button_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_2" />
<Button
android:id="#+id/button_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_3" />
<Button
android:id="#+id/button_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_4" />
<Button
android:id="#+id/button_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_5" />
<Button
android:id="#+id/button_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_6" />
<Button
android:id="#+id/button_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_7" />
<Button
android:id="#+id/button_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"
android:text="#string/button_8" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Here is what I have found on SO HERE to programmatically set the width. ALthough I haven't got it working yet
//Set HorizontalScrollView double screen width
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(metrics.widthPixels*2, LayoutParams.MATCH_PARENT);
LinearLayout topline_Buttons = (LinearLayout) findViewById(R.id.topline_Buttons);
topline_Buttons.setLayoutParams(params);
I think trying to increase the size of the scroll view is the wrong way to approach this. If you want the layout that the scroll view wraps to be larger, then change the height of the layout.
Remember that the buttons are in the LinearLayout, and that is currently set to wrap content, so it will only take the space needed to display its children (the buttons).
Try this (with the rest of your code of course):
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:id="#+id/topline_Buttons"
android:layout_width="770dp"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:orientation="horizontal" >
// try this way,hope this will help you..
Note : if fix HorizontalScrollView layout_width then it's not Scrollable so you have gave layout_width "wrap_content"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:orientation="vertical">
<HorizontalScrollView
android:id="#+id/topline_Buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_1" />
<Button
android:id="#+id/button_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_2" />
<Button
android:id="#+id/button_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_3" />
<Button
android:id="#+id/button_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_4" />
<Button
android:id="#+id/button_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_5" />
<Button
android:id="#+id/button_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_6" />
<Button
android:id="#+id/button_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_7" />
<Button
android:id="#+id/button_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button_8" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
**Also remove this peace of code from Activity**
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(metrics.widthPixels*2, LinearLayout.LayoutParams.MATCH_PARENT);
topline_Buttons.setLayoutParams(params);
Ok I've worked it out! Or at least one way of doing it...
If there is a better way let me know.
Instead of specifying the width of the LinearLayout or HorizontalScrollView I needed to specify the width of the buttons inside to 1/4th of the screensize. The HorizontalScrollView should fill the whole page, the LinearLayout wrap content and so the buttons make enough space for themselves.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:orientation="horizontal" >
<Button
android:id="#+id/button_1"
android:layout_width="92dp" // Specify width here manually
android:layout_height="wrap_content"
android:text="1" />
... etc
That worked but obviously wouldn't look right with many screen sizes, so I specified it programmatically:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x / 4;
button_1 = (Button)findViewById(R.id.button_1);
button_1.setLayoutParams(new LinearLayout.LayoutParams(screenWidth, LayoutParams.WRAP_CONTENT));

Categories

Resources