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).

 

You may also like to read

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. ...
NFC Mifare UID (Card Serial Number) reverse issue ... If you are using NFC (Near Field Communication) Mifare in your application, you should be careful while using NFC Mifare UID  (Unique IDentification ...
Read Logs programmatically in Android In this blog you will learn how to read logs programmatically in Android and used for bug report purposes. Its easy to read logs from Logcat an...
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...
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.