Wednesday, 28 March 2018

Play video in Android

Hi All,

Make sure you create a folder in res directory 'raw' in 'res'

Ref : http://www.coders-hub.com/p/android.html


SQLite Delete

Hi All,

See the code

mainactivity.java

------------------------------------------------------------------------------------

protected void onCreate(Bundle savedInstanceState) {
            //HUP2();
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main5);
            lstview=(ListView)findViewById(R.id.lst_book);
           
           

           
            int i=0;
            //listitem=new String[5];
           
            db=SQLiteDatabase.openDatabase("data/data/com.example.lms/databases/mydb", null, SQLiteDatabase.OPEN_READONLY);
            String s="select * from tbl_book";
            Cursor c=db.rawQuery(s, null);
            int count=c.getCount();
           
            listitem=new String[count];
            if(c!=null)
            {    
                  if(c.moveToFirst())
                  {
                        do
                        {
                              String book=c.getString(1);

                              listitem[i]=book;
                              i++;
                        }while(c.moveToNext());
                  }
            }

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.lst,R.id.txt_view_book,listitem);
            lstview.setAdapter(adapter);

      lstview.setOnItemClickListener(new OnItemClickListener() {
           
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                  // TODO Auto-generated method stub
                  String entry= (String) parent.getAdapter().getItem(position);
                  t1.speak(entry, TextToSpeech.QUEUE_FLUSH, null);
                  //String message = entry.getMessage();


                  //Toast.makeText(getApplicationContext(), entry, Toast.LENGTH_LONG).show();
                  Intent in=new Intent(Main5Activity.this,Main8Activity.class);
                 
                   in.putExtra("txt_view_book",entry);
                   startActivity(in);
            }
      });
           
      }

 ====================================================================

  Main8Activity.java

 ====================================================================

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main8);
            Intent in=getIntent();
            entry=in.getStringExtra("txt_view_book");
            tx2=(EditText) findViewById(R.id.txt_view);
            tx2.setText(entry);
      }
     
      public void deletbook(View view)
      {
           
            //db=SQLiteDatabase.openDatabase("data/data/com.example.lms/databases/mydb", null, SQLiteDatabase.OPEN_READONLY);
            db=this.openOrCreateDatabase("mydb", MODE_PRIVATE, null);
            String s="delete from tbl_book where b_title like '"+tx2.getText()+"'";
            db.execSQL(s);
            Toast.makeText(getApplicationContext(),"Deleted", Toast.LENGTH_LONG).show();
      }

Friday, 23 March 2018

Android Service

Hi All,

Use this material to play an audio file as service

https://www.javatpoint.com/android-service-tutorial

Broadcast Receiver

Hi all,
See the code for Broadcast receiver


<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >
   <receiver android:name="MyReceiver1">
  
      <intent-filter>
         <action android:name="android.intent.action.DATE_CHANGED">
         </action>
      </intent-filter>
  
   </receiver>
</application>

--------------------------------------------------------------------------------------------------------
package com.example.tutorialspoint7.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 * Created by TutorialsPoint7 on 8/23/2016.
 */
public class MyReceiver1 extends BroadcastReceiver{
   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Date Changed.", Toast.LENGTH_LONG).show();
   }
}
------------------------------------------------------------------------------------------------------------------

Find a few such broadcasts

1             

android.intent.action.BATTERY_LOW

Indicates low battery condition on the device.
2             

android.intent.action.BATTERY_CHANGED

Sticky broadcast containing the charging state, level, and other information about the battery.

3             

android.intent.action.BATTERY_OKAY

Indicates the battery is now okay after being low.
4             

android.intent.action.BOOT_COMPLETED

This is broadcast once, after the system has finished booting.
5             

android.intent.action.BUG_REPORT

Show activity for reporting a bug.

6            

android.intent.action.CALL_BUTTON

The user pressed the "call" button to go to the dialer or other appropriate UI for placing a call.
7            

android.intent.action.CALL

Perform a call to someone specified by the data.
8             

android.intent.action.DATE_CHANGED

The date has changed.
9             

android.intent.action.REBOOT

Have the device reboot.


Friday, 9 March 2018

Video Linking in your Android App

Hi All,

First you place one 'VideoView' from 'Images& Video" tab in layout toolbox

Then add this code in the onCreate() function.

Place a 3gp file in WWW folder of WAMP

-------------------------------------
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView)findViewById(R.id.videoView1); 
// videoView.setVideoURI(Uri.parse(path));

videoView.setVideoPath("http://10.0.2.2/HUP.3gp");

videoView.start();   
}

---------------------------------------------
Give INTERNET permission in manifest.xml file

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

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

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

Include List View

Hi All,

To show data in listview from Android there are two steps


Step I 
Create a Layout file with one TextView ,ie label box

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
<TextView
 android:id="@+id/listviewtext"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="TextView" />
</LinearLayout>
Step II
Place one List view in another activity.
Use this code in onCreate() function to view data from DB in list view.



public void HUP1()
                {
                String [] listitem;
                int i=0;
                listitem=new String[3];
                ListView lstview=(ListView)findViewById(R.id.lstview);
                SQLiteDatabase hupdb;
                hupdb=this.openOrCreateDatabase("hup", MODE_PRIVATE, null);
                hupdb.openDatabase("data/data/com.example.snit2/databases/hup", null, SQLiteDatabase.OPEN_READONLY);
                String s="select * from registration";
                Cursor c=hupdb.rawQuery(s, null);
                int count=c.getCount();
                listitem=new String[count];
                String val="";
                    if(c!=null)
                    {
                                   if(c.moveToFirst())
                       {
                         do
                         {
                                 String username=c.getString(0);
                                 String abc=c.getString(1);
                                 val=username + " " + abc;
                                 listitem[i]=val;
                                 i++;
                         }while(c.moveToNext());
                       }
                    }
                   
                    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.layoutview,R.id.txtview,listitem);

lstview.setAdapter(adapter);

lstview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewint positionlong id) {
// TODO Auto-generated method stub
String entry= (String) parent.getAdapter().getItem(position);
//String message = entry.getMessage();
Toast.makeText(getApplicationContext(), entry, Toast.LENGTH_LONG).show();
}
});

}