Ripple Drawable

Ripple Drawable Topics

  1. Programmatically create Ripple Drawable

 

Programmatically create Ripple Drawable :-

It comes in handy many times, to set Ripple Drawables programatically.

public static Drawable getAdaptiveRippleDrawable(int normalColor, int pressedColor) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return new RippleDrawable(ColorStateList.valueOf(pressedColor),
                null, getRippleMask(normalColor));
    } else { 
        return getStateListDrawable(normalColor, pressedColor);
    } 
} 
 
private static Drawable getRippleMask(int color) {
    float[] outerRadii = new float[8];
    Arrays.fill(outerRadii, 3);// 3 is radius of final ripple, instead of 3 you can give required final radius
 
    RoundRectShape r = new RoundRectShape(outerRadii, null, null);
 
    ShapeDrawable shapeDrawable = new ShapeDrawable(r);
    shapeDrawable.getPaint().setColor(color);
 
    return shapeDrawable;
} 
 
public static StateListDrawable getStateListDrawable(int normalColor, int pressedColor) {
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(pressedColor));
    states.addState(new int[]{android.R.attr.state_focused}, new ColorDrawable(pressedColor));
    states.addState(new int[]{android.R.attr.state_activated}, new ColorDrawable(pressedColor));
    states.addState(new int[]{}, new ColorDrawable(normalColor));
    return states;
} 

you can get the drawable and apply to any view using view.setBackgroundDrawable()

Drawable drawable = getPressedColorRippleDrawable(normalColor, pressedColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    view.setBackground(drawable);
} else {
    view.setBackgroundDrawable(drawable);
}

for lollipop+ devices you will get ripple else it will change the color of view

TextView

Topics

  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 :

TextView.getLineCount();

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.setText(arr2[position]);
textview.post(new Runnable() {
    @Override
    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.setTextSize(currentSize);
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 {
            mavenCentral()
        }
        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.
    JniLibs
  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
}
}

ADB and fastboot setup

First you need to have either sdk or android-platform-tools installed in your pc.

Basic Troubleshooting connectivity issues:
-try a reboot of the PC
-try different usb cables and ports
-dont use a usb hub
-dont use usb 3.0 only usb 2.0 works
-make sure nothing capable of comunicating with the phone is enabled and running. htc sync,pdanet,easy tether,and even itunes have all been known to cause issues.
-windows 8 has been known to have issues. try a windows 7 or older machine

What is adb?

The Android Debug Bridge (adb) is a development tool that facilitates communication between an Android device and a personal computer. This communication is most often done over a USB cable, but Wi-Fi connections are also supported. adb can also be used by developers to communicate with a virtual android machine running on the computer.

adb is like a “Swiss-army knife” of Android development. It provides numerous functions that are described in detail by the command: adb --help (see output here). Some of the more commonly used commands are listed in the Popular adb commands section below.

Installing adb & fastboot

Windows, Mac, and Linux

The best way to get adb and fastboot is to install the Android SDK directly from Google. After you install the SDK Tools, launch the SDK Manager and install the Android SDK Platform-toolspackage. Note that the manager automatically selects the latest Android X.x (API YY) package on launch which you can safely deselect if you are only interested in adb and fastboot. Then, by running the SDK manager periodically to check for updates, you can ensure these tools are always at the latest version.

The adb and fastboot executables will be located in the platform-tools subdirectory of the SDK Tools. You can add this directory to your system’s PATH so that these tools are available from any Command Prompt or Terminal:

  • Windows 7/8:
    1. From the desktop, right-click My Computer and select Properties
    2. In the System Properties window, click on the Advanced tab
    3. In the Advanced section, click the Environment Variables button
    4. In the Environment Variables window, highlight the Path variable in the Systems Variable section and click the Edit button
    5. Append ;<path-to-sdk>/platform-tools to the end of the existing Path definition (the semi-colon separates each path entry)
  • Linux
    1. Add the following to ~/.profile and then logout/login:
    if [ -d "<path-to-sdk>/platform-tools" ] ; then
    PATH="<path-to-sdk>/platform-tools:$PATH"
    fi
  • Mac
    1. Add the following to ~/.bash_profile and then logout/login:
    if [ -d "<path-to-sdk>/platform-tools" ] ; then
    export PATH="<path-to-sdk>/platform-tools:$PATH"
    fi

Ubuntu

An easy alternative to installing the SDK package as described above exists on Ubuntu and other debian-based Linux distributions. adb and fastboot can be installed via the following commands from the Terminal:

sudo apt-get install android-tools-adb
sudo apt-get install android-tools-fastboot

There is no need to manually edit your system’s Path if this method is used.

If even now your device is not being detected in adb devices or fastboot devices

  1. Verify your username is included in the plugdev group. Type
    groups
    from a terminal and look for plugdev in the listed groups. If you do not see plugdev listed, you can add your username to the group with:
    sudo gpasswd -a username plugdev
    where username should be replaced with your linux username.
  2. Copy the set of rules listed below these steps to a text file and save it as /etc/udev/rules.d/51-android.rules. You will need sudo/su to write to that directory. So, for instance:
    sudo nano /etc/udev/rules.d/51-android.rules
    These rules cover all vendors listed by Google. Optionally, you can add just the vendor corresponding to the device(s) you plan to connect to your computer.
  3. Restart your computer and then test plugging in your device to the computer with USB debugging enabled.
#Acer
SUBSYSTEM=="usb", ATTR{idVendor}=="0502", MODE="0664", GROUP="plugdev"
#ASUS
SUBSYSTEM=="usb", ATTR{idVendor}=="0b05", MODE="0664", GROUP="plugdev"
#Dell
SUBSYSTEM=="usb", ATTR{idVendor}=="413c", MODE="0664", GROUP="plugdev"
#Foxconn
SUBSYSTEM=="usb", ATTR{idVendor}=="0489", MODE="0664", GROUP="plugdev"
#Fujitsu & Fujitsu Toshiba
SUBSYSTEM=="usb", ATTR{idVendor}=="04c5", MODE="0664", GROUP="plugdev"
#Garmin-Asus
SUBSYSTEM=="usb", ATTR{idVendor}=="091e", MODE="0664", GROUP="plugdev"
#Google
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", MODE="0664", GROUP="plugdev"
#Haier
SUBSYSTEM=="usb", ATTR{idVendor}=="201e", MODE="0664", GROUP="plugdev"
#Hisense
SUBSYSTEM=="usb", ATTR{idVendor}=="109b", MODE="0664", GROUP="plugdev"
#HTC
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", MODE="0664", GROUP="plugdev"
#Huawei
SUBSYSTEM=="usb", ATTR{idVendor}=="12d1", MODE="0664", GROUP="plugdev"
#K-Touch
SUBSYSTEM=="usb", ATTR{idVendor}=="24e3", MODE="0664", GROUP="plugdev"
#KT Tech
SUBSYSTEM=="usb", ATTR{idVendor}=="2116", MODE="0664", GROUP="plugdev"
#Kyocera
SUBSYSTEM=="usb", ATTR{idVendor}=="0482", MODE="0664", GROUP="plugdev"
#Lenovo
SUBSYSTEM=="usb", ATTR{idVendor}=="17ef", MODE="0664", GROUP="plugdev"
#LG
SUBSYSTEM=="usb", ATTR{idVendor}=="1004", MODE="0664", GROUP="plugdev"
#Motorola
SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", MODE="0664", GROUP="plugdev"
#MTK
SUBSYSTEM=="usb", ATTR{idVendor}=="0e8d", MODE="0664", GROUP="plugdev"
#NEC
SUBSYSTEM=="usb", ATTR{idVendor}=="0409", MODE="0664", GROUP="plugdev"
#Nook
SUBSYSTEM=="usb", ATTR{idVendor}=="2080", MODE="0664", GROUP="plugdev"
#Nvidia
SUBSYSTEM=="usb", ATTR{idVendor}=="0955", MODE="0664", GROUP="plugdev"
#OTGV
SUBSYSTEM=="usb", ATTR{idVendor}=="2257", MODE="0664", GROUP="plugdev"
#Pantech
SUBSYSTEM=="usb", ATTR{idVendor}=="10a9", MODE="0664", GROUP="plugdev"
#Pegatron
SUBSYSTEM=="usb", ATTR{idVendor}=="1d4d", MODE="0664", GROUP="plugdev"
#Philips
SUBSYSTEM=="usb", ATTR{idVendor}=="0471", MODE="0664", GROUP="plugdev"
#PMC-Sierra
SUBSYSTEM=="usb", ATTR{idVendor}=="04da", MODE="0664", GROUP="plugdev"
#Qualcomm
SUBSYSTEM=="usb", ATTR{idVendor}=="05c6", MODE="0664", GROUP="plugdev"
#SK Telesys
SUBSYSTEM=="usb", ATTR{idVendor}=="1f53", MODE="0664", GROUP="plugdev"
#Samsung
SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", MODE="0664", GROUP="plugdev"
#Sharp
SUBSYSTEM=="usb", ATTR{idVendor}=="04dd", MODE="0664", GROUP="plugdev"
#Sony
SUBSYSTEM=="usb", ATTR{idVendor}=="054c", MODE="0664", GROUP="plugdev"
#Sony Ericsson
SUBSYSTEM=="usb", ATTR{idVendor}=="0fce", MODE="0664", GROUP="plugdev"
#Teleepoch
SUBSYSTEM=="usb", ATTR{idVendor}=="2340", MODE="0664", GROUP="plugdev"
#Toshiba
SUBSYSTEM=="usb", ATTR{idVendor}=="0930", MODE="0664", GROUP="plugdev"
#ZTE
SUBSYSTEM=="usb", ATTR{idVendor}=="19d2", MODE="0664", GROUP="plugdev"

Troubleshooting

1. Make sure your device is connected and accessible via usb. lsusb should show a list of connected devices. The section after ‘ID’ in the output should match one of the idVendor numbers from the udev rules. For example, a Nexus One with idVendor 18d1 should return something like:

Bus 001 Device 005: ID 17ef:480d Lenovo Integrated Webcam [R5U877]
Bus 002 Device 002: ID 0bda:0119 Realtek Semiconductor Corp. 
Bus 002 Device 009: ID 18d1:4e12 Google Inc. Nexus One (debug)
Bus 004 Device 003: ID 0a5c:2145 Broadcom Corp. Bluetooth with Enhanced Data Rate II
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

2. If the device is detected in lsusb, try running adb devices. If you get ???????????? device, try reloading the udev rules:

 sudo sh -c "(udevadm control --reload-rules && udevadm trigger --action=change)"

You can also try disconnecting and reconnecting the usb cable to your device.

3. If you cannot access your device via adb, even after adding your linux user to the plugdev group and restarting the computer, you can try starting the adb service as root. This is dangerous and not recommended:

adb kill-server
sudo $(which adb) start-server
adb devices

Similarly, if fastboot devices returns no permissions, try running fastboot as root:

sudo $(which fastboot) devices