I'm developing Android Application.there i have to use Swipe Views with Tabs .
i want to add drawable image(non_click image) when tabs load & press the tab there will be another image of same tab (click_state image) .Please find below the code I used.please help me to do it
package com.example.creatingswipeviewswithtabs;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.view.Menu;
public class MainActivity extends Activity implements TabListener {
ActionBar action_bar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
action_bar=getActionBar();
//action_bar.setBackgroundDrawable(d)
action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1=action_bar.newTab();
tab1.setText("Login");
tab1.setTabListener(this);
ActionBar.Tab tab2=action_bar.newTab();
tab2.setText("Compare Now");
tab2.setTabListener(this);
ActionBar.Tab tab3=action_bar.newTab();
tab3.setText("Search");
tab3.setTabListener(this);
action_bar.addTab(tab1);
action_bar.addTab(tab2);
action_bar.addTab(tab3);
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
tabs is an array for Tab Strings
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this).setIcon(R.drawable.ic_launcher));
}
Related
I have an app that has 3 swipe tabs: "Map, "Events" and "Reviews". I've logged the data (as seen in my code wherever there is a log), and the following occurs.
I run the app and it opens on the 'Map' tab. The log however, shows
02-14 05:37:56.240: D/curent tab position(1787): 0
02-14 05:37:56.260: D/arg0(1787): 0
02-14 05:37:56.260: D/tab chosen(1787): MapFragment
02-14 05:37:56.260: D/arg0(1787): 1
02-14 05:37:56.260: D/tab chosen(1787): List_Of_EventsFragment
I click on the "Events" tab (the second tab on my actionbar) and the events fragment displays just fine. But the log shows the following, and also shows JSON processing which is NOT part of the "Events" fragment. it's coded in the "Reviews" fragment. So it still shows the List_Of_EventsFragment yet in the background its loading ReviewsListFragment.
02-14 05:38:37.890: D/arg0(1787): 2
02-14 05:38:37.890: D/tab chosen(1787): ReviewsListFragment
02-14 05:38:37.910: D/current page position(1787): 1
02-14 05:38:37.910: D/current tab position(1787): 1
As you can see, the 'current page position' and 'current tab position' are correct. They display 0,1,2 respectfully for each tab. The problem seems to lie in "arg0". I have tried changing my if statements to "if (arg0==1)" instead of "(if arg0==0)" and so forth. That didn't work as it produced a nullpointerException. It seems when the app loads, arg0 equals 0 and then 1 immediately after, even though the "Map" fragment is open and nothing has been clicked.
package com.example.eventmapapp;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class FrontPage extends FragmentActivity implements TabListener{
ActionBar actionbar;
ViewPager viewpager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_front_page);
viewpager =(ViewPager) findViewById(R.id.pager);
viewpager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
actionbar.setSelectedNavigationItem(arg0);
Log.d("current page position", arg0 + "");
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = actionbar.newTab();
tab1.setText("Map");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionbar.newTab();
tab2.setText("Events");
tab2.setTabListener(this);
ActionBar.Tab tab3 = actionbar.newTab();
tab3.setText("Reviews");
tab3.setTabListener(this);
actionbar.addTab(tab1);
actionbar.addTab(tab2);
actionbar.addTab(tab3);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewpager.setCurrentItem(tab.getPosition());
Log.d("current tab position", tab.getPosition() + "");
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int arg0) {
Fragment fragment = null;
Log.d("arg0", arg0 + "");
if (arg0==0) //|| arg0==1
{
fragment = new MapFragment();
Log.d("tab chosen", "MapFragment");
} else
if (arg0==1)
{
fragment = new List_Of_EventsFragment();
Log.d("tab chosen", "List_Of_EventsFragment");
} else
if (arg0==2)
{
fragment = new ReviewsListFragment();
Log.d("tab chosen", "ReviewsListFragment");
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.front_page, menu);
return true;
}
}
This is pretty normal. When you load a tab it loads the tabs before and after it and destroy the other tabs and this is totally controlled by the ViewPager itself. This is called view preloading to show better smoothing while you are swiping between your views.
I am new to Android Studio..I am getting following error in Android Studio.."Can't resolve method getActionBar()' in the statement actionBar = getActionBar();..Project was running fine in Eclipse..plzz Help me with this error
package info.androidhive.tabsswipe;
import com.facebooklogin.R;
import info.androidhive.tabsswipe.adapter.TabsPagerAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.app.Fragment;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
if your target API is below hony must do change these lines:
Create your activity by extending ActionBarActivity.
ActionBar actionBar = getSupportActionBar();
Use (or extend) one of the Theme.AppCompat themes for your activity
I think you confuse API versions, for example you must call getSupportFragmentManager() in low API and getFragmentManager() on above API 11 and you do not need extend FragmentActivity in API 11 and above because it actually have all functions
I am trying to build a simple three (3) tabs app. I am supposed to be implementing three public methods of ActionBar.TabListener.
I believe that is exactly what I am doing in the below code. The compiler however believes that I do not. All three come with the error: "method does not override or implement a method from supertype"
I checked methods' signatures, do not even know where to look, now.
Please advise.
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.app.FragmentActivity;
public class InSync extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Backup", "Restore", "Settings"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_in_sync);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
Thank you.
I believe, you messed up with support libraries. Try
import android.app.FragmentTransaction;
instead of
import android.support.v4.app.FragmentTransaction;
Upd.
Or better use only support libraries:
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
The methods you try to implement are
void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
and
void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
I guess the Tab class you are using is not the "right" Tab class.
EDIT:
On this link you can see that there is a certain android.app.FragmentTransaction. However, you have imported android.support.v4.app.FragmentTransaction. Isn't it possible that this is the problem, as the links to FragmentTransaction from here point to the page of android.app.FragmentTransaction. I believe you should have this code:
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.app.FragmentActivity;
public class InSync extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Backup", "Restore", "Settings"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_in_sync);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
}
}
and if it works you can test even without namespaces.
I am trying to developing Tab Navigation app in Android. I have three tab. Every tab is swipable.
MainClass extends SherlockFragmentActivity from where tab are created. Every class is extends SherlockFragment.
If i swipe tab then i need to change ActionBar custom view and title . But i can not access getSupportActionBar()
method from SherlockFragment class. How can i do that.
Can anyone please help me.
Thanks in Advance
MainActivity.java
import info.androidhive.tabsswipe.adapter.TabsPagerAdapter;
import info.androidhive.tabsswipe.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
Use the above code and try it with the following sample link.. This link has the source..
Source link click here: Android Tab Layout with Swipeable Views.
//I am using action bar tabs in my app,I want to switch from one activity to another activity with in tabs when I press on tabs,how can I call the activity with in tabs. I want to display any activity with in actionbar tabs.
public class MainActivity extends Activity implements TabListener {
// Refresh menu item
private MenuItem action_search;
Tab tab1, tab2, tab3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//creating tabs
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//adding tabs to actionbar
tab1 = actionBar.newTab();
tab1.setText("camera");
tab1.setTabListener(this);
actionBar.addTab(tab1);
tab2 = actionBar.newTab();
tab2.setText("contacts");
tab2.setTabListener(this);
actionBar.addTab(tab2);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
switch (tab.getPosition()) {
case 0:
Intent i = new Intent(getApplicationContext(), MainActivity2.class);
startActivity(i);
break;
case 1:
Intent i = new Intent(getApplicationContext(), MainActivity2.class);
startActivity(i);
break;
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
MainActivity.java
package com.example.moviesswipe;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.Menu;
#SuppressWarnings("unused")
#SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "English", "Tamil", "Hindi" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
TabsPagerAdapter.java
package com.example.moviesswipe;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new English();
case 1:
return new Tamil();
case 2:
return new Hindi();
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
Here i placed three tabs , for each tab i created three activity.
English.java , Tamil.java and Hindi.java
sample one:
package com.example.moviesswipe;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
#SuppressWarnings("unused")
public class English extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.english, container, false);
return rootView;
}
}
For each class you need to create a layout:
No need to entry these classes in your manifest coz these are all fragments.
Try it like this dude :) Happy coding :)
Say if there any queries.