Technical Knowledge

technical knowledge is that type of blogger in which you can find all the soluctions of your problems which you cannot solve you can see the video given on this blog and solve your problem by yourself.

Like us on Facebook

Technical Knowledge
Showing posts with label start. Show all posts
Showing posts with label start. Show all posts

Sunday, March 29, 2015

How to start an activity and pass data to second screen: Android tutorial


This post will try to cover how to start a second activity from main activity on button click and pass data.
I will assume that you have 2 activities and 2 xml files (one for each activity).
First activity has one button, on clicking which we will start the second activity by using explicit intent. While starting the second activity we will use putExtra() method to pass the data to second activity.
Once second activity is started, the data passed from first activity will be displayed in second activity as toast messages.
Second activity has a button, on clicking which we will close second activity by using finish() method.

First screen                                                                  Second screen






First Activity

package com.techpalle.b15_startactivity;

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;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(),
SecondaryScreen.class);
in.putExtra("uid", "satish");
startActivity(in);
}
});
}

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

}

xml layout file for First Activity 
File name: activity_main.xml
  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" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />


Second Activity
package com.techpalle.b15_startactivity;

import java.io.Closeable;

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

public class SecondaryScreen extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
String action = in.getAction();
Bundle bnd = in.getExtras();
String uid = bnd.getString("uid");
Toast.makeText(this, "uid is.."+uid+"..action.."+action, 0).show();
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
// TODO Auto-generated method stub
}

}

xml layout file for Second Activity 
File name: second.xml
  
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome second.."
android:textAppearance="?android:attr/textAppearanceLarge" />

Tags : android, startactivity, putextras, pass data between activities, start second activity, android tutorial

Download complete code : Click to download

Android interview questions on this topic:
android activity interview questions and answers

Similar basic activity programs:
changing edittext text from button click listener 
Simple calculator program to add and subtract 2 numbers 
How to start an activity from main activity on clicking button, and pass data 
Starting second activity based on login and password details 
Getting results from child activity

How to start an activity from main activity on clicking button, and pass data: Android tutorial

Program description:

Simple android program with 2 activities. First activity will have a button to start second activity. This is done by using explicit intent.
Along with intent we will pass some data "uid" (user id) to second activity.

Once second activity is started, it takes the data passed from first activity and displays in toast message. Second activity will have a button , on clicking it second activity will be destroyed.


               First Activity                                                Second Activity




First Activity
package com.techpalle.b15_startactivity;

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;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
Intent in = getIntent();
String action = in.getAction();
Toast.makeText(this, "action.."+action, 0).show();
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(),
SecondaryScreen.class);
in.putExtra("uid", "satish");
startActivity(in);
}
});
}

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

}
xml layout file for First Activity 
File name: activity_main.xml
  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" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />


Second Activity
package com.techpalle.b15_startactivity;

import java.io.Closeable;

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

public class SecondaryScreen extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
String action = in.getAction();
Bundle bnd = in.getExtras();
String uid = bnd.getString("uid");
Toast.makeText(this, "uid is.."+uid+"..action.."+action, 0).show();
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
// TODO Auto-generated method stub
}

}

xml layout file for Second Activity 
File name: second.xml
  
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome second.."
android:textAppearance="?android:attr/textAppearanceLarge" />



For complete code, use below link to download it..
https://drive.google.com/file/d/0BzRRGL2xSFLGWnhtaVBkTjJMclk/edit?usp=sharing

Tags: startactivity, pass data between activities, getintent, android tutorial

android activity interview questions and answers

Monday, March 23, 2015

How to start an activity from main activity on clicking button, and pass data: Android tutorial

Program description:

Simple android program with 2 activities. First activity will have a button to start second activity. This is done by using explicit intent.
Along with intent we will pass some data "uid" (user id) to second activity.

Once second activity is started, it takes the data passed from first activity and displays in toast message. Second activity will have a button , on clicking it second activity will be destroyed.


               First Activity                                                Second Activity




