I have seen some similar posts here, but I haven't found any answers. I want to add a swipe gesture to my tab activity, so that I'm able to change tabs with the gesture.
Here's my code:
public class Domiciliaria extends TabActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
//requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seleccion);
//ActionBar bar = getActionBar();
//bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#cd3b05")));
final TabHost tabHost = getTabHost();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
setTabColor(tabHost);
}
}
);
// Tab for primera
TabSpec primeraspec = tabHost.newTabSpec("info 1");
primeraspec.setIndicator("Datos", getResources().getDrawable(R.drawable.primera));
Intent primeraIntent = new Intent(this, Parte_primera.class);
primeraspec.setContent(primeraIntent);
// Tab for segunda
TabSpec segundaspec = tabHost.newTabSpec("info 2");
segundaspec.setIndicator("Motivos", getResources().getDrawable(R.drawable.segunda));
Intent segundaIntent = new Intent(this, Parte_segunda.class);
segundaspec.setContent(segundaIntent);
// Tab for tercero
TabSpec tercerospec = tabHost.newTabSpec("info 3");
tercerospec.setIndicator("Examen", getResources().getDrawable(R.drawable.tercera));
Intent terceroIntent = new Intent(this, Parte_tercera.class);
tercerospec.setContent(terceroIntent);
// Tab for Cuarto
TabSpec cuartospec = tabHost.newTabSpec("info 4");
cuartospec.setIndicator("otro", getResources().getDrawable(R.drawable.cuarta));
Intent cuartoIntent = new Intent(this, Parte_cuarta.class);
cuartospec.setContent(cuartoIntent);
// Tab for Final
TabSpec finalspec = tabHost.newTabSpec("info 4");
finalspec.setIndicator("Final", getResources().getDrawable(R.drawable.pago));
Intent finalIntent = new Intent(this, Final.class);
finalspec.setContent(finalIntent);
// agregando TabSpec to TabHost
tabHost.addTab(primeraspec); // Adding photos tab
tabHost.addTab(segundaspec); // Adding songs tab
tabHost.addTab(tercerospec);
tabHost.addTab(cuartospec);
tabHost.addTab(finalspec);
tabHost.getTabWidget().setStripEnabled(true);
tabHost.getTabWidget().setRightStripDrawable(R.drawable.barra_mamei);
tabHost.getTabWidget().setLeftStripDrawable(R.drawable.barra_mamei);
setTabColor(tabHost);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
case R.id.CasoEspecial:
startActivity(new Intent(this, CasoEspecial.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
// respond to menu item selection
}
protected void setTabColor(TabHost tabHost) {
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#E04006")); //unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#fbfbf3")); // selected
}
}
You can achieve the TabSwipe using ViewPager with Fragments and ActionBar.
If you wish to work with Minimum API 11, the following link will show you how. Since API 11, ActionBar has the way to create tabs in application with fragments.
If you need to give support to the older versions, you should make use of Support Library
or ActionBarSherlock
Check out this Link as well
This is the best tutorial for horizontal swipe views straight from google.
Creating Swipe Views with Tabs
I would recommened to use Fragments and FragmentActivity instead of TabActivity
Related
Is it possible to add a swipe functionality to an existing Tab Menu? i.e. swipe left or right to move between the tabs rather than clicking on a tab.
I already have an exisiting 3 tab menu and do not want to greatly alter it's structure, but would love to add the swipe functionality.
How can I do so?
Main Menu/TabHost:
public class MainMenu extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec tab1 = tabHost.newTabSpec("Games");
Intent tab1Intent = new Intent(this, firstActivity.class);
tab1.setContent(tab1Intent);
tab1.setIndicator("Games");
TabSpec tab2 = tabHost.newTabSpec("Search");
Intent tab2Intent = new Intent(this, secondActivity.class);
tab2.setContent(tab2Intent);
tab2.setIndicator("Search");
TabSpec tab3 = tabHost.newTabSpec("Summary");
Intent tab3Intent = new Intent(this, thirdActivity.class);
tab3.setContent(tab3Intent);
tab3.setIndicator("Summary");
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
#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;
}
}
Example of tab (middle tab):
public class secondActivity extends ActionBarActivity implements View.OnClickListener {
TextView instruction;
Button date;
Button game;
Button med;
Button att;
Button score;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_tab);
initialiseVars();
}
/**
* Initialises vars and sets onclick listeners for buttons
*/
public void initialiseVars() {
// setting up vars
instruction = (TextView) findViewById(R.id.tvSearchHome);
date = (Button) findViewById(R.id.btnSearchHomeDate);
game = (Button) findViewById(R.id.btnSearchHomeGame);
med = (Button) findViewById(R.id.btnSearchHomeMedValues);
att = (Button) findViewById(R.id.btnSearchHomeAttValues);
score = (Button) findViewById(R.id.btnSearchHomeScore);
// set on click listeners for the buttons
date.setOnClickListener(this);
game.setOnClickListener(this);
med.setOnClickListener(this);
att.setOnClickListener(this);
score.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearchHomeDate:
Intent openDateSearch = new Intent(
"com.example.brianapp.SearchDate");
// Start activity
startActivity(openDateSearch);
break;
case R.id.btnSearchHomeGame:
Intent openGameSearch = new Intent(
"com.example.brianapp.SearchGame");
// Start activity
startActivity(openGameSearch);
break;
case R.id.btnSearchHomeMedValues:
// change this to make sure it opens the med screen
Intent openMedSearch = new Intent(
"com.example.brianapp.MeditationSearchHome");
// Start activity
startActivity(openMedSearch);
break;
case R.id.btnSearchHomeAttValues:
// change this to make sure it opens the med screen
Intent openAttSearch = new Intent(
"com.example.brianapp.AttentionSearchHome");
// Start activity
startActivity(openAttSearch);
break;
case R.id.btnSearchHomeScore:
// change this to make sure it opens the med screen
Intent openScoreSearch = new Intent(
"com.example.brianapp.SearchScore");
// Start activit
startActivity(openScoreSearch);
break;
}// switch end
}
}
I can just provide you with the following snippet. Please note that I haven't tested it. I just wanted to show how I would approach this problem.
public class MainMenu extends TabActivity implements GestureDetector.OnGestureListener{
...
#Override
public boolean onFling(MotionEvent e0, MotionEvent e1, float arg2, float arg3) {
...
int current = getCurrentTab(); // TabHost.getCurrentTab()
int next;
if(e0.getRawX() - e1.getRawX() < 0 ){
//fling right
next = current + 1; //TODO check for next = n, n=number of tabs
}
else{
next = current - 1; //TODO check for next = -1
}
setCurrentTab(next); // TabHost.setCurrentTab()
}
}
I have 2 tab while select first tab the accept menu icon should not visible.
while select second tab the accept menu icon should visible. I tried but i am getting null pointer exception.
And also should get the text from the second activity while select accept menu.
How can i do this ?
Here is my Code :
public class DetailTabActivity extends TabActivity implements OnTabChangeListener {
String name,address,rating,reference,lat,lng;
StringConstants constants;
Float rate;
MenuItem menuitem;
String url;
TabHost tabHost;
Menu menu1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.overridePendingTransition(R.anim.anim_out,
R.anim.anim_in);
constants=new StringConstants();
setContentView(R.layout.layout_detail_tab);
Intent intent = getIntent();
name = intent.getStringExtra("name");
Log.e("name",name+"");
address = intent.getStringExtra("address");
Log.e("address",address+"");
rating = intent.getStringExtra("rating");
Log.e("rating",rating+"");
reference = intent.getStringExtra("reference");
Log.e("reference",reference+"");
lat = intent.getStringExtra("lat");
Log.e("lat",lat+"");
lng = intent.getStringExtra("lng");
Log.e("lng",lng+"");
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
// Tab for Photos
TabSpec detailspec = tabHost.newTabSpec("Detail");
// setting Title and Icon for the Tab
detailspec.setIndicator("Detail");
Intent detailintent = new Intent(this, DetailActivity.class);
detailintent.putExtra("name",name);
detailintent.putExtra("address",address);
detailintent.putExtra("rating",rating);
detailintent.putExtra("reference",reference);
detailspec.setContent(detailintent);
// Tab for Songs
TabSpec reviewspec = tabHost.newTabSpec("Review");
reviewspec.setIndicator("Review/Rating");
Intent songsIntent = new Intent(this, ReviewAndRating.class);
songsIntent.putExtra("name",name);
songsIntent.putExtra("lat",lat);
songsIntent.putExtra("lng",lng);
reviewspec.setContent(songsIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(detailspec); // Adding photos tab
tabHost.addTab(reviewspec); // Adding songs tab
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#ffffff"));
// Set Tab1 as Default tab and change image
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#134960"));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.layout_detail_tab, menu);
menuitem = menu.findItem(R.id.action_accept);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_accept:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onTabChanged(String tabId) {
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
if(i==0)
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
else if(i==1)
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
}
Log.i("tabs", "CurrentTab: "+tabHost.getCurrentTab());
if(tabHost.getCurrentTab()==1)
{
menuitem.setVisible(true);
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
else if(tabHost.getCurrentTab()==0)
{
menuitem.setVisible(false);
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
}
}
set invalidateOptionsMenu();as,
if(tabHost.getCurrentTab()==1)
{
menuitem.setVisible(true);
invalidateOptionsMenu();
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
else if(tabHost.getCurrentTab()==0)
{
menuitem.setVisible(false);
invalidateOptionsMenu();
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
I have two tabs in my application and I want the menu to change depending on the Tab.
TabHost tabHost = tabHost = getTabHost();
TabSpec photospec = tabHost.newTabSpec("Photos");
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.photo));
Intent photosIntent = new Intent(this, Photos.class);
photospec.setContent(photosIntent);
TabSpec songspec = tabHost.newTabSpec("Songs");
songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.songs));
Intent songsIntent = new Intent(this, Songs.class);
songspec.setContent(songsIntent);
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int currentTab = tabHost.getCurrentTab();
if (currentTab == 0)
startActivity(new Intent(this, Photosoptions.class));
if (currentTab == 1)
{
startActivity(new Intent(this, Songsoptions.class));
}
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
int currentTab = tabHost.getCurrentTab();
if (currentTab == 0){
menu.clear();
inflater.inflate(R.menu.first, menu);
}
if (currentTab ==1){
menu.clear();
inflater.inflate(R.menu.second, menu);
}
return super.onPrepareOptionsMenu(menu);
}
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
closeOptionsMenu();
}});
Now, when I switch to the songs tab the photos menu is still there until I click on it then the songs menu appears. I want the menu to get updated once I click on the tab
you are preparing the option menus in the TabActivity. prepare the option menus in the individual activity instead. So that when you are on that activity only the option menu for that will be shown.
This is my code for tabs. Kindly tell me how to put listener on tab1 either through switch statement or by simple onclick listener. And this toast message is also not working
public class TabsActivity extends Activity implements OnClickListener {
TabHost th;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs);
th = (TabHost)findViewById(R.id.tabhost);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("Home");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("Goup");
th.addTab(specs);
specs = th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("Delete");
th.addTab(specs);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.tab1:
Toast.makeText(TabsActivity.this, "Action Item 0 selected!", Toast.LENGTH_LONG).show();
break;
case R.id.tab2:
TextView text= new TextView(TabsActivity.this);
text.setText("u have created a new tab");
break;
case R.id.tab3:
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_tabs, menu);
return true;
}
}
If you want to listener action click on a tab, you should use this:
th.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
Toast.makeText(getApplicationContext(), "Click on tab: "+ tabId, Toast.LENGTH_SHORT).show();
}
});
I have a tabhost and I add there 3 Activities(one activity per tab).
I need to know how to call a new intern in an activity each time i change a tab.
I added a listener for the tabhost.When I use the clearAllTabs(); method and add all the tabs again inside the listener then the app crash.
when I use code toto delete from the view the specific tab that the user clickes tabHost.getTabWidget().removeView(tabHost.getTabWidget().getChildTabViewAt(i));
tabHost.addTab(the tab I want to replace);
then the new tab is positioned in the end of the tabhost.
I just need an example of how to reload the proportionate activity each time the user clickes the specific tab.
my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ActionBar bar = getSupportActionBar();
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Resources res = getResources();
LocalActivityManager mlam = new LocalActivityManager(this, false);
final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
mlam.dispatchCreate(savedInstanceState);
tabHost.setup(mlam);
TabHost.TabSpec spec;
Intent intent;
// TabHost tabHost = getTabHost();
// tabHost.setup();
TabSpec specAll = tabHost.newTabSpec("All");
specAll.setIndicator("All");
Intent allIntent = new Intent(this, allActivity.class);
specAll.setContent(allIntent);
// specAll.setContent(R.id.allList);
Log.d("SpecAll",""+specAll.setContent(allIntent));
TabSpec specIn = tabHost.newTabSpec("in");
specIn.setIndicator("In");
Intent inIntent = new Intent(this, inActivity.class);
specIn.setContent(inIntent);
TabSpec specOut = tabHost.newTabSpec("Out");
specOut.setIndicator("Out");
Intent outIntent = new Intent(this, outActivity.class);
specOut.setContent(outIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(specAll); // Adding all tab
tabHost.addTab(specIn); // Adding in tab
tabHost.addTab(specOut); // Adding out tab
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
int i = tabHost.getCurrentTab();
//Log.i("######## ANN CLICK TAB NUMBER", "------" + i);
if (i == 0) {
Log.d("TAB","" +i);
} else if (i == 1) {
Log.d("TAB","" +i);
}
else
Log.d("TAB", ""+i);
}
});
}
It seems there is a matter with activity and tabhost.in order to reload an activity you just have to do:
specAll.setContent(yourIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
Just before the tabHost.addTab
In my simple case when I do not need to persist tab fragments I use the next code
int currentTabId = mTabHost.getCurrentTab();
mTabHost.clearAllTabs();
setupTabs();
mTabHost.setCurrentTab(currentTabId);
I also face the same problem but i resolve this issue like this way...
This is my TabActivity....
public class MainActivity extends TabActivity {
TabHost tabhost;
String cTab = "0";
String nTab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
tabhost = getTabHost();
TabSpec one = tabhost.newTabSpec("0");
// setting Title and Icon for the Tab
one.setIndicator("", getResources().getDrawable(R.drawable.ic_launcher));
Intent songsIntent = new Intent(this, FirstActivity.class);
one.setContent(songsIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
one.setContent(songsIntent);
TabSpec two = tabhost.newTabSpec("1");
// setting Title and Icon for the Tab
two.setIndicator("", getResources().getDrawable(R.drawable.ic_launcher));
Intent songsIntent1 = new Intent(this, SecondActivity.class);
two.setContent(songsIntent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
two.setContent(songsIntent1);
TabSpec three = tabhost.newTabSpec("2");
// setting Title and Icon for the Tab
three.setIndicator("",
getResources().getDrawable(R.drawable.ic_launcher));
Intent songsIntent4 = new Intent(this, ThirdActivity.class);
three.setContent(songsIntent4.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
three.setContent(songsIntent4);
TabSpec four = tabhost.newTabSpec("3");
// setting Title and Icon for the Tab
four.setIndicator("", getResources()
.getDrawable(R.drawable.ic_launcher));
Intent songsIntent5 = new Intent(this, FourthActivity.class);
four.setContent(songsIntent5.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
four.setContent(songsIntent5);
tabhost.addTab(one);
tabhost.addTab(two);
tabhost.addTab(three);
tabhost.addTab(four);
tabhost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
cTab = "" + tabhost.getCurrentTab();
}
});
int numberOfTabs = tabhost.getTabWidget().getChildCount();
for (int t = 0; t < numberOfTabs; t++) {
tabhost.getTabWidget().getChildAt(t)
.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
String currentSelectedTag = MainActivity.this
.getTabHost().getCurrentTabTag();
nTab = currentSelectedTag;
System.out.println(" nTab " + nTab);
System.out.println(" cTab " + cTab);
if (cTab.equals(nTab)) {
if (nTab.equals("0")) {
Intent intent = new Intent();
intent.setAction("com.ensis.first");
MainActivity.this.sendBroadcast(intent);
}
if (nTab.equals("1")) {
Intent intent = new Intent();
intent.setAction("com.ensis.second");
MainActivity.this.sendBroadcast(intent);
}
if (nTab.equals("2")) {
Intent intent = new Intent();
intent.setAction("com.ensis.third");
MainActivity.this.sendBroadcast(intent);
}
if (nTab.equals("3")) {
Intent intent = new Intent();
intent.setAction("com.ensis.fourth");
MainActivity.this.sendBroadcast(intent);
}
}
}
return false;
}
});
}
}
#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;
}}
This is my FirstActivity.java
public class FirstActivity extends ActivityGroup{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ok();
IntentFilter filter = new IntentFilter("com.ensis.first");
registerReceiver(myReceiver, filter);
/**/
}
private void ok() {
// TODO Auto-generated method stub
setContentView(R.layout.firstscreen);
Button bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it=new Intent(FirstActivity.this,SubActivity.class);
replaceContentView("activity3", it);
}
});
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
this.setContentView(view);
}
private BroadcastReceiver myReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent) {
Log.v("22222222222222222", "22222222222");
ok();
}
};}