onTabChanged method is not inwalking, - android

My Method onTabChanged() is not called when I clicked the tab bar to change the tab
public class ZoobuzzActivity extends TabActivity implements OnTabChangeListener {
/** Called when the activity is first created. */
TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_tab);
}
public void onTabChanged(String tabId) {
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.RED);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.GREEN);
}
}
please help me I want to change color when tab selection is change.

You need to set OnTabChangeListener to your TabHost as below:
//set tab change listener
tabHost.setOnTabChangedListener(this);
//where 'this' is a reference to your activity ZoobuzzActivity

Related

Move back to parent activity from child activity android

In my app, I have one tab which includes two activities say 1 and 2. Activity 1 extends ActivityGroup, which has one child activity say A. Now scenario becomes like this,
In first tab --> Activity 1 and its child activity A.
In second tab --> Activity 2. After doing some functionality in Activity 1 of first tab, user moves to child activity A of first tab. Now what I want to do, when user presses back button from child activity A, user should move back to its parent activity 1. How to do that? Below is my code..
MainActivity.java
public class MainActivity extends TabActivity {
TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources ressources = getResources();
tabHost = getTabHost();
Intent intentProfile = new Intent().setClass(this, Tab1.class);
TabSpec tabProfile = tabHost
.newTabSpec("Profile")
.setIndicator("Profile", ressources.getDrawable(R.drawable.ic_launcher))
.setContent(intentProfile);
Intent intentFriends = new Intent().setClass(this, Tab2.class);
TabSpec tabFriends = tabHost
.newTabSpec("Friends")
.setIndicator("Friends", ressources.getDrawable(R.drawable.ic_launcher))
.setContent(intentFriends);
tabHost.addTab(tabProfile);
tabHost.addTab(tabFriends);
tabHost.setCurrentTab(0);
}
public void switchTabBar(int tab) {
tabHost.setCurrentTab(tab);
}
#Override
public void onBackPressed() {
// Called by children
MainActivity.this.finish();
}
}
Tab1.java
public class Tab1 extends ActivityGroup {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(Tab1.this, Second.class);
replaceContentView("activity3", in);
}
});
}
public void replaceContentView(String id, Intent newIntent)
{
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}
#Override
public void onBackPressed() {
this.getParent().onBackPressed();
}
}
Second.java
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
#Override
public void onBackPressed() {
this.getParent().onBackPressed();
MainActivity parentTab = (MainActivity) this.getParent();
parentTab.switchTabBar(0);
}
}
Tab2.java
public class Tab2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
}
#Override
public void onBackPressed() {
// this.getParent().onBackPressed();
MainActivity parentTab = (MainActivity) this.getParent();
parentTab.switchTabBar(0);
}
}
Here I can move to tab 1 from tab 2 on back button pressed. But, I can't move back to parent activity from child activity of tab 1.
You have to override back button in parent activity and make a call from child. For more info please check this link : Navigate Between Activities in an ActivityGroup
In Tab1:
#Override
public void onBackPressed() {
super.onBackPressed();
}
#Override
public void onPause()
{
super.onPause();
finish();
}
in Tab1

static variable is null after initialization

