Tuesday 26 November 2013

Prevent Activity Restarting when orientation changes

Add following tag  under Activity tag in AndroidManifest.xml

android:configChanges="orientation|screenSize"

Wednesday 20 November 2013

Implicit Intent video tutorial

ActiviyLifecycle example video tutorial

IntentService Example in Android

IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.


Check output in LogCat.

step1: create a new project.
Step2: create new java class that extends with IntentService class.
step3: Call IntentService in Activity using startService() method.


ProjectName : IntentService
XML Layouts : activity_main.xml
Java Classes: MainActivity, MyService

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

    <string name="app_name">IntentService</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="button_title">Start Service</string>

</resources>

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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="94dp"
        android:onClick="startService"
        android:text="@string/button_title" />

</RelativeLayout>

MyService.java:
package com.ram.intentservice;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class MyService extends IntentService {

public MyService() {
super("helloservice");
// TODO Auto-generated constructor stub
}

@Override
public void onCreate() {
super.onCreate();
Log.d("Intent Service", "service created");
}

@Override
protected void onHandleIntent(Intent intent) {
Log.d("Intent Service", "service started");
synchronized (intent) {

}
}

}

MainActivity.java:
package com.ram.intentservice;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

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

public void startService(View v) {
Intent in = new Intent(getApplicationContext(), MyService.class);
startService(in);
}

}



Wednesday 13 November 2013

RadioButton in Android

Here i'm giving another example on RadioButton.

Output Screen Shots:




Project Name: WorkingwithRadioButton
XML files : activity_main.xml
Java files:MainActivity.java
values folder files: strings.xml 



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

    <string name="app_name">working with RadioButton</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="radio_male">Male</string>
    <string name="radio_female">Female</string>

</resources>

 activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="46dp" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_male" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_female" />
    </RadioGroup>

</LinearLayout>

MainActivity.java:
package com.ram.workingwithradiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {

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

 RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);

// Register a callback to be invoked when the checked radio button  changes in this group.

rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

RadioButton rb = (RadioButton) findViewById(checkedId);

String gender = rb.getText().toString();

Toast.makeText(getApplicationContext(), gender,
Toast.LENGTH_LONG).show();
}
});
}

}

Wednesday 6 November 2013

Facing problem in Google Maps Android API V2

Hi to all ,
Recently from few days back on-wards (i mean from October 28th) i checked my new maps v2 applications in  my mobile device they are not running. keep on i'm fighting with that i got solution. here i'm going ti keep that one.
Those who are now on wards developing maps v2 projects you need to keep the following tag under your application tag.


in your AndroidManifest.xml file include the following tag under application tag.

<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

or you can see my updated code on mapsv2 here: http://ramsandroid4all.blogspot.in/2013/03/google-maps-android-api-v2.html