I have an Android widget. I would like it to be 4 cells width and 2 cells height. Right now the size of the widget is 4 by 4 (for some reason). And the strange thing is that the content of the widget, i.e. the frame (according to this definition) is of small size.
So, most of the home screen is blank, but I cannot reposition the widget (seems that it takes all 4 by 4 cells on my home screen).
Here is the code of my widget-provider layout file:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="#layout/widget_demo"
android:minHeight="110dp"
android:minWidth="250dp"
android:updatePeriodMillis="1800000"
>
</appwidget-provider>
Here is the widget layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="110dp"
android:orientation="vertical"
android:layout_margin="1sp"
android:background="#android:drawable/alert_dark_frame" >
<TextView
android:id="#+id/widgetTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some_text"
android:textColor="#FFFFFF"
android:textSize="30dp" />
<Button
android:id="#+id/widget_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="some_text"
android:textSize="20dp"/>
</LinearLayout>
What is the problem? Why does it take 4 by 4 instead of 4 by 2?
App Widget layouts should be flexible, resizing to fit their parent container
So, change your LinearLayout
android:layout_width="wrap_content"
android:layout_height="110dp"
to match parent size
android:layout_width="match_parent"
android:layout_height="match_parent"
Related
So I'm creating a list view that grows and shrinks based off of the user input and I need four buttons but I don't know how to have buttons that are aligned with the bottom of the list view and are in a 2x2 grid fashion. I've already tried relative layout and it didn't seem to work. Thank you
I've created the XML according to what I believed you wanted to see.
Please check the following screen shot:
The following is the XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BUTTON3"/>
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BUTTON4"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_above="#id/linearLayout1">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BUTTON1"/>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BUTTON2" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/linearLayout2">
</ListView>
</RelativeLayout>
As you can see that I am using the RelativeLayout as the base wrapper and within the RelativeLayout, I have 2 linearlayouts for the buttons (2 each) and finally the list view. The purpose for the LinearLayout is simply to add weight to the buttons so they would share the equal amount of horizontal space. If that does not fit your criteria, feel free to remove them. The main thing to learn is that relative layout allows you to use attributes such as alignParenBottom, layout_above, layout_below and so on. These attributes allow you to place your elements anywhere on the screen and adjust them according to these attributes.
Let me know if you have any questions.
I have relative Layout that has Three Buttons, 1 anchored at the top(TopButton), One at The Bottom(BottomButton, and One that will either be placed directly below the top button or above the bottom button(MiddleButton) according to users touching my buttons.
Under Each of these buttons I have placed a ScrollView and then in there a textView. I am trying to make a set of Listeners so that when the User clicks on one of the buttons, the other two buttons ScrollViews (and their textViews) are set to View.GONE, and the button that was pushed will have their ScrollView set to View.Visible.
In addition to Setting the ScrollViews Visibility I wanted to Change the location of the buttons via a simple sliding animation (all on screen) to one of three set-ups according to the button pushed:
TopButton pushed - (Opening the Activity, what it is set to now) The scrollView for this button is visible, other two are set to gone, and both buttons are at the bottom of the screen.
MiddleButton pushed - ScrollView 1 and 3 will be set to gone and ScrollView 2 will be visible, and the MiddleButton will slide up just below the TopButton. So the order will be TopButton, MiddleButton, Scroll/TextView2, BottomButton.
BottomButton pushed - ScrollViews of 1 and 2 will be set to Gone and 3 will be set to Visible and the BottomButton will Slide up to reveal maximum space for the ScrollView. So the order in this one will be TopButton, MiddleButton, BottomButton, Scroll/TextView3
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/OverallLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="#+id/TopButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/BottomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<Button
android:id="#+id/MiddleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/BottomButton" />
<ScrollView
android:id="#+id/Scroll1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/MiddleButton"
android:layout_below="#id/TopButton"
android:visibility="visible" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</ScrollView>
<ScrollView
android:id="#+id/Scroll2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/BottomButton"
android:layout_below="#id/MiddleButton"
android:visibility="gone" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</ScrollView>
<ScrollView
android:id="#+id/Scroll3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/BottomButton"
android:visibility="gone" >
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</ScrollView>
</RelativeLayout>
I need help figuring out the fromY and toY values for this moving animation to work. What are these set to?
here's a translation animation I use. It's called "pull_in_from_top.xml"
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromYDelta="-100%"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="0%" />
This one does the opposite "push_out_to_top.xml"
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromYDelta="0%"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="-100%" />
I can't find a topic talking about my problem which seems to me standard ... And that make me think it is not possible. But I give a try.
I have a RelativeLayout with a 9 patch background (which contain a content area).
Is that possible to center horizontally and/or vertically a TextView inside this RelativeLayout in relation to the content area ?
this layout file will do the task
<?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"
android:background="#drawable/background"
android:gravity="center" >
<TextView
android:id="#+id/txtTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test TextView"
android:textColor="#000000" >
</TextView>
</RelativeLayout>
the 9-patch image i used is
I'm developing an android app for 2.2 ver.
My app must hav such structure:
[ Spinner here (fixed height) ]
[ ListView (not-fixed height) ]
[ ImageView (fixed-height) ]
I have to use only portrait orientation.
I use linearLayout for it. How can I calculate listView height to show spinner on the top of the screen, imageview on the bottom, and listview cover all free space, but not push others away from field of view.
It will be cool to make it dinamic for lots of screen resolutions.
Thank you
Use a relative layout to accomplish what you need.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="#drawable/icon" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/spinner1"
android:layout_above="#+id/imageView1" />
</RelativeLayout>
By doing this listview will adjust its size so that it fits between the spinner and imageview on your screen
You need to use the android:layout_weight attribute on the variable-height item, so it fills the available space.
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Here is another way to do this, using weights. This may be the easiest approach.
You can put in to the weight a percentage of the screen you would like it to take up.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"/>
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80"></ListView>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"/>
</LinearLayout>
EDIT:
Now that i think about it, this will "fix" the size of all the widgets. They will be "fixed" at percentages of the screen. This will scale well with other screen sizes, but you did say you wanted ListView to not be fixed.
I have some problem with multiple screen support, I work with dp(dpi) for specify the layout_heigth and layout_width and I hope that is the better way to support multiple screen, but when I tried with two smartphone I meet two different result.
I give an example, this is a layout I use:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cities_main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/citieslist"
android:layout_width="wrap_content"
android:layout_height="320dip"
android:layout_gravity="center_vertical"
android:layout_below="#id/cities_main_layout"
/>
<LinearLayout
android:id="#+id/cities_button_layout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/citieslist"
android:layout_gravity="center_vertical">
<Button
android:id="#+id/bycountry"
android:layout_height="50dip"
android:layout_width="105dip"
android:background="#drawable/buttonmarket"
android:text="#string/button_bycountry"
/>
<Button
android:id="#+id/top10"
android:layout_height="50dip"
android:layout_width="105dip"
android:background="#drawable/buttonmarket"
android:text="#string/button_top10"
/>
<Button
android:id="#+id/recommended"
android:layout_height="50dip"
android:layout_width="105dip"
android:background="#drawable/buttonmarket"
android:text="#string/button_recommended"
/>
</LinearLayout>
</RelativeLayout>
The button are at the bottom of the layout, and I see two different result:
http://img600.imageshack.us/img600/5513/htcmagicg2.png http://img600.imageshack.us/img600/5513/htcmagicg2.png
http://img441.imageshack.us/img441/6440/samsunggalaxys.png http://img441.imageshack.us/img441/6440/samsunggalaxys.png
In the last smartphone I can see the buttons, instead in the first I cannot...what's wrong?
I have to write a layout for any set of screen??!!!
Your ListView has
android:layout_height="320dip"
Now if the phone screen is smaller, it will not fit.
Try doing this instead: (Edited due to comments. This is displayed correcty in eclipse)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:layout_above="#+id/linlay">
</ListView>
<LinearLayout
android:id="#+id/linlay"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="#00FF00"
android:layout_alignParentBottom="true">
</LinearLayout>
</RelativeLayout>
Thant should fix it I think.
Cheers
As others have indicated, your problem is that you hardwired in a size for the ListView. If you want a business rule of "have the buttons at the bottom and have the ListView fill up the rest", you actually need to write code that implements "have the buttons at the bottom and have the ListView fill up the rest".
There are two main approaches here:
Use a LinearLayout parent for the buttons and the ListView. Use
android:layout_height="0px" and android:layout_weight="1" for the
ListView. Use a regular android:layout_height for the buttons (presumably in their own LinearLayout) and no
android:layout_weight for for them
Use a RelativeLayout parent for the buttons and the ListView.
Define the buttons as having android:layout_alignParentBottom="true".
Define the ListView as having android:layout_alignParentTop="true"
and android:layout_above="...", where the ... is the ID of the buttons' LinearLayout.
I would say it's because you are specifically declaring a height for your ListView and then laying the LinearLayout that holds your buttons at the bottom. Try changing it instead of being at the bottom of the ListView to something like
<LinearLayout
android:id="#+id/cities_button_layout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alignparentbottom="true"
android:layout_gravity="center_vertical">
I'm not entirely sure if align_parent_bottom is the 100% correct spelling of that.
Well, others have beaten me to it while I was typing, haha, but yeah, you're hardwiring a lot of things that shouldn't be, both the ListView and the Buttons. Take a look at this:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cities_main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="#+id/cities_button_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:id="#+id/bycountry"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="#drawable/buttonmarket"
android:text="#string/button_bycountry"
/>
<Button
android:id="#+id/top10"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="#drawable/buttonmarket"
android:text="#string/button_top10"
/>
<Button
android:id="#+id/recommended"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="#drawable/buttonmarket"
android:text="#string/button_recommended"
/>
</LinearLayout>
<ListView
android:id="#+id/citieslist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/cities_button_layout"
/>
</RelativeLayout>
You have android:orientation on your RelativeLayout, which isn't actually an attribute that RelativeLayout contains.
You should use the layout_weight attribute rather than hardwiring sizes for the Buttons. In my example, all buttons have a width of fill_parent, and a weight of 1. This makes them distribute the space evenly.
List the fixed button layout first, setting it to alignParentBottom="true". Then set the ListView to fill_parent, and layout_above your button layout. This keeps the button layout at the bottom, and makes the ListView take all the space above your buttons.
Tada!