Wednesday, 15 June 2016

HYBRID SPLASH SCREEN USING JAVASCRIPT

(1)WRITE FOLLOWING CODE  AND SAVE INTO YOUR  WWW FOLDER AS  index.html :
 <!DOCTYPE html>
<html>
<head>
<style>
.blink {
    animation:fade 3000ms infinite;
    -webkit-animation:fade 3000ms infinite;
}

@keyframes fade {
    from { opacity: 1.0; }
    50% { opacity: 0.4; }
    to { opacity: 1.0; }
}
 
@-webkit-keyframes fade {
    from { opacity: 1.0; }
    50% { opacity: 0.4; }
    to { opacity: 1.0; }
}
</style>

</head>
<body>
<div  class="blink">Apps is running wait...</div >
<script>
function myFunction() 
{
    setInterval(function(){ 
window.location='home.html' 
}, 3000);
}

myFunction();
</script>

</body>
</html>

Friday, 10 June 2016

DISPLAYING SQLLITE DATABASE RECORDS INTO LISTVIEW

 Writing   code for     Inserting   records    into   database   :

(1)write   code for  Main2Activity.java  file:

package testing.om.com.listview;



import android.content.Context;

import android.content.Intent;

import android.database.sqlite.SQLiteDatabase;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;





public class Main2Activity extends ActionBarActivity implements View.OnClickListener {



    private EditText editTextName;

    private EditText editTextAdd;

    private Button btnAdd;

    private Button btnView;



    private SQLiteDatabase db;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);



        createDatabase();



        editTextName = (EditText) findViewById(R.id.editTextName);

        editTextAdd = (EditText) findViewById(R.id.editTextAddress);



        btnAdd = (Button) findViewById(R.id.btnAdd);

        btnView = (Button) findViewById(R.id.btnView);



        btnAdd.setOnClickListener(this);

        btnView.setOnClickListener(this);

    }





    protected void createDatabase(){

        db=openOrCreateDatabase("PersonDB", Context.MODE_PRIVATE, null);

        db.execSQL("CREATE TABLE IF NOT EXISTS
 persons(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR,address VARCHAR);");

    }



    protected void insertIntoDB(){

        String name = editTextName.getText().toString().trim();

        String add = editTextAdd.getText().toString().trim();

        if(name.equals("") || add.equals("")){

            Toast.makeText(getApplicationContext(),"Please fill all fields",
 Toast.LENGTH_LONG).show();

            return;

        }



        String query = "INSERT INTO persons (name,address) VALUES('"+name+"', '"+add+"');";

        db.execSQL(query);

        Toast.makeText(getApplicationContext(),"Saved Successfully", Toast.LENGTH_LONG).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);

    }





    private void showPeoples(){

        Intent intent = new Intent(this,Main2Activity.class);

        startActivity(intent);

        finish();

    }



    @Override

    public void onClick(View v) {

        if(v == btnAdd){

            insertIntoDB();

        }

        if(v==btnView){

            showPeoples();

        }

    }

}


(2)write code for activity_main2.xml file:
<?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="testing.om.com.listview.Main2Activity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" 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_main2" />



    <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>
 
 
(3)write code  for  content_main2.xml   file:
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:orientation="vertical"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Name"

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



    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

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



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Address"

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



    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

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



    <Button

        style="?android:attr/buttonStyleSmall"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Add"

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



    <Button

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="View Peoples"

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

</LinearLayout>


Write   code to display record   to  listview

(1)write    code for  MainActivity.java    file  :
package testing.om.com.listview;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

   
private static final String SELECT_SQL = "SELECT * FROM persons";

   
private SQLiteDatabase db;

   
private Cursor c;



   
private ListView monthsListView;
   
private ArrayAdapter arrayAdapter;

   
/**
     * Called when the activity is first created.
     */
   
@Override
   
public void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        openDatabase();

       
c = db.rawQuery(SELECT_SQL, null);
       
c.moveToFirst();
        showRecords();

    }

   
protected void openDatabase() {
       
db = openOrCreateDatabase("PersonDB", Context.MODE_PRIVATE, null);
    }


   
protected void showRecords() {

        String id =
c.getString(0);
        String name =
c.getString(1);
        String add =
c.getString(2);
         String[] monthsArray = {id,name,add};


       
monthsListView = (ListView) findViewById(R.id.listView);

       
// this-The current activity context.
        // Second param is the resource Id for list layout row item
        // Third param is input array
       
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, monthsArray);

       
monthsListView.setAdapter(arrayAdapter);


    }


}

(2)write    code    for     activity_main.xml       file :

<?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=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" 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>

(3)write code  for  content_main.xml   file:

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

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

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

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

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_listview" tools:context=".listview">



    <TextView android:text="Hello World!" android:layout_width="wrap_content"

        android:layout_height="wrap_content"

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



    <ListView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/listView"

        />

</RelativeLayout>



Tuesday, 7 June 2016

Internal storage

Internal storage is the storage of the private data on the device memory.
By default these files are private and are accessed by only your application and get deleted , when user delete your application.
Writing file
In order to use internal storage to write some data in the file, call the openFileOutput() method with the name of the file and the mode. The mode could be private , public e.t.c. Its syntax is given below −
FileOutputStream fOut = openFileOutput("file name here",MODE_WORLD_READABLE);
The method openFileOutput() returns an instance of FileOutputStream. So you receive it in the object of FileInputStream. After that you can call write method to write data on the file. Its syntax is given below −
String str = "data";
fOut.write(str.getBytes());
fOut.close();
Reading file
In order to read from the file you just created , call the openFileInput() method with the name of the file. It returns an instance of FileInputStream. Its syntax is given below −
FileInputStream fin = openFileInput(file);
After that, you can call read method to read one character at a time from the file and then you can print it. Its syntax is given below −
int c;
String temp="";
while( (c = fin.read()) != -1){
   temp = temp + Character.toString((char)c);
}

//string temp contains all the data of the file.
fin.close();
Apart from the the methods of write and close, there are other methods provided by the FileOutputStream class for better writing files. These methods are listed below −
Sr.No
Method & description
1
FileOutputStream(File file, boolean append)
This method constructs a new FileOutputStream that writes to file.
2
getChannel()
This method returns a write-only FileChannel that shares its position with this stream
3
getFD()
This method returns the underlying file descriptor
4
write(byte[] buffer, int byteOffset, int byteCount)
This method Writes count bytes from the byte array buffer starting at position offset to this stream
Apart from the the methods of read and close, there are other methods provided by the FileInputStream class for better reading files. These methods are listed below −
Sr.No
Method & description
1
available()
This method returns an estimated number of bytes that can be read or skipped without blocking for more input
2
getChannel()
This method returns a read-only FileChannel that shares its position with this stream
3
getFD()
This method returns the underlying file descriptor
4
read(byte[] buffer, int byteOffset, int byteCount)
This method reads at most length bytes from this stream and stores them in the byte array b starting at offset
Example
Here is an example demonstrating the use of internal storage to store and read files. It creates a basic storage application that allows you to read and write from internal storage.
To experiment with this example, you can run this on an actual device or in an emulator.
Steps
Description
1
You will use Android Studio IDE to create an Android application under a package com.example.sairamkrishna.myapplication. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2
Modify src/MainActivity.java file to add necessary code.
3
Modify the res/layout/activity_main to add respective XML components
4
Run the application and choose a running android device and install the application on it and verify the results















(1)write  code for MainAcitivity.java file:
package testing.om.com.sdcard;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class MainActivity extends Activity
{
    Button
b1,b2;
    TextView
tv;
    EditText
ed1;

    String
data;
   
private String file = "mydata";

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

       
b1=(Button)findViewById(R.id.button);
       
b2=(Button)findViewById(R.id.button2);

       
ed1=(EditText)findViewById(R.id.editText);
       
tv=(TextView)findViewById(R.id.textView2);
       
b1.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                
data=ed1.getText().toString();

               
try {
                    FileOutputStream fOut = openFileOutput(
file,MODE_WORLD_READABLE);
                    fOut.write(
data.getBytes());
                    fOut.close();
                    Toast.makeText(getBaseContext(),
"file saved",Toast.LENGTH_SHORT).show();
                }

               
catch (Exception e) {
                   
// TODO Auto-generated catch block
                   
e.printStackTrace();
                }
            }
        });

       
b2.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
               
try{
                    FileInputStream fin = openFileInput(
file);
                   
int c;
                    String temp=
"";

                   
while( (c = fin.read()) != -1){
                        temp = temp + Character.toString((
char)c);
                    }
                   
tv.setText(temp);
                    Toast.makeText(getBaseContext(),
"file read",Toast.LENGTH_SHORT).show();
                }
               
catch(Exception e){
                }
            }
        });
    }

   
@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);
    }
}


(2)write  code for activity_main.xml    file :
<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:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">



    <TextView android:text="Internal storage" android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/textview"

        android:textSize="35dp"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true" />



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Android by om "

        android:id="@+id/textView"

        android:layout_below="@+id/textview"

        android:layout_centerHorizontal="true"

        android:textColor="#ff7aff24"

        android:textSize="35dp" />



    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Save"

        android:id="@+id/button"

        android:layout_alignParentBottom="true"

        android:layout_alignLeft="@+id/textView"

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



    <EditText

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/editText"

        android:hint="Enter Text"

        android:focusable="true"

        android:textColorHighlight="#ff7eff15"

        android:textColorHint="#ffff25e6"

        android:layout_below="@+id/imageView"

        android:layout_alignRight="@+id/textView"

        android:layout_alignEnd="@+id/textView"

        android:layout_marginTop="42dp"

        android:layout_alignLeft="@+id/imageView"

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



    <ImageView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/imageView"

        android:src="@drawable/icon"

        android:layout_below="@+id/textView"

        android:layout_alignBottom="@+id/textView"

        android:layout_toLeftOf="@+id/textView"

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



    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="load"

        android:id="@+id/button2"

        android:layout_alignTop="@+id/button"

        android:layout_alignRight="@+id/editText"

        android:layout_alignEnd="@+id/editText" />



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Read"

        android:id="@+id/textView2"

        android:layout_below="@+id/editText"

        android:layout_toLeftOf="@+id/button2"

        android:layout_toStartOf="@+id/button2"

        android:textColor="#ff5bff1f"

        android:textSize="25dp" />



</RelativeLayout>


Output:



Recent Posts

Powered by Blogger.

Pages

About Me

My Photo
B.E.(COMPUTER ENGINEER) PHP Web developer & Provide Training for Web Development , Digital Marketing ,Android Mobile Apps Development

 

© 2013 ANDROID TRAINING IN MUMBAI BY OM SIR. All rights resevered. Designed by Templateism

Back To Top