1

I am trying to make a simple Checkbook app, whose MainActivity stores a list of transactions. I would like a TextView at the top and bottom of the screen that show the account balance and an option to add a new transaction, respectively. I would like a list of transactions in between that scroll. I was able to implement a ListView and add a header and footer view, but if the transaction list exceeds the size of the screen the headers and footers can scroll off screen.

Is there any way to position a ListView within the linear layout, or freeze the headers/footers to stay on the screen?

Here is my XML file so far:

<TextView
    android:id="@+id/header_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/default_header_string">
</TextView>

<ListView
    android:id="@+id/transaction_list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</ListView>

<TextView
    android:id="@+id/footer_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/add_transaction_string">
</TextView>

And here is my onCreate, which has no syntax errors but I am unable to click the footerview to add a transaction:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_checkbook);

    ListView list = (ListView) findViewById(R.id.transaction_list_view);

    // Create a new Adapter
    mAdapter = new TransactionAdapter(list.getContext());

    // Inflate footerView and headerView
    LayoutInflater inflater = getLayoutInflater();
    TextView headerView = (TextView) inflater.inflate(R.layout.header_view, null);
    TextView footerView = (TextView) inflater.inflate(R.layout.footer_view, null);

    // Set listener for footerView
    footerView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent transactionIntent = new Intent(CheckbookActivity.this, AddTransactionActivity.class);
            startActivityForResult(transactionIntent, ADD_TRANSACTION_REQUEST);
        }
    });

    list.setAdapter(mAdapter);
}
AdamMc331
  • 16,492
  • 10
  • 71
  • 133
  • Can you say me why you depend on the LinearLayout. W ether it'll be done easily with RelativeLayout. – GovindRathod Jun 09 '14 at 04:29
  • Well, I tried using just a ListView, because you can add footer and header views, but as my list grows longer these views scroll off the screen, and I tried to find a way to keep them on screen. I want the user to ALWAYS see the account balance, and ALWAYS see the option to add a new transaction. – AdamMc331 Jun 09 '14 at 04:32
  • Yes my friend it will possible with the RelativeLayout easily then LinearLayout. – GovindRathod Jun 09 '14 at 04:34

6 Answers6

3

use the below code. This will satisfy your requirement. I tried this and working for me. Relative layout with below,above attributes. Relativelayout is better than Linear layout with weight method.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:gravity="center_horizontal"
    android:text="ListView Heading" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:gravity="center_horizontal"
    android:text="ListView Footer" />

<ListView 
     android:id="@+id/listView1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_below="@id/textView1"
     android:layout_above="@id/textView2"

    ></ListView>

The UI will like this

enter image description here

Arundas K V
  • 801
  • 2
  • 14
  • 28
1

Yes, you can do it with weightsum and layout_weight in linearlayout and also you can create this type of view using RelativeLayout.

1) In LinearLayout just add weightsum="1" to your linearlayout and add layout_weight="0.2" to each of your header and footer and add layout_weight="0.6" to your listview.

2) In relativeLayout add alignParentTop to your header and alignParentBottom to your footer and set listview to layout_below="@+id/header" and layout_above="2+id/footer"

learner
  • 3,092
  • 2
  • 21
  • 33
Jayesh Khasatiya
  • 2,140
  • 1
  • 14
  • 15
  • The layout weight was a good idea. I think in this particular case the layout_above and layout_below method worked best, because it allows me to play around with text sizes and such without concerning myself about screwing up the size of other objects. Thanks for the input! – AdamMc331 Jun 10 '14 at 00:34
1

Try this way, hope this will help you to solve your problem. Instead of using header/footer just put as below code in your XML:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:id="@+id/header_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/default_header_string">
    </TextView>

    <ListView
        android:id="@+id/transaction_list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </ListView>

    <TextView
        android:id="@+id/footer_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_transaction_string">
    </TextView>
</LinearLayout>
Kromster
  • 7,181
  • 7
  • 63
  • 111
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • That was a good idea, I implemented something really similar based on your answer as well as the relative layout that the other person shared. Thanks! – AdamMc331 Jun 10 '14 at 00:35
0

I found a possible solution for your problem from a similiar post. Hope this helps you.

Community
  • 1
  • 1
  • Thanks for the link! That is similar to what I was trying to do, and it's definitely an idea for a layout I can implement in the future. – AdamMc331 Jun 10 '14 at 00:36
0

For what you are trying to accomplish to freeze the header/footer. It will be easier to use a relative layout to position the header/footer then have your listview in the middle

<RelativeLayout ...>
<TextView
    android:id="@+id/header_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:text="@string/default_header_string">
</TextView>

<ListView
    android:id="@+id/transaction_list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/header_view"
    android:layout_above="@+id/footer_view">
</ListView>

<TextView
    android:id="@+id/footer_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center"
    android:text="@string/add_transaction_string">
</TextView>
</RelativeLayout>

You can use a LinearLayout for this task. But I don't recommend it as it's a bit "hacky".

over_optimistic
  • 1,399
  • 2
  • 18
  • 27
0
  1. Get all the elements in a array: Example:- (weatherArray)

  2. Loop through all the elements :-

Example:-

mainLayout = ((LinearLayout)refreshObj.get("mainLayout"));
    mainLayout.removeAllViews();

for (int i = 0; i < weatherArray.length(); i++) {

 View childView = getLayoutInflater().inflate(R.layout.weather_row4_item,   mainLayout,false); 

 TextView todayTempStatus = (TextView) childView.findViewById(R.id.todayTempStatus);

todayTempStatus.setText("");

}
  1. This is an example without using listview, which we will populate lienarlayout using child view.
learner
  • 3,092
  • 2
  • 21
  • 33
Ramesh_D
  • 689
  • 1
  • 7
  • 25