Wednesday 11 January 2012

Diffrence between FILL_PARENT and MATCH_PARENT and WRAP_CONTENT in Android

FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher),
which means that the View wants to be as big as its parent (minus padding)

WRAP_CONTENT, which means that the View wants to be just big enough to enclose
its content (plus padding)

Friday 6 January 2012

Log in Android

Every Android developer use Logging during the developing of android applications.
Android provides a great interface to work with the systems logcat.
actually a number of developers don’t turn down their logging when they
go to ship their app to the android market.

This short post demonstrates how to wrap calls to the Log class
to control the amount of information visible in logcat.

The benefit here is you won’t have to worry about removing print lines or
maintaining variables for levels that you might forget
to change before building your release apk in Android market.

Changing the log level on device is actually very simple.
Simply run adb shell setprop log.tag.<YOUR_LOG_TAG> <LEVEL>

In Android Log class contains some methods to print debugging information to the logcat utility.
Logcat is visible in the debug perspective in Eclipse.

Here below is a sample Class showing log level checking prior to printing to the log.

import com.androidsurya.util.Log;

public class MyLogClass {

public static void d(String tag, String msg) {
if (Log.isLoggable(tag, Log.DEBUG)) {
Log.d(tag, msg);
}
}

public static void i(String tag, String msg) {
if (Log.isLoggable(tag, Log.INFO)) {
Log.i(tag, msg);
}
}

public static void e(String tag, String msg) {
if (Log.isLoggable(tag, Log.ERROR)) {
Log.e(tag, msg);
}
}

public static void v(String tag, String msg) {
if (Log.isLoggable(tag, Log.VERBOSE)) {
Log.v(tag, msg);
}
}

public static void w(String tag, String msg) {
if (Log.isLoggable(tag, Log.WARN)) {
Log.w(tag, msg);
}
}
}

MyLogClass is simply calls to the android Log class
Now you can simply reference  
MyLogClass.d(“MyAndroidApp”, “This is sample debug message”);
in your Android activity and if the device/emulator running the app has the log level set
to debug the message will appear.running the app has the log level set to debug the message will appear.

For more information about Log:

http://developer.android.com/reference/android/util/Log.html

Monday 2 January 2012

StrikeTextView in Android

Here we can see how to strike a TextView programmatic way.  
In Android using StrikethroughSpan to draw an underline
for the text on TextView.
 
Through the SpannableString we can StrikethroughSpan the text,
it can be static or dynamic text no problem,  
first we have to set the string to the SpannableString and then
we need to setSpan, and then we need to set that variable
to the TextView.
 
Example:
 UI Layout (activity_strike_text.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" >

 

    <TextView
        android:id="@+id/striketxt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="25px" />

</LinearLayout>

Android Activity class(StrikeText.Class)
 
package com.androidsurya.striketext;

import android.os.Bundle;
import android.app.Activity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.Menu;
import android.widget.TextView;

public class StrikeText extends Activity {

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

       
        SpannableString Strickstr = new SpannableString("This is Stricke text");
        Strickstr.setSpan(new StrikethroughSpan(), 0, Strickstr.length(), Spanned.SPAN_PARAGRAPH);
        TextView StrickstrTv = (TextView) findViewById(R.id.striketxt);
        StrickstrTv.setText(Strickstr);
       
    }

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

}


Register Activity in Android Manifestfile
 <activity
            android:name="com.androidsurya.striketext.StrikeText"
            android:label="@string/app_name" >

Output Screenshot:
 





Sunday 1 January 2012

Underline TextView in Android

Here we can see how to underline a TextView programmatic way.  
In Android using UnderlineSpan to draw an underline for the text on TextView.
 
Through the SpannableString we can underline the text,
it can be static or dynamic text no problem,  
first we have to set the string to the SpannableString and then
we need to setSpan, and then we need to set that variable to the TextView.
 
Example:
 UI Layout (activity_underline_text.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" >

    <TextView
        android:id="@+id/underlinetxt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="25px" />


</LinearLayout>

Android Activity class(UnderlineText.Class)
package com.androidsurya.underlinetext;

import android.app.Activity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.UnderlineSpan;
import android.view.Menu;
import android.widget.TextView;

public class UnderlineText extends Activity {

 @Override
   protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_underline_text);
SpannableString str = new SpannableString("This is Underline text");
str.setSpan(new UnderlineSpan(), 0, str.length(),
Spanned.SPAN_PARAGRAPH);
TextView txtView = (TextView) findViewById(R.id.underlinetxt);
txtView.setText(str);
  
 }

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

}

Register Activity in Android Manifestfile

<activity
            android:name="com.androidsurya.underlinetext.UnderlineText"
            android:label="@string/app_name" >

Output Screenshot:
 

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...