i have two tab one is login and 2nd is inquiry. i set layout in both tab. but i don't have idea how to set click event on login activity and inquiry activity. just take example currently i am on login tab. In login i have two button submit and cancel if user press submit go to next page ..how i do like that
LoginEnquiryTab.java
public class LoginEnquiryTab extends AppCompatActivity {
TabHost myTabHost;
//LocalActivityManager mlam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler))
{
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
}
//mlam = new LocalActivityManager(this, false);
//mlam.dispatchCreate(savedInstanceState);
myTabHost =(TabHost) findViewById(android.R.id.tabhost);
myTabHost.setup();
TabHost.TabSpec login = myTabHost.newTabSpec("tab1");
TabHost.TabSpec enquiry = myTabHost.newTabSpec("tab2");
//Below name is display on screen
login.setIndicator(getResources().getString(R.string.btn_login));
login.setContent(R.id.tab1);
enquiry.setIndicator(getResources().getString(R.string.enquiry));
enquiry.setContent(R.id.tab2);
myTabHost.addTab(login);
myTabHost.addTab(enquiry);
myTabHost.setCurrentTab(0);
}
@Override
protected void onResume(){
super.onResume();
//mlam.dispatchResume();
}
@Override
protected void onPause(){
super.onPause();
// mlam.dispatchPause(isFinishing());
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<LinearLayout android:id="@+id/tab1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/login" />
</LinearLayout>
<LinearLayout android:id="@+id/tab2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/changepwd" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
See I have made a sample program. But I am using Fragments for tab content-
Class TabActivity.java
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.TabHost;
import android.widget.Toast;
/**
* Created by jimitpatel on 14/04/16.
*/
public class TabActivity extends AppCompatActivity implements OnTabEvent {
private FragmentTabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
// create the TabHost that will contain the Tabs
tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Tab1");
tab2.setIndicator("Tab2");
tab3.setIndicator("Tab3");
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1, Tab1Fragment.class, null);
tabHost.addTab(tab2, Tab2Fragment.class, null);
tabHost.addTab(tab3, Tab3Fragment.class, null);
}
@Override
public void onButtonClick(String text) {
if (!TextUtils.isEmpty(text) && null != tabHost) {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
switch (text) {
case "Tab1" :
tabHost.setCurrentTab(1);
break;
case "Tab2" :
tabHost.setCurrentTab(2);
break;
case "Tab3" :
tabHost.setCurrentTab(0);
break;
}
}
}
}
It's xml file activity_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Class Tab1Fragment.java
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Tab1Fragment extends Fragment {
private OnTabEvent mListener;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
Button button = (Button) view.findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onButtonClick("Tab1");
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (OnTabEvent) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnTabEvent interface");
}
}
}
Class Tab2Fragment.java
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Tab2Fragment extends Fragment {
private OnTabEvent mListener;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab2, container, false);
Button button = (Button) view.findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onButtonClick("Tab2");
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (OnTabEvent) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnTabEvent interface");
}
}
}
And similarly for Tab3Framgent
.
TabOnEvent
over here is interface which has been implemented in TabActivity
and it's callback are used in Fragment classes
Interface TabOnEvent.java
public interface OnTabEvent {
void onButtonClick(String text);
}
I guess this will suffice for what you were looking for. onButtonClick
method is called from Fragment
when button is clicked. And interface will ensure it's called back in activity class and then you can change the tab as per your requirement.