Tuesday, 28 October 2014

FatchValueFromEditText

Fatch Value From EditText And Display in TextView

Step:-1 Create  New Android Project.

         Ex:-

Step :-2 Go to res/layout open activity_main.xml file and Edit it.

         :-android Text come from the value/string.xml folder

          :-copy below code and paste it you will get the reg layout.
< 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" >
< TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/Title"
android:textSize="20sp" / >
< TextView
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:text="Your Message" />

< EditText
android:id="@+id/entermsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/msg"
android:hint="Enter Your Message" / >

< Button
android:id="@+id/sub"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/entermsg"
android:layout_centerHorizontal="true"
android:text="Submit"
/>
< /RelativeLayout>

STEP 3:-Now go to src/MainActivity.java file.
             :-get the value based on id that you gave in xml file.

             
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

//initialize widget that you need.
TextView yourmsg;
EditText entermsg;
Button submit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* now fatch the id from the activity_main.xml
*/
yourmsg=(TextView)findViewById(R.id.msg);
entermsg=(EditText)findViewById(R.id.entermsg);
submit=(Button)findViewById(R.id.sub);
/**
* now apply the click event when some one click on button.
* you can't get the value before the click event fire.
* if you try to get than u wil get the nullpointer exception.
* implement OnClickListener interface
* now go to onClick() and get the value
*/
submit.setOnClickListener(this);
}
@Override
public void onClick(View v) {

String msg=entermsg.getText().toString();
/**
* now set this value in textview
*/
yourmsg.setText("YOur msg is:="+msg);
}
}
STEP:-4
           Now You can Run application on your Emulator You will get the Out Put.


Sunday, 5 October 2014

SqLite Example in Andorid

Store Register Layout data to  SqLite DataBase :-

STEP:-1 Create  New Android Project.
        Ex:-RegSqlliteDataBase

STEP:-2  Go to res/layout open activity_main.xml file and Edit it.
          :-copy below code and paste it you will get the reg layout.

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

< EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="26dp"
android:ems="10"
android:hint="First Name"
android:inputType="textPersonName" >
< / EditText>
< EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="Last Name"
android:inputType="textPersonName" />

< EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="Mobile Number"
android:inputType="textPersonName" />
< EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText3"
android:layout_marginTop="27dp"
android:ems="10"
android:hint="Email id"
android:inputType="textPersonName" />

< Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText4"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp"
android:text="SUBMIT" />

< Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/editText4"
android:layout_marginRight="14dp"
android:text="Login" />

< / RelativeLayout>



STEP:- 2 Create the One class in src/example/regsqlitedatabase for create the database and create the table.It should be extends from the SQLiteOpenHelper.
          :-see the following code
          :- Open MyDbHelper.java file  put the following code.


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDbHelper extends SQLiteOpenHelper{

public static final int VERSION = 2;
public static final String DBNAME = "Register.db";
public static final String TABLENAME="Reg";
public static final String NAME = "NAME";
public static final String MNAME = "LNAME";
public static final String EMAIL = "EMAIL";
public static final String MNO = "MNO";
public MyDbHelper(Context context) {

//context is use for the create the database or open the database
//DBNAME is the name of the database
//null:- CursorFactory(Interface) its creating the cursor object .
//default value of object is null.
// version of the database.
super(context, DBNAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+TABLENAME+"("+NAME+" text,"+MNAME+" text, "+MNO+" text,"+EMAIL+" text)" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}


STEP 3:-  Now Fetch Data from activity_main.xml to MainActivity.java.
             :-Edit  MainActivity.java based on following code.

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
SQLiteStatement statement;
MyDbHelper dbHelper;
SQLiteDatabase database;
String Name,Lname,Mob,Email;
EditText name,lname,mob,email;
Button b;
Button login;


@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//create the database connection and make it as writeable database.
dbHelper=new MyDbHelper(this);
database=dbHelper.getWritableDatabase();
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.editText1);
lname=(EditText)findViewById(R.id.editText2);
email=(EditText)findViewById(R.id.editText3);
mob=(EditText)findViewById(R.id.editText4);
login=(Button)findViewById(R.id.button2);
login.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Intent log=new Intent(MainActivity.this,LoginActivity.class);
startActivity(log);

}
});
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// convert the data into the string
Name=name.getText().toString();
Lname=lname.getText().toString();
Email=email.getText().toString();
Mob=mob.getText().toString();

try{
//now compile the statement and apply the insert query.

statement=database.compileStatement("insert into "+MyDbHelper.TABLENAME+" values(?,?,?,?)" );
statement.bindString(1, Name);
statement.bindString(2, Lname);
statement.bindString(3, Email);
statement.bindString(4, Mob);
statement.executeInsert();
Toast.makeText(MainActivity.this, "Register succefully done", Toast.LENGTH_LONG).show();
//if the data is successfully inserted than you get the toast and redirect to login screen. Intent i=new Intent(MainActivity.this,LoginActivity.class);
startActivity(i);
}
catch(Exception e){
Toast.makeText(MainActivity.this, "database exception", Toast.LENGTH_LONG).show();
}
}
});

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}



