Skip to main content

How to scroll to the bottom of a ListView programmatically in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#40c7a2"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ListView Scroll To Bottom"
        />
    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get reference of widgets from XML layout
        final ListView lv = (ListView) findViewById(R.id.lv);
        Button btn = (Button) findViewById(R.id.btn);

        // Initializing a new String Array
        String[] fruits = new String[] {
                "Abiu",
                "Batuan",
                "Black Mulberry",
                "Cape Gooseberry",
                "Desert banana",
                "Eastern May Hawthorn",
                "Fibrous Satinash",
                "Gooseberry",
                "Hairless rambutan",
                "Illawarra Plum",
                "Jelly Palm",
                "Kepel fruit",
                "Little gooseberry tree",
                "Mammee Apple",
                "Nagami Kumquat",
                "Oil Palm",
                "Peanut butter fruit",
                "Queensland Ebony",
                "Red Mulberry",
                "Sageretia",
                "Tahitian apple",
                "Ugni",
                "Vanilla",
                "Watermelon",
                "Yellow Granadilla",
                "Zig Zag Vine"
        };

        // Create a List from String Array elements
        final List fruits_list = new ArrayList(Arrays.asList(fruits));

        // Create an ArrayAdapter from List
        final ArrayAdapter arrayAdapter = new ArrayAdapter
                (this, android.R.layout.simple_list_item_1, fruits_list);

        // DataBind ListView with items from ArrayAdapter
        lv.setAdapter(arrayAdapter);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*
                    public void smoothScrollToPosition (int position)
                        Smoothly scroll to the specified adapter position. The view
                        will scroll such that the indicated position is displayed.

                        position : Scroll to this adapter position.

                    Adapter getCount()
                        How many items are in the data set represented by this Adapter.

                 */
                lv.smoothScrollToPosition(arrayAdapter.getCount());
            }
        });
    }
}

Comments

Popular posts from this blog

QWERTY-keyboard when this idean came

QWERTY QWERTY   / ˈ k w Éœr t i /  is the most common modern-day  keyboard layout . The name comes from the first six  keys  appearing on the top left letter row of the keyboard and read from left to right: Q-W-E-R-T-Y. The QWERTY design is based on a layout created for the  Sholes and Glidden typewriter  and sold to  Remington  in 1873. It became popular with the success of the Remington No. 2 of 1878, and remains in use on electronic keyboards due to the  network effect  of a standard layout and a belief that  alternatives  fail to provide very significant advantages. [ 1 ]  The use and adoption of the QWERTY keyboard is often viewed as one of the most important case studies in  open standards  because of the widespread, collective adoption and use of the product. [ 2 ] History and purposes [ edit ] Keys are arranged on diagonal columns, to give space for the levers. Main article:  Sholes and Glidden typewriter Still used to this day, the QWERTY layout was devised

Dragon Age: Inquisition Digital Deluxe Edition + All DLCs (torrent) Repack Size: 20.1~23.9 GB

Brief : Dragon Age: Inquisition  is an  action role-playing video game  developed by  Bioware Edmonton  and published by  Electronic Arts . The third major game in the  Dragon Age  franchise,  Dragon Age: Inquisition  is the sequel to  Dragon Age: Origins  and  Dragon Age II . The game was released worldwide in November 2014 for  Microsoft Windows ,  PlayStation 3 ,  PlayStation 4 ,  Xbox 360 , and  Xbox One . Repack Size: 20.1~23.9 GB 

python program to Print Starting Series OF Indian Mobile Number for a State or operator or both

import requests import urllib.request import time from bs4 import BeautifulSoup as bs import re url = ' https://en.wikipedia.org/wiki/Mobile_telephone_numbering_in_India' state_to_extract = "UE" #if set to None all state is considered telecom_to_extracted = None #if set to none all operator from particular city is extracted response = requests . get(url) print (response) soup = bs(response . text, "html.parser" ) one_a_tag = soup . findAll( 'tr' )[ 35 :] lst = [] for k in one_a_tag: s = k . findAll( 'td' ) limit = len (s) i = 0 while True : if i == limit: break no = s[i] . text i += 1 if i == limit: break operator = s[i] . text i += 1 if i == limit: break state = s[i] . text i += 1 if i == limit: break res = f "{no} {operator} {state}" if state_to_extract is None : if telecom_to_extracted is None : lst . append(no) elif telecom_to_e

robot.txt what is this and how to use this

What do they do exactly? Robot.txt files tell your instructions to a search engine robot.. The first thing a search engine spider looks at when it is visiting a page is the robots.txt file. It looks for it because it wants to know what it should do. If you have instructions for a search engine robot, you must tell it those instructions. The most common problem people have with robot.txt files is that they don't know how to make them. If you can make web pages, you can also make a robot.txt file. The file is a text file, which means that you can use notepad, wordpad, or any other plain text editor. You can also make them in Frontpage or Dreamweaver by using the "code" view. You can even "copy and paste" them. So instead of thinking "I am making a robot.txt file", just think, "I am writing a note" they are the exact same process. However you would write a note or a letter on your computer will work for the robot.txt file. What should

How to open facebook through Uninor when it is blocked

Tweet Just Follow this steps . 1. Goto www.uninor.in 2. choose your circle 3.Goto  http://www.uninor.in/customer-care/Pages/customer-complaint-log.aspx 4. Now in new complaint just type your moblie number 5.now you will receive passwrod just type in and hit submit 7. NOW select as following as type as it in every box Complaint Type   -- Select --   DND Related   GPRS   Network Related   Tariff & VAS Related   Activation/ Deactivation   Barring/ Unbarring   Balance Related    Complaint Area   -- Select --   Activation / Deactivation   PC/Modem Connectivity   Usage / Charges related   GPRS issues   Non-Uninor sites   Complaint sub Area   -- Select --   Unable to browse   Unable to download   Please ensure all questions below are answered in the text box below. 1. Handset make and model no. 2. Web Site Name/ URL 3. Error message

Overview of C

Why use C?: C has been used successfully for every type of programming problem imaginable from operating systems to spreadsheets to expert systems - and efficient compilers are available for machines ranging in power from the Apple Macintosh to the Cray supercomputers. The largest measure of C's success seems to be based on purely practical considerations: the portability of the compiler; the standard library concept; a powerful and varied repertoire of operators; an elegant syntax; ready access to the hardware when needed; and the ease with which applications can be optimized by hand-coding isolated procedures C is often called a "Middle Level" programming language. This is not a reflection on its lack of programming power but more a reflection on its capability to access the system's low level functions. Most high-level languages (e.g. Fortran) provides everything the programmer might want to do already built into the language. A low l

Download pocket tank delux with 295 weapons free total 295 weapons version 1.6

Download Pocket Tanks Deluxe Full Version Free With 295 Weapons Pack | Size: 30MB UPDATED 2019 /19/april Description: Pocket Tanks is a 1-2 player computer game for Windows and Mac OS X, created by Blitwise Productions, developer of Super DX-Ball and Neon Wars. Adapted from Michael Welch's earlier Amiga game Scorched Tanks, this newer version features modified physics, dozens of weapons ranging from simple explosive shells to homing missiles, and the ability to move the tank. It supports several expansion packs. At the moment, players can have up to 295 different weapons total. Pocket Tanks is often abbreviated as PTanks. Have Fun! NOTE: FILE NAME IS SCRAMBLED FOR AVOIDING HARD DETECTION & FILE TAKEN DOWN . How to Play: Best with 2 players on the same computer at school or at work. UPDATED LINK https://mirr.re/d/u1Y https://nl26.seedr.cc/ff_get/447027537/ptd16.295.exe?st=lUp-PbRp4YOwToHIOGwStQ&e=1555747979 http://www.uploadmagnet.com/7gfzhbyfe

Error:(1, 0) Plugin with id 'com.android.application' not found

just add the following code to your top level build.gradle  {note by top level build.gradle i means that if you open that build.gradle you will see a comment like this // Top-level build file where you can add configuration options common to all sub-projects/modules. add the following code  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:0.12.+' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! and click on sync now ..it will do the rest..

3g what it is

Definition of 3G: 3G is the third generation of wireless technologies. It comes with enhancements over previous wireless technologies, like high-speed transmission, advanced multimedia access and global roaming. 3G is mostly used with mobile phones and handsets as a means to connect the phone to the Internet or other IP networks in order to make voice and video calls, to download and upload data and to surf the net. How is 3G Better?: 3G has the following enhancements over 2.5G and previous networks: Several times higher data speed; Enhanced audio and video streaming; Video-conferencing support; Web and WAP browsing at higher speeds; IPTV (TV through the Internet) support. 3G Technical Specifications: The transfer rate for 3G networks is between 128 and 144 kbps (kilobits per second) for devices that are moving fast and 384 kbps for slow ones(like for pedestrians). For fixed wireless LANs, the speed goes beyond 2 Mbps. 3G is a set of technologies and stand