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 file and paste below code inside it.

Step 5

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


You may also like to read

APK Analysis : Spotlight on WhatsApp | How to use ... Android Studio includes an APK Analyzer that provides immediate insight into the composition of your APK after the build process is complete. In th...
Develop apps faster using Data Binding – Par... Google has been pushing updates to existing tools and making the life of android developers easier. They also have been releasing new libraries and AP...
Android 6.0 “M” – new features r... Google is providing a newer version of Android OS each year. This time also we are expecting a newer version of Android 6.0 by the end of this year. ...
Using Facebook’s Shimmer effect in Android &... In this tutorial, you will learn how to implement "Shimmer effect" developed by Facebook in RecyclerView for Android. In Facebook, you see that un...
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.