0 comments

Android set difference color for textView

Android textView you can set the text Italic or Bold or set The text Color for everywhere you want.

this is you string.xml
<string name="description">Your description where you want to set the red color <i>here you want to set italic</i> <b> here is your Bold text here</b></string>

your main.xml


    



and this is your java file
package monstercodz.blogspot.com;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

public class AndroidSetTextViewDifferenceColorActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SpannableStringBuilder text  = new SpannableStringBuilder(getResources().getText(R.string.description));
        int start = 32;
        int end = 52;
        TextView display = (TextView)findViewById(R.id.display);
        text.setSpan(new ForegroundColorSpan(Color.RED),start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        display.setText(text);
        //display.setTextColor(Color.BLUE);
        
    }
}

you can download the example project by Click here
2 comments

Android vertical seekbar example

In Android there is no vertical seekbar . so we can use rotate the horizontal seekbar by 90 degrees. so this tutorial will teach you how to use it.



you need to create a class call VerticalSeekBar.java and extends from Seekbar like this.
package monstercodz.blogspot.com;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;

public class VerticalSeekBar extends SeekBar {

    public VerticalSeekBar(Context context) {
        super(context);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas c) {
     c.rotate(90);
     c.translate(0, - getWidth());
     
        super.onDraw(c);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
             setProgress((int) (getMax() * event.getY() / getHeight()) - 0);
                onSizeChanged(getWidth(), getHeight(), 0, 0);
                break;

            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return true;
    }
}

in your main.xml






and finally your
AndroidVerticalSeekbarExampleActivity.java
package monstercodz.blogspot.com;

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

public class AndroidVerticalSeekbarExampleActivity extends Activity implements OnSeekBarChangeListener {
 private TextView tv;
 private VerticalSeekBar seekbar;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView)findViewById(R.id.tv_value);
        seekbar = (VerticalSeekBar)findViewById(R.id.seekbar);
        tv.setText(String.valueOf(seekbar.getProgress()) + "/" + String.valueOf(seekbar.getMax()));
        seekbar.setMax(100);
        seekbar.setOnSeekBarChangeListener(this);
    }
 @Override
 public void onProgressChanged(SeekBar seekBar, int progress,
   boolean fromUser) {
  // TODO Auto-generated method stub
  tv.setText(String.valueOf(seekbar.getProgress()) + "/" + String.valueOf(seekbar.getMax()));
 }
 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {
  // TODO Auto-generated method stub
  
 }
}

you can download the sample code by Click here