Open top menu








You need to remove or replaces some Special Character eg- Backslash (\n), [ , ’, and \n ,\u,\s, etc. You replaces some regular expressions are complicated things,

String str="hello, who are \u you";
 
Step 1- The (\u )escape character replace very complicated.

String value=str. replaceAll(Pattern.quote("\\u") , "new String value");
 
 
Step 2- the replace  all digit Value from String.

String str="hello, who are 24314 you ";

String something=str.replaceAll(
"[\\p{Digit}]", "");

Output:-hello who are you

Step 3- The replace  all regular expression from String.

String alphaOnly = input.replaceAll("[^a-zA-Z]+","");
String alphaAndDigits = input.replaceAll(
"[^a-zA-Z0-9]+","");
 
 Or equivalent

String alphaOnly = input.replaceAll(
"[^\\p{Alpha}]+","");
String alphaAndDigits = input.replaceAll(
"[^\\p{Alpha}\\p{Digit}]+","");









 thank you  
Read more


Android- How to get Rgb Color programmaticall.



 Get the pixel in height And width when touch on imageView.


activity_main.xml
...................................................................................................................................................................
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:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Android Beginner tutorial point"
       
tools:context=".MainActivity"
       
android:textSize="40dp"
       
android:textStyle="bold"
 
   
        android:id="@+id/colorAvarage"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="touched color: "/>
            android:id="@+id/image"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:src="@drawable/multicolor"
       
android:layout_marginTop="40dp"/>





...................................................................................................................................................................

 MainActivity.java
...................................................................................................................................................................



import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;

public class  MainActivit extends Activity {

    TextView touchedXY, invertedXY, imgSize, colorRGB;
    ImageView imgSource;

    @Override
   
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.color_code_activity);

        touchedXY = (TextView)findViewById(R.id.TutorialBlog);
        colorRGB = (TextView)findViewById(R.id.colorAvarage);
        imgSource = (ImageView)findViewById(R.id.image);
        imgSource.setOnTouchListener(imgOnTouchListener);

             }

        OnTouchListener imgOnTouchListener = new OnTouchListener(){

        @Override
       
public boolean onTouch(View view, MotionEvent event) {

            float eventX = event.getX();
            float eventY = event.getY();
            float[] eventXY = new float[] {eventX, eventY};

            Matrix invertMatrix = new Matrix();
            ((ImageView)view).getImageMatrix().invert(invertMatrix);

            invertMatrix.mapPoints(eventXY);
            int x = Integer.valueOf((int)eventXY[0]);
            int y = Integer.valueOf((int)eventXY[1]);

                        Drawable imgDrawable = ((ImageView)view).getDrawable();
            Bitmap bitmap = ((BitmapDrawable)imgDrawable).getBitmap();

            //Limit x, y range within bitmap

            
if(x < 0){
                x = 0;
            }else if(x > bitmap.getWidth()-1){
                x = bitmap.getWidth()-1;
            }

            if(y < 0){
                y = 0;
            }else if(y > bitmap.getHeight()-1){
                y = bitmap.getHeight()-1;
            }

            int touchedRGB = bitmap.getPixel(x, y);
            colorRGB.setText("Color Code In Hexa------->   " + "#" +    Integer.toHexString(touchedRGB));
            colorRGB.setTextColor(touchedRGB);
            touchedXY.setTextColor(touchedRGB);
            return true;
        }};

}
...................................................................................................................................................................


Read more

 Android- Explain how to get Color code from Image programmatically.

First Set Image in Bitmap :-
final Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
 
Then Get Touch X and Y coordinate from ImageView.
public boolean onTouch(View v, MotionEvent event) {

    x = (int)event.getX();

    y= (int)event.getY();

    return false;

}
 
Get the color code in Hex String touch on imageView.
Integer.toHexString(bitmap.getPixel(x, y);
 
Get the color code in Hex String touch on imageView.

activity_main.xml
.............................................................................................................................................................

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/activity_main"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Android Beginner point"

        android:textSize="25dp"

        android:textStyle="bold"

        android:id="@+id/textView" />



    <ImageView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:src="@drawable/multicolor"

        android:layout_below="@+id/textView"

        android:layout_toRightOf="@+id/textView"

        android:layout_toEndOf="@+id/textView"

        android:id="@+id/imageView" />



</LinearLayout>

.............................................................................................................................................................

MAinActivity.java
.............................................................................................................................................................
import android.graphics.Bitmap;

import android.graphics.drawable.BitmapDrawable;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.MotionEvent;

import android.view.View;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity {

     int x,y;

    TextView txtView;

    ColorUtils utils=new ColorUtils();

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        txtView=(TextView)findViewById(R.id.textView);

        ImageView imageView=(ImageView)findViewById(R.id.imageView);

        final Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

         imageView.setOnTouchListener(new View.OnTouchListener() {

             @Override

             public boolean onTouch(View v, MotionEvent event) {

                 x = (int)event.getX();

                 y= (int)event.getY();

                 int pixel = bitmap.getPixel(x,y);

                 txtView.setTextColor(bitmap.getPixel( x,y));

                   Toast.makeText(getApplicationContext(),"#"+Integer.toHexString(bitmap.getPixel(x, y)),Toast.LENGTH_LONG).show();



                 return false;

             }

         });



    }

}
 .............................................................................................................................................................


 


