Intent to open a specific tab of tabbed activity - android

I have a tabbed activity with 5 tabs. Each tab has only one Imageview. On a previous page I have 5 buttons and I want to create an interface such that each button starts the tabbed activity but the first tab which is visible is specific to that button. eg. gallery apps open a specific tab corresponding to the thumnail of the photo and are also left/right swappable.

You can pass the tab id you want to open as an extra to the Intent you are creating. Then in the tabbed Activity, assuming you are using TabLayout, you can do something like this -
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
TabLayout.Tab tab = tabLayout.getTabAt(getIntent().getStringExtra("selected_index"));
tab.select();

Try This
First activity
int page = 2;
Intent intent = new Intent(FirstActivity.this,TabActivityClass.class);
intent.putExtra("One", page);// One is your argument
startActivity(intent);
2.In oncreate method of TabActivity class
int defaultValue = 0;
int page = getIntent().getIntExtra("One", defaultValue);
viewPager.setCurrentItem(page);

Related

Open particular tab on click of imageview

I have used multiple tab layout activities (ex.5) & multiple image view (ex:5 ).i click (Ex:3) image then open particular tab (Ex:3). how to write code anybody help.Image used in main activity image click enter tab layout particular tab.
Use Android TabHost and its method setCurrentTab() in your onCreate()-method.Pass your required tab index to this onCreate()
On using ViewPager pass the selected imageview position
viewPager.setCurrentItem(position, true);
Using TabLayout, add onTabSelectedListener to swipe views
tabLayout.setOnTabSelectedListener(this);
When it comes to slide
viewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
viewPager.setCurrentItem(position);
tabLayout.setScrollPosition(position, 0f, true);
}
});

android tablayout - how to use a button instead of tab indicators to trigger click event?

so i have a standard tabLayout that is in a viewpager.
But the very last tab needs to open a new activity. its only purpose is to open a new activity. and when the user returns from the activity by pressing the back button the last tab the user was at should be selected.
my tabs are custom views and there are 4 of them but the last one should be a button to trigger an event. so it will look just like a tab but really will be a button with a onclick listener. how can i acheive this ? i am wondering if a framelayout could be used and activated for the very last tab icon . thats the only way i can see to do it is to get the custom view and set its parent to be clickable that way it absorbs the click event. then i can set a onclick listener on the parent to open the activity. but then how to handle swipping to that tab ? swipping should open that activity as well.
Assuming you're using TabLayout.setupWithViewPager(), you can accomplish what you want by simply adding an extra tab and changing the OnTabSelectedListener.
// normal setup
tabs.setupWithViewPager(pager);
// our extra tab
tabs.addTab(tabs.newTab().setText("extra"));
// remove the `OnTabSelectedListener` created by `setupWithViewPager()`
tabs.clearOnTabSelectedListeners();
// add our own `OnTabSelectedListener`
tabs.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(pager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == pager.getAdapter().getCount()) {
// special case for the last tab in the list
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
else {
// otherwise, handle as normal
super.onTabSelected(tab);
}
}
});

Get EditText Value from an Activity to a Tab in Android Tabhost

I have Tab host contains Three tabs "STEP 1,STEP 2,and STEP 3". Main Tab Host
Activity "MainActiveTab" is Parent activity ,child Activity "TabActStep_1,TabActStep_2,TabActStep_3 " resp.
I Want to access on EditText and Other Value from child Tab Activity like "TabActStep_1,TabActStep_2,TabActStep_3 " .
//Assign id to Tabhost.
TabHostWindow = (TabHost) findViewById(android.R.id.tabhost);
//Creating tab menu.
TabHost.TabSpec TabMenu1 = TabHostWindow.newTabSpec("First tab");
TabHost.TabSpec TabMenu2 = TabHostWindow.newTabSpec("Second Tab");
TabHost.TabSpec TabMenu3 = TabHostWindow.newTabSpec("Third Tab");
//Setting up tab 1 name.
TabMenu1.setIndicator("STEP 1");
//Set tab 1 activity to tab 1 menu.
TabMenu1.setContent(new Intent(this, TabActStep_1.class));
//Setting up tab 2 name.
TabMenu2.setIndicator("STEP 2");
//Set tab 3 activity to tab 1 menu.
TabMenu2.setContent(new Intent(this, TabActStep_2.class));
//Setting up tab 2 name.
TabMenu3.setIndicator("STEP 3");
//Set tab 3 activity to tab 3 menu.
TabMenu3.setContent(new Intent(this, TabActStep_3.class));
//Adding tab1, tab2, tab3 to tabhost view.
TabHostWindow.addTab(TabMenu1);
TabHostWindow.addTab(TabMenu2);
TabHostWindow.addTab(TabMenu3);
This is "MainActiveTab" Here I Want To Get Child Tab Activity Values EditText. etc
Am Try to Send Value from Child Tab Like This
Intent intent = new Intent(getApplicationContext(), MainActiveTab.class);
// Intent intent = new Intent(getApplicationContext(),MainActiveTab.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("name","tab");
// intent.putExtra("HouseName", strHouseName);
startActivity(intent);
This passing Value Get From Main Tab in MainActiveTab Using This Code Using Here
Bundle bundle = getIntent().getExtras();
String id=bundle.get("name").toString();
Declaring intent function on child Tab the that Tab is Showing "Unfortunately App Has Stopped" I Hope You Can Help Me.Thank You !!!
You cannot directly send a value from Activity to a Fragment.You have use an interface to achieve that.
Fragment1->Interface->ActivityClass->Fragment2.

