Tuesday, March 16, 2021

Android HorizontalScrollView

 

Android HorizontalScrollView

HorizontalScrollView is a FrameLayout. The android.widget.HorizontalScrollView class provides the functionality of horizontal scroll view. HorizontalScrollView is used to scroll the child elements or views in a horizontal direction. HorizontalScrollView only supports horizontal scrolling.

For vertical scroll, android uses ScrollView.

Let's implement simple example of HorizontalScrollView.

activity_main.xml

Now, drag HorizontalScrollView from palette to activity_main.xml file and place some views or elements inside it.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.   
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:textAppearance="?android:attr/textAppearanceSmall"  
  11.         android:text="Horizontal ScrollView Example"  
  12.         android:id="@+id/textView"  
  13.         android:layout_alignParentTop="true"  
  14.         android:layout_centerHorizontal="true" />  
  15.   
  16.   
  17.     <LinearLayout  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="match_parent"  
  20.         android:layout_marginTop="25dp">  
  21.         <HorizontalScrollView  
  22.             android:layout_width="match_parent"  
  23.             android:layout_height="60dp"  
  24.             android:id="@+id/horizontalScrollView">  
  25.   
  26.             <LinearLayout  
  27.                 android:layout_width="wrap_content"  
  28.                 android:layout_height="wrap_content"  
  29.                 android:orientation="horizontal">  
  30.                 <Button  
  31.                     android:layout_width="wrap_content"  
  32.                     android:layout_height="wrap_content"  
  33.                     android:text="New Button1"  
  34.                     android:id="@+id/button1" />  
  35.                 <Button  
  36.                     android:layout_width="wrap_content"  
  37.                     android:layout_height="wrap_content"  
  38.                     android:text="New Button2"  
  39.                     android:id="@+id/button2" />  
  40.   
  41.                 <Button  
  42.                     android:layout_width="wrap_content"  
  43.                     android:layout_height="wrap_content"  
  44.                     android:text="New Button3"  
  45.                     android:id="@+id/button3" />  
  46.                 <Button  
  47.                     android:layout_width="wrap_content"  
  48.                     android:layout_height="wrap_content"  
  49.                     android:text="New Button4"  
  50.                     android:id="@+id/button4" />  
  51.                 <Button  
  52.                     android:layout_width="wrap_content"  
  53.                     android:layout_height="wrap_content"  
  54.                     android:text="New Button5"  
  55.                     android:id="@+id/button5" />  
  56.                 <Button  
  57.                     android:layout_width="wrap_content"  
  58.                     android:layout_height="wrap_content"  
  59.                     android:text="New Button6"  
  60.                     android:id="@+id/button6" />  
  61.                 <Button  
  62.                     android:layout_width="wrap_content"  
  63.                     android:layout_height="wrap_content"  
  64.                     android:text="New Button7"  
  65.                     android:id="@+id/button7" />  
  66.                 <Button  
  67.                     android:layout_width="wrap_content"  
  68.                     android:layout_height="wrap_content"  
  69.                     android:text="New Button8"  
  70.                     android:id="@+id/button8"/>  
  71.             </LinearLayout>  
  72.   
  73.         </HorizontalScrollView>  
  74.     </LinearLayout>  
  75. </RelativeLayout>  

Activity class

This is auto generated code, we have not written any code here.

File: MainActivity.java

  1. package com.example.test.horizantalscrollview;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5.   
  6. public class MainActivity extends AppCompatActivity {  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.     }  
  13. }  
android ScrollView Horizontal 1
android ScrollView Horizontal 2

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile