Tuesday, 28 August 2012

Difference between dp,dip, sp,px in android

px is one pixel. scale-independent pixels ( sp ) and density-independent pixels ( dip ) you want to use sp for font sizes and dip for everything else. dip==dp from here http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

px
Pixels - corresponds to actual pixels on the screen.
in
Inches - based on the physical size of the screen.
mm
Millimeters - based on the physical size of the screen.
pt
Points - 1/72 of an inch based on the physical size of the screen.
dp
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.


  • If running on hdpi device 150x150 px image will take up 100*100 dp of screen space.
  • If running on mdpi device 150x150 px image will take up 150*150 dp of screen space.
  • If running on xhdpi device 150x150 px image will take up 75*75 dp of screen space.
The other way around: say, you want to add an image to your application and you need it to fill 100*100 dp control, you'll need to create different size images for supported screen sizes:
  • 100*100 px image for mdpi
  • 150*150 px image for hdpi
  • 200*200 px image for xhdpi


Thursday, 2 August 2012

How to create Rich text view in android


/ this is the text we'll be operating on
    SpannableString text = new SpannableString("This is red in color ");

    // make "This" (characters 0 to 4) red
    text.setSpan(new ForegroundColorSpan(Color.RED), 0, 4, 0);

    // make "is" (characters 5 to 6) one and a half time bigger than the textbox
    text.setSpan(new RelativeSizeSpan(1.5f), 5, 6, 0);

    // make "red" (characters 7 to 9) display a toast message when touched
    final Context context = this;
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "dolor", Toast.LENGTH_LONG).show();
        }
    };
    text.setSpan(clickableSpan, 7, 9, 0);

    // make "in" (characters 10 to12) struck through
    text.setSpan(new StrikethroughSpan(), 10, 12, 0);

    // make "color" (characters13 to 18) twice as big, green and a link to this site.
    // it's important to set the color after the URLSpan or the standard
    // link color will override it.
    text.setSpan(new RelativeSizeSpan(2f), 13, 18, 0);
    text.setSpan(new URLSpan("http://www.chrisumbel.com"), 13, 18, 0);
    text.setSpan(new ForegroundColorSpan(Color.GREEN), 13, 18, 0);

    // make our ClickableSpans and URLSpans work
    richTextView.setMovementMethod(LinkMovementMethod.getInstance());

    // shove our styled text into the TextView      
    richTextView.setText(text, BufferType.SPANNABLE);