Thursday, December 15, 2011
Working with Intent in Android :
# We have to create an "Intent" for moving one activity to another activity :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</TableRow>
<TableRow >
<Button
android:id="@+id/btnNext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go to Second Activity"/>
</TableRow>
<TableRow >
<ImageView
android:id="@+id/first"
android:src="@drawable/first"
android:layout_width="280px"
android:layout_height="320px"/>
</TableRow>
</TableLayout>
</LinearLayout>
// Source Code of second.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</TableRow>
<TableRow >
<Button
android:id="@+id/btnEnd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go to first activity" />
</TableRow>
<TableRow >
<ImageView
android:id="@+id/second"
android:src="@drawable/second"
android:layout_width="280px"
android:layout_height="320px"/>
</TableRow>
</TableLayout>
</LinearLayout>
// Source Code of IntentExampleActivity.java :
package com.myApp.IntentExample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentExampleActivity extends Activity
{
/** Called when the activity is first created. */
Button btnNext;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnNext= (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Intent firstintent=new Intent(IntentExampleActivity.this,SecondExampleActivity.class);
startActivity(firstintent);
}
});
}
}
// Source Code of SecondExampleActivity.java :
package com.myApp.IntentExample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class SecondExampleActivity extends Activity {
/** Called when the activity is first created. */
Button btnEnd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Button btnEnd=(Button) findViewById(R.id.btnEnd);
btnEnd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent secondintent=new Intent(SecondExampleActivity.this,IntentExampleActivity.class);
startActivity(secondintent);
}
});
}
}
Output :
# We have to create an "Intent" for moving one activity to another activity :
- Create an xml file named as "second.xml" and copy and paste below code to this xml file.
- Create a class file under "src" folder named as "SecondExampleActivity" and renamed first class file to "IntentExampleActivity".
- Pick two images file and copy this in res/drawable folder.(for first and second activity)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</TableRow>
<TableRow >
<Button
android:id="@+id/btnNext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go to Second Activity"/>
</TableRow>
<TableRow >
<ImageView
android:id="@+id/first"
android:src="@drawable/first"
android:layout_width="280px"
android:layout_height="320px"/>
</TableRow>
</TableLayout>
</LinearLayout>
// Source Code of second.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</TableRow>
<TableRow >
<Button
android:id="@+id/btnEnd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go to first activity" />
</TableRow>
<TableRow >
<ImageView
android:id="@+id/second"
android:src="@drawable/second"
android:layout_width="280px"
android:layout_height="320px"/>
</TableRow>
</TableLayout>
</LinearLayout>
// Source Code of IntentExampleActivity.java :
package com.myApp.IntentExample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentExampleActivity extends Activity
{
/** Called when the activity is first created. */
Button btnNext;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnNext= (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Intent firstintent=new Intent(IntentExampleActivity.this,SecondExampleActivity.class);
startActivity(firstintent);
}
});
}
}
// Source Code of SecondExampleActivity.java :
package com.myApp.IntentExample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class SecondExampleActivity extends Activity {
/** Called when the activity is first created. */
Button btnEnd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Button btnEnd=(Button) findViewById(R.id.btnEnd);
btnEnd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent secondintent=new Intent(SecondExampleActivity.this,IntentExampleActivity.class);
startActivity(secondintent);
}
});
}
}
Output :
![]() |
| First Activity |
![]() |
| Second Activity |
Read More »
How to install .apk file in Android Emulator :
You have to follow these steps :
Step.1 : Download any .apk file.
Step.2 : Paste this into your "android-sdk-windows/platform-tools/" folder.
Step.3 : Run command prompt.
Step.4 : Set path of your command prompt to "..../platform-tools" folder with the help of "cd"(change directory) command.
Step.5 : Finally , run following command :
C:\.........\platform-tools> adb install filename.apk and press enter.
Please wait for a while , for successfull installation of apk. Filename would be name of your downloaded .apk file.
Step.6 : Restart Emulator , open menu and you got that application in your grid menu icons.
Same process will be follow with "tools" folder if "adb"(android debugger bridge) is not available in "platform-tools" folder.
Step.1 : Download any .apk file.
Step.2 : Paste this into your "android-sdk-windows/tools/" folder.
Step.3 : Run command prompt.
Step.4 : Set path of your command prompt to "..../tools" folder with the help of "cd"(change directory) command.
Step.5 : Finally , run following command :
C:\.........\tools> adb install filename.apk and press enter.
Please wait for a while , for successfull installation of apk. Filename would be name of your downloaded .apk file.
Step.6 : Restart Emulator , open menu and you got that application in your grid menu icons.
If "adb" is not available in "tools" folder then there will be a text file named as "adb_has_moved" then it is clear that "adb" present in "platform-tools" folder.
You have to follow these steps :
Step.1 : Download any .apk file.
Step.2 : Paste this into your "android-sdk-windows/platform-tools/" folder.
Step.3 : Run command prompt.
Step.4 : Set path of your command prompt to "..../platform-tools" folder with the help of "cd"(change directory) command.
Step.5 : Finally , run following command :
C:\.........\platform-tools> adb install filename.apk and press enter.
Please wait for a while , for successfull installation of apk. Filename would be name of your downloaded .apk file.
Step.6 : Restart Emulator , open menu and you got that application in your grid menu icons.
Same process will be follow with "tools" folder if "adb"(android debugger bridge) is not available in "platform-tools" folder.
Step.1 : Download any .apk file.
Step.2 : Paste this into your "android-sdk-windows/tools/" folder.
Step.3 : Run command prompt.
Step.4 : Set path of your command prompt to "..../tools" folder with the help of "cd"(change directory) command.
Step.5 : Finally , run following command :
C:\.........\tools> adb install filename.apk and press enter.
Please wait for a while , for successfull installation of apk. Filename would be name of your downloaded .apk file.
Step.6 : Restart Emulator , open menu and you got that application in your grid menu icons.
If "adb" is not available in "tools" folder then there will be a text file named as "adb_has_moved" then it is clear that "adb" present in "platform-tools" folder.
Read More »
Saturday, December 10, 2011
Play Audio File in Android :
# We have to create an application in which when we click on "play" button , audio file should be play :
package com.myApp.ListeningSong;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class ListeningSongActivity extends Activity
{
/** Called when the activity is first created. **/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.playSound);
b.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.kunfaya);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
mp.release();
}
});
}
return true;
}
});
}
}
// Source Code of main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/playSound"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginTop="170dp"
android:layout_marginLeft="20dp"
android:layout_gravity="center"
android:background="@drawable/btnscreen"/>
<MediaController
android:id="@raw/kunfaya"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Output :
Note : Uploading and installation of .apk will be depend on audio file size , it may be take too much time so don't irritate, just wait.
# We have to create an application in which when we click on "play" button , audio file should be play :
- Create a new project named as "ListeningSong" and class name will be such as "ListeningSongActivity.java".
- Pick an audio file.
- Create a new folder named as "raw" under res directory.Paste audio file under this raw folder.
- Pick an image icon file named as "btnscreen.png". Paste this file under drawable folders.
package com.myApp.ListeningSong;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class ListeningSongActivity extends Activity
{
/** Called when the activity is first created. **/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.playSound);
b.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.kunfaya);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
mp.release();
}
});
}
return true;
}
});
}
}
// Source Code of main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/playSound"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginTop="170dp"
android:layout_marginLeft="20dp"
android:layout_gravity="center"
android:background="@drawable/btnscreen"/>
<MediaController
android:id="@raw/kunfaya"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Output :
![]() |
| After Clicking on Play Button , Song plays |
Note : Uploading and installation of .apk will be depend on audio file size , it may be take too much time so don't irritate, just wait.
Read More »
Friday, December 09, 2011
Browser Calling in Android :
# We have to open an URL as "www.google.com" by calling android browser :
// Source Code of registration.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<WebView
android:id="@+id/mywebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
// Source Code of InternetSurfActivity.xml :
package com.myApp.InternetSurf;
import android.webkit.WebView;
import android.app.Activity;
import android.os.Bundle;
public class InternetSurfActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView wv=(WebView) findViewById(R.id.mywebview);
wv.loadUrl("http://www.google.com");
}
}
Output :
# We have to open an URL as "www.google.com" by calling android browser :
- Create a new project named as "InternetSurf" and class name will be such as "InternetSurfActivity.java".
- Create an xml file named as "mywebview.xml" and copy and paste below code to this xml file.
// Source Code of registration.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<WebView
android:id="@+id/mywebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
// Source Code of InternetSurfActivity.xml :
package com.myApp.InternetSurf;
import android.webkit.WebView;
import android.app.Activity;
import android.os.Bundle;
public class InternetSurfActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView wv=(WebView) findViewById(R.id.mywebview);
wv.loadUrl("http://www.google.com");
}
}
Output :
Read More »
Subscribe to:
Posts (Atom)




