user3388473
user3388473

Reputation: 963

How to identify textview reached the bottom of screen?

I am developing book reader application. I have LinearLayout.

<LinearLayout
    android:id="@+id/llReader"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:orientation="vertical" >
</LinearLayout>

I am taking html files from internal storage line by line. I am assigning one text view for one line and putting them into LinearLayout.

private void readFile(LinearLayout ll) throws IOException {

        fileName = myDatabase.getBookById(book_id) + ".html";

        FileInputStream fis = openFileInput(fileName);
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));

        String line = "";

        while (null != (line = br.readLine())) {

            TextView tv = new TextView(this);
            tv.setTextSize(24);
            tv.setText(Html.fromHtml(line));
            ll.addView(tv);
        }
        br.close();
    }

How to identify that TextViews reached the bottom of the screen?

Upvotes: 1

Views: 107

Answers (1)

Raskilas
Raskilas

Reputation: 691

You can via pixel manipulation:

    private void readFile(LinearLayout ll) throws IOException {

    fileName = myDatabase.getBookById(book_id) + ".html";

    FileInputStream fis = openFileInput(fileName);
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));

    String line = "";

    int parentHeight = ll.getHeight(); // in pixels
    int sumHeigth = 0;

    while (null != (line = br.readLine())) {

        TextView tv = new TextView(this);
        tv.setTextSize(24);
        tv.setText(Html.fromHtml(line));
        ll.addView(tv);
        sumHeigth += tv.getHeight();
        if(sumHeigth>parentHeight) {
            // if can't fit in LinearLayout
            ll.removeView(tv);
            // break; // or what you want in this situation
        }
    }
    br.close();
}

Upvotes: 1

Related Questions