I have a ListActivity that I'm trying to inflate a Button from this layout file:
<?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:id="#+id/add_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/add_prop"
android:layout_alignParentBottom="true"
/>
</LinearLayout>
Using this code:
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.addFooterView(footer, null, false);
The problem is that since the list isn't so big that takes the entire screen (at least I think this is why) the button stays at the end of the list instead of sticking to the bottom because of the android:layout_alignParentBottom="true" property. What should I do to make it stick at the bottom?
You have to create a new layout file with this code:
<?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" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/add_button" />
<Button
android:id="#+id/add_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/add_prop"
android:layout_alignParentBottom="true" />
</RelativeLayout >
Then set it as content view of your Activity using setContentView()
Related
fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/movies_fragment_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MoviesFragment">
<ListView
android:id="#+id/lvMovies"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#efefef"
android:layout_weight="100"/>
<Button
android:id="#+id/bLoadData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load api data"
android:layout_weight="1"/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include layout="#layout/fragment_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
onCreate from MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.movies_fragment_ll, new MoviesFragment())
.commit();
}
}
As you can see from the screenshot, the code is meant to display what's on the left, but what it's actually displaying is different. I've tried to play around with the XML and java but it keeps doing that. In some instances, depending on how I arrange the ListView and Button on the screen, I sometimes get two Buttons instead of one.
Why?
To clear the confusion.
Data is being added to the ListView.
Data is added on button click.
But before the button is clicked, and data is added to the ListView.
How do I make the ListView height finish just where the button starts at the bottom. To make the layout look like the one in the XML preview.
android preview shows a list with child.But at runtime you are not adding any element in the list view , so list view is empty . in that case your button moves to up.
if you want your button to stick in bottom use Relative layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/movies_fragment_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MoviesFragment">
<Button
android:id="#+id/bLoadData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load api data"
/>
<ListView
android:id="#+id/lvMovies"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#efefef"
android:layout_above="#id/bLoadData"
android:layout_weight="100"/>
</RelativeLayout>
the list wont be visible if you dont have any data in that list. So that why the button will be shown on the top because listview's height will be 0.
Set some data to the list, and use relative layout to manage the location of the button at bottom
Well change it like This,and Please add some data to adapter
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/movies_fragment_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MoviesFragment">
<ListView
android:id="#+id/lvMovies"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#efefef"
android:layout_weight="100"/>
<Button
android:id="#+id/bLoadData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load api data"
android:layout_weight="1"/>
</LinearLayout>
I'm updating an app someone made a couple of years back and I'm trying to add a footer visible all the time. I'm working on adding it on one of my activity, but here's the result:
Here is my .xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Footer -->
<include layout="#layout/footer"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/footer">
<TextView
android:id="#+id/list_items"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="22dp" >
</TextView>
</RelativeLayout>
And here's my line that calls it
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.list_items, mRegions));
Thanks in advance!
I am using following configuration to properly fit image inside a scrollview.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" >
<ImageView
android:id="#+id/textNimbo"
android:layout_width="match_parent"
android:layout_height="2492dp"
android:layout_alignParentRight="true"
android:src="#drawable/inicio"
android:adjustViewBounds="true" />
</ScrollView>
However I need to set also a button inside scrollView and below the ImageView. I tried relative and linear layouts inside scrollView but no succes, button is not visible or image doesn't fit to scrollView. Any recommended configuration? Thank you.
A ScrollView can only have one direct child element. So to add a Button and an ImageView, you'll need something like this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" >
<LinearLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android"layout_height="fill_parent">
<ImageView
android:id="#+id/textNimbo"
android:layout_width="match_parent"
android:layout_height="2492dp"
android:layout_alignParentRight="true"
android:src="#drawable/inicio"
android:adjustViewBounds="true" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
You could also use ImageButton I
instead of a ImageView with a transparent Button on it
I have an ListView element in my layout,with other views:
...
<ListView android:layout_height="wrap_content" android:id="#+id/listView1" android:layout_width="match_parent" android:visibility="gone" android:></ListView>
...
when the Activity is created I fill the Listview with an Array of values:
ListView list_ultime_ricette=(ListView) findViewById(R.id.listView1);
list_ultime_ricette.setVisibility(android.view.View.VISIBLE);
list_ultime_ricette.setAdapter(new ArrayAdapter<String>(Home.this, R.layout.list_item, ricette_name));
And this is list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp" >
But when I try the whole, I see only the first element of my array, does anyone know why?
Don't put ListView inside of a ScrollView :)
Try changing list_item.xml to be this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" >
Only change is settings layout_height to "wrap_content" instead of "fill_parent".
Don't put Listview inside a ScrollView
I am trying to show a list view with calendar view in a linear layout. when i am using vertical layout then list view appears but in horizontal layout the same listview disaappears. I solved the problem using relative layout, but can i do this using linear layout.here is my xml...
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.exina.android.calendar.CalendarView
android:layout_height="wrap_content"
android:id="#+id/calendar" android:layout_width="wrap_content">
</com.exina.android.calendar.CalendarView>
<ListView android:layout_height="fill_parent" android:id="#+id/android:list"
android:layout_weight="1" android:scrollingCache="false"
android:layout_width="fill_parent" android:drawSelectorOnTop="false"
android:dividerHeight="4.0sp"></ListView>
</LinearLayout>
</ScrollView>
You need to set the fillviewport flag in the scrollview :
android:fillViewPort="true"
Check this link to know what fillviewport flag does: ScrollView