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

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...
RecyclerView in Android Studio – Part 1 What is RecyclerView? RecyclerView is advanced version of ListView. It is designed to work with large scale of data with better performance and mem...
Customizing TextInputLayout – Part 2 In previous article Customizing TextInputLayout - Part 1, we learnt how to customize TextInputLayout and how to apply hint color to it. Going t...
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 ...
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.