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.