Saturday 27 February 2016

ContactList

MainActivity.java

package com.example.contactlistph;

import java.util.ArrayList;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.view.Menu;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    String phoneNumber;
    ListView lv;
    ArrayList <String> ab= new ArrayList<String>();
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       requestWindowFeature(Window.FEATURE_NO_TITLE);

       setContentView(R.layout.activity_main);
        lv= (ListView) findViewById(R.id.lv);

       getNumber(this.getContentResolver());
   }

   public void getNumber(ContentResolver cr)
   {
       Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
       while (phones.moveToNext())
       {
         String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
         phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
         System.out.println(".................."+phoneNumber);
          ab.add(name+"\n"+phoneNumber);
       }
                phones.close();// close cursor
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                   android.R.layout.simple_list_item_1,aa);
         lv.setAdapter(adapter);
                 //display contact numbers in the list
   }

     }


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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   <ListView
   android:layout_width="match_parent"
   android:layout_height="fill_parent"
   android:id="@+id/lv"/>


</RelativeLayout>

Uses Permission

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.contactlistph.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>

**After running this code all the contacts in ur phone has been visualize on ur running device . If uh have any issue ,, comment in comment box .

Wednesday 10 February 2016

Android camera Application

This is android code for camera application

MainActivity.java


package com.example.hanu.owncamera;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;

public class MainActivity extends AppCompatActivity {
    Button button;
    ImageView Imgview;
    static final int CAMERA_REQUEST=1;

    @Override    
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.btn);
        Imgview = (ImageView) findViewById(R.id.Imgview);
        button.setOnClickListener(new View.OnClickListener() {

            @Override            
               public void onClick(View view) {
               Intent camera_intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file=get_file();
                camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(camera_intent,CAMERA_REQUEST);
            }
        });

    }  //Create a folder where images are stored
    private File get_file() {
        File folder = new File("sdcard/camera_app");
        if (!folder.exists()) {
            folder.mkdir();
        }
        File image_file = new File(folder, "camera_image.jpg");
        return image_file;
        // return image_file;    }
    @Override    protected void onActivityResult(int requestcode, int resultcode ,Intent data)
    {
        String path="sdcard/camera_app/camera_image.jpg";
        Imgview.setImageDrawable(Drawable.createFromPath(path));
    }
}


content_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:layout_width="match_parent"    
android:layout_height="match_parent"    
android:paddingBottom="@dimen/activity_vertical_margin"    
android:paddingLeft="@dimen/activity_horizontal_margin"    
android:paddingRight="@dimen/activity_horizontal_margin"    
android:paddingTop="@dimen/activity_vertical_margin"    
android:orientation="vertical"    
app:layout_behavior="@string/appbar_scrolling_view_behavior"    
tools:context="com.example.hanu.owncamera.MainActivity"    
tools:showIn="@layout/activity_main">
<Button android:layout_width="200dp"        
android:layout_height="wrap_content"        
android:text="Capture Image"        
android:id="@+id/btn"        
android:layout_gravity="center_horizontal"/>
<ImageView android:layout_width="350dp"        
android:layout_height="300dp"        
android:id="@+id/Imgview"        
android:layout_gravity="center_horizontal"        
android:layout_marginTop="40dp"/>
</LinearLayout>
Add two lines in manifest file for permission and they are:
 <uses-feature android:name="android.hardware.camera2"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
and full code of manifest is given below.

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    
package="com.example.hanu.owncamera">
    <uses-feature android:name="android.hardware.camera2"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application android:allowBackup="true"        
android:icon="@mipmap/ic_launcher"        
android:label="@string/app_name"        
android:supportsRtl="true"        
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"            
android:label="@string/app_name"            
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

If you have any queries regarding this code .. comment us

Monday 8 February 2016

TextToSpeech Application


This is the android code for TextToSpeech Application.

MainActivity.java

package com.example.hanu.texttospeech;

import android.os.Bundle;
import android.speech.tts.TextToSpeech; //Class TextToSpeech
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;

import java.util.Locale;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {

    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tts = new TextToSpeech(this,this);
        btnSpeak = (Button)findViewById(R.id.button);
        txtText = (EditText)findViewById(R.id.editText);
        btnSpeak.setOnClickListener(new View.OnClickListener() {

            @Override            
                public void onClick(View v) {
                speakout();

            }


        });

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override            
              public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override    
        public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.        
         getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override    
      public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will        
// automatically handle clicks on the Home/Up button, so long        
// as you specify a parent activity in AndroidManifest.xml.        
             int id = item.getItemId();

        //noinspection SimplifiableIfStatement        
            if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override    
      public void onInit(int i) {
        if(i==TextToSpeech.SUCCESS){
            int result = tts.setLanguage(Locale.ENGLISH);
            if(result==TextToSpeech.LANG_NOT_SUPPORTED || result==TextToSpeech.LANG_MISSING_DATA){
                Log.e("TTS", "This language is not supported");
            }
            else{
                btnSpeak.setEnabled(true);
                speakout();
            }
        }
        else{
            Log.e("TTS" , "Initialization Failed");
        }

    }
    private void speakout() {
       String text=txtText.getText().toString();
        tts.speak(text,TextToSpeech.QUEUE_FLUSH,null);
    }
}

Content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"  
android:layout_height="match_parent"    
android:paddingBottom="@dimen/activity_verticaan"    
android:paddingLeft="@dimen/activity_horizontal_margin"    
android:paddingRight="@dimen/activity_horizontal_margin"    
android:paddingTop="@dimen/activity_vertical_margin"    
app:layout_behavior="@string/appbar_scrolling_view_behavior"    
tools:context="com.example.hanu.texttospeech.MainActivity"    
tools:showIn="@layout/activity_main">
<TextView android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:text="Hello World!"        
android:id="@+id/textView" />
<Button android:layout_width="wrap_content"  
android:layout_height="wrap_content"        
android:text="New Button"        
android:id="@+id/button"        
android:layout_below="@+id/textView"        
android:layout_centerHorizontal="true"        
android:layout_marginTop="173dp" />
<EditText android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:id="@+id/editText"        
android:layout_marginTop="53dp"      
android:layout_below="@+id/textView"     
android:layout_alignParentRight="true"        
android:layout_alignParentEnd="true"        
android:layout_toRightOf="@+id/textView"        
android:layout_toEndOf="@+id/textView" />
</RelativeLayout>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"    
android:layout_height="match_parent"    
android:fitsSystemWindows="true"    
tools:context="com.example.hanu.texttospeech.MainActivity">
<android.support.design.widget.AppBarLayout 
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar            
android:id="@+id/toolbar"            
android:layout_width="match_parent"            
android:layout_height="?attr/actionBarSize"            
android:background="?attr/colorPrimary"            
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton        
android:id="@+id/fab"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_gravity="bottom|end"        
android:layout_margin="@dimen/fab_margin"        
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>

If uh have queries for this program . Comment the query in comment box below .