I have an action bar activity with 3 tabs.
I have a main activity, where I post an xml to a webservice and get back an xml in an AsyncTaskj. I'm initializing a static variable of the main activity in AsncTask. From main activity another activity is called, where Action Bar is initialized. Each action bar has a ListFragment, where I use the static variable. I have no issue accessing a Static variable from 2 List activities but in first ListFragment m not able to access the initialized static variable.
Edit 1
My Main Activity
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
GenerateXml gx=new GenerateXml();
String requestXml=gx.generateXml(tv1.getText().toString(),tv2.getText().toString(),tv3.getText().toString(),tv4.getText().toString());
myNewTask = new MyTask(requestXml);
myNewTask.execute();
Intent intent=new Intent(getApplicationContext(),TabActivity.class);
startActivity(intent);
}
});
In my AsyncTask doInBackground
MainActivity.responseXml=responseXML;
The Activity where tabs are created
public class TabActivity extends Activity {
MyTask myNewTask;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
/** Creating All Tab */
Tab tab = actionBar.newTab()
.setText("All")
.setTabListener(new CustomTabListener<AllMsgFragment>(this, "All", AllMsgFragment.class));
//.setIcon(R.drawable.android);
actionBar.addTab(tab);
/** Creating Success Tab */
tab = actionBar.newTab()
.setText("Success")
.setTabListener(new CustomTabListener<SuccessMsgFragment>(this, "Success", SuccessMsgFragment.class));
//.setIcon(R.drawable.apple);
actionBar.addTab(tab);
/** Creating Error Tab */
tab = actionBar.newTab()
.setText("Error")
.setTabListener(new CustomTabListener<ErrorMsgFragment>(this, "error", ErrorMsgFragment.class));
//.setIcon(R.drawable.apple);
actionBar.addTab(tab);
}
}
My first ListFragment:
public class AllMsgFragment extends ListFragment {
public static String response;
public ArrayList<HashMap<String, String>> msgDetails;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
response=MainActivity.responseXml;
XmlToArrayList xmlArray=new XmlToArrayList();
try {
msgDetails=xmlArray.arrayListXml(response);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(getActivity(), msgDetails,
R.layout.msg_preview,
new String[] { "ObSystem", "ObName", "Msgstate"}, new int[] {
R.id.bs, R.id.si, R.id.msgStatus});
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onStart() {
super.onStart();
Log.e("first","1");
/** Setting the multiselect choice mode for the listview */
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}

Problems with OnClickListener for a tab in android

I'm new to android programing and I'm having problems with OnClickListener for tabs in my app. I found on stack a solution how it should be done, but for some reason it's not working.
I'm trying to use the 2nd answer
For some reason I'm getting 2 errors.
First one is on the name on of my activity: The type DragonLords must implement the inherited abstract method View.OnClickListener.onClick(View).
Second one is on the OnClick method: The method onClick(View) of type new View.OnClickListener(){} must override a superclass method.
Here is a part of my code:
public class DragonLords extends TabActivity implements OnClickListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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, Home.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("home").setIndicator("home",
res.getDrawable(R.drawable.hometab))
.setContent(intent);
tabHost.addTab(spec);
getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (getTabHost().getCurrentTab()==0) {
getTabHost().setCurrentTab(0);
}else
{
getTabHost().setCurrentTab(0);
}
}
});
After that I'm creating more tabs. With out the onclicklistener it's working, the thing is I need to be able to reload the tabs when they are active.
Anyone have an idea what I'm doing wrong?
I added the necessary imports.
Gatz
You must implement the onClick method not in an anonymous inner class like you have done in your code.
Try using new TabWidget.OnClickListener instead of just the normal OnClickListener
Something akin to the following:
public class TestActivity extends TabActivity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getTabWidget().getChildAt(0).setOnClickListener(new TabWidget.OnClickListener() {
public void onClick(View v) {
if (getTabHost().getCurrentTab()==0) {
getTabHost().setCurrentTab(0);
}else
{
getTabHost().setCurrentTab(0);
}
}
});
}
public void onClick(View theView) {
// Do something with view here
}
}
you are doing it pretty hard way.. i did it like this...
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tablayout);
res = getResources();
tabHost=(TabHost)this.findViewById(android.R.id.tabhost);
tabHost.getTabWidget().setDividerDrawable(R.drawable.vertical_seperator);
setupTab(new TextView(this), "Login",new Intent().setClass(this,loginForm.class));
setupTab(new TextView(this), "Can't Login",new Intent().setClass(this,ForgotPwd.class));
setupTab(new TextView(this), "Register",new Intent().setClass(this,RegisterUser.class));
}
private void setupTab(final View view, final String tag,final Intent myIntent)
{
View tabview = createTabView(tabHost.getContext(), tag);
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(
new TabContentFactory()
{
public View createTabContent(String tag)
{return view;}
}).setContent(myIntent);
tabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text)
{
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
hope this helps....
tabs_bg is just an xml with
<TextView android:id="#+id/tabsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="15dip"
android:textColor="#drawable/tab_text_selector" />

change TabActivity title dynamically according to selected activity tab data

i have created MyTabActivity extending TabActivity class with two tabs.
public class MyTabActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("My App Name");
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec(TAB_1).setIndicator(TAB_1, getResources().getDrawable(R.drawable.tab1)).setContent(new Intent(this, first.class)));
tabHost.addTab(tabHost.newTabSpec(TAB_2).setIndicator(TAB_2, getResources().getDrawable(R.drawable.tab2)).setContent(new Intent(this, second.class)));
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
}
});
tabHost.setCurrentTabByTag(TAB_1);
}
}
i need to change the title of MyTabActivity according to change in one of activity data.
public class first extends Activity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
//change the MyTabActivity title from here ???
}
}
Please help me how to do this.
Thanks
You can get your TabActivity inside child Activity like this.
If you are in FirstActivity or SecondActivity try something like this.
MyTabActivity myTabs = (MyTabActivity) this.getParent();
myTabs.setTitle("This is First cool Activity");
you can do the same in SecondActivity.
Cheers!!

When switching tabs in Android, what does it constitute?

I have three tabs and each one is its own activity. When I switch tabs I want my Spinner to update but I don't know what method gets called on tab switch. Any help?
Hook up a listener to the TabHost.OnTabChangeListener. You main activity extends TabActivity and its onCreate method probably looks something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("tab1")
.setContent(R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab2")
.setContent(R.id.view2));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab3")
.setContent(R.id.view3));
}
To hook up the listener, add the following code in your onCreate() method:
tabHostt.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
if(tabId.equals("tab1")) {
//tab1
}
else if(tabId.equals("tab2")) {
//tab2
}
else if(tabId.equals("tab3")) {
//tab3
}
}
});
HTH
Something like this should work:
tabHost.setOnTabChangedListener(new OnTabChangeListener()
{
public void onTabChanged(String tabId)
{
//Do stuff in here
}
});

Categories

Resources