STEP:4  Run application on Your Emulator.You will get the out put.



NOTE:- IF you have any question regard android & java you can comment here i will give                       feedback to you .
           :-learn java :-http://www.javawithjnext.blogspot.com






Monday, 29 September 2014

Customiztion in ExpandableView

STEP 1:- Create the New Android Project In your Android Eclipse tools.
  • Ex:-Expandableviewapp (My application Name)
  • Now You can see the Application Structure.
STEP 2:- Go to in res/layout/activity_main.xml Edit it.Copy below Code and Paste In your 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: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" >
< ExpandableListView
android:id="@+id/exp" android:layout_width="wrap_content" android:layout_height="wrap_content" />
< / RelativeLayout>

STEP 3:- Create list_group.xml & list_childitem.xml file in /layout folder.
             :- we create these xml file for displaying our Expandable content.
             :- put following code in appropriate .xml fil.

STEP 4:- Edit list_group.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" android:background="@android:color/black" >
< TextView
android:id="@+id/lblListHeader" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="17dp" android:paddingLeft="25dp" android:textColor="#f9f93d" />
< / LinearLayout>

STEP 5:- Edit list_childitem.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" >
< TextView android:id="@+id/lblListItem" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="17dip" android:paddingTop="5dp" android:paddingBottom="5dp" />
< / LinearLayout>

STEP 6:- Open  MainActivity.java file and Edit.


package com.example.expandableviewapp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {
ExpandableListView exListView;
ExpandableListAdapter eListAdapter;
List parent;
HashMap> child;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exListView=(ExpandableListView)findViewById(R.id.exp);
preparelistdata(); // populate the data for arrray and
//int[] res=new int[R.layout.list_group,R.layout.list_childitem]
eListAdapter=new CustomExpandAdpter(this,parent,child);
exListView.setAdapter(eListAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void preparelistdata()
{
//data population for expandableview parent=new ArrayList();
parent.add("Jnext");
parent.add("Java");
parent.add("Android");
child =new HashMap>();
List Jnext=new ArrayList();
Jnext.add("software Development");
Jnext.add("Training");
Jnext.add("Resume Buillding");
Jnext.add("Testing");
Jnext.add("Interview pretion");
List java=new ArrayList();
java.add("object");
java.add("core java");
java.add("exception");
java.add("collection");
List android=new ArrayList();
android.add("introdution");
android.add("layout");
android.add("xml");
android.add("web services");
//String[] a= new String[]{parent.get(0),parent.get(1),parent.get(2)};
child.put(parent.get(0), Jnext);
child.put(parent.get(1), java);
child.put(parent.get(2), android);
// return a;
} }

STEP:- 7  Now Create the New class in your package structure.
          :-Ex CustomExpandAdpter.java And Extend from the BaseExpandableListAdapter
see the following code.


package com.example.expandableviewapp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.R.layout;
import android.R.string;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;


public class CustomExpandAdpter extends BaseExpandableListAdapter
{
Context context;
List< String> lst;
HashMap< String, List< String >> hm;
public CustomExpandAdpter(Context context, List lst, HashMap> hm)< br/> {
this.context=context;
this.lst=lst;
this.hm=hm;
}

// Gets the number of children in a specified group. @Override
public int getGroupCount()
{
System.out.println("======= lst size***** :======="+lst.size());
return lst.size();
}
//Gets the data associated with the given group.
@Override
public int getChildrenCount(int groupPosition) {
System.out.println("=====group position====="+groupPosition);
System.out.println("========lst get======");
System.out.println("============child sizeeeeee=========="+hm.get(lst.get(groupPosition)).size());
return hm.get(lst.get(groupPosition)).size();
}
//Gets the data associated with the given child within the given group.
@Override
public Object getGroup(int groupPosition) {
return lst.get(groupPosition);
}
//Gets the data associated with the given child within the given group.
@Override
public Object getChild(int groupPosition, int childPosition)
{
System.out.println("======getchild========"+hm.get(lst.get(groupPosition)).get(childPosition));
return hm.get(lst.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public boolean hasStableIds() {
return false;
} //this method is use for view parent that you want to expand.
@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
String grouptext=(String)getGroup(groupPosition);
if (convertView==null) {
LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=layoutInflater.inflate(R.layout.list_group, null);
} TextView t=(TextView)convertView.findViewById(R.id.lblListHeader);
t.setTypeface(null, Typeface.BOLD);
t.setText(grouptext);
return convertView;
}
@Override
// This method is use for dispayling group of child for apporopriate parent.
public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
String childtxt=(String)getChild(groupPosition, childPosition);
System.out.println("=============child text is:="+childtxt);
if (convertView==null) {
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.list_childitem,null);
}
TextView tw=(TextView)convertView.findViewById(R.id.lblListItem);
tw.setText(childtxt);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}

}

STEP :- 8 Now Run this application in your Android Emulator .

OUT PUT:-