Changing between action bar tabs by clicking a common button in android

I have these 5 tabs and in the first tab/fragment I have a button, I want to be able to switch to another tab by clicking this button. Here's my code which contains the tabs:
actBar = getSupportActionBar();
actBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
secPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
vPager = (ViewPager) findViewById(R.id.pager);
vPager.setAdapter(secPagerAdapter);
vPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actBar.setSelectedNavigationItem(position);
}
});
Tab tab = actBar.newTab()
.setIcon(R.drawable.home)
.setTabListener(this);
actBar.addTab(tab, true);
tab = actBar.newTab()
.setIcon(R.drawable.cart)
.setTabListener(this);
actBar.addTab(tab);
tab = actBar.newTab()
.setIcon(R.drawable.users)
.setTabListener(this);
actBar.addTab(tab);
tab = actBar.newTab()
.setIcon(R.drawable.products)
.setTabListener(this);
actBar.addTab(tab);
tab = actBar.newTab()
.setIcon(R.drawable.settings)
.setTabListener(this);
actBar.addTab(tab);
This creates me a pretty nice action bar with tabs and all, and as you see the one with the home drawable has the below code:
actBar.addTab(tab, true);
since it is true, when this activity is opened it starts with this tab. So... I have a button within this tab. When I tap this button, I want it to scroll right through to the third tab which has the users drawable as an icon. I've seen things about tabhost around here and well, if that's the 'only' case, I gotta say, I don't know about tabhost. I tried to change that true boolean to be able to switch between tabs onClick but it didn't work.
Thanks in advance. I'd really appreciate it.
use this inside button click listener
actionBar.setSelectedNavigationItem(tab_position);

Android how to update 2nd tab according to 1st tab input?

I have build 2 tabs using Fragment. 1st tab has a EditText and a button. If user input something in the EditText and click on the button it goes to 2nd tab. onButtonClick I have set to show 2nd tab but my value is not being updated. Although I have successfully written the 1st tab value in session. But don't know how to update the 2nd tab? How to rebuild the 2nd tab?
Added my code here:
In FragmentA I onButtonClick I have called this function:
private void goToFragmentB(String 1stTabValue)
{
ViewPager mViewPager = (ViewPager) getActivity().findViewById(R.id.pager);
// My 2nd tab has a TextView where I need to set the 1stTabValue. What should be the code
// to set 1stTabValue in 2nd tab
mViewPager.setCurrentItem(1);
}
Edit:
Tab will not refresh because Intent will be called for the first time only.. While adding tabs you will set intent add this flag there addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Example :
Intent i = new Intent().setClass(this, YourClass.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
You Can use Shared Preferences, Store the input values in stored preferences and get in the second Tab
You can store the values in the activity which holds the fragment. I Hope, your next fragment will be using the same activity. But this method will not store your data persistently, If the activity is closed all your previous data is wiped out.
If you would need the data to be persistent then you would have to use Shared Preferences. Data will be stored inside your application folder, under shared_pref folder. It is stored as key-value pairs.
Hope this helps.
try this while adding your tab
private void addTab(int drawableId,String name, Class<?> c)
{
TabHost.TabSpec spec = tabHost.newTabSpec(name);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
TextView text = (TextView) tabIndicator.findViewById(R.id.title);
icon.setImageResource(drawableId);
text.setText(name);
tabHost.setTag(name);
tabHost.addTab(spec
.setIndicator(tabIndicator)
.setContent(new Intent(this, c)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
}
This Intent.FLAG_ACTIVITY_CLEAR_TOP will refresh your tab everytime when you hits your tab
and for storing value, use shared preference or database , its all about your choice

Categories

Resources