Wednesday, October 20, 2010

Android : How to have multiple activities under a single tab of TabActivity

Yes, its possible.
1. Lets first have our main activity as
public class multiple extends TabActivity {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost();  // The activity TabHost
TabHost.TabSpec spec;  // Resusable TabSpec for each tab
Intent intent;  // Reusable Intent for each tab // Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Activity1.class);
spec = tabHost.newTabSpec("Activity1").setIndicator(new CustomTabView(this,
R.drawable.options_selected, "Activity1")) .setContent(intent);
tabHost.addTab(spec); // Do the same for the other tabs
intent = new Intent().setClass(this, Activity2.class);
spec = tabHost.newTabSpec("Activity2").setIndicator(new CustomTabView(this, R.drawable.options_selected, "Activity2")) .setContent(intent);
tabHost.addTab(spec);
}
 
}
 
 
2. Here we create 2 tabs. Activity1  Activity2
Activity1 tab launches Activity1
Activity2 tab launches Activity2
 
Now tricky part is how to launch Activity3 from Activity1 so that Activity3 also shows the tabs.
 
3. To achieve this we extend Activity1 to be derived from ActivityGroup. Clicking on a button in activity1 will launch Activity3 as subactivity of Activity1.
 
public class Activity1 extends ActivityGroup {
protected static LocalActivityManager mLocalActivityManager;
Button launchButton;
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
launchButton = (Button)findViewById(R.id.Button01);
launchButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent activity3Intent = new Intent(v.getContext(), Activity3.class);
StringBuffer urlString = new StringBuffer();
//Activity1 parentActivity = (Activity1)getParent();
replaceContentView("activity3", activity3Intent);
}
});
 
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}        }
 
4. We can even launch another activity, Activity4 from Activity3 and still have the tabs at the bottom.
public class Activity3 extends Activity {
Button launchButton;
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity3);
launchButton = (Button)findViewById(R.id.Button01);
launchButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {               Intent activity4Intent = new Intent(v.getContext(), Activity4.class);
StringBuffer urlString = new StringBuffer();
Activity1 parentActivity = (Activity1)getParent();
parentActivity.replaceContentView("activity4", activity4Intent);
} }); }}
 
Enjoy!!

Your Ad Here

Tuesday, October 19, 2010

Using Handler in Android

 in Android there are several ways in making your setInterval, one of which is using a Handler with Thread or using Timer, on this article would focus on using Handler. The reference of where i learn this is on your android sdk folder, under the samples, there is an app named Snake.java. You can take a look at that one and see more advance than the codes below.


Main.java
package com.monmonja.firstDemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class Main extends Activity {
  private TextView txtStatus;
  private RefreshHandler mRedrawHandler = new RefreshHandler();

  class RefreshHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
      Main.this.updateUI();
    }

    public void sleep(long delayMillis) {
      this.removeMessages(0);
      sendMessageDelayed(obtainMessage(0), delayMillis);
    }
  };

  private void updateUI(){
    int currentInt = Integer.parseInt((String) txtStatus.getText()) + 10;
    if(currentInt <= 100){
      mRedrawHandler.sleep(1000);
      txtStatus.setText(String.valueOf(currentInt));
    }
  }

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    this.txtStatus = (TextView) this.findViewById(R.id.txtStatus);
    updateUI();
  }
}


main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

<TextView android:id="@+id/txtStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="20px"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
</TextView>
</RelativeLayout>



Quick Explanation
This one is probably the most hardest for me to explain so far, okay so here is how it goes. We created a class inside our Activity that extends Handler, now on this handler we override handleMessage so what we can use this Handler to handle our messages to it. We then declare a sleep function to delay another execute of this handler with the function sendMessageDelayed. Now on our updateUI we set our own codes and call mRedrawHandler.sleep(1000); to delay our message. But before all of this to happen on our onCreate function we call updateUI to start updating our textfield.

Your Ad Here

Monday, October 18, 2010

How to add and remove Widgets from your Android home screen


This brief tutorial will show you how to add and/or remove Widgets to/from your Android home screen.
  1. On your Android home screen, tap any blank or “empty” area and hold down your finger.
  2. An Add to Home screen window will pop up. From that window, tap Widgets.
  3. You’ll be presented with a list of all the installed Widgets on your Android device. Select the one you want to add to your home screen by tapping it. In this example, I’ll be adding the Calendar widget.
  4. Now the widget will appear on your home screen. Tap the widget and hold down your finger again – this time you can “drag” it to the location on the screen you want it to reside.
  5. Should you decide you want to remove the widget from your home screen, tap it and hold your finger down. You’ll notice that the “slider” that normally brings up all of your installed programs turns into a “Trash can”.
  6. Drag the widget to that trash can. Both the widget and the trash can will turn red. At this point, let go of the widget (release your finger). That will “drop” it in the trash. Note: this does not actually uninstall the widget. It just removes it from your home screen. Should you ever decide to add it again, it will be there waiting for you.
  7. That’s it – the widget has been removed from your home screen.


Your Ad Here