Wednesday, 9 April 2014

how to set font to textview in android


main.xml

<RelativeLayout 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" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="25dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="34dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="38dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_centerVertical="true"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView5"
        android:layout_below="@+id/textView5"
        android:layout_marginTop="32dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView6"
        android:layout_below="@+id/textView6"
        android:layout_marginTop="42dp"
        android:text="TextView" />
</RelativeLayout>

 MainActivity 

package com.ttfdemo;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

   
    TextView textView1,textView2,textView3,textView4,textView5,textView6,textView7;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        textView1=(TextView)findViewById(R.id.textView1);
        textView2=(TextView)findViewById(R.id.textView2);
        textView3=(TextView)findViewById(R.id.textView3);
        textView4=(TextView)findViewById(R.id.textView4);
        textView5=(TextView)findViewById(R.id.textView5);
        textView6=(TextView)findViewById(R.id.textView6);
        textView7=(TextView)findViewById(R.id.textView7);       
       
         Typeface Text_Font_1 = Typeface.createFromAsset(this.getAssets(), "Courier.ttf");
         textView1.setTypeface(Text_Font_1);
       
         Typeface Text_Font_2 = Typeface.createFromAsset(this.getAssets(), "arial.ttf");
         textView2.setTypeface(Text_Font_2);
       
         Typeface Text_Font_3 = Typeface.createFromAsset(this.getAssets(), "gillsans.ttf");
         textView3.setTypeface(Text_Font_3);
       
         Typeface Text_Font_4 = Typeface.createFromAsset(this.getAssets(), "HelveticaNeue-Regular.ttf");
         textView4.setTypeface(Text_Font_4);
       
         Typeface Text_Font_5 = Typeface.createFromAsset(this.getAssets(), "HelveticaNeue-Roman.ttf");
         textView5.setTypeface(Text_Font_5);
       
         Typeface Text_Font_6 = Typeface.createFromAsset(this.getAssets(), "segoeui.ttf");
         textView6.setTypeface(Text_Font_6);
       
         Typeface Text_Font_7 = Typeface.createFromAsset(this.getAssets(), "trebuchet.ttf");
         textView7.setTypeface(Text_Font_7);
                
         textView1.setText("Courier.ttf");
         textView2.setText("arial.ttf");
         textView3.setText("gillsans.ttf");
         textView4.setText("HelveticaNeue-Regular.ttf");
         textView5.setText("HelveticaNeue-Roman.ttf");
         textView6.setText("segoeui.ttf");
         textView7.setText("trebuchet.ttf");
      }
  }

How to add a shadow and a border on imageView in android

main.xml

<RelativeLayout 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" >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/drop_shadow"
        android:orientation="vertical" >
     <ImageView
        android:id="@+id/imageView1"
        android:src="@drawable/logo"
        style="@style/myImageView"
        android:layout_width="160dp"
        android:layout_height="160dp"/>
    </LinearLayout>
</RelativeLayout>

values
   colors.xml
  
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="image_border_start">#6698ff</color>
    <color name="image_border_end">#6698ff</color>
    <color name="image_border_center">#6698ff</color>
    <color name="image_backg">#FFFFFF</color>
</resources>

values
   styles.xml

<resources>
    <style name="myImageView">
    <item name="android:padding">5dp</item>
    <item name="android:background">@drawable/image_stroke</item>
    <item name="android:scaleType">fitCenter</item>
</style>
</resources>

res
  drawable
    drop_shadow.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
     <item>
        <shape>
            <padding
                android:bottom="1.5dp"
                android:right="1.5dp" />
            <solid android:color="#00CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="1.5dp"
                android:right="1.5dp" />
            <solid android:color="#10CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="1.5dp"
                android:right="1.5dp" />
            <solid android:color="#20CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="1.5dp"
                android:right="1.5dp" />
            <solid android:color="#30CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="1.5dp"
                android:right="1.5dp" />
            <solid android:color="#50CCCCCC" />
        </shape>
    </item>
    <!-- Background -->
    <item>
        <shape>
            <solid android:color="#FFFFFF" />
            <corners android:radius="3dp" />
        </shape>
    </item>
</layer-list>

res
  drawable
    image_stroke.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <gradient
                android:angle="90"
                android:centerColor="@color/image_border_center"
                android:endColor="@color/image_border_end"
                android:startColor="@color/image_border_start" />
        </shape>
    </item>
    <item
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/image_backg" />
        </shape>
    </item>
</layer-list>




How turn on camera flash light using SurfaceView in Android

main.xml

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

 <Button
                    android:id="@+id/btn_Flashlight"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

<SurfaceView
                android:id="@+id/hiddenSurfaceView"
                android:layout_width="1dp"
                android:layout_height="1dp" />

</RelativeLayout>

MainActivity

package com.example;

private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
public Button Btn_Flashlight;
public static boolean hasFlash;
public static Camera mCamera;
public static Parameters mParameters;
public static PowerManager mPowerManager;
public int isLighOn =0;

public class MainActivity extends Activity implements OnClickListener,SurfaceHolder.Callback {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

Btn_Flashlight = (Button) findViewById(R.id.btn_Flashlight);

hasFlash=getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        surfaceView = (SurfaceView) this.findViewById(R.id.hiddenSurfaceView);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceHolder.addCallback(this);
 getCamera();
}
}
@Override
    public void onClick(View v) {

if (v == Btn_Flashlight) {
           
            if (hasFlash){
               
                    if (isLighOn==1){
                       
                      turnOffFlash();
                       
                    } else {
                   
                      turnOnFlash();
                       
                    }
               
            } else {
               
                Dialog();
               
            }
        }
    }
private void getCamera() {
        if (mCamera == null) {
            try {
                mCamera = Camera.open();
                mParameters = mCamera.getParameters();
            } catch (RuntimeException e) {
                Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
            }
        }
    }
      private void turnOnFlash() {
        if (!isFlashOn) {
            if (mCamera == null || mParameters == null) {
                return;
            }
            mParameters = mCamera.getParameters();
            mParameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
            mCamera.setParameters(mParameters);
            mCamera.startPreview();
            isFlashOn = true;
        }
    }
    private void turnOffFlash() {
        if (isFlashOn) {
            if (mCamera == null || mParameters == null) {
                return;
            }
            mParameters = mCamera.getParameters();
            mParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
            mCamera.setParameters(mParameters);
            mCamera.stopPreview();
            isFlashOn = false;
        }
    }
@Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        try {
            Constant.mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            Log.e(TAG, "Unexpected IO Exception in setPreviewDisplay()", e);
        }
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub  
    }
public void Dialog () {
       
        if (!hasFlash) {
            // device doesn't support flash
            // Show alert message and close the application
            final AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
            alert.setTitle("Error");
            alert.setMessage("Sorry, your device doesn't support flash light!");
            alert.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                   
                    alert.dismiss();
                }
            });
            alert.show();
            return;
        }
    }

AndroidManifest.xml

 <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-feature android:name="android.hardware.camera.flash" />
    <uses-feature android:permissionGroup="android.permission-group.HARDWARE_CONTROLS" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />