Thursday, February 8, 2018

How to copy text on Clipboard

Android provides a lots of feature, clipboard isone of them.
Android provides a powerful clipboard-based framework for copying and pasting. It supports both simple and complex data types, including text strings, complex data structures, text and binary stream data, and even application assets. Simple text data is stored directly in the clipboard, while complex data is stored as a reference that the pasting application resolves with a content provider. Copying and pasting works both within an application and between applications that implement the framework.

When you use the clipboard framework, you put data into a clip object, and then put the clip object on the system-wide clipboard. The clip object can take one of three forms:

ClipboardManager

In the Android system, the system clipboard is represented by the global ClipboardManager class. You do not instantiate this class directly; instead, you get a reference to it by invoking


ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Simple Text", "Text to Copy");
clipboard.setPrimaryClip(clip);

How to share Text in Android

In Android, we can share a text, image anr many thing things.

In the post we will learn how to share text in Android.

To do this we need an Object of class Intent and set action " android.intent.action.SEND"


Code:


Intent localIntent = new Intent();
   
    localIntent.setAction("android.intent.action.SEND");
    localIntent.putExtra("android.intent.extra.TEXT", "Text To Share");
    localIntent.setType("text/plain");
    startActivity(Intent.createChooser(localIntent, "Share..."));


It will open a chooser doalog with many options like SMS, Whatsapp, Mail, facebook etc,  in which user can select any option.

Wednesday, February 7, 2018

How to make Phone Calls In Android

In Android we can make Phone calls to a particular Phone Number using Intents.



Permission Required


android.permission.CALL_PHONE

You need to write <uses-permission android:name="android.permission.CALL_PHONE" />  in order to make a call 


You will use ACTION_CALL action to trigger built-in phone call functionality available in Android device. Following is simple syntax to create an intent with ACTION_CALL action

Intent phoneIntent = new Intent(Intent.ACTION_CALL);


You can use ACTION_DIAL action instead of ACTION_CALL, in that case you will have option to modify hardcoded phone number before making a call instead of making a direct call.


To make a phone call at a given number 91-000-000-0000, you need to specify tel: as URI using setData() method as follows −
phoneIntent.setData(Uri.parse("tel:91-000-000-0000"));a


Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:0377778888"));
    
            if (ActivityCompat.checkSelfPermission(MainActivity.this,
               Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                  return;
               }
               startActivity(callIntent);

Tuesday, February 6, 2018

How to hide Title Bar in Android

In Android we can hide the title bar and run our app in full screeb view.

To Do this we will use  requestWindowFeature(Window.FEATURE_NO_TITLE)


The requestWindowFeature(Window.FEATURE_NO_TITLE) method of Activity must be called to hide the title. But, it must be coded before the setContentView method.


  1.  @Override  
  2.     protected void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.           
  5.         requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title not the title bar  
  6.           
  7.         setContentView(R.layout.activity_main);  
  8.         
  9.     }  
  10. }  

he setFlags() method of Window class is used to display content in full 
screen mode. You need  to pass the 
WindowManager.LayoutParams.FLAG_FULLSCREEN 
constant in the setFlags method.

  1. equestWindowFeature(Window.FEATURE_NO_TITLE);  
  2.     //code that displays the content in full screen mode  
  3.     this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  4.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);//int flag, int mask  
  5.       
  6.     setContentView(R.layout.activity_main);  
  7.     




Sending SMS In Android

Sending SMS is very easy in Android.

In this tutotial we learn how to send SMS from one device to another.


How to send SMS


In android we use SMS Manager  class to send a SMS and to perform all activities related to SMS.

In android we need  permission to send SMS , we use  "android.permission.SEND_SMS"  permission to send SMS so do not forget to include this permission in manifest file

Note: all the permissions must be declared in manifest file.



Code to send SMS



private void sendSMS(String phoneNumber, String message)
    {        
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";
 
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT), 0);
 
        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);
 
        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));
 
        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;                        
                }
            }
        }, new IntentFilter(DELIVERED));        
 
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
    }

Monday, February 5, 2018

Android Rating Bar Example

What is Rating Bar

A RatingBar is an extension of SeekBar and ProgressBar that shows a rating in stars. The user can touch/drag or use arrow keys to set the rating when using the default size RatingBa

In Android, you can use “android.widget.RatingBar” to display rating bar component in stars icon. The user is able to touch, drag or click on the stars to set the rating value easily.


Android RatingBar displays the rating in stars. Android RatingBar is the subclass of AbsSeekBar class.
The getRating() method of android RatingBar class returns the rating number

Lets create a project and use Rating bar


main.xml

  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <RatingBar  
  8.         android:id="@+id/ratingBar1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="44dp" />  
  14.   
  15.     <Button  
  16.         android:id="@+id/button1"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignLeft="@+id/ratingBar1"  
  20.         android:layout_below="@+id/ratingBar1"  
  21.         android:layout_marginLeft="92dp"  
  22.         android:layout_marginTop="66dp"  
  23.         android:text="submit" />  
  24.   
  25. </RelativeLayout>  


MainActivity.xml

  1. public class MainActivity extends Activity {  
  2.     RatingBar ratingbar1;  
  3.     Button button;  
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.activity_main);  
  8.         addListenerOnButtonClick();  
  9.     }  
  10.   
  11.     public void addListenerOnButtonClick(){  
  12.         ratingbar1=(RatingBar)findViewById(R.id.ratingBar1);  
  13.         button=(Button)findViewById(R.id.button1);  
  14.         //Performing action on Button Click  
  15.         button.setOnClickListener(new OnClickListener(){  
  16.   
  17.             @Override  
  18.             public void onClick(View arg0) {  
  19.                 //Getting the rating and displaying it on the toast  
  20.                 String rating=String.valueOf(ratingbar1.getRating());  
  21.                 Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();  
  22.             }  
  23.               
  24.         });  
  25.     }  
  26.     @Override  
  27.     public boolean onCreateOptionsMenu(Menu menu) {  
  28.         // Inflate the menu; this adds items to the action bar if it is present.  
  29.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  30.         return true;  
  31.     }  
  32.   
  33. }  

Now run the Project, you will see how rating bar works.