MaxHeightScrollView in Android using Android Studio

In Android, there are two constants for providing height and width of any view. But if you provide any specific height to any view, it won’t be change until you change it dynamically or statically in layout file.

What if you want to provide MAX height to any view? Max height means, your view must not exceed the limit of provided height.

In this article, I am explaining how to provide MAX height to ScrollView in Android. So let’s begin.

Step 1

Create a project in Android Studio

1. MaxHeightScrollView
Create Project
Select Target Devices
Select Target Devices
Select Activity type
Select Activity type
Give name to activity and layout files
Give name to activity and layout files

Step 2

Create a new file called “MaHeightScrollView” which extends “ScrollView” and paste below code inside it.

In above code, there is one method called onMeasure is overriden here.

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overriden by subclasses to provide accurate and efficient measurement of their contents.

Read more : onMeasure(int,int);

heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);

This line will calculate MAX height of view and it will be provided to super.onMeasure(..) method.

Step 3

Open layout file and paste below code inside it.

Step 4

Open MainActivity.java file and paste below code inside it.

Step 5

Build your project and Run your sample code in device or AVD (Android Virtual Device).

 

Share on Facebook3Share on Google+12Tweet about this on TwitterShare on LinkedIn0Share on Reddit0Pin on Pinterest0Share on Tumblr0Email this to someone
  • Bell

    Hi to every one, since I am really keen of reading this weblog’s
    post to be updated regularly. It includes good stuff.

  • Celesta

    What’s up to all, how is everything, I think every one is getting more from
    this web page, and your views are pleasant designed
    for new visitors.