Resize and make sizes equal 0 GridLayout (android) - android

In my application I have several buttons with images in its drawableTop property. Also, every Button has a label (text).
All images have the same size: 394px X 336px.
I want all the buttons to have the same size when using GridLayout. Also, I don't want the user to scroll down to see the rest of the buttons. I want all the buttons to be placed in just one screen.
I'm trying to do this in here, but since images are big (I am using the cellphone emulator), user needs to scroll down to see the rest of the buttons and also, even though all images have the same size, buttons don't have the same size.
Here is my code (buttons names and images don't matter, I just wanted to create a 2X2 grid with same size for all items, no scrolling at all):
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:keepScreenOn="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnCount="2"
android:rowCount="2" >
<Button
android:id="#+id/this_is_a_test"
android:background="#layout/roundcorners"
android:layout_gravity="fill"
android:text="This is a test"
android:textSize="20dip"
android:textStyle="bold"
android:textColor="#000000"
android:drawableTop="#drawable/test">
</Button>
<Button
android:id="#+id/this_is_a_test"
android:background="#layout/roundcorners"
android:layout_gravity="fill"
android:text="This is a test"
android:textSize="20dip"
android:textStyle="bold"
android:textColor="#000000"
android:drawableTop="#drawable/test">
</Button>
<Button
android:id="#+id/this_is_a_test"
android:background="#layout/roundcorners"
android:layout_gravity="fill"
android:text="This is a test"
android:textSize="20dip"
android:textStyle="bold"
android:textColor="#000000"
android:drawableTop="#drawable/test">
</Button>
<Button
android:id="#+id/this_is_a_test"
android:background="#layout/roundcorners"
android:layout_gravity="fill"
android:text="This is a test"
android:textSize="20dip"
android:textStyle="bold"
android:textColor="#000000"
android:drawableTop="#drawable/test">
</Button>
</GridLayout>
Can you guys help me, please?

Related

How to make an android view take the remaining space

I want to have a list of input fields with labels to their left for an edit screen. At the bottom there needs to be a set of buttons, which should be aligned centrally.
The labels can have different widths depending on the language, so the EditText views need to span across the remaining space. I am using a grid layout, because I need to have all EditText views aligned to a vertical line at their left.
I have spent half a day now to solve this, but I fail to make progress. The closest I could get to is this:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_column="0"
android:layout_row="0"
android:text="Label 1234545"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvHeightLabel"/>
<EditText
android:layout_column="1"
android:layout_row="0"
android:text="Some text which is too long that it could fit in one single row"
android:layout_gravity="fill_horizontal"
android:inputType="numberDecimal"
android:ellipsize="end"
android:id="#+id/etHeight"
android:layout_width="259dp"/>
<TextView
android:layout_column="0"
android:layout_row="1"
android:text="Label 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvHeightLabel2"/>
<EditText
android:layout_column="1"
android:layout_row="1"
android:text="Some text which is too long that it could fit in one single row"
android:layout_gravity="fill_horizontal"
android:inputType="numberDecimal"
android:ellipsize="end"
android:id="#+id/etHeight2"
android:layout_width="259dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_row="3"
android:layout_column="0"
android:layout_columnSpan="2">
<Button
style="?android:attr/buttonBarButtonStyle"
android:text="Button 1"
android:layout_height="wrap_content"
android:id="#+id/btCancel"
android:layout_weight="1"
android:layout_width="wrap_content"/>
<Button
style="?android:attr/buttonBarButtonStyle"
android:text="Button 2"
android:layout_height="wrap_content"
android:id="#+id/btSave"
android:layout_weight="1"
android:layout_width="wrap_content"/>
</LinearLayout>
</GridLayout>
This layout looks like this:
It seems to work first, but when the labels exceed a certain size, the EditText view elements grow out of the screen to the right:
I don't even get why the above solution works for labels, which just grow slightly. The width of the EditText elements is set to 259dp, which was set by Android Studio automatically. (I tried to arrange the EditText views by sizing it with the mouse on the preview on the design screen. Android Studio arranged it automatically at a certain point as given above.)
There must be some property to let a view eat up what is left independent of its content. The text could scroll, I'm ok with that. I feel like I have not understood the layout concept at all.
Use padding to restrict the flow of the text.

Unable to see completed view layout in Android Studio

I am creating a vertical LinearLayout that will be displayed in a vertical ScrollView. I can't find a way in Android Studio to see the portion of the LinearLayout that is below the bottom of the ScrollView. Is there any way to see the full layout rather than constraining it to the viewport provided by the renderer?
Like Drew said, creating a user defined device definition has been the only solution which worked for me. Below i show you the steps to follow:
Step 1)
Within the preview-pane, open the Virtual Device Selection drop-down and select Add Device Definition..
Step 2)
Within the Your Virtual Devices dialog click the Create Virtual Device button.
Step 3)
Within the Select Hardware dialog click the New Hardware Profile button.
Step 4)
Within the Configure Hardware Profile dialog specify (f.e.) a Resolution of 720 x 4000 px and a Screensize of 12 inch. Also setting the Screensize results in a density of xhdpi (thanks to Mete).
Step 5) Close all dialogs and restart Android Studio.
Step 6) Open the Virtual Device Selection drop-down. The new user defined hardware profile can be found under Generic Phones and Tablets.
I've just discovered how. You need to create a user defined device definition in Android AVD - I created one that was 480 x 4000 pixels. Exit and restart Android Studio and then you can select this device in the preview renderer and can see 4000 pixels worth of LinearLayout.
Found a easier solution, in your layout add something like below and adjust as you need:
android:layout_marginTop="-1500dp"
in new version of android studio (i don't know from which version, but i'm using latest 2.3.3 now) you can resize your preview layout by drag it
Android Studio will offer a toggleable full view, if the ScrollView is the root of the layout. I had put mine on top of the default RelativeLayout and had to manually fix that in the XML before the GUI would give me the full view.
I hope the screenshot help you :
You can use scrollY attribute to scroll the ScrollView in the preview. Using scrollY attribute with tools namespace will only scroll the view in the display and not in the actual app. And make sure you use px as the unit with scrollY attribute.
tools:scrollY="150px"
Update: Now you can scroll a ScrollView directly inside preview panel
(I tested on Android studio version 2.3.2)
Short answer: Right click on your ScrollView and select Refactor > Extract > Layout.
Android-studio will extract your ScrollView into a new layout file and put tools:showIn="#layout/original_layout_file" attribute at root layout(your ScrollView).
Note: Android-studio will preview full layout if root layout is ScrollView.
In Android Studio version 3.2.1 you can select "Custom" device on preview panel and then resize preview to whatever height you need.
click on the scrollView in conponent tree (on the right)
click on the new button wich appears on the left (second row of toolbar)
Another quick alternative solution is to hide the layouts temporarily at the top so that the layouts at the bottom can appear in the visible window in Android Studio.
Put android:visibility="gone" on the layouts that you want to hide.
Example:
<HorizontalScrollView
android:visibility="gone"
android:id="#+id/hsv02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/ll24"
android:layout_below="#+id/ll199">
</HorizontalScrollView>
I might be late but it'll be a lot easier for you to just set the margins to negative to shift the scroll view to the left.
For example:
margin:
left: -100px
Now you can see and edit the scrollview however you like!
TABLE LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<TableRow android:padding="5dip">
<TextView
android:layout_height="wrap_content"
android:text="New Product Form"
android:typeface="serif"
android:layout_span="2"
android:gravity="center_horizontal"
android:textSize="20dip" />
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Product Code:"
android:layout_column="0"/>
<EditText
android:id="#+id/prod_code"
android:layout_height="wrap_content"
android:layout_column="1"/>
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Product Name:"
android:layout_column="0"/>
<EditText
android:id="#+id/prod_name"
android:layout_height="wrap_content"
android:scrollHorizontally="true" />
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Product Price:" />
<EditText
android:id="#+id/prod_price"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<Button
android:id="#+id/add_button"
android:text="Add Product"
android:layout_height="wrap_content" />
<Button
android:id="#+id/cancel_button"
android:text="Cancel"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
GridLAYOUT
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="2"
tools:context=".Main3Activity" >
<Button
android:id="#+id/button3"
android:layout_column="0"
android:layout_gravity="fill_horizontal"
android:layout_row="0"
android:text="Button"
/>
<Button
android:id="#+id/button1"
android:layout_column="1"
android:layout_gravity="fill_horizontal"
android:layout_row="0"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_column="2"
android:layout_gravity="fill_vertical"
android:layout_row="0"
android:layout_rowSpan="2"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:layout_row="1"
android:text="Button" />
</GridLayout>
ANOTHER TABLE LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<android.widget.TableLayout 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"
android:stretchColumns="1"
tools:context="com.example.dhanya.uitablelayout.MainActivity">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textClock"
android:layout_column="1" />
</TableRow>
<TableRow>
<TextView
android:text="First Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0" />
<EditText
android:width="200px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView
android:text="Last Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0" />
<EditText
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2"
android:id="#+id/ratingBar"
android:layout_column="0" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_span="2"
android:id="#+id/button"
android:layout_column="0" />
</TableRow>
</android.widget.TableLayout>
The best I found is to set the device to custom. Then we can change the layout simply by dragging.
My layout is long and need to scroll layout even using the vertical long resolution and when I set the layout editor to high resolutions, the screen does not scroll completely and smoothly
By setting the layout to
3.2_HVGA_slider_ADP1_API_27, the screen scrolled and my problem was solved

