android layout question - android

Currently I'm working on a dialog which consists of title, description, tags and footer. The title can be long and in this case the text should automatically be displayed in multiple lines. The description is also longer and should fill multiple lines.
At the bottom of the dialog has to be the footer (also if the title and description don't fill whole screen).
I tried to do create the layout described about but had a problem with long text - if the content is long it doesn't display multiple lines but extends main view (LinearView) so the content extends over the visible area.
Here I'm pasting the print screen of the current state and the mockup of the desired layout:
alt text http://img52.imageshack.us/img52/9697/androidscreenshot.png
alt text http://img69.imageshack.us/img69/3609/screenshot20091227at716.png
TextView "Footer" and buttons OK and Cancel should appear at the bottom of the screen and the title ("Title Title Title...) and description text should automatically appear in multiple lines instead of extending the parent view.
I would really appreciate if anyone of you could give a tip about solving these layout issues.
Thanks!

Something like this should help: placing the text elements up top, and forcing the buttons to the bottom of the layout using weightSum. I can't remember the settings for multi-line text offhand, but if you're not using the singleLine attribute set to true, then things should work out ok.
Check the XML attribute documentation for more info.
<?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="fill_parent"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#id/android:title"
android:layout_width="fill_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2">
<Button
android:id="#+id/btn_ok"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/btn_ok" />
<Button
android:id="#+id/btn_cancel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/btn_cancel" />
</LinearLayout>
</LinearLayout>

Related

Why does the visibility "gone" for button not work with scrollviews?

I have a layout like:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:scrollbars="vertical"
android:layout_weight="1">
<-- A relative layout that contains a lot of edit text fields and a few text views at the bottom that cover up the screen-->
</ScrollView>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="something"
android:layout_weight="0"
android:visibility="gone"/>
</LinearLayout>
and what i want to do is just achieve the effect that when the keyboard pops up from tapping on any EditView inside the ScrollView, the button at the bottom should NOT cover up any text views, because it is in the gone state. but right now, whenever i tap on any of the EditText fields, it seems to be covering up some of the text views that i have.
why is the screen rendered such that it designates space for the gone button i have at the bottom?
Try adding the following to the activity tag in your manifest.
android:windowSoftInputMode="adjustNothing"

Layout two text views

I have to put two text views in one line. Second one should be placed next to first one (as shown in figure 1).
My problem is: text of first text view may be long so I need to ellipsize it. It should take as much space as needed to make it possible to read the whole text in second text view. Figure 2 shows it.
Is it possible to achieve that using Android built-in layours or I need to write my custom view?
Create an inner layout and use the android:weightSum="100" attribute. Then place two text views insode this inner layout and add the attribute android:layout_weight="50" to each textview!
Try something like this
<?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="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:text="A long long text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A long text" />
</LinearLayout>
Regards

Android, how to add icon *and* text to a button, programmatically?

I need to add an icon and some text to a button, in code (not xml), in my Android app.
The icon (a stock icon, "expander_open_holo_light.9.png") should be on the left and the text on the right.
I can't find any clue...
Take a look at setCompoundDrawableWithIntrinsicBounds(). You can find the info here:
http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)
Basically, that is the way to set the android:drawableLeft property of your button programatically.
If you aren't doing much with the button (like resizing, instantiating a few of them) i would just create a custom xml view with an imageview and textview side by side contained in a container.
If you are changing the picture and text often, create a custom ImageView or Button class.
E.G
textbutton.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/base"
android:orientation="horizontal"
android:layout_width="200dp"
android:layout_height="100dp"
android:gravity="center"
android:background="#ff623466">
<ImageView android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:src="#drawable/icon"
android:gravity="center"
/>
<TextView android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Hello!"
android:gravity="center"
/>
</LinearLayout>
Then post this line in main.xml or any other layout you want the button:
<include layout="#layout/textbutton" />
Take a TextView and set ImageView as the background.After setting image, append text to it. Text will be moved to right. Hope it helps.

How to get UI to look right

I am having a problem getting the ListView to display properly. It currently looks like this with the following xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/favs_main">
<Button
android:text="Return to Home"
android:id="#+id/return_button"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:textSize="15sp"/>
<ListView
android:id="#+id/favsListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:layout_above="#id/return_button"/>
</RelativeLayout>
If you notice the list is down on the screen. I want it to be just below the favorites text instead of just above the return to home button. The catch however is that I always want the button to show and the list view to just occupy the space between the favorites text and the button. The text is from the background image so I can't just align below that. So even with 100 items I would still like to show the button.
Thanks for the help
If the word "favorites" is part of a background image as suggested in the RelativeLayout's background attribute, then you won't be able to align an element below it without using hacky margins or something to that effect. If you want to align an element below the word, separate that into a different ImageView and set the layout_below of the ListView to the id of that ImageView. To get an element to align properly in between two other elements, use a combination of layout_above and layout_below.
Couldn't you just align the ListView to the Parents' Top and set a margin for the ListView so that it is below the Text of the Background?
Also you could change the background to provide the Text in an ImageView and align the ListView to be below the ImageView.
Instead of trying to make a persistent View always show up under the ListView and align it (which you can do, see other suggestions), you might want to take a look at using a footerView:
http://developer.android.com/intl/de/reference/android/widget/ListView.html#addFooterView
"Add a fixed view to appear at the bottom of the list."
Note that it can be another layout too if you eventually need to do more than just one Button.
this my listview which have multiple entries and textview and button fixed in the botton. i haven't inserted background. try this hope it will help.
http://www.techuv.com/layout-with-butoon-and-textview-fixed-in-bottom/
You could use a simple LinearLayout and use the weight attribute on the ListView :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/favs_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ListView
android:id="#+id/favsListView"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"/>
<Button
android:text="Return to Home"
android:id="#+id/return_button"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:textSize="15sp"/>
</LinearLayout>

Nested Linear layout only shows first view after being set from gone to visible in Android

I am developing an Android app but I'm still pretty new. I want to have a button, and when you push that button, a few TextViews and Buttons will appear. So I have a main linear layout, and then another linear layout nested inside containing the things I want hidden. I have the nested linear layout set to android:visibility="gone".
The problem I am having is that it only shows the first item inside the hidden linear layout instead of all of them. The way I try to make it appear is
vgAddView = (ViewGroup)findViewById(R.id.add_details);
btnAche.setOnClickListener(new OnClickListener(){
public void onClick(View v){
vgAddView.setVisibility(0);
}
});
My XML file is this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:text="#string/but_stomach_ache"
android:id="#+id/but_stomach_ache"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="#string/but_food"
android:id="#+id/but_food"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/add_details"
android:visibility="gone">
<TextView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/when_happen">
</TextView>
<Button
android:text="#string/happen_now"
android:id="#+id/happen_now"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
Your TextView in the LinearLayout is set to android:layout_width="fill_parent" and android:layout_height="fill_parent", which means it will take up the entire space of the LinearLayout, leaving no room for the Button. If you use the hierarchyviewer tool that shipped with the SDK, you can see that when you look at the activity.
You need to set the height of the TextView to be wrap_content or otherwise have it leave room for the Button.
After you are setting its visibility to true set like below:
vgAddView.setVisibility(View.VISIBLE); - to show it
vgAddView.setVisibility(View.GONE); - to hide it
You first TextView has a layout_width="fill_parent".
It's mean,that you TextView don't leave space for Button.
You may try to set layout_weight attribute or set layout_width to wrap_content

Categories

Resources