Tuesday, 20 August 2013

Starting activities on tab click

Starting activities on tab click

I have 3 tabs which show different content on click. I want them to open
certain activities on click. These activities are nothing but database
reports which just need to be displayed. I used web view and works fine on
emulator with API level 11 but gives an error saying "Web page not found"
on my device which is API level 10. Also, The tabs display data only after
clicking on other tabs and coming back to the first. Is there a way I can
start the activities directly instead of using web view?
Any help is appreciated.
Heres my code:
xml file:-
<LinearLayout
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:id="@+id/linearLayout1"
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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabs1"
android:orientation="vertical"
>
<WebView
android:id="@+id/txtGetFuelInfo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Get info from Database" />
<TextView
android:id="@+id/txtGetFuel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabs2"
android:orientation="vertical"
>
<WebView
android:id="@+id/txtMainGetInfo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Get info from Database"/>
<TextView
android:id="@+id/txtGetMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabs3"
android:orientation="vertical"
>
<WebView
android:id="@+id/txtSpareGetInfo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Get info from Database"/>
<TextView
android:id="@+id/txtGetspare"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
CLass file :-
public class DetailsTabs extends Activity
{
TabSpec tspec;
FuelStoredInfo fuelInfo;
WebView fuelView,mainView,sparesView;
TextView tvFuel, tvMain, tvSpares;
WebSettings fuelSetting, mainSetting, spareSetting;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.detailstabs);
fuelView = (WebView)findViewById(R.id.txtGetFuelInfo);
mainView = (WebView)findViewById(R.id.txtMainGetInfo);
sparesView = (WebView)findViewById(R.id.txtSpareGetInfo);
//tvFuel = (TextView)findViewById(R.id.totalFuelId);
//tvMain = (TextView)findViewById(R.id.totalmainId);
//tvSpares = (TextView)findViewById(R.id.totalspareId);
fuelInfo = new FuelStoredInfo(this);
fuelInfo.open();
TabHost th = (TabHost)findViewById(R.id.tabhost);
th.setup();
// Fuel Expense tab
tspec = th.newTabSpec("Tag 1");
tspec.setContent(R.id.tabs1);
tspec.setIndicator("Fuel"+"\n"+"Expense");
String Data = fuelInfo.getData();
double fuelTotal =fuelInfo.getTotalFuel();
fuelSetting = fuelView.getSettings();
fuelSetting.setDefaultFontSize(10);
//tvFuel.setText("Total fuel expense : Rs. "+
String.valueOf(fuelTotal));
fuelView.loadData(Data,"text/html",null);
th.addTab(tspec);
//Maintenance Expense tab
tspec = th.newTabSpec("Tag 2");
tspec.setContent(R.id.tabs2);
tspec.setIndicator("Maintenance"+"\n"+"Expense");
mainSetting = mainView.getSettings();
mainSetting.setDefaultFontSize(10);
String mainData = fuelInfo.getMainData();
double mainTotal =fuelInfo.getTotalMain();
tvMain.setText("Total maintenance expense : Rs. "+
String.valueOf(mainTotal));
mainView.loadData(mainData,"text/html",null);
th.addTab(tspec);
//Spares Expense tab
tspec = th.newTabSpec("Tag 3");
tspec.setContent(R.id.tabs3);
tspec.setIndicator("Spares"+"\n"+"Expense");
spareSetting = sparesView.getSettings();
spareSetting.setDefaultFontSize(10);
String sparesData= fuelInfo.getSpareData();
double spareTotal = fuelInfo.getTotalSpares();
tvSpares.setText("Total spares expense : Rs."+
String.valueOf(spareTotal));
sparesView.loadData(sparesData,"text/html",null);
th.addTab(tspec);
fuelInfo.close();
}
}

No comments:

Post a Comment