Android: Buttons swapping place at runtime in horizontal, weighted LinearLayout? - android

I'm using a horizontal LinearLayout to divide a rectangular block of the device's screen into 3 equal parts. This is accomplished using weights.
The purpose of this is putting 3 buttons side by side, a settings button, a help button, and a contact button. As per my knowledge, it should follow that the first button in the LinearLayout is on the left, the next is in the middle, and the last is on the right, due to the hierarchy rules of a LinearLayout.
This is what the Visual Editor also shows. But when I run the application (and I've tried on multiple devices), the contact button ends up on the left, the settings button ends up in the middle, and the help button ends up on the right.
I tried changing the hierarchical structure of the code (i.e. I placed the code for contact button above the code for settings button, etc.) but it didn't make any difference to the result.
The code for the LinearLayout is below:
<LinearLayout
android:id="#+id/icons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100">
<ImageButton
android:id="#+id/settings"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="33"
android:src="#drawable/settings" />
<ImageButton
android:id="#+id/help"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="33"
android:src="#drawable/help" />
<ImageButton
android:id="#+id/contact"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="33"
android:src="#drawable/contact" />
</LinearLayout>

There is nothing wrong in the XML. If the application is really using the above XML, then changing the ordering of the ImageButtons in the layout should indeed change the ordering on the screen.
Here's 2 things that come to mind:
There is a problem with your build/run process, and updated code changes are not reflect when you run your application. I suggest making a change to some obvious string resource and test if this is the problem. You should also do a clean build just in case.
You have multiple layout files with the same name (for example, one in layout/ and the other in layout-land/), and you're not changing the right one.

Related

UI of my app gets broken when testing on a phone [Android]

I am trying to do some simple app to practice, and the user interface gets broken for some reason I don't know why when I run test on my phone. I did an app on android only once before and I didn't have this problem, I was using a different phone though. I'm testing this on Samsung Galaxy A5.
That's how it looks in project: http://imgur.com/Pnbg5ns
And that's how it looks on my phone: http://imgur.com/a/uki84
Anyone knows how to resolve this?
All your views have locations set with the tools:... attribute. The locations set this way (using tools:) position the views within the Android Studio editor, and Android Studio editor only. It doesn't do anything at all to position the views for when the app is actually run, that's why all your views are on top of each other, they simply don't have any attributes to indicate their positioning on the screen when the app is run.
You should check tutorials for how to use ConstraintLayout (if you want to use that layout) which is a recent (2016) addition to Android and position your views with the constraints you need. Or you could use some of the older layouts like LinearLayout which should be perfectly fine for your layout needs.
And absolute positioning of the views like you have with the use of the tools:... attribute is also a big no most of the time. Views need to be positioned in some relative way within a layout, which doesn't mean it has to be within a RelativeLayout :), just saying that the views should be positioned in reference to the layout containing them, not just at some absolute point f.e. (150, 110).
This is a very simple layout. You can use LinearLayout instead of ConstraintLayout.
Here is an example using LinearLayout:
<?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"
android:layout_margin="16dp">
<EditText
android:id="#+id/etLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Login"
android:inputType="textPersonName" />
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Hasło"
android:inputType="textPassword" />
<Button
android:id="#+id/bLogin"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Loguj"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="#+id/tvRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Nie masz jeszcze konta? Kliknij tutaj."
android:layout_gravity="center_horizontal"/>
</LinearLayout>
OUTPUT:
Hope this will help~

Changing the Size of my ListView by one Pixel messes up Formatting

Let me show you two links to demonstrate my point:
http://i.snag.gy/QP1i2.jpg (The ListView is 60 pixels)
http://i.snag.gy/DvXsL.jpg (The ListView is 61 pixels)
The whole file is done with an outer Vertical LinearLayout to provide weighted percentages (e.g. the ListView is 75%, the search part is 4%, so on and so forth) independent of device screen size. Within that outer LinearLayout I have a nested Layout (Linear or Relative) depending on my needs for the particular row.
It works beautifully, except for the last part. And it seems a lot of the items I try adding end up messing the formatting so I'm not sure if the problem is how I'm doing the ListView. Anyways, here's the relevant code:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:background="#null"
android:layout_weight="0.75"
android:paddingLeft="0dip" >
<ImageView
android:id="#+id/searchdivider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:contentDescription="#string/desc"
android:src="#drawable/searchdivider" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/searchdivider" >
</ListView>
</RelativeLayout>
Thank you for your help!
EDIT: I should add that I don't want the height to be 60dp, obviously. I want it to fill_parent; however I picked the arbitrary value of 60 and 61 to figure out exactly what change makes the layout mess up.
2nd EDIT: I think I figured out the problem. It seems when I make the Theme AppCompat, it looks and acts fine. But when I make the Theme NoTitleBar (with or without fullscreen) then it acts all screwy. I haven't changed it in the manifest but rather the place in the graphical layout that lets you modify it for that one activity only.
Any suggestions?
android:gravity="left" on imageView and android:gravity="right" on the other listView

Selecting best Android layout

I am new to Android, and wish to do a layout as below:
A Logo on top.
Following with a Rectangle with Rounded corners
Within that Rectangle, I will have two EditText box for User ID and Password, plus one Login button
Below the Rectangle with Rounded corners (outside) I have a Html Link to Terms & Conditions
I have tried various ways of layout out
Using only layout. Different kinds of layouts. All seems to be very difficult to achieve what I need
Using Layout + Background. The background is not really a background, but is more like a template, it will affect your layout, and is very difficult to control where you wants your control located.
Using onDraw. Flexible but worried that it might have problem with different screen sizes.
So, someone please enlight which is the best way to achieve what I need?
No one can really tell you what is best, it depends on exactly what you want but I would suggest using a RelatvieLayout as they are typically the easiest and most efficient to use once you work with them a little, in my opinion. You can read Here to see how to do the rectangle. You basically will use shape drawable and adjust the radius of the corners.
As far as the logo on top, if it will be reused in other Activitys then you can put it in its own layout and use the include tag in your layouts to reuse the logo layout
If you are worried about different screen sizes then read the Docs and find what works for you.
Just start on it and adjust as you go. Don't be afraid to screw up and redo some of it. Hopefully this is enough information to get you started
Using a RelativeLayout will give you more flexibility and allow you to use less Layouts such as nested LinearLayouts and Layouts with only one child which can improve performance
this is how it should be done:
start with linear layout with vertical orientation :
<linearLayourt xmlns=............
android:orientation="vertical"
.....other stuffs goes here
......
.....
<LinearLayout ......this is the child linearlayout
.....other stuffs goes here like width and height
<ImageView ...this is where you are gonna put your logo in
/>
</LinearLayout> ....close your child linear layout
<RelativeLayout ...
.........other stuffs here
<EditText ....1st edit text
...you position your boxes here
/>
<EditText ....2nd edit text
...you position your boxes here
/>
</RelativeLayout>
<TextView
....
...
...put yout hyperlink for this text
/>
</LinearLayout> ...this is the parent linear layout
For your case of creating a Log in screen it's not really matter as it is a relatively easy screen to design. I personally like to use XML to design my layouts and never seen it done using the onDraw method.
My suggestion to you as #codeMagic said is to learn how to use and manipulated RelativeLayouts,as those will prevent you from creating cascaded layouts that are really not recommended and take long time to load.
When I started to program for Android I found LinearLayout to be the easiest to understand and use but using it would bring me to many LinearLayouts inside of a LinearLayouts on complex screen designz, later with the use of RelativeLayout I realized that in most cases one RelativeLayout can replace many cascaded Linear ones.
in your case you could do some thing like that:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/drop_down_icon" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageView1" >
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/editText1" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:text="TextView" />
</RelativeLayout>
All what left is to add the desired padings and margins.

Android GUI: what are the good UI patterns for entering an integer number?

There are two numbers constantly displayed in my UI. I need the user to be able to specify those numbers. In a desktop app I simply use 2 spinbuttons, and can beat it (I guess). However, there are problems with this in Android (I am targeting Android 3-based tablets).
First of all, I've tried NumberPicker widget, but it simply does not show itself properly (I've asked a question about it, but no response so far).
Then I've tried some 3rd-party numberpicker implementations one cand find on the Web, but for various reason didn't like neither of them. Some of them are too large to fit the side panel I'm squeezing controls into, some are just ugly (and too small, probably), and all of them automatically grab input focus causing the keyboard to show every time I open this side panel.
There is another option - only show the uneditable label with a number on the panel, and show popup window with some numberpickers in it. Apart from me not knowing how to implement such a popup (just learning), how do I decorate the label so that the user understands he can tap it to edit?
Perhaps, there are better solutions for this problem? I took a look at some apps from Google Market, and they all seem to either use huge NumberPickers at the center of the screen (and I need as much of the screen as possible clear), or a popup with the same huge Numberpickers I don't particularly like.
Can you suggest anything else? Thanks in advance!
There are two different issues in your question:
For the number picker for eaxmple in my case I have created a simple tool. You can just put a button with the text "-" then a textview and then a button with "+". You put it in a linear layout and t can be vertical or horizontal. If you want it you can make it very small. For example here is what I have wrote but you can try playing with the options to see which is better for you.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical" android:id="#+id/linearLayout1"
android:layout_width="wrap_content" android:layout_weight="1"
android:layout_height="wrap_content">
<Button android:id="#+id/button22" android:layout_gravity="center"
android:layout_weight="1" android:text="+" android:gravity="center"
android:layout_width="30dip" android:layout_height="30dip"></Button>
<Button android:id="#+id/button11"
android:layout_gravity="center"
android:layout_weight="1" android:text="-"
android:gravity="center" android:textAppearance="?android:attr/textAppearanceSmall" android:layout_height="30dip" android:layout_width="30dip"></Button>
</LinearLayout>
<LinearLayout android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" android:orientation="vertical">
<Button android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="30dip"
android:text="0" android:id="#+id/quantidad"></Button>
</LinearLayout>
then you can add onClick listeners on the buttons and setup to increase or decrase the text displayed in the text view.
As for the keyboard well if you use that example you not need it but if you use an edittext then you can setup an option for the keyboard to be hidden in the manifest: you can see the option here http://developer.android.com/guide/topics/manifest/activity-element.html

Android EditText won't take up remaining space

In my Android app, I have a tabbed Activity. In one of the tabs I have two TextViews and two EditTexts.
The first EditText is only one line, and that's fine. However, I want the other EditText, android:id="#+id/paste_code", to take up the remaining space, but no matter what I do to it, it will only show one line. I don't want to manually set the number of lines, since the number that would fit on the screen differs based on your device.
Here's the relevant code. It's nested inside all the necessary components for a tabbed Activity.
<ScrollView
android:id="#+id/basicTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Paste title"
android:layout_weight="0" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/paste_title_hint"
android:id="#+id/paste_title"
android:lines="1"
android:gravity="top|left"
android:layout_weight="0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Paste text"
android:layout_weight="0" />
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="#string/paste_hint"
android:id="#+id/paste_code"
android:gravity="top|left"
android:layout_weight="1" />
</LinearLayout>
</ScrollView>
Since the accepted answer doesn't address the situation fully, here's a proper fix for people coming to this while searching:
Firstly, Romain Guy from the Android dev team addresses this well in this blog post:
http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/
Essentially, your ScrollView needs to contain the android:fillViewport="true" attribute.
If things aren't working once you've done that, here are a couple of things to check:
The layout inside the ScrollView (such as a LinearLayout) needs to have layout_height="wrap_content"
The view(s) you want to expand should have layout_height="wrap_content"
The view(s) you want to expand should have layout_weight="1.0" or similar
Don't forget to set minLines="3" or similar in the view(s) you want to expand if you don't want it/them to shrink too much.
The problem seems to come from your use of ScrollView. I've tested your code using a ScrollView as the parent container, and got the same problem. However if I replaced the ScrollView with a LinearLayout, then the second EditText properly expanded to fill the whole screen. The problem must be that ScrollViews are designed to wrap to their smallest possible size, regardless of what settings you put in android:layout_height. I experimented with another few layouts, e.g. a RelativeLayout using layout_above and layout_below, but those only affected its maximum size, not its size when empty. Unfortunately, that means I'm not sure how to solve your problem... Is there a way you can redesign your layout to use something other than the ScrollView as the parent container?

Categories

Resources