How to make a list of textviews? - android

Basically here is a mspaint demonstraiton of what I want to accomplish:
I do not want the outline to be visible to the user, it was just to give you an idea of what I want to do. I want new textviews to take position under each other whenever a new goal is added from the edittext-button combo on the bottom part of the GUI.
I know how to save the data - SQLite. But I have no idea how to make new textviews be on the bottom of the last one - and if there is no more room on the screen I want that textview part to be scrollable.
I don't know what I need to use to accomplish this.
Thanks in advance!

create two xml files.
One consisting of list view
<?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:background="#drawable/bkgrnd_320x285">
<ListView
android:id="#+id/List1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#000000"
android:dividerHeight="1dp"
android:fastScrollEnabled="true"/>
</LinearLayout>
and another xml for just the textView eg. row.xml
<?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:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Name"
android:textSize="20dp"
android:layout_marginTop="20dp" />
</LinearLayout>
Then in the main activity u need to refer to both the xml files as follows
ListView ll = (ListView) findViewById(R.id.List1);
ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.row,R.id.textView2, arraylist_you_want_to_display);
ll.setAdapter(arrayAdapter);
where R.layout.row is the row.xml consisting of textView
And R.id.textView2 is the id of the textView present in the row.xml

You may create a LinearLayout, setting the orientation to vertical.
Adding a ScrollView or ListView to show the upper part, with android:weight=1
Add the EditText to the LinearLayout, and this is done.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/List1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weight="1"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

Related

adding button to ListActivity

I'm developing and android application in which I have created a ListActivity which contains an ImageView and a TextView. There is also the ability to drag and drop those objects so that they can be moved. What I want to do now is to have a button at the bottom of the screen,not the list.
As the user scrolls the button will always stay there. I found this link but when i did what this link suggested no button did appear, and instead i got a huge blank space that takes over a quarter of the screen. Can anyone point me to the right direction?
ok so here is the .xml file that is set to the ListActivity
<?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="wrap_content">
<DragNDrop.DragNDropListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</DragNDrop.DragNDropListView>
</LinearLayout>
hopefully it can help
Your view should be like this:
<?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" >
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button"
android:layout_centerHorizontal="true"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/btn"/>
</RelativeLayout>
Populate the listview with the data and see the button will be on its place though list being scrolled.
I think that ListActivity is a bad practice.
What you need to do is to is to change the Activity from ListActivity to Activity. In the layout file, do the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_contant"
android:orientation="horizontal">
<DragNDrop.DragNDropListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_contant">
</DragNDrop.DragNDropListView>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
In on create you can get the list reference by doing:
ListView lv = (ListView)findViewById(R.id.list);
In order to make the button clickable, user OnClickListener

How to manipulate the ListView and layout in the android xml

Im trying to manipulate the to do the following but its so far just lead to frustration... following is the poorly draw picture of what I am trying to accomplish:
http://i.stack.imgur.com/WZOta.png
http://i.stack.imgur.com/6cEQk.png
<?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"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<ListView
android:id="#+id/android:list"
android:layout_width="wrap_content"
android:layout_height="419dp" >
</ListView>
<TextView
android:id="#+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/FAP001"
/>
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="+1" />
</LinearLayout>
</ScrollView>
Im not sure if the listview is the way to go.. heck im not even sure if linearlayout is a good call.. Im trying to have end result close to what is on the 2nd image.
THANKS IN ADVANCE!!!
You can't place an ListView within a ScrollView. Thats not possible. Because then the ListView has got two options to scroll trough the content, as you will. And this won't work.
For more Information:
https://groups.google.com/forum/m/?fromgroups#!topic/android-beginners/LRpLgGOy2Pc
If you want to have a ListView with 3 "sections" I would recommend you to create your own custom ListView with a Custom Adapter.
Here is a an Example of an custom ListView
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
You can just take this ListView and just have to modify the Adapter and the Row XML to your own needs.

have a TextView with a layout determined a ListAdapter

