1

I want to display a toggle button that with On/state enlarge the whole listview and with Off/state return it to normal previous view. I have managed to create the toggle button but when it's pressed it enlarges itself instead of the listview. How do I associate the toggle button to enlarge On/off the whole listview? Thanks in advance.

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

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

<ToggleButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/toggle_button"
    android:textOn="Resize On"
    android:textOff="Resize Off"
    android:layout_weight="0.03"
    android:onClick="changeSizeState"/>

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ToggleButton;
import android.view.ViewGroup.LayoutParams;
import static john.android.study.R.id.tvList;



public class MainActivity extends Activity {

    private ListView lvPhone;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lvPhone = (ListView)findViewById(R.id.tvList);

        String[] values = new String[]{"Nikos", "Yiannis", "Kostas", "Thanasis", "Paulos",};

        ListView lvPhone = (ListView)findViewById(tvList);

        List<PhoneBook> listPhoneBook = new ArrayList<PhoneBook>();

        for (String value : values) {
            PhoneBook entry = new PhoneBook(value);
            listPhoneBook.add(entry);
        }

        PhoneBookAdapter adapter = new PhoneBookAdapter(this,listPhoneBook);
        lvPhone.setAdapter(adapter);
    }
    public void changeSizeState (View view){
        boolean checked = ((ToggleButton)view).isChecked();
        if(checked) {
            LayoutParams params = view.getLayoutParams();
            params.height = 250;
            view.setLayoutParams(params);
        }
    }
}
Pavlo
  • 43,301
  • 14
  • 77
  • 113

1 Answers1

1

You are setting layout params to toogle button, try it on listview:

public void changeSizeState (View view){
        boolean checked = ((ToggleButton)view).isChecked();
        if(checked) {
            LayoutParams params = view.getLayoutParams();
            params.height = 250;
            lvPhone.setLayoutParams(params);
        }
    }

Also you are initiating a new listView in onCreate, modify like below :

lvPhone = (ListView)findViewById(tvList);

Hope this helps.

tahsinRupam
  • 6,325
  • 1
  • 18
  • 34