How to enable USB debugging mode in Jelly Bean - Android 4.2 and above Devices



In devices like Nexus 4,Nexus 7,Nexus 10 and other Android 4.2 plus devices the developer options is hidden. To make them appear in your Settings you should enable it first

Follow these steps to enable developer options:

1.Go to “Setting” in your device and tap on it.
2.Scroll down to “About Phone” and tap on it.
3.Now scroll to the bottom to find “Build Number” and tap on it 7 times ( Yeah 7 times)

If you have done it you will get a pop-up message saying “You are now a developer”. Thats it!!!

You can now see the Developer options in Settings where you can find USB Debugging.

Android Long Press Event Handle - Example



There may be situation in which your app needs to provide options to the user on long pressing a view. For example as in the messenger app, when you long press a conversation there will be a pop-up menu option to delete the conversation. Thankfully android has the View.setOnLongClickListener to handle it. In this post I will create an android application project with a simple text view on clicking the view, a toast 'Not Long Enough' is displayed and on long pressing the view a toast 'You have pressed it long' is displayed.

So lets start

1.Create an Android Application Project with any package name, any project name , activity as LogPress and layout file as activity_long_press.
2.First let us design the UI for the activity with a normal text view displaying “Long Press Me!!!”

activity_long_press.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".LongPress" >

<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="Long Press Me!!!!" />

</RelativeLayout>

3.Now in the activity file handle the text views onClick and onLongClick Events like the below code.

LongPress.java:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class LongPress extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_long_press);
  TextView txtView = (TextView) findViewById(R.id.txtView);
  txtView.setOnLongClickListener(new OnLongClickListener() {
   @Override
   public boolean onLongClick(View v) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(),
      "You have pressed it long :)", 2000).show();
    return true;
   }
  });
  txtView.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "Not Long Enough :(",
      1000).show();
   }
  });
 }

}

4.Run the project by right-clicking project Run as → android application. The output should be similar to the one below when you long press the text view

Output:



Youtube Video in Android Application - Example


Nowadays most of the android devices ship with a youtube player in it. In this post we will see how to play a youtube video in android, provided the youtube player application is already installed in your mobile.It is done by creating an ACTION_VIEW Intent and starting the activity.This example uses a standard youtube URL

So lets Start:

1.Create an android application project with any projectname , an activity named MainActivity and a layout file named activity_main

2.Now design the UI activity_main.xml for the MainActivity.java with a button to initiate the youtube video

activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="105dp"
        android:text="Play Youtube Video" />

</RelativeLayout>

3.Now create the MainActivity with ACTION_VIEW intent to start the youtube video on clicking the button

MainActivity.java:

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

 Button b;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  b = (Button) findViewById(R.id.button1);
  b.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    String video_path = "https://www.youtube.com/watch?v=qpQtlAW3mag";
    Uri uri = Uri.parse(video_path);
    uri = Uri.parse("vnd.youtube:" + uri.getQueryParameter("v"));
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
   }
  });
 }

}
4.Include internet permission <uses-permission android:name="android.permission.INTERNET"/> in your manifest file

5.Run the application in a youtube installed android device the output should be similar the the one as follows

Output:
 


Android - Twitter Timeline Feeds Example


Today we will see how to display Twitter Timeline feeds in an android application through API calls. In this example we will fetch the recent 10 tweets posted from BBCNews Twitter account.

So let us start:

1.Create a new project File ->New -> Project ->Android ->Android Application Project. While creating the new project, name the activity as TwitterTimelineActivity(TwitterTimelineActivity .java) and layout as main.xml.

2.Now let us design the UI for the TwitterTimelineActivity i.e main.xml with a TextView within a ScrollView. Note the attribute autoLink of the TextView is set to web to display Url links.

main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >


        <TextView
            android:id="@+id/tweetTextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:autoLink="web"
            android:background="#FFFFFF"
            android:textColor="@android:color/black" />
    </ScrollView>

</LinearLayout>
3.Now in the TwitterTimelineActivity we will call http://api.twitter.com/1/statuses/user_timeline/BBCNews.json?count=10&include_rts=1&callback=? To get the json response which will contain data like users name,id,date created text etc.. we will parse the response and fetch the date created and tweet posted and display it in the textview.To call another twitter account replace ”BBCNews” in the url with the desired account .

TwitterTimelineActivity.java
package com.twitter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class TwitterTimelineActivity extends Activity {
 TextView tweetTextView;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  String twitterTimeline = getTwitterTimeline();
  try {
   String tweets = "";
   JSONArray jsonArray = new JSONArray(twitterTimeline);
   for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    int j = i + 1;
    tweets += "Tweet #" + j + " \n";
    tweets += "Date:" + jsonObject.getString("created_at") + "\n";
    tweets += "Post:" + jsonObject.getString("text") + "\n\n";
   }
   tweetTextView = (TextView) findViewById(R.id.tweetTextView);
   tweetTextView.setText(tweets);
  } catch (JSONException e) {
   e.printStackTrace();
  }
 }

 public String getTwitterTimeline() {
  StringBuilder builder = new StringBuilder();
  HttpClient client = new DefaultHttpClient();
  HttpGet httpGet = new HttpGet(
    "http://api.twitter.com/1/statuses/user_timeline/BBCNews.json?count=10&include_rts=1&callback=?");
  try {
   HttpResponse response = client.execute(httpGet);
   StatusLine statusLine = response.getStatusLine();
   int statusCode = statusLine.getStatusCode();
   if (statusCode == 200) {
    HttpEntity entity = response.getEntity();
    InputStream content = entity.getContent();
    BufferedReader reader = new BufferedReader(
      new InputStreamReader(content));
    String line;
    while ((line = reader.readLine()) != null) {
     builder.append(line);
    }
   } else {
    // Display couldn't obtain the data from twitter
   }
  } catch (ClientProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return builder.toString();
 }
}
4.Make the changes in AndroidManifest.xml file by including internet permission <uses-permission android:name="android.permission.INTERNET" />

5.This project is tested in android 2.2 call the url in a separate thread in android 3.0+ if a NetworkonMainThreadExecption occurs. Run the project by rightclicking project Run as → android project.

Output:

The output of this example would be similar to the one as follows: