flipkart

Tuesday, May 20, 2014

JNI Development using eclipse

Hi, every body knows programming of C language so they write already code in C language. This post i will try explain how to use C function in Java using eclipse.Following explain is based on Ubuntu operating system


First create java project 


File -> New -> Java Project

After created java project add java source file

right click on src folder select New->class

Write java source code file and save it

Right click on project folder select new -> folder

Give folder name as jni after press finish

Once press build project

Right click on project press Build project

Next go Run -> External tool configurations 

It's opened one window select program next click new 

Location field is javah exceutable file path

working directory is present project bin folder

Arguments is "-jni -classpath ${workspace_loc:/Hello/bin} -d ${workspace_loc:/Hello/jni} Hello " 

it's created java header file inside jni folder use that file for creation c source file before that one move C/C++ perspective

Right click on project New -> Other -> Convert to a C/C++ Project 

After selecting press Next inside select C project or C++ project after Shared library next press finish button

Before writing C source code right click project select Build Path -> Configure Build Path after it opend window inside window 

C/C++ Build -> Settings ->Includes -> select New after give path /usr/lib/jvm/java-6-openjdk/include

Next press OK button and Create New C source file using java Header file

After Build C/C++ perspective next return Java perspective

Once Build project

Right click project Run as -> Run Configurations 

Select inside Java Application -> project name -> Arguments -> 

Field VM arguments -Djava.library.path=$UR_PATH/project_name/Debug/

Monday, May 19, 2014

Multiple colours inside a TextView in android

Set color of TextView span in Android  or Highlight text view in android

Hi this post will explain customized TextView in android using Spannable that means using TextView will show Multiple colors in message.

Following file are my source code:

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" >

    <TextView
        android:id="@+id/textview"
        android:textSize="35dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/textView1"
        android:clickable="true"
        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="157dp"
        android:text="TextView" />

</RelativeLayout>


MainActivity.java


package com.example.helloworld;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv=(TextView)findViewById(R.id.textview);
Spannable wordtoSpan = new SpannableString("Algorithms run on OBU, Messages displayed on Android according to the priority.Applications listed on Android based tab");        

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
wordtoSpan.setSpan(new ForegroundColorSpan(Color.MAGENTA), 35, 45, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new ForegroundColorSpan(Color.LTGRAY), 50, 55, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new ForegroundColorSpan(Color.CYAN), 60, 65, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new ForegroundColorSpan(Color.YELLOW), 70, 75, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new ForegroundColorSpan(Color.GREEN), 80, 85, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(wordtoSpan);

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

After running above code using eclipse it will give following screenshot. It will print text different colors (BLUE,MAGENTA,LTGRAY,CYAN,YELLOW,GREEN).


Thursday, May 15, 2014

How do I make links in a TextView clickable in Android?


Current days everybody using web for searching their needs so web-search engine is most popular.
This post is is clearly explainedhow do I make links in a TextView clickable in Android.

Following is my source code .

MainActivity.java

package com.example.hid;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}



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" >

    <TextView
        android:autoLink="web"
        android:linksClickable="true"
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="20dp"
        android:layout_centerVertical="true"
        android:text="@string/web" />

</RelativeLayout>


strings.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">hid</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="web">http://www.google.com</string>

</resources>

After wrote code I am running in my mobile phone using eclipse it giving following screen

                                                     


After clicking user above on link it will trying to open google browser based on internet speed
Above screen is trying to open google page it opened some seconds difference


Finally i got google web page using my application it will useful when ever you need links in your application.



Friday, December 13, 2013

JNI Development in Linux(Ubuntu) command line



for JNI (Java Native Interface)  development we need two languages

1)Java
2)C or C++

Step 1:Open terminal(Ctrl+Alt+T) first next create java file

Prompt@vamsi:~$vi VamsiJni.java

public class VamsiJni{

             static{
                           System.loadLibrary("sum");
                      }
             public native int sum(int  a, int b);
             public static void main(String[] args){
            
           
                    VamsiJni vjni= new VamsiJni();
            System.out.println("sum of two numbers is ...."+vjni.sum(10,45));

}//main is closed 

}//class is closed

Save program

Step 2: Compile java program

Prompt@vamsi:~$javac VamsiJni.java

Now our directory having VamsiJni.class file (This is called Byte file)

Step 3: For generating header file

Prompt@vamsi:~$javah -jni VamsiJni

Now our directory having VamsiJni.h file (This is C Header file for communicating jni syntax)

Step 4: now create c source file

Before creation source file once open header file copy prototype function name

Prompt@vamsi:~$vi sum.c


#include<stdio.h>
#include"VamsiJni.h"
#include<jni.h>

JNIEXPORT jint JNICALL Java_VamsiJni_sum
  (JNIEnv * env, jobject object, jint a, jint b){

        printf("This is sum function inside C");
        return a+b;

}

first save C program

Step 5:now our job is creation shared library using C source code

Prompt@vamsi:~$gcc -I /usr/lib/jvm/java-1.6.0-openjdk/include/ -shared -o libsum.so  sum.c

Now our directory having sum.so

Step 6: now executing java program

Prompt@vamsi:~$java -Djava.library.path=. VamsiJni

Now our output is:

sum of two numbers is ....55
This is sum function inside C







Wednesday, September 18, 2013

Find out cell tower Location using Android API

How to find out cell tower Location in Android pragmatically?

The following code will show the your cell tower location showing

        LocationManager locationmanager;
        String context=Context.LOCATION_SERVICE;
        locationmanager=(LocationManager) getSystemService(context);
        String provider=LocationManager.NETWORK_PROVIDER;
        Location location= locationmanager.getLastKnownLocation(provider);
        String latitude=location.getLatitude();
        String longitude=location.getLongitude()

Saturday, May 4, 2013

CUSTOMIZED TOAST MESSAGE

Toast toast=new Toast(this);
ImageView view =new ImageView(this);
view.setImageResources(R.drawable.icon);
toast.setview(view);
toast.show();

copy following icon in drawable folder with name warn.jpg format[project/res/drawable]


Now it will show following screen but it not showing animations.