Scaleable size of application with non 9-patch buttons

I'm currently learning android development, and before I get too stuck in to a large project, I decided I would need to learn about making an application accessible from as many devices as possible.
So I have a test application using RelativeLayout. The top-level activity has 6 large menu buttons on it. These buttons are square graphical images (not 9-patch buttons as they are - to my knowledge - too graphically primitive). On the device I'm using for testing, these buttons appear in a perfect 2x3 arrangement like such:
However, when I try and run this application on a larger device, the buttons will appear like so:
Is there a way to scale non-9-patch buttons based on the size of the screen, so that they will always appear like the first image? Is this recommended? If not, is there an alternative way of doing such a layout for different screen sizes?
My ideal layout would be scalable across different devices, like so:
I am using similar menu. And here is first row of it. The buttons in this menu has labels too.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:gravity="center" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip" >
<Button
android:id="#+id/screen_home_btn_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/selector_ic_my_profile" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="5dip" >
<TextView
style="#style/label_text_style_home_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/screen_home_label_my" />
<TextView
style="#style/label_text_style_home_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/screen_home_label_profile"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip" >
<Button
android:id="#+id/screen_home_btn_application"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/selector_ic_my_application" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="5dip" >
<TextView
style="#style/label_text_style_home_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/screen_home_label_my" />
<TextView
style="#style/label_text_style_home_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/screen_home_label_application"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
It seems you are giving outer margins to the buttons. Align them in center and give spaces between 2 buttons and not the screen border & buttons.

How to scale a drawable inside a button with text with XML

I have set up some buttons with icons on the left and text on the right. Everything scales with screen size (phone tablet) but the problem is that the button is just the text. I want the button to be the entire line so that the button that has focus lights up correctly (I use a remote).
This code works except for the button is just the text
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onSetTargets">
<ImageView
android:id="#+id/imageView1"
android:layout_width="#dimen/menu"
android:layout_height="#dimen/menu"
android:layout_marginTop="#dimen/padding_small"
android:src="#raw/target"
android:onClick="onSetTargets"/>
<Button
android:id="#+id/set_Targets"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonselector"
android:ellipsize="none"
android:gravity="left"
android:onClick="onSetTargets"
android:singleLine="true"
android:text=" Targets "
android:textColor="#android:color/white"
android:textSize="#dimen/menu" />
</LinearLayout>
This is just a section within a linear layout and there are then several buttons in a row.
Following is what I would like to get working but I can't figure out how to scale the drawable. I know there is a scaleDrawable command but I can't find any posts on how to make it work in the XML other than people complaining that it doesn't work and getting solutions for dealing with it in java.
<Button
android:id="#+id/set_start_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonselector"
android:gravity="left"
android:onClick="onStartTime"
android:text=" Timer"
android:textColor="#android:color/white"
android:textSize="#dimen/menu"
android:drawableLeft="#raw/hourglass"/>
This is what I came up with that worked. The overall page is a linear layout with buttons arranged vertically on the page. Each button is a vertical linear layout consisting of an image and some text. All elements of the vertical layout are clickable so clicking on the image or the text does the same thing. Here is an example for a single button. The size #dimen/menu is defined for both phone and tablet sizes and the image will scale to match the specification.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onSetTargets">
<ImageView
android:id="#+id/imageView1"
android:layout_width="#dimen/menu"
android:layout_height="#dimen/menu"
android:layout_marginTop="#dimen/padding_small"
android:src="#raw/target"
android:onClick="onSetTargets"/>
<Button
android:id="#+id/set_Targets"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/buttonselector"
android:ellipsize="none"
android:gravity="left"
android:onClick="onSetTargets"
android:singleLine="true"
android:text=" Targets "
android:textColor="#android:color/white"
android:textSize="#dimen/menu" />
</LinearLayout>

