Friday, July 12, 2013

ScrollView practice

I have recently developed a small application that make use of ScrollView and HorizontallScrollView in Android.

It is an application that has 8 colored buttons using RGB + white and CMYK color schemes and when clicked they create text view instance colored with appropriate color, all added to one ScrollView and HorizontallScrollView.

I needed to edit the strings.xml so I did this:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">MySecondSVAndroidApp</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="click_me">Click me!</string>

    <color name="red">#FF0000</color>
    <color name="green">#008000</color>
    <color name="blue">#0000FF</color>
    <color name="black">#000000</color>
    <color name="cyan">#00FFFF</color>
    <color name="magenta">#FF00FF</color>
    <color name="yellow">#FFFF00</color>
    <color name="white">#FFFFFF</color>
</resources>


I added colors to be used on the buttons.

Here is my activity_main.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@color/black" >

            <Button
                android:id="@+id/red_btn"
                android:layout_width="58dp"
                android:layout_height="58dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1dp"
                android:background="@color/red"
                android:text="@string/click_me"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@color/black" >

            <Button
                android:id="@+id/green_btn"
                android:layout_width="58dp"
                android:layout_height="58dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1dp"
                android:background="@color/green"
                android:text="@string/click_me"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@color/black" >

            <Button
                android:id="@+id/blue_btn"
                android:layout_width="58dp"
                android:layout_height="58dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1dp"
                android:background="@color/blue"
                android:text="@string/click_me"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@color/black" >

            <Button
                android:id="@+id/white_btn"
                android:layout_width="58dp"
                android:layout_height="58dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1dp"
                android:background="@color/white"
                android:text="@string/click_me"/>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@color/black" >

            <Button
                android:id="@+id/cyan_btn"
                android:layout_width="58dp"
                android:layout_height="58dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1dp"
                android:background="@color/cyan"
                android:text="@string/click_me"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@color/black" >

            <Button
                android:id="@+id/magenta_btn"
                android:layout_width="58dp"
                android:layout_height="58dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1dp"
                android:background="@color/magenta"
                android:text="@string/click_me"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@color/black" >

            <Button
                android:id="@+id/yellow_btn"
                android:layout_width="58dp"
                android:layout_height="58dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1dp"
                android:background="@color/yellow"
                android:text="@string/click_me"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@color/black" >

            <Button
                android:id="@+id/black_btn"
                android:layout_width="58dp"
                android:layout_height="58dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1dp"
                android:background="@color/black"
                android:textColor="#ffffff"
                android:text="@string/click_me"/>
        </LinearLayout>
    </LinearLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp">

        <LinearLayout
            android:id="@+id/scroll_vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/scroll_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center_vertical"
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>


This look something like this:


 I Started to code but I found that many things are repetitive and also MainActivity.java would get very big and clumsy so I separated all things that can be separated. I added two more classes:

ColorButton - class that holds data and structure about each of the buttons shown on upper image.
ColorButtonTextView - class that holds data and formatting about newly created text view which is added to the vertical and horizontal scrollviews.

Note: I am adding text view object to LinearLayout objects inside ScrollView and HorizontallScrollView. I don't edit them directly.

Here are the classes

ColorButtonTextView

package com.example.mysecondsvandroidapp;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class ColorButtonTextView extends TextView{

    private LayoutParams textViewParams = null;
   
    public ColorButtonTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public ColorButtonTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public ColorButtonTextView(Context context) {
        super(context);
        this.textViewParams = new LayoutParams(100, 100);
        this.textViewParams.setMargins(2, 2, 2, 2);
        this.setLayoutParams(textViewParams);
        this.setWidth(100);
        this.setHeight(100);
    }

}


ColorButton

package com.example.mysecondsvandroidapp;

import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;


public class ColorButton {

    private String textStr;
    private int buttonColor;
    private Button button = null;
    private ColorButtonTextView textView = null;
    private ColorButtonTextView textView1 = null;
    private LinearLayout vertical;
    private LinearLayout horizontal;
   
    public ColorButton(Button button, String text, int color, LinearLayout v, LinearLayout h) {
        super();
        this.textStr = text;
        this.buttonColor = color;
        this.vertical = v;
        this.horizontal = h;
       
        this.button = button;
        this.button.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                textView = new ColorButtonTextView(v.getContext());
                textView.setText(textStr);
                textView.setBackgroundColor(buttonColor);
                vertical.addView(textView);
               
                textView1 = new ColorButtonTextView(v.getContext());
                textView1.setText(textStr);
                textView1.setBackgroundColor(buttonColor);
                horizontal.addView(textView1);
               
                if(buttonColor == Color.BLACK)
                {
                    textView.setTextColor(Color.WHITE);
                    textView1.setTextColor(Color.WHITE);
                }
                else
                {
                    textView.setTextColor(Color.BLACK);
                    textView1.setTextColor(Color.BLACK);
                }
            }
        });
       
    }
   
}


Note: You cannot use same text view instance to be added in both Vertical and Horizontall scroll views aka linear layouts. It will give an error after running the application. So thats why I create two separate instances:  textView and textView1.

MainActivity.java

package com.example.mysecondsvandroidapp;

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

public class MainActivity extends Activity {

    private ColorButton redButton = null;
    private ColorButton greenButton = null;
    private ColorButton blueButton = null;
    private ColorButton whiteButton = null;
    private ColorButton cyanButton = null;
    private ColorButton magentaButton = null;
    private ColorButton yellowButton = null;
    private ColorButton blackButton = null;
    private LinearLayout scrollVertical = null;
    private LinearLayout scrollHorizontal = null;

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

        initView();
    }

    private void initView() {
       
        this.scrollVertical = (LinearLayout) findViewById(R.id.scroll_vertical);
        this.scrollHorizontal = (LinearLayout) findViewById(R.id.scroll_horizontal);
       
        Button redBtn = (Button) findViewById(R.id.red_btn);
        this.redButton = new ColorButton(redBtn, "Red Color", Color.RED, scrollVertical, scrollHorizontal);
       
        Button greenBtn = (Button) findViewById(R.id.green_btn);
        this.greenButton = new ColorButton(greenBtn, "Green Color", Color.GREEN, scrollVertical, scrollHorizontal);
       
        Button blueBtn = (Button) findViewById(R.id.blue_btn);
        this.blueButton = new ColorButton(blueBtn, "Blue Color", Color.BLUE, scrollVertical, scrollHorizontal);
       
        Button whiteBtn = (Button) findViewById(R.id.white_btn);
        this.whiteButton = new ColorButton(whiteBtn, "White Color", Color.WHITE, scrollVertical, scrollHorizontal);
       
        Button cyanBtn = (Button) findViewById(R.id.cyan_btn);
        this.cyanButton = new ColorButton(cyanBtn, "Cyan Color", Color.CYAN, scrollVertical, scrollHorizontal);
       
        Button magentaBtn = (Button) findViewById(R.id.magenta_btn);
        this.magentaButton = new ColorButton(magentaBtn, "Magenta Color", Color.MAGENTA, scrollVertical, scrollHorizontal);
       
        Button yellowBtn = (Button) findViewById(R.id.yellow_btn);
        this.yellowButton = new ColorButton(yellowBtn, "Yellow Color", Color.YELLOW, scrollVertical, scrollHorizontal);
       
        Button blackBtn = (Button) findViewById(R.id.black_btn);
        this.blackButton = new ColorButton(blackBtn, "Black Color", Color.BLACK, scrollVertical, scrollHorizontal);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


Be sure to run this application. I tested it on Nexus S - 4.0" - WVGA (480x800 hdpi)
Clicking on each button creates new text view in the scroll views at the same time. You can scroll them vertically and horizontally.


You can find the code on git hub here: https://github.com/bluePlayer/practices/tree/master/MySecondSVAndroidApp

1 comment: