The 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:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayoutandroid:orientation="horizontal"android:id="@+id/lcharthart"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="bottom"android:layout_marginTop="60dp"> <View android:id="@+id/view"android:layout_width="35dp"android:layout_height="wrap_content"android:background="#00FF00"> </View> </LinearLayout> </RelativeLayout>
The MainActivity.java is as below
import android.content.Context; import android.graphics.Color; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.LinearLayout; public class MainActivity extends Activity { LinearLayout la; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); la=(LinearLayout) findViewById(R.id.lcharthart); int color[]={ 2,2,2,1,1,3,3,3,3}; int Height[]={100,100,100,200,200,300,300,300,300,200,200,400,400}; for(int j=0;j<color.length;j++) { drawChart(1, color[j], Height[j]); } } private void drawChart(int count, int color, int height) { System.out.println(count+color+height); if(color==1) { color= Color.BLUE; } if(color==2) { color=Color.YELLOW; } if(color==3) { color=Color.GREEN; } if(color==4) { color=Color.MAGENTA; } if(color==5) { color=Color.RED; } for(int k=1; k<= count; k++) { View view= new View(this); view.setBackgroundColor(color); view.setLayoutParams(new LinearLayout.LayoutParams(10, height)); LinearLayout.LayoutParams parms=(LinearLayout.LayoutParams)view.getLayoutParams(); parms.setMargins(3,0,0,0); view.setLayoutParams(parms); la.addView(view); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }