11 Scroll View
Algorithm:
Step 1) Create a new Android Studio Project
Step 2)Goto Project Structure and open activity_main.xml file
Step 3) Remove the default layout i.e Constraint layout and create Scroll View by using tag or drag and drop from the palette.
Step 4) Follow the hierarchy structure
< RelativeLayout>
<LinearLayout>
<HorizontalScrollView>
<LinearLayout>
//create 8 to 10 Buttons
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
ScrollView can take only one children, so you can add LinearLayout or RelativeLayout/ Step 4)
Step 5) Run the application from the menu bar select Run and Click on Run App option.
Program:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" android:text="Horizontal ScrollView Example"
android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" />
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="25dp">
<HorizontalScrollView android:layout_width="match_parent" android:layout_height="60dp" android:id="@+id/horizontalScrollView>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button1" android:id="@+id/button1" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button2" android:id="@+id/button2" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button3" android:id="@+id/button3" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button4" android:id="@+id/button4" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button5" android:id="@+id/button5" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button6" android:id="@+id/button6" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button7" android:id="@+id/button7" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button8"
android:id="@+id/button8"/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.example.scrollview;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle;
public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
}
}
Comments
Post a Comment