Search code examples
androidandroid-layoutmpandroidchartandroid-graphview

Android Horizontal Graph Matrix (see atached design)


I try MPChart Android Library but unable to create this type of layout .

There are 1-24 are fixed and 5 type of hours fix, only we have to set data for these 5 hours.

Please see this image and help me for the solution

enter image description here


Solution

  • try this below code:

    activity_graph.xml:

            <?xml version="1.0" encoding="utf-8"?>
                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:weightSum="3"
                    >
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:orientation="vertical"
                        android:weightSum="5"
                        android:layout_weight="1.2"
                        android:layout_height="match_parent">
                        <TextView
                            android:layout_width="match_parent"
                            android:text="TExxt v1"
                            android:gravity="center"
                            android:layout_weight="1"
                            android:layout_height="match_parent" />
                        <TextView
                            android:layout_width="match_parent"
                            android:text="TExxt v2"
                            android:gravity="center"
                            android:layout_weight="1"
                            android:layout_height="match_parent" />
                        <TextView
                            android:layout_width="match_parent"
                            android:text="TExxt v3"
                            android:gravity="center"
                            android:layout_weight="1"
                            android:layout_height="match_parent" />
                        <TextView
                            android:layout_width="match_parent"
                            android:text="TExxt v4"
                            android:gravity="center"
                            android:layout_weight="1"
                            android:layout_height="match_parent" />
                        <TextView
                            android:layout_width="match_parent"
                            android:text="TExxt v5"
                            android:gravity="center"
                            android:layout_weight="1"
                            android:layout_height="match_parent" />
                    </LinearLayout>
    
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:orientation="vertical"
                        android:layout_weight="0.6"
                        android:background="@color/colorAccent"
                        android:layout_height="match_parent">
                        <HorizontalScrollView
                            android:layout_width="match_parent"
                            android:scrollbars="none"
                            android:layout_height="match_parent">
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:orientation="horizontal"
                                android:id="@+id/views"
                                android:padding="1dp"
                                android:layout_height="wrap_content">
                            </LinearLayout>
                        </HorizontalScrollView>
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:weightSum="5"
                            android:orientation="vertical"
                            android:layout_height="match_parent">
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_weight="1"
                                android:layout_height="match_parent">
                            <ProgressBar
                                style="@android:style/Widget.ProgressBar.Horizontal"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center"
                                android:max="100"
                                android:progress="10"
                                android:progressDrawable="@drawable/custom_progressbar"
                                 />
                            </LinearLayout>
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_weight="1"
                                android:layout_height="match_parent">
                                <ProgressBar
                                    style="@android:style/Widget.ProgressBar.Horizontal"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center"
                                    android:max="100"
                                    android:progress="80"
                                    android:progressDrawable="@drawable/custom_progressbar"
                                    />
                            </LinearLayout>
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_weight="1"
                                android:layout_height="match_parent">
                                <ProgressBar
                                    style="@android:style/Widget.ProgressBar.Horizontal"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center"
                                    android:max="100"
                                    android:progress="20"
                                    android:progressDrawable="@drawable/custom_progressbar"
                                    />
                            </LinearLayout>
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_weight="1"
                                android:layout_height="match_parent">
                                <ProgressBar
                                    style="@android:style/Widget.ProgressBar.Horizontal"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center"
                                    android:max="100"
                                    android:progress="30"
                                    android:progressDrawable="@drawable/custom_progressbar"
                                    />
                            </LinearLayout>
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_weight="1"
                                android:layout_height="match_parent">
                                <ProgressBar
                                    style="@android:style/Widget.ProgressBar.Horizontal"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center"
                                    android:max="100"
                                    android:progressDrawable="@drawable/custom_progressbar"
                                    android:progress="45"
                                    />
                            </LinearLayout>
    
                        </LinearLayout>
                    </RelativeLayout>
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:orientation="vertical"
                        android:weightSum="5"
                        android:layout_weight="1.2"
                        android:layout_height="match_parent">
                        <TextView
                            android:layout_width="match_parent"
                            android:text="TExxt v1"
                            android:gravity="center"
                            android:layout_weight="1"
                            android:layout_height="match_parent" />
                        <TextView
                            android:layout_width="match_parent"
                            android:text="TExxt v2"
                            android:gravity="center"
                            android:layout_weight="1"
                            android:layout_height="match_parent" />
                        <TextView
                            android:layout_width="match_parent"
                            android:text="TExxt v3"
                            android:gravity="center"
                            android:layout_weight="1"
                            android:layout_height="match_parent" />
                        <TextView
                            android:layout_width="match_parent"
                            android:text="TExxt v4"
                            android:gravity="center"
                            android:layout_weight="1"
                            android:layout_height="match_parent" />
                        <TextView
                            android:layout_width="match_parent"
                            android:text="TExxt v5"
                            android:gravity="center"
                            android:layout_weight="1"
                            android:layout_height="match_parent" />
                    </LinearLayout>
    
    
                </LinearLayout>
    

    Graph Activity:

                import android.graphics.Color;
                import android.support.v7.app.AppCompatActivity;
                import android.os.Bundle;
                import android.util.Log;
                import android.view.View;
                import android.view.ViewGroup;
                import android.widget.LinearLayout;
    
                public class GraphActivity extends AppCompatActivity {
    
                    private LinearLayout views;
                    @Override
                    protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_graph);
                        views = (LinearLayout)findViewById(R.id.views);
                        addViews();
                    }
                    private void addViews(){
                        views.removeAllViews();
                        for(int i =0;i<24;i++){
                            views.addView(getLinearLayoutView());
                        }
                    }
    
                    private LinearLayout getLinearLayoutView(){
                        LinearLayout linearLayout = new LinearLayout(this);
                        linearLayout.setOrientation(LinearLayout.VERTICAL);
                        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    
                        int h = getResources().getDisplayMetrics().heightPixels/6;
                        Log.d("DevicdInfo", "Device info: "+h +" / "+getResources().getDisplayMetrics().heightPixels);
                        for(int j=0;j<5;j++){
                            View v = new View(this);
                            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(h,h);
                            lp.setMargins(0,0,10,10);
                            v.setLayoutParams(lp);
                            v.setPadding(0,0,10,10);
                            v.setBackgroundColor(Color.WHITE);
                            linearLayout.addView(v);
                        }
                        return linearLayout;
                    }
                }
    

    custom_progressbar.xml:

         <?xml version="1.0" encoding="utf-8"?>
                <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
                    <item android:id="@android:id/background">
                        <shape>
                            <gradient
                                android:startColor="@android:color/transparent"
                                android:centerColor="@android:color/transparent"
                                android:endColor="@android:color/transparent"
                                android:angle="270"
                                />
                        </shape>
                    </item>
    
    
                    <item android:id="@android:id/progress">
                        <clip>
                            <shape>
                                <gradient
                                    android:startColor="#ff0000"
                                    android:centerColor="#ff0000"
                                    android:endColor="#ff0000"
                                    android:angle="270"
                                    />
                            </shape>
                        </clip>
                    </item>
                </layer-list>
    

    Mani-fest changes:

          <activity android:name=".GraphActivity"
            android:screenOrientation="landscape"></activity>