Sunday, May 26, 2013

Android Custom Bar Chart | Dynamic Chart with different color in Android | Bar Chart Sample Program in Android

Hello Friends,
I am sharing very important code for draw bar chart in Android in different color and size.
I have one more article on simple bar chart but this one is more good.

1)Print Screen


2)MainActivity

package com.manish.barchartdemo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
 LinearLayout linearChart;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  linearChart = (LinearLayout) findViewById(R.id.linearChart);
  int colerloop[] = { 1, 2, 2, 2, 3, 3, 3, 3, 1, 1 };
  int heightLoop[] = { 300, 200, 200, 200, 100, 100, 100, 100, 300, 300 };
  for (int j = 0; j < colerloop.length; j++) {
   drawChart(1, colerloop[j], heightLoop[j]);
  }
 }

 public void drawChart(int count, int color, int height) {
  System.out.println(count + color + height);
  if (color == 3) {
   color = Color.RED;
  } else if (color == 1) {
   color = Color.BLUE;
  } else if (color == 2) {
   color = Color.GREEN;
  }

  for (int k = 1; k <= count; k++) {
   View view = new View(this);
   view.setBackgroundColor(color);
   view.setLayoutParams(new LinearLayout.LayoutParams(25, height));
   LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view
     .getLayoutParams();
   params.setMargins(3, 0, 0, 0); // substitute parameters for left,
           // top, right, bottom
   view.setLayoutParams(params);
   linearChart.addView(view);
  }
 }
}

3)activity_main

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

    <LinearLayout
        android:id="@+id/linearChart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/view"
        android:gravity="bottom"
        android:layout_marginTop="50dp" >
    </LinearLayout>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_alignParentBottom="true"
        android:background="#00FF00" />

</RelativeLayout>


Thanks!

You may like(simple bar chart)-
http://www.androidhub4you.com/2013/05/bar-chart-example-in-android-simple-bar.html

Android Bar Chart Example | Simple Bar Chart Demo in Android | Charts in Android, Java

Hello Friends!
Today i am going to share very simple code for Draw Bar Chart in Android.
*No Library needed.
*No Canvas needed.
Just copy paste below code and enjoy :)

1)Print Screen


2)MainActivity


package com.manish.barchartdemo;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
LinearLayout linearChart;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		linearChart=(LinearLayout)findViewById(R.id.linearChart);
		drawChart(5);
	}

	
	public void drawChart(int count) {
		System.out.println(count);
		
		for (int k = 1; k <= count; k++) {
			View view = new View(this);
			view.setBackgroundColor(Color.parseColor("#ff6233"));
			view.setLayoutParams(new LinearLayout.LayoutParams(30, 400));
			LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view
					.getLayoutParams();
			params.setMargins(5, 0, 0, 0); // substitute parameters for left,top, right, bottom
			view.setLayoutParams(params);
			linearChart.addView(view);
		}
	}
}

3)activity_main


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

    <LinearLayout
        android:id="@+id/linearChart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/view"
        android:layout_marginTop="50dp" >
    </LinearLayout>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_alignParentBottom="true"
        android:background="#00FF00" />

</RelativeLayout>

Thanks!

You may like(Custom Bar Chart)-
http://www.androidhub4you.com/2013/05/custom-bar-chart-in-android-dynamic.html


Friday, May 24, 2013

Date Piker Example In Android | Simple Date Piker Demo in Android | DatePikerDialog sample Code

Sample code for date piker in android using DatePickerDialog-

1-Print Screen

   

2-MainActivity.java

package com.manish.datepikerdemo;

import java.util.Calendar;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity {
	TextView textDate;
	private int year;
	private int month;
	private int day;
	static final int DATE_DIALOG_ID = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textDate = (TextView) findViewById(R.id.text_date);

		final Calendar c = Calendar.getInstance();
		year = c.get(Calendar.YEAR);
		month = c.get(Calendar.MONTH);
		day = c.get(Calendar.DAY_OF_MONTH);
		textDate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				showDialog(DATE_DIALOG_ID);
			}
		});
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener, year, month,
					day);
		}
		return null;
	}

	// updates the date we display in the TextView
	private void updateDisplay() {
		/*
		 * Hide virtual keyboard
		 */
		textDate.setText(new StringBuilder()
				// Month is 0 based so add 1
				.append(year).append("-").append(month + 1).append("-")
				.append(day).append(""));
	}

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int myear, int monthOfYear,
				int dayOfMonth) {
			year = myear;
			month = monthOfYear;
			day = dayOfMonth;
			updateDisplay();
		}
	};
}

3-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" >

    <TextView
        android:id="@+id/text_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:textSize="22sp"
        android:textColor="#FF0000"
        android:text="Pick Date" />

</RelativeLayout>
Please put your comment for more help...
Thanks!

Tuesday, May 21, 2013

Zoom Image Demo In Android | Zoom Image Example with code in Android | MotionEvent, Matrix example in Android

Hello friends!
Today I am going to share zoom image code in Android using TouchListner, MotionEvent, Matrix.
Please copy paste below code and enjoy!

1-Print Screen

   
2-MainActivity

package com.manish.zoomimage;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {
 ImageView imageDetail;
 Matrix matrix = new Matrix();
 Matrix savedMatrix = new Matrix();
 PointF startPoint = new PointF();
 PointF midPoint = new PointF();
 float oldDist = 1f;
 static final int NONE = 0;
 static final int DRAG = 1;
 static final int ZOOM = 2;
 int mode = NONE;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  imageDetail = (ImageView) findViewById(R.id.imageView1);
  /**
   * set on touch listner on image
   */
  imageDetail.setOnTouchListener(new View.OnTouchListener() {

   @Override
   public boolean onTouch(View v, MotionEvent event) {

    ImageView view = (ImageView) v;
    System.out.println("matrix=" + savedMatrix.toString());
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:

     savedMatrix.set(matrix);
     startPoint.set(event.getX(), event.getY());
     mode = DRAG;
     break;

    case MotionEvent.ACTION_POINTER_DOWN:

     oldDist = spacing(event);

     if (oldDist > 10f) {
      savedMatrix.set(matrix);
      midPoint(midPoint, event);
      mode = ZOOM;
     }
     break;

    case MotionEvent.ACTION_UP:

    case MotionEvent.ACTION_POINTER_UP:
     mode = NONE;

     break;

    case MotionEvent.ACTION_MOVE:
     if (mode == DRAG) {
      matrix.set(savedMatrix);
      matrix.postTranslate(event.getX() - startPoint.x,
        event.getY() - startPoint.y);
     } else if (mode == ZOOM) {
      float newDist = spacing(event);
      if (newDist > 10f) {
       matrix.set(savedMatrix);
       float scale = newDist / oldDist;
       matrix.postScale(scale, scale, midPoint.x, midPoint.y);
      }
     }
     break;

    }
    view.setImageMatrix(matrix);

    return true;
   }

   @SuppressLint("FloatMath")
   private float spacing(MotionEvent event) {
    float x = event.getX(0) - event.getX(1);
    float y = event.getY(0) - event.getY(1);
    return FloatMath.sqrt(x * x + y * y);
   }

   private void midPoint(PointF point, MotionEvent event) {
    float x = event.getX(0) + event.getX(1);
    float y = event.getY(0) + event.getY(1);
    point.set(x / 2, y / 2);
   }
  });

 }

}

3-activity_main

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="matrix"
        android:src="@drawable/images" />

</RelativeLayout>

Thanks!

Thursday, May 2, 2013

Simple Scroll-View example in Android | Vertical Scroll View Demo in Android | Scroll View in Android

Hello Friends, On a specific demand I am going to share very simple code for vertical scroll view in android. Hope it will help you-

1)MainActivity.java


package com.example.scrollviewdemo;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

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

}

2)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" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/imageView1" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <Button
                android:id="@+id/button1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button5"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button6"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button7"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button8"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button9"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button10"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button11"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button12"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button13"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />

            <Button
                android:id="@+id/button14"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Button" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

Thanks,