Explanation:
In this tutorial i am going to
explain that how to work with tabs and how can we add tabs also how to work
with stop watch. Lets Go..
Create Android Application:
- File >> New >> Android
Application
- Enter Project name: TabsApp
- Package: com.ambilpursunil.newapp
- Keep other default selections, click Next until you reach Finish
2.Copy and past the following
code in the MainActivity.java
package com.ambilpursunil.newapp;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
public class Tabs extends Activity implements OnClickListener {
TabHost th;
TextView showResults;
Long start, stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
th = (TabHost) findViewById(R.id.tabhost);
Button
newTab = (Button) findViewById(R.id.bAddTab);
Button
bStart = (Button) findViewById(R.id.bStartWatch);
Button
bStop = (Button) findViewById(R.id.bStopWatch);
showResults = (TextView) findViewById(R.id.tvShowResults);
bStart.setOnClickListener(this);
bStop.setOnClickListener(this);
newTab.setOnClickListener(this);
// what type of content is tabhost class
hold,what to organize thae date
th.setup();
TabSpec
specs = th.newTabSpec("tab1");
specs.setContent(R.id.tab1);
specs.setIndicator("StopWatch");
th.addTab(specs);
specs
= th.newTabSpec("tab2");
specs.setContent(R.id.tab2);
specs.setIndicator("Tab 2");
th.addTab(specs);
specs
= th.newTabSpec("tab3");
specs.setContent(R.id.tab3);
specs.setIndicator("Add a Tab");
th.addTab(specs);
start = (long) 0;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bAddTab:
TabSpec
ourSpecs = th.newTabSpec("tab1");
ourSpecs.setContent(new TabHost.TabContentFactory() {
@Override
public View createTabContent(String tag) {
// TODO Auto-generated method stub
TextView
text = new TextView(Tabs.this);
text.setText("Entered Text is ");
return text;
}
});
ourSpecs.setIndicator("New");
th.addTab(ourSpecs);
break;
case R.id.bStartWatch:
start = System.currentTimeMillis();
break;
case R.id.bStopWatch:
stop = System.currentTimeMillis();
if (start != 0) {
long result = stop - start;
int millis = (int) result;
int seconds = (int) result / 1000;
int minutes = seconds / 60;
millis
= millis % 100;
seconds
= seconds % 60;
showResults.setText(String.format("%d:%02d:%02d", minutes,
seconds,
millis));
}
break;
}
}
}
2.Make the change the of activity_main.xml file as the
below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/bStartWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="@+id/bStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
<TextView
android:id="@+id/tvShowResults"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 2"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/bAddTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add a Tab"
/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
3.Copy and past the code for manifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ambilpursunil.newapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ambilpursunil.newapp.Tabs"
android:configChanges="keyboard|keyboardHidden|orientation" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
4.Right click on the project and Run As-->Android
Application
If you click on the stopwatch the 2 buttons will appear called start and stop just click on the start button and after some time if you click on stop button then it will display the time, if you click on tab2 nothing will display except textview name, but if you click on Add a Tab button it will add the number of tabs as much as you click.
Please Send Your Comments To ambilpura.sunil@gmail.com
Stay Tune For Next Tutorial... Shared Preference in Android:
No comments:
Post a Comment