Contextual Action Bar with ListView

krishjlk:

Multiple Selection Simplified …………

Originally posted on ANDROID PERSPECTIVE:

Nexus-4-Flat

In this tutorial we’ll see how to use Contextual Action Bar with ActionBarSherlock and ListView in case when data items selected with long-click, and don’t have any UI components like checkbox to perform selection.

Contextual Action Bar (CAB) is an important mode of Action Bar and a very user friendly design pattern. It is recommended to use when you perform actions with selected data like plain text or data items from ListView or GridView components. Although it is available from Android 3.0 it is a good practice to use it in apps working on earlier API versions.

Implement CAB with ListView

If you’re using Android 3.0 or higher there is a detailed description of CAB setup on each selection method – whether contextual action should be performed on a single selected item or on a group of selected items.

If your app works on android version 2.x and higher the…

View original 671 more words

2012 in review

The WordPress.com stats helper monkeys prepared a 2012 annual report for this blog.

Here’s an excerpt:

600 people reached the top of Mt. Everest in 2012. This blog got about 3,100 views in 2012. If every person who reached the top of Mt. Everest viewed this blog, it would have taken 5 years to get that many views.

Click here to see the complete report.

How to get parameters from a url in android and java

public static Map<String, String> getParametersFromUrl(String url)
 {
 Map<String, String> map = new HashMap<String, String>();

 if(url!=null && URLUtil.isValidUrl(url))
 {
 String[] params = url.split("[&,?]");
 for (String param : params)
 {
 try {
 String name = param.split("=")[0];
 String value = param.split("=")[1];
 map.put(name, value);
 } catch (Exception e)
 {
 Log.d("Error", "No value for parameter");
 }
 }
 }
 return map;
 }

How to scale a bitmap as per device width and height

public Bitmap scaleToActualAspectRatio(Bitmap bitmap) {
 if (bitmap != null) {
 boolean flag = true;

int deviceWidth = getWindowManager().getDefaultDisplay()
 .getWidth();
 int deviceHeight = getWindowManager().getDefaultDisplay()
 .getHeight();

int bitmapHeight = bitmap.getHeight(); // 563
 int bitmapWidth = bitmap.getWidth(); // 900

// aSCPECT rATIO IS Always WIDTH x HEIGHT rEMEMMBER 1024 x 768

if (bitmapWidth > deviceWidth) {
 flag = false;

// scale According to WIDTH
 int scaledWidth = deviceWidth;
 int scaledHeight = (scaledWidth * bitmapHeight) / bitmapWidth;

try {
 if (scaledHeight > deviceHeight)
 scaledHeight = deviceHeight;

bitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth,
 scaledHeight, true);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

if (flag) {
 if (bitmapHeight > deviceHeight) {
 // scale According to HEIGHT
 int scaledHeight = deviceHeight;
 int scaledWidth = (scaledHeight * bitmapWidth)
 / bitmapHeight;

try {
 if (scaledWidth > deviceWidth)
 scaledWidth = deviceWidth;

bitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth,
 scaledHeight, true);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 }
 }
 return bitmap;
 }

How to convert inputstream to byte array in android and java


public byte[] convertInputStreamToByteArray(InputStream inputStream)
 {
 byte[] bytes= null;

 try
 {
 ByteArrayOutputStream bos = new ByteArrayOutputStream();

 byte data[] = new byte[1024];
 int count;

 while ((count = inputStream.read(data)) != -1)
 {
 bos.write(data, 0, count);
 }

bos.flush();
 bos.close();
 inputStream.close();

bytes = bos.toByteArray();
 }
 catch (IOException e)
 {
 e.printStackTrace();
 }
 return bytes;
 }

How to convert bitmap to byte array in android and java

/**
 * @param bitmap
 * Bitmap object from which you want to get bytes
 * @return byte[] array of bytes by compressing the bitmap to PNG format <br/>
 * null if bitmap passed is null (or) failed to get bytes from the
 * bitmap
 */
 public static byte[] convertBitmapToByteArray(Bitmap bitmap) {
 if (bitmap == null) {
 return null;
 } else {
 byte[] b = null;
 try {
 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 bitmap.compress(CompressFormat.PNG, 0, byteArrayOutputStream);
 b = byteArrayOutputStream.toByteArray();
 } catch (Exception e) {
 e.printStackTrace();
 }
 return b;
 }
 }

How to get / extract video id from an youtube url in android / java


public static String getYoutubeVideoId(String youtubeUrl)
 {
 String video_id="";
  if (youtubeUrl != null && youtubeUrl.trim().length() > 0 && youtubeUrl.startsWith("http"))
 {

String expression = "^.*((youtu.be"+ "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*"; // var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
 CharSequence input = youtubeUrl;
 Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
 Matcher matcher = pattern.matcher(input);
 if (matcher.matches())
 {
String groupIndex1 = matcher.group(7);
 if(groupIndex1!=null && groupIndex1.length()==11)
 video_id = groupIndex1;
 }
 }
 return video_id;
 }

how to upload an android project to github through command line in ubuntu / linux / windows

Go to the project root directory which you want to upload to git hub

Step 1. git init

If you dont have README.md then

{

Step 1.1 touch README.md

Step 1.2 git add README.md

}
else
{

Step 1.1 git clone https://github.com/<username>/<projectname&gt;.git

Step 1.2 git remote rm origin

}

Step 2. git add .

Step 3. git commit -m “initial commit”

Step 4. git remote add origin https://github.com/<username>/<projectname&gt;.git

Step 5. git push -u origin master

How to run / execute a c++ file / program through command prompt / terminal in linux / windows / ubuntu

Step 1 : Open Terminal

Step 2 : Go to the directory where your .cpp file is located.

Eg: cd /home/test/test.cpp (for ubuntu)

or

Eg: cd D:\Test\test.cpp (for windows)


/******************** test.cpp ********************/

#include <iostream>
int main()
 {
 std::cout << "My first C++ program\n";
 return 585;
 }
/******************** test.cpp ********************/

Step 3 : Execute the following command

 g++ test.cpp -o test

Step 4 : The following command will create a test.exe file

Step 5 : Once test.exe is created your cpp file has been compiled successfully.
Now execute the following command
./test

Step 6 : You can see the o/p of test.cpp

How to run / execute a c file / program through command prompt / terminal in linux / windows / ubuntu

Step 1 : Open Terminal

Step 2 : Go to the directory where your .c file is located.

Eg: cd /home/test/test.c (for ubuntu)

or

Eg: cd D:\Test\test.c (for windows)


/******************** test.c ********************/

#include <stdio.h>
 void main()
 {
 printf("Hello, World! \n");
 }

/******************** test.c ********************/

Step 3 : Execute the following command

 gcc test.c -o test

Step 4 : The following command will create a test.exe file

Step 5 : Once test.exe is created your c file has been compiled successfully.
Now execute the following command
./test

Step 6 : You can see the o/p of test.c

Follow

Get every new post delivered to your Inbox.