1. Find No of Lines in a TextView

Find no of lines in a textview

There are different ways to find no of lines in a textview.

First method :


Simple but might lead to errors. Because this can be only used in when the view is rendered (i.e after your textview onDraw is called).
So you can use this if you are calling this method after your view is created.

Second Method :

textview.post(new Runnable() {
    public void run() {
        int lineCnt = textview.getLineCount();
        // Perform any actions you want based on the line count here.

This is similar to first method but as we are calling in post runnable the call happens after view is created. So no errors.

What if i want to find out no of lines before a textview is created ??  Is there a way ?
Yes, there is a way

Third Method :

Rect bounds = new Rect();
Paint paint = new Paint();
paint.getTextBounds(testString, 0, testString.length(), bounds);

int width = (int) Math.ceil( bounds.width());
int noOfLines = (int) Math.ceil(width/textview.getWidth());

This is used when you need no of lines before textview is created. We never used Textview in above code except for the width (Which can be found even before TextView onDraw method). So it works perfect in most of the cases. But might not be accurate because in real textView you might be using padding etc.. which we are not specifying here. Adding all those to our paint object makes code more complex. Use third only if you disparately need the textView lines before it’s creation.

So, Out of all three the second method should be opted for most of the cases.

Usage :

1) Auto Adjust Text size based on of lines in textView

This can be used to set text size based on text length. (i.e You have a textview with only one line and you are displaying text larger than the view can hold, then textview will truncate the text, Instead you can check no of lines  and auto adjust textsize based on it). Take care that if your text is too long the text size will be reduced too low user might not able to see text. Check all use cases before you plan to you this.

How to Setup Android Studio with OpenCV (OpenCV4Android)

You can do this very easily in Android Studio.

Follow the below steps to add Open CV in your project as library.

  1. Create a libraries folder underneath your project main directory. For example, if your project isOpenCVExamples, you would create a OpenCVExamples/libraries folder.
  2. Go to the location where you have SDK “\OpenCV-2.4.8-android-sdk\sdk” here you will find the java folder, rename it to opencv.
  3. Now copy the complete opencv directory from the SDK into the libraries folder you just created.
  4. Now create a build.gradle file in the opencv directory with the following contents
    apply plugin: 'android-library'
    buildscript {
        repositories {
        dependencies {
            classpath 'com.android.tools.build:gradle:0.9.+'
    android {
        compileSdkVersion 19
        buildToolsVersion "19.0.1"
        defaultConfig {
            minSdkVersion 8
            targetSdkVersion 19
            versionCode 2480
            versionName "2.4.8"
        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = ['src']
                resources.srcDirs = ['src']
                res.srcDirs = ['res']
                aidl.srcDirs = ['src']
  5. Edit your settings.gradle file in your application’s main directory and add this line:
    include ':libraries:opencv'
  6. Sync your project with Gradle and it should looks like this.Module Settings
  7. Right click on your project then click on the Open Module Settings then Choose Modules from the left-hand list, click on your application’s module, click on the Dependencies tab, and click on the + button to add a new module dependency.
    Dependency Addition
  8. Choose Module dependency. It will open a dialog with a list of modules to choose from; select “:libraries:opencv”.
    Module Dependency
  9. Create a jniLibs folder in the /app/src/main/ location and copy the all the folder with *.so files (armeabi, armeabi-v7a, mips, x86) in the jniLibs from the OpenCV SDK.
  10. Click OK. Now everything done, go and enjoy with OpenCV.

OpenCV Initialisation :

add this static block to activity in which you are using OpenCV functions.

static {
if (!OpenCVLoader.initDebug()) {
// Handle initialization error