Thursday, 25 July 2013

ViewPager with PageNumbers Example in Android

ViewPager is a Layout manager that allows the user to flip left and right through pages of data.
You supply an implementation of a PagerAdapter to generate the pages that the view shows.

Here we can see how to add page numbers in to a ViewPager.
This Example create 6 pages and show pagenumbers in Textview.

UI layout(pages.xml)

This layout is used to show pagenumber in ViewPager.


<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"
    android:id="@+id/rel" >

    <TextView
        android:id="@+id/pagenumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

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

    <RelativeLayout
        android:id="@+id/relativeTextview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/header"
        android:padding="5dp" >

        <android.support.v4.view.ViewPager
            android:id="@+id/reviewpager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </RelativeLayout>

</RelativeLayout>


Android Activity (ViewPagerAdapter.java)

This calss supply an implementation of a PagerAdapter to generate the pages that the view shows


package com.androidsurya.androidviewpager;

import android.app.Activity;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ViewPagerAdapter extends PagerAdapter {
    int size;
    Activity act;
    View layout;
    TextView pagenumber;
    Button click;

    public ViewPagerAdapter(MainActivity mainActivity, int noofsize) {
        // TODO Auto-generated constructor stub
        size = noofsize;
        act = mainActivity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return size;
    }

    @Override
    public Object instantiateItem(View container, int position) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) act
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = inflater.inflate(R.layout.pages, null);
        pagenumber = (TextView) layout.findViewById(R.id.pagenumber);
        int pagenumberTxt=position + 1;
        pagenumber.setText("Now your in Page No  " +pagenumberTxt );
        ((ViewPager) container).addView(layout, 0);
        return layout;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    // }

}

Android Activity (MainActivity.java)

Here in MainActivity we are creating object to ViewPagerAdapter class
and set Adapter to ViewPager object using setAdapter method.


package com.androidsurya.androidviewpager;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends Activity {
   
          int noofsize = 6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this,
                noofsize);
        ViewPager myPager = (ViewPager) findViewById(R.id.reviewpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
    }

    @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_main, menu);
        return true;
    }

}

Register the Activity in AndroidManifest file


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

Output Screenshots






































https://docs.google.com/file/d/0B4r4Yoc9-rj3SnhTclktYVdTWjQ/edit?usp=sharing




For More information about ViewPager: Android Developers site

Draw Line Chart in Android using achartengine

Here Below showlineChart() method will be create piechart using Achartengine in Android.
In  showlineChart()  method org.achartengine.GraphicalActivity class is Responsable to create LineChart.


Android Activity(MainActivity.java)
package com.androidsurya.achartengine;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

public class MainActivity extends Activity {

private String[] mMonth = new String[] {
"Jan", "Feb" , "Mar", "Apr", "May", "Jun",
};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showlineChart();
    }  
    private void showlineChart(){
    int[] x = { 1,2,3,4,5};
    int[] income = { 240,467,259,570,500};
    int[] expense = {200, 521, 290, 219, 457};
   
    // Creating an  XYSeries for Income
    XYSeries incomeSeries = new XYSeries("Income");
    // Creating an  XYSeries for Income
    XYSeries expenseSeries = new XYSeries("Expenses");
    // Adding data to Income and Expense Series
    for(int i=0;i<x.length;i++){
    incomeSeries.add(x[i], income[i]);
    expenseSeries.add(x[i],expense[i]);
    }
   
    // Creating a dataset to hold each series
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    // Adding Income Series to the dataset
    dataset.addSeries(incomeSeries);
    // Adding Expense Series to dataset
    dataset.addSeries(expenseSeries);    
   
   
    // Creating XYSeriesRenderer to customize incomeSeries
    XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
    incomeRenderer.setColor(Color.WHITE);
    incomeRenderer.setPointStyle(PointStyle.CIRCLE);
    incomeRenderer.setFillPoints(true);
    incomeRenderer.setLineWidth(2);
    incomeRenderer.setDisplayChartValues(true);
   
    // Creating XYSeriesRenderer to customize expenseSeries
    XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
    expenseRenderer.setColor(Color.BLUE);
    expenseRenderer.setPointStyle(PointStyle.CIRCLE);
    expenseRenderer.setFillPoints(true);
    expenseRenderer.setLineWidth(3);
    expenseRenderer.setDisplayChartValues(true);
   
   
    // Creating a XYMultipleSeriesRenderer to customize the whole chart
    XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
    multiRenderer.setXLabels(0);
    multiRenderer.setChartTitle("Income vs Expense Chart");
    multiRenderer.setXTitle("Year 2011");
    multiRenderer.setYTitle("Amount in Rupees");
    multiRenderer.setZoomButtonsVisible(true);        
    for(int i=0;i<x.length;i++){
    multiRenderer.addXTextLabel(i+1, mMonth[i]);    
    }    
   
    // Adding incomeRenderer and expenseRenderer to multipleRenderer
    // Note: The order of adding dataseries to dataset and renderers to multipleRenderer
    // should be same
    multiRenderer.addSeriesRenderer(incomeRenderer);
    multiRenderer.addSeriesRenderer(expenseRenderer);
   
    // Creating an intent to plot line chart using dataset and multipleRenderer
    Intent intent = ChartFactory.getLineChartIntent(getBaseContext(), dataset, multiRenderer);
   
    // Start Activity
    startActivity(intent);
   
    }



}
Android Manifest file

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

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

Note: 
 Don't Forget Register this Below Activity In Manifest File
   <activity android:name="org.achartengine.GraphicalActivity" />

Output Screenshot:

















Wednesday, 24 July 2013

Android 4.3 Jelly Bean : New Features announced in San Francisco

By Suhel Sayyad Thursday, July 25, 2013 Android , Android 4.3 , Features , Nexus 7 Leave a Comment
In Android 4.2, Google added the ability to created additional users for a tablet, much the same way that Windows handles users. In Android 4.3, device owners can create restricted profiles that have limits on what a user can do. For users that have skinned versions of Android, this will probably be pretty boring, but stock Android can now auto complete names and phone numbers directly from the dialer. Wi-Fi often does double-duty as a location service if you don't want to leave GPS on all the time. If you switch off Wi-Fi to save battery, though, this brings location services down with it. In Android 4.3, your device can continue scanning for Wi-Fi in a more passive mode that uses much less battery, but still pings for networks so you can keep location-based features. It is available in Nexus 7


Here are the new features in the latest Android .


1. Bluetooth Smart
2. OpenGL ES 3.0 for better 3D graphics
3. DRM APIs
4. Restricted profiles
5. Easier text input
6. Faster user-switching
7. Support for Hebrew and Arabic right-to-left languages
8. Bluetooth AVRCP
9. Background WiFi location
10. Dial pad autocomplete
11. Support for Hindi, Africaans, Amharic, Swahili and Zulu languages

For More information   Android Developers Blog

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...