0

I have 2 activities, The first activity consist of two buttons that goes to the second activity.

The second activity consists of 2 tab host.

I want to enable the 2nd tabhost from the button of the first activity. how can i do that, please help me and thank you... ^_^

The First Activity Code:

public void OnClickRanoNews(View v)
{
    if (v.getId() == R.id.btnRanoNews) 
    {
        Intent i = new Intent(this, RanoNews.class);
        startActivity(i);
    }

}

public void OnClickRanoNews2(View v)
{
        if(v.getId()== R.id.btnRanoNews2)
        {
            Intent i = new Intent(this, RanoNews.class);
            startActivity(i);
            //I want to disable the second tabhost in this button
        }
}

The Second Activity Code:

tabHost = (TabHost)findViewById(R.id.tabHost);
    tabHost.setup();
    TabHost.TabSpec
    tabSpec  = tabHost.newTabSpec("rano news");
    tabSpec.setContent(R.id.tabRanoNews);
    tabSpec.setIndicator("Rano News");
    tabHost.addTab(tabSpec);

    tabSpec  = tabHost.newTabSpec("adding form");
    tabSpec.setContent(R.id.tabAddingForm);
    tabSpec.setIndicator("Adding Form");
    tabHost.addTab(tabSpec);
    tabHost.getTabWidget().getChildTabViewAt(1).setEnabled(true);
Squall79
  • 3
  • 2

3 Answers3

1
public void OnClickRanoNews2(View v)
{
        if(v.getId()== R.id.btnRanoNews2)
        {
            Intent i = new Intent(this, RanoNews.class); 
            i.putExtra("msg", "0");
            startActivity(i);
        }
} 

on the second Activity

if(getIntent() != null){
      if(getIntent().getStringExtra("msg").equals("0")){
         tabHost.getTabWidget().getChildTabViewAt(1).setEnabled(true);
     }
}
0

Just pass additional extra parameter to new activity (in Bundle)

    if (v.getId() == R.id.btnRanoNews2) {
        Intent i = new Intent(this, RanoNews.class);
        i.putExtra(RanoNews.TABHOST_ENABLED, false /* put your value here */);
        startActivity(i);
    }

And handle it in new activity as last line of onCreate() method

    public static final String TABHOST_ENABLED = "tabhost_enabled";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* TabHost initialization */

        if (getIntent().getExtras() != null) {
            boolean isEnabled = getIntent().getExtras().getBoolean(TABHOST_ENABLED);
            tabHost.setEnabled(isEnabled);
        }
    }
Oleksandr
  • 5,446
  • 42
  • 52
  • i was wandering what would be the value that i put? – Squall79 Sep 18 '15 at 11:29
  • @Squall79 If you want to make it enabled - put true, false - otherwise – Oleksandr Sep 18 '15 at 11:31
  • You can also check this - http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android – Oleksandr Sep 18 '15 at 11:37
  • I try already, but still the second tabhost is still enabled when i clicked both of the button from the first activity... – Squall79 Sep 18 '15 at 11:55
  • @Squall79 Did you put false as TABHOST_ENABLED ? See edited answer. – Oleksandr Sep 18 '15 at 12:30
  • On the first Activity, the TABHOST_ENABLED i put it false, while on the second activity, i put setEnabled to true, but still but buttons can click the second tab host – Squall79 Sep 18 '15 at 12:41
  • @Squall79 you should put value from intent's extra. So if you put false in main Activity, value should be same in second activity – Oleksandr Sep 18 '15 at 12:43
0

This is what i do to the First Activity Code:

public void OnClickRanoNews(View v)
    {
        if (v.getId() == R.id.btnRanoNews)
        {
            Intent i = new Intent(this, RanoNews.class);
            i.putExtra("msg","0");
            startActivity(i);

        }

    }
    public void OnClickRanoNews2(View v)
    {
            if(v.getId()== R.id.btnRanoNews2)
            {
                Intent i = new Intent(this, RanoNews.class);
                i.putExtra("msg","1");
                startActivity(i);

            }
    }

Second Activity Code:

tabHost = (TabHost)findViewById(R.id.tabHost);
        tabHost.setup();
        TabHost.TabSpec
        tabSpec  = tabHost.newTabSpec("rano news");
        tabSpec.setContent(R.id.tabRanoNews);
        tabSpec.setIndicator("Rano News");
        tabHost.addTab(tabSpec);

        tabSpec  = tabHost.newTabSpec("adding form");
        tabSpec.setContent(R.id.tabAddingForm);
        tabSpec.setIndicator("Adding Form");
        tabHost.addTab(tabSpec);

        if(getIntent()!=null)
        {
            if(getIntent().getStringExtra("msg").equals("0"))
                tabHost.getTabWidget().getChildTabViewAt(1).setEnabled(true);

            else if (getIntent().getStringExtra("msg").equals("1"))
                tabHost.getTabWidget().getChildTabViewAt(1).setVisibility(View.GONE);
        }
Squall79
  • 3
  • 2
  • Thanks @akhil Batlawala for solving my problem, please do commend if there's something wrong with the answer code above. – Squall79 Sep 18 '15 at 12:52