Friday, 30 August 2013

Share With Intents


 MainActivity :

package com.example.shareintent;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        Button sharingButton = (Button)findViewById(R.id.imageButton1);
        sharingButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                shareIt();
            }

            private void shareIt() {
                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
              
                sharingIntent.setType("text/plain");
                String shareBody = "Here is the share content body";
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                startActivity(Intent.createChooser(sharingIntent, "Share via"));
            }
        });
      
      
    }
}

 Output:


Thursday, 29 August 2013

Capture Active Screen in Android

AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.capturescreen"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.capturescreen.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity :

package com.example.capturescreen;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
Bitmap bmScreen;
 
Dialog screenDialog;
static final int ID_SCREENDIALOG = 1;
 
ImageView bmImage;
Button btnScreenDialog_OK;
TextView TextOut;
 
View screen;
EditText EditTextIn;
 
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     screen = (View)findViewById(R.id.screen);
     Button btnCaptureScreen = (Button)findViewById(R.id.capturescreen);
     EditTextIn = (EditText)findViewById(R.id.textin);
   
     btnCaptureScreen.setOnClickListener(new OnClickListener(){
 
  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   screen.setDrawingCacheEnabled(true);
   bmScreen = screen.getDrawingCache();
   showDialog(ID_SCREENDIALOG);
  }});
 }
 
 
@Override
protected Dialog onCreateDialog(int id) {
 // TODO Auto-generated method stub
 
 screenDialog = null;
 switch(id){
 case(ID_SCREENDIALOG):
  screenDialog = new Dialog(this);
  screenDialog.setContentView(R.layout.dialog);
  bmImage = (ImageView)screenDialog.findViewById(R.id.image);
  TextOut = (TextView)screenDialog.findViewById(R.id.textout);
  btnScreenDialog_OK = (Button)screenDialog.findViewById(R.id.okdialogbutton);
  btnScreenDialog_OK.setOnClickListener(btnScreenDialog_OKOnClickListener);
 }
 return screenDialog;
}
 
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
 // TODO Auto-generated method stub
 switch(id){
 case(ID_SCREENDIALOG):
  dialog.setTitle("Captured Screen");
  TextOut.setText(EditTextIn.getText().toString());
  bmImage.setImageBitmap(bmScreen);
  break;
 }
}
 
private Button.OnClickListener btnScreenDialog_OKOnClickListener
 = new Button.OnClickListener(){
 
 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  screenDialog.dismiss();
 }};
}

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:id="@+id/screen">
    <Button
     android:id="@+id/capturescreen"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Capture Screen"/>
    <TextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Enter some text here which you will see on captured screen"/>
    <EditText
     android:id="@+id/textin"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"/>
</LinearLayout>

dialog.xml :
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="10dip"
android:paddingRight="10dip">
    <TextView
    android:id="@+id/textout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
    <ImageView
    android:id="@+id/image"
    android:layout_width="400px"
    android:layout_height="400px"/>
    <Button
    android:id="@+id/okdialogbutton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="OK"/>
</LinearLayout>

Output Screen :

Error : The connection to adb is down, and a severe error has occured.

Getting Error:

>The connection to adb is down, and a severe error has occured.
>You must restart adb and Eclipse.
>Please ensure that adb is correctly located at 'C:\Android\android-sdk-windows\platform-tools\adb.exe' and can be executed.
>Daemon not running.ADB server didn't ACK


1. Go to the platform-tools directory:(you can find where you have AVD Manager.exe or SDK Manager.exe)
or in some paths given below:
Path 1 : C:\Android\android-sdk-windows\platform-tools>
Path 2 : C:\Program files\Android\android-sdk-windows\platform-tools>
Path 3 : C:\Program files(x86)\Android\android-sdk-windows\platform-tools>













 


2. Now check you have adb.exe present.

3. Now you need to have proper "android-sdk" normally it can never be absent.
    As it will be definitely there so open you terminal ( LINUX / UBUNTU users ) or command    prompt/ cmd ( Windows users ) and go to the above path where you have adb.exe:
cd\
cd C:\Android\android-sdk-windows\platform-tools [ or your adb.exe path ]
Now, use following two command:
adb kill-server
adb start-server
[ you can use this commands when Eclipse closed or open; if it doesn't work when your eclipse is open do it after closing eclipse. ]


NOTE: you can also end or kill process of adb.exe from task manager.

in Windows, just use task manager to kill the process.


 And Restart Eclipse.