Created
May 31, 2018 10:55
-
-
Save imhardiklakhani/60b940fac1cc7f9b12672f804563c941 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout 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:orientation="vertical" | |
tools:context="growwithus.com.chart.MainActivity"> | |
<com.chart.BarView | |
android:id="@+id/bar_view" | |
android:layout_width="match_parent" | |
android:layout_height="150dp" | |
android:layout_gravity="center_vertical" | |
android:background="@android:color/white" | |
android:gravity="bottom" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.chart; | |
import android.content.Context; | |
import android.graphics.Color; | |
import android.support.annotation.Nullable; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.view.Gravity; | |
import android.view.View; | |
import android.view.ViewTreeObserver; | |
import android.view.animation.Animation; | |
import android.view.animation.ScaleAnimation; | |
import android.widget.LinearLayout; | |
import java.util.ArrayList; | |
/** | |
* Created by Hardik Lakhani on 18/05/18/ | |
*/ | |
public class BarView extends LinearLayout { | |
private OnBarClickListener onBarClickListener; | |
public BarView(Context context) { | |
super(context); | |
init(); | |
} | |
public BarView(Context context, @Nullable AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public BarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
private void init() { | |
setOrientation(HORIZONTAL); | |
setGravity(Gravity.BOTTOM); | |
} | |
public void setupBars(final ArrayList<Integer> barDetails, final String barColor) { | |
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { | |
@Override | |
public void onGlobalLayout() { | |
getViewTreeObserver().removeOnGlobalLayoutListener(this); | |
Log.i("BarView", "onGlobalLayout: " + getHeight()); | |
for (int i = 0; i < barDetails.size(); i++) { | |
Float mFloat = (float) (getHeight() / 100.0) * barDetails.get(i); | |
final int height = Math.round(mFloat); | |
Log.i("BarView", " " + height); | |
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height, 1f); | |
params.setMarginEnd(5); | |
params.setMarginStart(5); | |
View btn = new View(getContext()); | |
btn.setLayoutParams(params); | |
btn.setBackgroundColor(Color.parseColor(barColor)); | |
ScaleAnimation animation = new ScaleAnimation(0.75f, 1.0f, 0.25f, 1.0f, Animation.ABSOLUTE, 0, | |
Animation.RELATIVE_TO_SELF, 1); | |
animation.setDuration(1500); | |
btn.startAnimation(animation); | |
final int finalI = i; | |
btn.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.i("BarView", "onClick: " + barDetails.get(finalI)); | |
if (onBarClickListener != null) { | |
onBarClickListener.onBarCLicked(finalI, barDetails.get(finalI)); | |
} | |
} | |
}); | |
addView(btn); | |
} | |
} | |
}); | |
} | |
public void setOnBarClickListener(OnBarClickListener onBarClickListener) { | |
this.onBarClickListener = onBarClickListener; | |
} | |
public interface OnBarClickListener { | |
public void onBarCLicked(int position, int percentage); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.chart; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
BarView barView = (BarView) findViewById(R.id.bar_view); | |
//Here you have to pass the Integer ArrayList with percentage of bars | |
//Here number of bars will be automatically counted according to your array list size. | |
//In the second argument you have to pass the hex code color of Bars. | |
barView.setupBars(getBarDetails(), "#FF0000"); | |
//By Using this you can get a Bar click with position and percentage | |
barView.setOnBarClickListener(new BarView.OnBarClickListener() { | |
@Override | |
public void onBarCLicked(int position, int percentage) { | |
Toast.makeText(MainActivity.this, position + " " + percentage, Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
private ArrayList<Integer> getBarDetails() { | |
ArrayList<Integer> bars = new ArrayList<>(); | |
bars.add(10); | |
bars.add(15); | |
bars.add(20); | |
bars.add(30); | |
bars.add(8); | |
bars.add(10); | |
bars.add(50); | |
bars.add(60); | |
bars.add(45); | |
bars.add(60); | |
bars.add(25); | |
bars.add(90); | |
bars.add(28); | |
bars.add(12); | |
bars.add(100); | |
return bars; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment