Tuesday, March 4, 2014

Tutorial of passing Strings or ArrayList or Arrays from one java file to another using Intents in Android..

Here I declared two java files in package and two xml files with respect to java files..

MainActivity.java:

package com.example.ex;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
/*ArrayList for Storing values*/
ArrayList ar=new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button) findViewById(R.id.button1);
/*inserting values into Arraylist*/
for(int i=0;i<10;i++)
{
/*add is method through which values are binded to ArrayList */
ar.add(i);
}
/*setting up OnClickListener and Passing values to Second.java file */
b1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

/*Declaring an Intent to Start another activity*/
Intent i=new Intent(MainActivity.this,Second.class);
/*putExtra() is method used to add some extra content to Intent variable..
* here as i am passing ArrayList i choose putStringArrayListExtra(),
* this method takes two parameters,first refers to key or id for ArrayList and
* Second is ArrayList name9It is similar for others also */
i.putStringArrayListExtra("key",ar);
/*now i am Starting activity Second */
startActivity(i);
}
});
}
}

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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="38dp"
        android:layout_marginTop="152dp"
        android:text="@string/pas" />

</RelativeLayout>

Second.java:

package com.example.ex;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lay2);
ListView li=(ListView) findViewById(R.id.listView1);
/* Bundle for getting extras sent through Intent from MainActivity*/
Bundle b1=getIntent().getExtras();
/* ArrayList for Storing values which are sent from from MainActivty,
* getStringArrayList() will get values From Intent
* this method takes one parameter which is the first parameter of putStringArrayListExtra()*/
ArrayList s1=b1.getStringArrayList("key");
/*setting values of ArrayList to ListView */
li.setAdapter(new ArrayAdapter(Second.this,android.R.layout.simple_list_item_1,s1));
}
}

lay2.xml:

<?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" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>


The output for the above Application is:




Thursday, February 27, 2014


Hie,This Is Naresh...[Senior Member of Xda-Developers]..

Tutorial of How to Create and Access Database in single java file in Android?

MainActivity code:

public class MainActivity extends Activity{
   //creating access variable for DataBase
  SQLiteDatabase db;

  @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
          Button b=(Button) findViewById(R.id.delapp);
         //To open or create a database
                final SQLiteDatabase db =openOrCreateDatabase("Example",MODE_WORLD_WRITEABLE, null);
        //for creating table in database
                    db.execSQL("CREATE TABLE IF NOT EXISTS EXAMPLE(NAME VARCHAR);");
       
//inserting values into table
                 for(int i=0;i<10;i++)
                 {
                db.execSQL("INSERT INTO EXAMPLE VALUES('"+i+"')");

}      
                  b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//cursor is used for accessing data from database table..
//SELECT NAME[you can add columns hereseparating them with comma] FROM EXAMPLE..
Cursor c1=db.rawQuery("SELECT NAME FROM EXAMPLE", null);
                             //moveToFirst() is used to point to starting row of the database table
      if (c1.moveToFirst())
      {
          do
          {
                                 //c1.getString(0) will provide the column NAME value..here '0' is column index..if you have //any other column //in database after NAME then the index number will be "1" and so on..
                                  String v = c1.getString(0);
    //Toasting the values inserted into table..   
                                  Toast.makeText(app.getApplicationContext(),v,100).show();
          }
          while (c1.moveToNext());
//c1.moveToNext() will make cursor to move to next position..
      }
      if (c1 != null && !c1.isClosed())
      {
//c1.close(); is closing of cursor.[it is not mandatory in some cases..but better practice with closing cursor]..
          c1.close();
      }
   
});
 
 }
}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fullView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
  
        <Button
            android:id="@+id/delapp"
            android:layout_width="45dp"
            android:layout_height="match_parent"
            android:layout_above="@+id/button3"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="61dp"
            android:text="Button" />

</RelativeLayout>
Any Queries,post a comment...


NLAUNCHER:

Home replacement App For ANdroid...

Launcher With Multi-uses..
Swipe To Acces Your Favourite Apps...

Version2.2 features[11/2/2014]:
UI changed..
Now you can choose your dock apps..
Guide for how to Use app..
Swiping Gestures:
  1. Swipe from right to access fav app list
  2. Swipe from left to access Settings
  3. Swipe from top to load wireless and network settings..
  4. Swipe from bottom to load Manage Applications Settings..
Swipe to access Favourite apps..
Deleting of Apps From Favourite List..
Battery percentage added in toggles[for those who are with stock]..
9 Inbuilt Toggles..
Smooth..

Version2.2.2 features:
Changes in UI..
transparent App Drawer..
Reduced Memory Usage[Need Testing]..

to download: