main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#F4F4F4">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="center">
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="bottom|center_horizontal"></TabWidget>
</LinearLayout>
</TabHost>
TabGrouActivity.java
package com.statepermit;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
/**
* The purpose of this Activity is to manage the activities in a tab.
* Note: Child Activities can handle Key Presses before they are seen here.
* @author Eric Harlow
*/
public class TabGroupActivity extends ActivityGroup {
private ArrayList<String> mIdList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mIdList == null) mIdList = new ArrayList<String>();
}
/**
* This is called when a child activity of this one calls its finish method.
* This implementation calls {@link LocalActivityManager#destroyActivity} on the child activity
* and starts the previous activity.
* If the last child activity just called finish(),this activity (the parent),
* calls finish to finish the entire group.
*/
@Override
public void finishFromChild(Activity child) {
LocalActivityManager manager = getLocalActivityManager();
int index = mIdList.size()-1;
if (index < 1) {
finish();
return;
}
manager.destroyActivity(mIdList.get(index), true);
mIdList.remove(index); index--;
String lastId = mIdList.get(index);
Intent lastIntent = manager.getActivity(lastId).getIntent();
Window newWindow = manager.startActivity(lastId, lastIntent);
setContentView(newWindow.getDecorView());
}
/**
* Starts an Activity as a child Activity to this.
* @param Id Unique identifier of the activity to be started.
* @param intent The Intent describing the activity to be started.
* @throws android.content.ActivityNotFoundException.
*/
public void startChildActivity(String Id, Intent intent) {
Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
mIdList.add(Id);
Log.v("log_tag", "startchildactivity: " + mIdList.size() + ":" + Id);
setContentView(window.getDecorView());
}
}
/**
* The primary purpose is to prevent systems before android.os.Build.VERSION_CODES.ECLAIR
* from calling their default KeyEvent.KEYCODE_BACK during onKeyDown.
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.v("log_tag", " this function involve when press-down " );
if (keyCode == KeyEvent.KEYCODE_BACK) {
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
onBackPressed();
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* Overrides the default implementation for KeyEvent.KEYCODE_BACK
* so that all systems call onBackPressed().
*/
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.v("log_tag", " this function involve when press-up ");
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
/**
* If a Child Activity handles KeyEvent.KEYCODE_BACK.
* Simply override and add this method.
*/
@Override
public void onBackPressed() {
Log.v("log_tag", "this function involve when press back");
int length = mIdList.size();
Log.v("log_tag", "length: " + length);
if (length >= 1) {
Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
Log.v("log_tag", "the child view :"+current);
this.finishFromChild(current);
}
}
}
StatePermitApplication.java
package com.statepermit;
import android.app.Application;
public class StatePermitApplication extends Application {
private static String from = "state";
public static String getFrom() {
return from;
}
public static void setFrom(String fromPage) {
from = fromPage;
}
}
statepermit.java
package com.statepermit;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
public class statepermit extends TabActivity
{
TabHost mTabHost;
TabWidget mTabWidget;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
mTabHost = getTabHost();
TabHost.TabSpec spec;
mTabWidget = mTabHost.getTabWidget();
Intent intent;
intent = new Intent().setClass(this, Home.class);
spec = mTabHost.newTabSpec(getString(R.string.state))
.setIndicator("Home", res.getDrawable(R.drawable.home))
.setContent(intent);
mTabHost.addTab(spec);
intent = new Intent().setClass(this, Search.class);
spec = mTabHost.newTabSpec("Search")
.setIndicator("Search", res.getDrawable(R.drawable.search))
.setContent(intent);
mTabHost.addTab(spec);
intent = new Intent().setClass(this, Back.class);
spec = mTabHost.newTabSpec("Back")
.setIndicator("Back", res.getDrawable(R.drawable.back))
.setContent(intent);
mTabHost.addTab(spec);
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++)
{
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundColor(Color.parseColor("#CCCCCC"));
TextView tv = (TextView) mTabWidget.getChildAt(i).findViewById(
android.R.id.title);
tv.setTextColor(Color.BLACK);
}
mTabHost.getTabWidget().getChildAt(0)
.setBackgroundColor(Color.parseColor("#FFFFFF"));
mTabHost.setCurrentTab(0);
mTabHost.setOnTabChangedListener(new OnTabChangeListener()
{
@Override
public void onTabChanged(String tabId)
{
int tab = mTabHost.getCurrentTab();
if (tab == 0)
{
mTabHost.getTabWidget().getChildAt(1)
.setBackgroundColor(Color.parseColor("#CCCCCC"));
mTabHost.getTabWidget().getChildAt(2)
.setBackgroundColor(Color.parseColor("#CCCCCC"));
}
else if (tab == 1)
{
mTabHost.getTabWidget().getChildAt(0)
.setBackgroundColor(Color.parseColor("#CCCCCC"));
mTabHost.getTabWidget().getChildAt(2)
.setBackgroundColor(Color.parseColor("#CCCCCC"));
}
else
{
mTabHost.getTabWidget().getChildAt(0)
.setBackgroundColor(Color.parseColor("#CCCCCC"));
mTabHost.getTabWidget().getChildAt(1)
.setBackgroundColor(Color.parseColor("#CCCCCC"));
}
mTabHost.getTabWidget().getChildAt(tab)
.setBackgroundColor(Color.WHITE);
}
});
}
}
Login.java
package com.statepermit;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Login extends TabGroupActivity {
protected static final String TAG = Login.class.getName();
private static final String LOG = "log_tag";
public static XMLParser xmlParser;
public static User myUser;
private EditText urname;
private EditText urpass;
private String username;
private String password;
public String myError;
private Dialog dialog;
int l = 0;
protected ProgressDialog mProgressDialog;
private Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
Log.v(LOG, "Message Received");
if (msg.what == 0) {
Log.v("log_tag", "Print Error Message");
errorMessage();
}
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Log.v("log_tag", "from where" + StatePermitApplication.getFrom());
TextView forgot = (TextView) findViewById(R.id.text1);
SpannableString content = new SpannableString("Forgot Password");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
forgot.setText(content);
forgot.setTextColor(Color.BLACK);
forgot.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Login.this, Forgotpassword.class);
StatePermitApplication.setFrom("state_login");
startChildActivity("forgot", intent);
}
});
StatePermitApplication.setFrom("state");
Button login = (Button) findViewById(R.id.button1);
urname = (EditText) findViewById(R.id.edit1);
urpass = (EditText) findViewById(R.id.edit2);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
username = urname.getText().toString().trim();
password = urpass.getText().toString().trim();
if (username.equals("") && password.equals("")) {
Log.v("log_tag", "data is blank");
insertdata();
} else {
if (checkInternetConnection()) {
//Login.this.showDialog(1);
mProgressDialog=ProgressDialog.show(getParent(), "Please Wait", "Login In Process", true);
new LoadUserData()
.execute(
"http://www.retailpermits.com/WebServices/iPhone/WS_iPhone.asmx/ValidateUser?strUserName="
+ username
+ "&strPassword="
+ password, username, password);
StateApplicationClass.setLoginname(username);
StateApplicationClass.setPassword(password);
Log.v("log_tag",
"Full Path :"
+ "http://www.retailpermits.com/WebServices/iPhone/WS_iPhone.asmx/ValidateUser?strUserName="
+ username + "&strPassword=" + password);
} else {
// showInternetErrorDialog();
internetError();
Log.e(TAG, "Internet Error: ");
}
}
}
});
}
private class LoadUserData extends AsyncTask<String, Integer, Integer> {
protected Integer doInBackground(String... paths) {
String url;
int type;
try {
url = paths[0];
Log.v("log_tag", "URL: " + url);
xmlParser = new XMLParser("ValidateUser");
xmlParser.parseUser(url);
if (xmlParser.getUserId() < 0) {
myError = "Could not complete the login. Try again later.";
type = 0;
Log.v("log_tag", "Error: " + myError);
} else {
type = 1;
Log.v("log_tag", "XMLPARSER: " + xmlParser.getUserId());
}
return type;
} catch (Exception e) {
Log.v("log_tag", "Exception: " + e.toString());
}
return 0;
}
protected void onPostExecute(Integer result) {
//dialog.dismiss();
mProgressDialog.dismiss();
if (result == 1) {
Log.v("log_tag", "Succesfully Login");
Intent intent = new Intent(Login.this, Info.class);
StatePermitApplication.setFrom("state_login");
startChildActivity("state_info", intent);
// startActivity(intent);
l = xmlParser.getUserId();
} else if (result == 0) {
// errorMessage();
// showErrorMessage(myError);
Log.v("log_tag", "the wrong password");
Message msg = new Message();
msg.what = 0;
myHandler.sendMessage(msg);
Log.v("log_tag", "Message Sent");
}
}
}
private boolean checkInternetConnection() {
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
} else {
Log.v(TAG, "Internet Connection Not Present");
return false;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.DONUT
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed() {
Log.v("log_tag", "On home Back");
Intent intent = new Intent(Login.this, statepermit.class);
StatePermitApplication.setFrom("state");
startActivity(intent);
finish();
return;
}
private void internetError() {
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getParent());
alt_bld.setMessage(R.string.con_error_text)
.setCancelable(false)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
// Title for AlertDialog
alert.setTitle(R.string.connection_error);
// Icon for AlertDialog
// alert.setIcon(R.drawable.icon);
alert.show();
}
private void errorMessage() {
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getParent());
alt_bld.setMessage(R.string.error_message)
.setCancelable(false)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
}
});
AlertDialog alert = alt_bld.create();
// Title for AlertDialog
alert.setTitle(R.string.error);
alert.show();
}
private void insertdata() {
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getParent());
alt_bld.setMessage(R.string.insertdata)
.setCancelable(false)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
}
});
AlertDialog alert = alt_bld.create();
// Title for AlertDialog
alert.setTitle(R.string.error);
alert.show();
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == 1) {
if (dialog == null) {
dialog = new Dialog(getParent());
dialog.setTitle(R.string.please_wait);
}
return dialog;
}
return super.onCreateDialog(id);
}
}
screenshot, bro. provide a screenshot. without that people are reluctant to try it out or even read it.
ReplyDeleteIs there any other way to have multiple activities on single tab because, ActivityGroup is deperected ?
ReplyDeletealways put scrreeeen shot
ReplyDelete