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

Resizable Rectangle Overlay on Touch in Android When I read this question http://stackoverflow.com/questions/8974088/how-to-create-a-resizable-rectangle-with-user-touch-events-on-android on StackOve...
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...
Exit from Activity with twice “Back” b... When we are doing some important operation or work on activity and it may possible that by mistake we pressed "back" button. Then what?? This will sim...
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.