First Activity
package com.techpalle.b15_startactivity;

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;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
Intent in = getIntent();
String action = in.getAction();
Toast.makeText(this, "action.."+action, 0).show();
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(),
SecondaryScreen.class);
in.putExtra("uid", "satish");
startActivity(in);
}
});
}

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

}
xml layout file for First Activity 
File name: activity_main.xml
  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" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />


Second Activity
package com.techpalle.b15_startactivity;

import java.io.Closeable;

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

public class SecondaryScreen extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
String action = in.getAction();
Bundle bnd = in.getExtras();
String uid = bnd.getString("uid");
Toast.makeText(this, "uid is.."+uid+"..action.."+action, 0).show();
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
// TODO Auto-generated method stub
}

}

xml layout file for Second Activity 
File name: second.xml
  
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome second.."
android:textAppearance="?android:attr/textAppearanceLarge" />



For complete code, use below link to download it..
https://drive.google.com/file/d/0BzRRGL2xSFLGWnhtaVBkTjJMclk/edit?usp=sharing

Tags: startactivity, pass data between activities, getintent, android tutorial

android activity interview questions and answers

How to start an activity and pass data to second screen: Android tutorial


This post will try to cover how to start a second activity from main activity on button click and pass data.
I will assume that you have 2 activities and 2 xml files (one for each activity).
First activity has one button, on clicking which we will start the second activity by using explicit intent. While starting the second activity we will use putExtra() method to pass the data to second activity.
Once second activity is started, the data passed from first activity will be displayed in second activity as toast messages.
Second activity has a button, on clicking which we will close second activity by using finish() method.

First screen                                                                  Second screen






First Activity

package com.techpalle.b15_startactivity;

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;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(),
SecondaryScreen.class);
in.putExtra("uid", "satish");
startActivity(in);
}
});
}

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

}

xml layout file for First Activity 
File name: activity_main.xml
  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" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />


Second Activity
package com.techpalle.b15_startactivity;

import java.io.Closeable;

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

public class SecondaryScreen extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
String action = in.getAction();
Bundle bnd = in.getExtras();
String uid = bnd.getString("uid");
Toast.makeText(this, "uid is.."+uid+"..action.."+action, 0).show();
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
// TODO Auto-generated method stub
}

}

xml layout file for Second Activity 
File name: second.xml
  
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome second.."
android:textAppearance="?android:attr/textAppearanceLarge" />

Tags : android, startactivity, putextras, pass data between activities, start second activity, android tutorial

Download complete code : Click to download

Android interview questions on this topic:
android activity interview questions and answers

Similar basic activity programs:
changing edittext text from button click listener 
Simple calculator program to add and subtract 2 numbers 
How to start an activity from main activity on clicking button, and pass data 
Starting second activity based on login and password details 
Getting results from child activity

Monday, December 1, 2014

Androidguruz - Ready to start its operations in India




The Good news : We will see a lot of Chinese super-brands enter India. Androidguruz - Exclusive Chinese Phones Retailer In India - has finally geared up to start its services.


The Chinese brands like Zopo, Oppo, UMI, Iocean, Neo, Mogu, Meizu, Xiaomi, etc will now available under one roof -- AndroidGuruz.  India is a vast market and it is difficult to setup retail and distribution network. Thus all these Chinese brands have mutually agreed to launch the AndroidGuruz (retail store), where all these Chinese manufacturers will be able to sell, service, provide warranty replacements along with experience zone (where users can experience the handset before buying, as in Nokia/Samsung stores )

First retail store of AndriodGuruz will be operational soon in New Delhi in coming month and another in Bengaluru later on. Androidguruz will be one stop destination of all the Chinese mobile phones.All the Chinese brands will provide 3 months replacement warranty with 1 year support. In the next 12 to 15 months AndriodGuruz will setup around 200 retail store in India. For more information you may check androidguruz or their face-book page.

I think this will revolutionize the mobile phone market in India. Brands like Samsung have already suffered a lot due to the local brands like Micromax. Now the Chinese dragon will add to worries of Samsung and other such brands. Since there will be no intermediaries involved as such, prices of phones at Androidguruz is expected to be on lower side. This will also pose a threat for local brands like Micromax. In nut shell it will be a good environment for consumers. And this is all we want :)

Older Posts Home