One game layout

I want to make a match game layout in android.
I'm having problem with making my layout compitable to all screen resolutions.
What I want, is an image icon of the home team on the left, a strechable home team image name, home team score text, seperator, away team score text, a strechable away team image name, an image icon of the away team on the right.
The score should be in the middle. I just can't get it right. before this try, I've tried to divide the main layout to 3 layouts: home team layout, score layout and away layout - with no success of getting the expected result.
Can someone please help ?
Here is my piece of code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/GamesBGColor" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/homeTeamIcon"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/holonlogo" />
<ImageView
android:id="#+id/homeTeamText"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/holontext"
android:layout_width="115dip"
android:layout_marginLeft="3dip" />
<TextView
android:id="#+id/homeTeamScore"
android:layout_width="25dip"
android:layout_height="wrap_content"
android:text="123"
android:textColor = "#color/purple"
android:layout_gravity="center"
android:textStyle="bold"
android:layout_marginLeft="5dip" />
<TextView
android:id="#+id/seperator"
android:layout_width="10dip"
android:layout_height="wrap_content"
android:text="-"
android:layout_gravity="center"
android:layout_marginLeft="7dip" />
<TextView
android:id="#+id/awayTeamScore"
android:layout_width="25dip"
android:layout_height="wrap_content"
android:text="122"
android:textColor = "#color/red"
android:layout_gravity="center"
android:textStyle="bold"
android:layout_marginLeft="7dip" />
<ImageView
android:id="#+id/awayTeamText"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/jerusalemtext"
android:layout_width="115dip"
android:layout_marginLeft="3dip"/>
<ImageView
android:id="#+id/awayTeamIcon"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/jerusalemlogo" />
</LinearLayout>
</LinearLayout>
If you want your layouts to work on different resolution screens your should be sizing them using dip as the unit. For example:
android:layout_marginRight="10dip"
dip stands for Device Independent Pixel and using these in your layout means that Android will automatically scale your layout depending on which display the device running your application has.
You can read about these in the Supporting Multiple Screens page in the Android Developer Documentation. This document also has some other options for handling different displays but I think using dip is probably the easiest.

Categories

Resources