Wednesday 30 May 2012

Text Rotation in Android

In Android we can rotate text view after API Level 11 .

In android sdk using public void setRotation (float rotation) method
on Textview we can rotate Text.
and here rotation parameter is The degrees of rotation.

Example:
In below example we can see diffrence between normal TextView and rotated TextView.

UI Layout(text_roation.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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/rotationtextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="androidsurya(RotationText)"
        android:textColor="#00FF00"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/normaltextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/normaltextview"
        android:layout_alignParentTop="true"
        android:layout_marginTop="18dp"
        android:text="androidsurya(NormalText)"
        android:textColor="#00FF00"
        android:textStyle="bold" />

</RelativeLayout>

Android Activity(TextRoationActivity.java)


package com.androidsurya.textrotation;

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

public class TextRoationActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text_roation);
        TextView rotationTextview = (TextView) findViewById(R.id.rotationtextview);
        rotationTextview.setRotation(85);

    }

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

}

Register Activity in AndroidManifest file


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

Output Screenshot:




Note : In AndroidManifest file minsdk level should be "11"
    <uses-sdk
        android:minSdkVersion="11" />








Tuesday 29 May 2012

How To Get current layout object on Android

In android using getLayoutInflater method in LayoutInflater class we can get current layout object.

Example Code:

 UI Layout (mainlayout.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/darker_gray"
    tools:context=".MainActivity" >
  <Button
                android:id="@+id/button"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                  />

  </RelativeLayout>

Here if we want to get current layout object use below code.
LayoutInflater inflater = getLayoutInflater();
RelativeLayout
root= (RelativeLayout) inflater.inflate(R.layout.mainlayout,null);

And we can access Button  using root object
Button button= (Button) root.findViewById(R.id.record);

Sunday 27 May 2012

How to convert string type ArrayList to string Array

Here the below code is used to convert list of string object(ArrayList with String) to String Array(String[ ])

using toArray( ) method on ArrayList object we can convert arraylist to string[]

ArrayList<String> number_list = new ArrayList<String>();

number_list .add("one");
number_list .add("two");
number_list .add("three");

String[] numberArr = new String[number_list .size()];

numberArr = number_list.toArray(numberArr );

Tuesday 22 May 2012

How to open an URL in Browser Programmatically in Android

In Android using "android.content.Intent” (Intent) to open an specify URL in 
Android’s web browser.

Simple Source Code:
Intent browser_intent= new Intent(Intent.ACTION_VIEW, Uri.parse("http://androidsurya.blogspot.in/"));
startActivity(browser_intent);

Wednesday 16 May 2012

Custom Toast in Android

Some time a simple text message isn't enough, to display toast.
at that time you can create a customized layout for your toast notification.

To create a custom layout, define a View layout, in XML or in your application code,
and pass the root View object to the setView(View) method.

Example Project:

Create layout for custom layout of Toast(toast_layout.xml) in drawable folder


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#DAAA"
    android:orientation="horizontal"
    android:padding="8dp" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF" />

</LinearLayout>

(UI layout for activity(toast_display.xml)) in drawable folder

<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"
    tools:context=".ToastDisplayActivity" >

    <Button
        android:id="@+id/showToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Show Toast" />

</RelativeLayout>

Activity class(ToastDisplayActivity.java)


package com.androidsurya.toastexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ToastDisplayActivity extends Activity {
    Button showToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.toast_display);
        showToast = (Button) findViewById(R.id.showToast);
        showToast.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.toast_layout,
                        (ViewGroup) findViewById(R.id.toast_layout_root));
                TextView text = (TextView) layout.findViewById(R.id.text);
                text.setText("This is a custom toast");
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
            }
        });
    }

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

}

Register Actvity in AndroidManifest file


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

Output Screenshot



 






For more information about Toast: Android Developers Site

Friday 4 May 2012

Toast in Android

A toast provides simple feedback about an operation in a small popup.

First, instantiate a Toast object with one of the makeText() methods.

This method takes three parameters:

 1)application Context,
 2)text message
 3)duration for the toast.

It returns a properly initialized Toast object.

You can display the toast notification with show(), as shown in the following example:

Context context = getApplicationContext();
CharSequence text = "Hello toast message!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();


(Or)

Chain process

Toast.makeText(context, text, duration).show();

Positioning your Toast:

A standard toast notification appears near the bottom of the screen, centered horizontally.
You can change this position with the setGravity(int, int, int) method.
This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.

For example, if you decide that the toast should appear in the top-left corner.

you can set the gravity like this:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

Example Project:

UI Layout(toast_display.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"
    tools:context=".ToastDisplayActivity" >

    <Button
        android:id="@+id/showToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Show Toast" />

</RelativeLayout>

Activity class(ToastDisplayActivity.java)

package com.androidsurya.toastexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ToastDisplayActivity extends Activity {
    Button showToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.toast_display);
        showToast = (Button) findViewById(R.id.showToast);
        showToast.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Toast.makeText(getApplicationContext(), "Toast message Display",
                        Toast.LENGTH_LONG).show();
            }
        });
    }

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

}

Register Activity in AndroidManifest file


 <activity
            android:name="com.androidsurya.toastexample.ToastDisplayActivity"
            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...