Showing posts with label TextView. Show all posts
Showing posts with label TextView. Show all posts

Monday, August 27, 2012

Android | Seekbar | Color Maker

This is another example of Seekbar. i used it to develop color maker.

As you know, color is combination of 4 elements, Alpha, Red, Green and Blue. Alpha is used to set value between transparency and opaque states.

I have verified the code in Android 4.0.3 emulator.

Code of Layout File:


<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:id="@+id/llcolormaker" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvalphacolor"
        android:textColor="#FFFFFFFF"
        />  

    <SeekBar
        android:id="@+id/sbalpha"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="255"  />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvredcolor"
        android:textColor="#FFFFFFFF"
        />

    <SeekBar
        android:id="@+id/sbred"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="0"  />
       
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvgreencolor"
        android:textColor="#FFFFFFFF"
        />

    <SeekBar
        android:id="@+id/sbgreen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="0"  />  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvbluecolor"
        android:textColor="#FFFFFFFF"
        />

    <SeekBar
        android:id="@+id/sbblue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="0"  />  
</LinearLayout>

Code of Strings.xml file:


<resources>

    <string name="app_name">ColorMakerExample</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_color_maker_example">Color Maker Example</string>

    <string name="tvalphacolor">Select Alpha :</string>
    <string name="tvredcolor">Select Red Color :</string>
    <string name="tvgreencolor">Select Green Color :</string>
    <string name="tvbluecolor">Select Blue Color :</string>
</resources>

code of Java file:



package com.example.colormakerexample;

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

public class ColorMakerExample extends Activity implements OnSeekBarChangeListener {

SeekBar sbAlpha, sbRed, sbGreen, sbBlue;
LinearLayout llColorMaker;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_maker_example);
       
        sbAlpha = (SeekBar) findViewById(R.id.sbalpha);
        sbRed = (SeekBar) findViewById(R.id.sbred);
        sbGreen = (SeekBar) findViewById(R.id.sbgreen);
        sbBlue = (SeekBar) findViewById(R.id.sbblue);
        llColorMaker = (LinearLayout) findViewById(R.id.llcolormaker);
       
        sbAlpha.setOnSeekBarChangeListener(this);
        sbRed.setOnSeekBarChangeListener(this);
        sbGreen.setOnSeekBarChangeListener(this);
        sbBlue.setOnSeekBarChangeListener(this);
       

        llColorMaker.setBackgroundColor(Color.argb(sbAlpha.getProgress(), sbRed.getProgress(), sbGreen.getProgress(), sbBlue.getProgress()));
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_color_maker_example, menu);
        return true;
    }


public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub

        llColorMaker.setBackgroundColor(Color.argb(sbAlpha.getProgress(), sbRed.getProgress(), sbGreen.getProgress(), sbBlue.getProgress()));

}


public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}

public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
sbAlpha = sbRed = sbGreen = sbBlue = null;
super.onDestroy();
}
}



Result:




Android | Seekbar Example

Seekbar is widget and we can use to select the value from selected range.

max property will set the max value for seekbar and progress property will give the current value of where seekbar positioned. ensure, seekbar starts with "0".

below is example of Seekbar which i have verified on Android 4.0.3 emulator.

Code of Layout File:


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

    <TextView
        android:id="@+id/tvvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvvalue"
/>
   
    <SeekBar
        android:id="@+id/sbvalue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="99"
        android:progress="0"/>

</LinearLayout>

Code of Strings.xml File:


<resources>

    <string name="app_name">SeekBarExample</string>
    <string name="tvvalue"></string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_seek_bar_example">SeekBar Example</string>

</resources>


Source code of .JAVA file:


package com.example.seekbarexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class SeekBarExample extends Activity implements OnSeekBarChangeListener {

private TextView tvValue;
private SeekBar sbValue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek_bar_example);
       
        tvValue = (TextView) findViewById(R.id.tvvalue);
        sbValue = (SeekBar) findViewById(R.id.sbvalue);
       
        sbValue.setOnSeekBarChangeListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_seek_bar_example, menu);
        return true;
    }

public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub

tvValue.setText(String.valueOf(arg1));


}

public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}

public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}
}

Result:




Sunday, August 19, 2012

Android - TextView Example

In Android, There are UI components which are used to develop more interactive interface. To develop user friendly interface we need to explore each component.

Below is example for Textview, it can be used to display static text in the application. It is having more features and through which we can develop interface like text editor.

I used Eclipse as development tool and tested the sample in Emulator for Android 4.0.3.

There are lot of resources are used to develop android app. but for this example, we are using three basis components like code base file, layout file and string file.

Below is code of Layout file.


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

    <TextView
android:id="@+id/tvtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvtitle"
        tools:context=".TextViewExample" />

    <Button
        android:id="@+id/btnok"
        android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnok"
/>
</LinearLayout>

Code of Strings.xml file:


<resources>

    <string name="app_name">TextViewExample</string>
    <string name="tvtitle"></string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_text_view_example">Text View Example</string>
    <string name="btnok">Click Me</string>

</resources>


Code of Java file (Source):


package com.example.textviewexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TextViewExample extends Activity {

Button btnOk;
TextView tvTitle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view_example);
        
        btnOk = (Button) findViewById(R.id.btnok);
        tvTitle = (TextView) findViewById(R.id.tvtitle);
        
        onButtonClickListener();
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_text_view_example, menu);
        return true;
    }
    
    public void onButtonClickListener(){
    btnOk.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub
tvTitle.setText("Hi Friends!!!");
}
});
    }
}



Note: click on button and it will show you "Hi Friends!!!" text in textview.