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

Thursday, 31 October 2013

Android 4.4 KitKat realesed

checkout the highlights:

Making Android for everyone
Android 4.4 is designed to run fast, smooth, and responsively on a much broader range of devices than ever before — including on millions of entry-level devices around the world that have as little as 512MB RAM.

New NFC capabilities through Host Card Emulation

  • Printing framework
  • Storage access framework
  • Low-power sensors
  • SMS provider
  • New ways to build beautiful apps
  • New media capabilities
Remaining i will update soon...

Tuesday, 29 October 2013

XML PullParser

 1.There are two key methods: next() and nextToken(). While next() provides access  to high level    
     parsing events, nextToken()  allows access to lower level tokens.

2.The current event state of the parser can be determined by calling the getEventType()   
    method.Initially, the parser is in the  START_DOCUMENT state 

3.The method next() advances the parser to the next event.

The following event types are seen by next()
START_TAG: An XML start tag was read.
TEXT: Text content was read; the text content can be retrieved using the getText() method.
END_TAG: An end tag was read

END_DOCUMENT: No more events are available


Example:
public class SimpleXmlPullApp
 {

     public static void main (String args[])
         throws XmlPullParserException, IOException
     {
         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         factory.setNamespaceAware(true);
         XmlPullParser xpp = factory.newPullParser();

         xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
         int eventType = xpp.getEventType();
         while (eventType != XmlPullParser.END_DOCUMENT) {
          if(eventType == XmlPullParser.START_DOCUMENT) {
              System.out.println("Start document");
          } else if(eventType == XmlPullParser.START_TAG) {
              System.out.println("Start tag "+xpp.getName());
          } else if(eventType == XmlPullParser.END_TAG) {
              System.out.println("End tag "+xpp.getName());
          } else if(eventType == XmlPullParser.TEXT) {
              System.out.println("Text "+xpp.getText());
          }
          eventType = xpp.next();
         }
         System.out.println("End document");
     }
 }



Example Application:

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:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>


MainActivity.java:
package com.ram.xmlpullparser;

import java.io.IOException;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
    TextView text;

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

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        text = (TextView) findViewById(R.id.textView1);

        try {
            parse();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    void parse() throws IOException, XmlPullParserException {

        URL url = new URL("http://www.w3schools.com/xml/note.xml");

        // Create a new instance of a PullParserFactory that can be used to
        // create XML pull parsers

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

        // Specifies that the parser produced by this factory will provide
        // support for XML namespaces. By default the value of this is set to
        // false.

        factory.setNamespaceAware(true);

        // Creates a new instance of a XML Pull Parser using the currently
        // configured factory features

        XmlPullParser xpp = factory.newPullParser();

        // Sets the input stream the parser is going to process. This call
        // resets the parser state and sets the event type to the initial value
        // START_DOCUMENT.

        xpp.setInput(url.openStream(), null);

        // Returns the type of the current event (START_TAG, END_TAG, TEXT,
        // etc.)

        int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {

               //  text.append(xpp.getName());
            } else if (eventType == XmlPullParser.TEXT) {

                text.append(xpp.getText());
            } else if (eventType == XmlPullParser.END_TAG) {

                // text.append(xpp.getName());
            }
            eventType = xpp.next();
        }
    }
}


AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ram.xmlpullparser"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ram.xmlpullparser.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


ScreenShot: