Posts Tagged android

Android TabHost Basic Code Example

Here is a basic example of a TabHost, which allows a user to create a tabs view (the default android ‘contacts/dialer’ menu uses tabhost).

First, the code:

package com.android.mytabviewtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class tabViewTest2 extends Activity {

private int ADD_NEW_TAB = Menu.FIRST;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TabHost tabs = (TabHost) this.findViewById(R.id.my_tabhost);
tabs.setup();
TabSpec tspec1 = tabs.newTabSpec(”First Tab”);
tspec1.setIndicator(”One”);
tspec1.setContent(R.id.content);
tabs.addTab(tspec1);
TabSpec tspec2 = tabs.newTabSpec(”Second Tab”);
tspec2.setIndicator(”Two”);
tspec2.setContent(R.id.content);
tabs.addTab(tspec2);
TabSpec tspec3 = tabs.newTabSpec(”Third Tab”);
tspec3.setIndicator(”Three”);
tspec3.setContent(R.id.content);
tabs.addTab(tspec3);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

menu.add(0, ADD_NEW_TAB, 0, “New Tabs”);
return true;
}

//Dynamically delete tabs, then add one again
//Problem with SDK 1.1 returns null pointer exception
@Override
public boolean onOptionsItemSelected(MenuItem item) {

TabHost tabs = (TabHost) this.findViewById(R.id.my_tabhost);
tabs.clearAllTabs();
tabs.setup();
TabSpec tspec1 = tabs.newTabSpec(”New Tab”);
tspec1.setIndicator(”NEWTAB”, this.getResources().getDrawable(R.drawable.icon));
tspec1.setContent(R.id.content);
tabs.addTab(tspec1);

return super.onOptionsItemSelected(item);
}
}

Now, the XML (i.e. main.xml):

<?xml version=”1.0″ encoding=”utf-8″?>
<TabHost xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/my_tabhost”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<TabWidget
android:id=”@android:id/tabs”
android:layout_width=”fill_parent”
android:layout_height=”65px”/>

<FrameLayout
android:id=”@android:id/tabcontent”
android:layout_width=”fill_parent”
android:layout_height=”200px”
android:paddingTop=”65px”>
<LinearLayout
android:id=”@+id/content”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
</LinearLayout>
</FrameLayout>
</TabHost>

For more information, follow this link to the documentation for a TabHost.

http://developer.android.com/reference/android/widget/TabHost.html

Tags: , , , , , , , ,

Help! What does ERROR Error: No resource found that matches the given name (at ’src’ with value ‘@drawable/mypicture_48′) Mean?

ERROR Error: No resource found that matches the given name (at ’src’ with value ‘@drawable/mypicture_48′).

This is an error that you get because eclipse cannot find the resource you are referring to. A resource could be a picture/audio/etc, whose type is specified after the @ symbol, such as @string, or @drawable in my case. In general, resources are stored under the folder ‘res’ in your project. In this case, eclipse is looking under ‘res’ and then under the ‘drawable’ folder for a resource (such as a picture, i.e. mypicture_48.png) but it doesn’t see anything named this because I forgot to copy my picture into the ‘res’ folder, so it is throwing an error. To fix it, all I have to do is copy this resource into the res folder. If I am using an @string, then I need to modify my strings.xml by adding some xml to declare a new string resource.

Tags: , , , , , , , , ,

Basic Android Tutorial: Countdown Timer Code

Here is some basic code to make a timer that counts down.

You can specify the start value you want and the amount you want it to count down by.

package com.android.countdown;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;

public class CountDownTest extends Activity {

TextView tv; //textview to display the countdown

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

tv = new TextView(this);
this.setContentView(tv);

//5000 is the starting number (in milliseconds)
//1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(5000,1000);

counter.start();

}

//countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer{

public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onFinish() {
tv.setText(”done!”);
}

@Override
public void onTick(long millisUntilFinished) {
tv.setText(”Left: ” + millisUntilFinished/1000);

}

}
}

Tags: , , , , , , , ,