Wednesday, 26 June 2013
Platform 2 Code: Understanding activities in Android
Platform 2 Code: Understanding activities in Android: To create an activity,you create a java class that extends the activity class package com.firstactivity; import android.os.Bundle...
Understanding activities in Android
- To create an activity,you create a java class that extends the activity class
package com.firstactivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
}
Your activity class would the load its UI component using the XML file defined in your res/layout folder.In this example you would load the UI from the main.xml file:
setContentView(R.layout.activity_main);Every activity you have in your application must be declared in your AndroidManifest.xml file like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.firstactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.firstactivity.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thursday, 20 June 2013
Progress Bar in Android
Creating Prograss Bar in Android
-------------------------------------
Activity File
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ProgBar" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/prog_msg" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="@string/msg"
/>
</LinearLayout>
Java Code
-------------
package com.progbar;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ProgBar extends Activity {
ProgressDialog pb;
int value=0;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progbar_activity);
Button b=(Button) findViewById(R.id.b1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(1);
pb.setProgress(0);
handler.sendEmptyMessage(0);
}
});
handler=new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
if(value>=100)
{
pb.dismiss();
}
else
{
value++;
pb.incrementProgressBy(1);
handler.sendEmptyMessageDelayed(0,100);
}
}
};
}
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case 0:
return new AlertDialog.Builder(this).create();
case 1:
pb=new ProgressDialog(this);
pb.setIcon(R.drawable.ic_launcher);
pb.setTitle("Download files...");
pb.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pb.setButton(DialogInterface.BUTTON_POSITIVE,"Hide",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int whichButton)
{
Toast.makeText(getBaseContext(), "Hide Clicked !",Toast.LENGTH_LONG).show();
}
});
return pb;
}
return null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.progbar_activity, menu);
return true;
}
}
Subscribe to:
Posts (Atom)