Read more





Example- Explain how to ON and OFF Flash Light in android device programmatically.

Step 1- Add below permission in manifest file.

<uses-permission android:name="android.permission.CAMERA" />
<
uses-feature android:name="android.hardware.camera" />

.............................................................................................

activity_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"
    android:background="@drawable/gradient"
    tools:context=".MainActivity" >

    <ImageButton
       
android:id="@+id/btnSwitch"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dip"
        android:src="@drawable/switch_on_icon"
        android:background="@null"
        android:scaleType="fitXY"
        android:contentDescription="@null"
        />
</RelativeLayout>


.............................................................................................

MainActivity.Java
.............................................................................................

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {
    ImageButton btnSwitch;

    private Camera camera;
    private boolean isFlashOn;
    private boolean hasFlash;
    Parameters params;
    MediaPlayer mp;

    @Override
   
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);

        
        hasFlash = getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        if (!hasFlash) {

            AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
            alert.setTitle("Error");
            alert.setMessage("device doesn't support flash light!");
            alert.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            alert.show();
            return;
        }
       
        getCamera();
       
        toggleButtonImage();
       
        btnSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
           
public void onClick(View v) {
                if (isFlashOn) {
                    turnOffFlash();
                } else {
                    turnOnFlash();
                }
            }
        });
    }
    private void getCamera() {
        if (camera == null) {
            try {
                camera = Camera.open();
                params = camera.getParameters();
            } catch (RuntimeException e) {

            }
        }
    }
   
    private void turnOnFlash() {
        if (!isFlashOn) {
            btnSwitch.setBackgroundResource(R.drawable.switch_on_icon);
            if (camera == null || params == null) {
                return;
            }
           
            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.startPreview();
            isFlashOn = true;
            toggleButtonImage();
        }

    }
   
    private void turnOffFlash() {
        if (isFlashOn) {
            if (camera == null || params == null) {
                return;
            }
            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);
            camera.stopPreview();
            isFlashOn = false;
            toggleButtonImage();
        }
    }
   
    private void toggleButtonImage(){
        if(isFlashOn){
            btnSwitch.setImageResource(R.drawable.switch_on_icon);
        }else{
            btnSwitch.setImageResource(R.drawable.switch_off_icon);
        }
    }

    @Override
   
protected void onDestroy() {
        super.onDestroy();
    }

    @Override
   
protected void onPause() {
        super.onPause();
        turnOffFlash();
    }

    @Override
   
protected void onRestart() {
        super.onRestart();
    }

     

    @Override
   
protected void onStart() {
        super.onStart();
        getCamera();
    }

    @Override
   
protected void onStop() {
        super.onStop();
        if (camera != null) {
            camera.release();
            camera = null;
        }
    }
}

 ............................................................................................




Read more






 Android Example –How to Vibrate android device programmatically?

Step 1:- Add bellow permission in your manifest file.
<uses-permission android:name="android.permission.VIBRATE"/>

Step 2: Get Vibrate System Service.
Vibrator vibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

Step 3: Set Vibrate  Time period in milliseconds and Time Pattern. Time Pattern means like start/stope vibration.
vibrate.vibrate(300);//in millisecond.

Pattern
long pattern[] = {60,120,180,240,300,360,420,480};
vibrate.vibrate(pattern,1);

 .................................................................................................................................................................

 activity_main.xml
.................................................................................................................................................................
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/activity_main"
    
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:gravity="center"
  
android:orientation="vertical">

    <
TextView
       
android:id="@+id/textView1"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text=""
       
android:textAppearance="?android:attr/textAppearanceLarge" />

    <
Button
       
android:id="@+id/btn_start"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/textView1"
       
android:text="Vibrate Start"
       
android:layout_margin="20dp"/>
    <
Button
       
android:id="@+id/btn_stope"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/textView1"
       
android:text="Vibrate Stope"
       
android:layout_margin="20dp"/>

</
LinearLayout>
.................................................................................................................................................................



MainActivity.java
.................................................................................................................................................................

 

import android.content.Context;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView
text;
    Vibrator
vibrate;
    Button
btn_start,btn_stope;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
text = (TextView) findViewById(R.id.textView1);
       
btn_start = (Button) findViewById(R.id.btn_start);
       
btn_stope = (Button) findViewById(R.id.btn_stope);

       
vibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
       
btn_start.setOnClickListener(new View.OnClickListener() {

           
@Override
           
public void onClick(View v) {
               
long pattern[] = {60,120,180,240,300,360,420,480};
               
vibrate.vibrate(pattern,1);
               
//vibrate.vibrate(300);
               
text.setText("Vibrate Start...");

            }
        });
       
btn_stope.setOnClickListener(new View.OnClickListener() {

           
@Override
           
public void onClick(View v) {
               
vibrate.cancel();
               
text.setText("Vibrate Stope...");


            }
        });
    }

}

.................................................................................................................................................................


Read more