I have a screen where I just simply display the list of items. I have used setListAdapter(new ArrayAdapter<String>(ClassListActivity.this,android.R.layout.simple_list_item_1,getItems));
Now we dont have to create a separate xml layout for this right. But now I want to have a TextView that is the heading of this list may be saying "myItems"..When I try to link a xml layout with this activity, my application stops. and when I try to create a dynamic TextView,
this way TextView tv = new TextView(this);
and set text in it later. I dont get any errors but I get no results too just the old list no heading..
what should I do? thanks
You have to create your own XML layout for the ListAdapter. Put the TextView as the heading at the top followed by a ListView widget with the id #android:id/list.
See more details here: ListActivity
This one should work:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="My list"/>
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>

Prevent ListView from scrolling

I have a scrollview with a linear layout in it. Within that linear layout there is a list view. The problem I am having is that my list view only has a certain amount of space and then becomes scrollable. I don't want this to happen. I want the entire list to be shown. Does anyone know how I might achieve this? I have tried using this link however my layout does not appear when I add the footer. ListView in ScrollView potential workaround This is my code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="vertical"
android:background="#drawable/darkimg"
android:weightSum="1">
<LinearLayout
android:id="#+id/numbersLL"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/round_rectangle"
android:layout_margin="4px"
android:paddingLeft="6px"
android:paddingTop="3px">
<TextView
android:id="#+id/numberPrint"
android:layout_height="wrap_content"
android:textSize="14px"
android:textStyle="bold"
android:textColor="#color/white"
android:layout_width="fill_parent"
android:text="#string/numberPrint">
</TextView>
<ListView
android:id="#+id/numberListView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"></ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
This is my footer:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/formLL">
<TextView
android:id="#+id/numberFormTextViewt"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20px"
android:text="Add a number:"
android:textColor="#color/white" />
<Button
android:id="#+id/numberFormButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_add"></Button>
</LinearLayout>
And this is the Java code to add the footer to the list:
LayoutInflater factory = getLayoutInflater();
LinearLayout numberFooter = (LinearLayout) factory.inflate(R.layout.number_form, null);
// List of numbers
numberListView = (ListView) this.findViewById(R.id.numberListView);
numberListView.addFooterView(numberFooter);
What is going wrong here? The footer does not appear when the activity is displayed. One thing I should mention is the footer is used to add items to the list above it. I'm not sure if that makes a difference but when I am testing this the list adapter is empty. Even still should the footer show up anyway? Any help would be appreciated thanks.
List view under scroll view is not working(means List view is not scrollable).
So u can put empty Linear Layout instead of List view in ur xml file. In ur code, first u can get the list view size, based on this size value, for loop can rotated. In that for loop u can call the get view method of the adapter class(that class is not extending any class.) this will solve the list view problem.
try this in your listview:
android:layout_height="fill_parent"

Android delete from listview [duplicate]

I have listview that contain checkbox and an image when the checkbox is clikced I show a button at bottom of the screen that perform deletion, but when listview height more ,then the listview some portion move under the button ,so I need an alternative option for delete can anyone help me, I except something like menu?
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_centerHorizontal="true"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="#+id/DelPhto"
android:text="Delete" android:layout_alignParentBottom="true" />
<ListView
android:layout_width="280dp"
android:layout_height="fill_parent"
android:id="#+id/list_pictures"
android:layout_alignParentTop="true"
android:layout_above="#+id/DelPhto" />
</RelativeLayout>
Do you mean you wish for the delete button to be always visible even when the list contents are larger than the list control?
If that's the case try setting the layout_weight of your ListView to 1 and see if that solves your problem.
Put ListView in ScrollView (so one will be able to scroll entire list), like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainView"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listView"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:headerDividersEnabled="true">
</ListView>
</ScrollView>
</LinearLayout>
I have had a similar issue in the past. I found that using a relative layout and defining the button before the list solved my issues. Lets consider the following.
<?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"
>
<Button
android:id="#+id/someButtonId"
android:background="#drawable/gray_button"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_alignParentBottom="true"
android:text="#string/some_button_value"
android:textColor="#color/button_text"
/>
<ListView
android:id="#+id/someList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/someButtonId"
/>
</RelativeLayout>
Here I have defined a Relative layout, the layout will occupy the full screen's width and height. I then place the button on the bottom of the RelativeLayout. My expectation is that the list will be placed above the defined button, and fill the remainder of the screen with list contents. Because we are telling the list View to be placed above the button, it will never grow large enough to cover the button causing the list view to mask the button clicks from the user.
hope this helps.

Categories

Resources