Thursday, February 28, 2013

Android Data Passing between Activities

                          PASSING DATA BETWEEN ANDROID ACTIVITES
This is very basic knowledge to pass data from one activity to another activity, first we will create an android project then its activity and we will create an layout xml and associate activity lastly we will call the second activity from first one with the data.

If you are using eclipse
File --> New --> New Android Application

Then come to the layout folder and create an android layout xml file.

Then come to the main package folder in src and create an class file named as Second.

Your new layout file

<?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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>
Your new activity class

package com.example.datapassexample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Second extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView textView = (TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
if(intent.getExtras() != null)
{
String text = intent.getStringExtra("name");
textView.setText(text);
}
}

}
Your first activity class
package com.example.datapassexample;

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText) findViewById(R.id.editText1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClassName(getApplicationContext(), Second.class.getName());
intent.putExtra("name", editText1.getText().toString());
startActivity(intent);
}
});
}

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

}

!!!!!!
The value that you pass in activity depends on the passed activity so there will be no value in another activity associated with this intent extra even if you use getIntent() function instead of new Intent() function.

So how can i get the same value in another activities?
There are lots of ways to get data in another activities such as you can write to your sqlite database or you can pass using intents again even you can send to any server you defined then you get the value from that server but the fast way is that i also use the singleton method you can store the value among the usage of program if there are some states that the program will be cancelled you should use db. Singleton is like below
open a new class named as Single
package com.example.datapassexample;

public class Single {
private static Single single;
private String name;
protected Single(){
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public static Single getInstance(){
if(single == null){
single = new Single();
}
return single;
}
}

open your MainActivity
add this line into the button click function before startActivity function
Single single = Single.getInstance();
single.setName(editText1.getText().toString());

open your Second Activiy then insert code below
Single single = Single.getInstance();
String text = single.getName();
textView.setText(text);


Now you can use the value that you store in Single class in each of the activities for active states of the program.

Thursday, February 21, 2013

HOW TO ADD IMAGE INSIDE A DIV AND CONTINUE FOR A QUICK PROCESS

                        HOW TO ADD IMAGE INSIDE A DIV AND CONTINUE FOR A QUICK PROCESS
You can traverse the html tree and you can handle all the leafs individually that gives you possibility to play some part of tree. For example if you want to add a div then continue to work that can cause some problem such as you can not see new image in the specified div. Lets code for a button when we click it lets change the div' s image and continue with the other page. I used html5 on Lg and Samsung when i coded. For example this is the button below;


<a href="javascript:route();" id="signup" style="height: 200px; width: 100px"><div id="container"><IMG SRC="oldimage.gif" id="myid"></div></a>

now script turns;
<script type="text/javascript" >

function route(){
var willBeRemoved = document.getElementById("myid");
var container= document.getElementById("container");
container.removeChild(willBeRemoved);
var newimage = document.createElement("img");
oImg.src = "newPicture.gif";
container.appendChild(newimage);
setInterval(function(){changePage()},100);
}
function changePage(){
window.location.href="signup.html";
}

</script>

The code above will add functionality to change image before loading another page because of the process speed there will be no time to change the image but if you specify a duty to process the function it will find a spare time to change...

It is also problem in jquery mobile that if you want to show an information page in time of process you will use the function below;

$.mobile.showPageLoadingMsg("e", "Your Page Is Loading \n Please Wait", true);

the function above takes 2 argument first one is the theme of the window second one is the message that you want to show. Now if you use this function in the gap between two process that will work in a series that will show error in logcat so you can prevent such a strategy we followed for the image and div example.