I want to set icon on a tab. My codes are right and I am sure.Is it a problem because of the version of android? I looked lots of codes but all of them are the same.The codes are;
package com.mesutemre.androidtabkullanimi;
import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
#SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabhost = getTabHost();
tabhost.setup();
tabhost.addTab(tabhost
.newTabSpec("tabindis1")
.setIndicator("SEKME1",
getResources().getDrawable(R.drawable.document))
.setContent(new Intent(MainActivity.this, Sekme1Activity.class)));
tabhost.addTab(tabhost
.newTabSpec("tabindis2")
.setIndicator("SEKME2",
getResources().getDrawable(R.drawable.music))
.setContent(new Intent(MainActivity.this, Sekme1Activity.class)));
tabhost.addTab(tabhost
.newTabSpec("tabindis3")
.setIndicator("SEKME3",
getResources().getDrawable(R.drawable.delete))
.setContent(new Intent(MainActivity.this, Sekme1Activity.class)));
tabhost.addTab(tabhost
.newTabSpec("tabindis4")
.setIndicator("SEKME4",
getResources().getDrawable(R.drawable.close))
.setContent(new Intent(MainActivity.this, Sekme1Activity.class)));
}
#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;
}
}
I read from some links that this is a problem adroid 4.0 and later. What can i do for setting icon on a tab.
Pls try setView to set TextView instead of setIndicator.
Set icon and indicator text to TextView.
Related
I wanna use the navigation style in my android project. Style is drop down menu so action bar spinner menu. but it isn't like user input one spinner. I wanna use spinner in menu. But i have no idea. i searched something but they didn't work. so any tutorial about that will be very good.
Thanks to much
edit: Drop Down Menu on Action bar in this picture,there is a maps, and it has a map,local,navigation,check in etc. i wanna like this one. i wanna select local (for this picture) show something but select navigation show something else
You're going to want to use an Actionbar spinner dropdown for this.
AndroidHive has an EXCELLENT write up on all things actionbars. I've grabbed the bit that you're looking for, but if you want to read the full article, Click Here.
This is the result:
Here is the code:
MainActivity.java
package info.androidhive.actionbar;
import info.androidhive.actionbar.model.SpinnerNavItem;
import info.androidhive.info.actionbar.adapter.TitleNavigationAdapter;
import java.util.ArrayList;
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;
public class MainActivity extends Activity implements ActionBar.OnNavigationListener{
// action bar
private ActionBar actionBar;
// Title navigation Spinner data
private ArrayList<SpinnerNavItem> navSpinner;
// Navigation adapter
private TitleNavigationAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getActionBar();
// Hide the action bar title
actionBar.setDisplayShowTitleEnabled(false);
// Enabling Spinner dropdown navigation
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// Spinner title navigation data
navSpinner = new ArrayList<SpinnerNavItem>();
navSpinner.add(new SpinnerNavItem("Local", R.drawable.ic_location));
navSpinner.add(new SpinnerNavItem("My Places", R.drawable.ic_my_places));
navSpinner.add(new SpinnerNavItem("Checkins", R.drawable.ic_checkin));
navSpinner.add(new SpinnerNavItem("Latitude", R.drawable.ic_latitude));
// title drop down adapter
adapter = new TitleNavigationAdapter(getApplicationContext(), navSpinner);
// assigning the spinner navigation
actionBar.setListNavigationCallbacks(adapter, this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
....
}
/**
* On selecting action bar icons
* */
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
}
/**
* Actionbar navigation item select listener
* */
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
// Action to be taken after selecting a spinner item
return false;
}
}
I am pulling my hair out trying to get a simple tabhost to work with ABS, it seems Android only complicates things more when attempting to "improve" them. I was considering converting my project to use fragemnts for tabs , but that proved to be wayyyyyyyyy to much work for the complex layouts im using in each of my tabs. Id settle now for just a plain ol working tabhost w. working abs bar.
package com.abs.tabs.fragments;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.buhz.feeds.Buhdz;
import com.buhz.login.R;
import com.buhzhyve.localz.Localz;
import com.buhzhyve.trails.Trails;
import com.buhzhyve.trendz.Trendz;
public class FragmentTabs extends SherlockActivity {
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
actionBar=getSupportActionBar();
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost =(TabHost) findViewById(android.R.id.tabhost); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Do the same for the other tabs
intent = new Intent().setClass(this, Buhdz.class);
spec = tabHost.newTabSpec("Buhdz").setIndicator("Feeds",
res.getDrawable(R.drawable.buhdz_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Localz.class);
spec = tabHost.newTabSpec("Localz").setIndicator("Locals",
res.getDrawable(R.drawable.locals_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Trendz.class);
spec = tabHost.newTabSpec("Trendz").setIndicator("Trends",
res.getDrawable(R.drawable.trends_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Localz.class);
spec = tabHost.newTabSpec("convos").setIndicator("Convos",
res.getDrawable(R.drawable.convos_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Trails.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Trails").setIndicator("Trails",
res.getDrawable(R.drawable.trails_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(4);
}
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
return true;
case R.id.subItem1:
Toast.makeText(this, "Sub Menu item 1 tapped", Toast.LENGTH_SHORT).show();
return true;
case R.id.subItem2:
Toast.makeText(this, "Sub Menu item 2 tapped", Toast.LENGTH_SHORT).show();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
Use this library: https://github.com/JakeWharton/Android-ViewPagerIndicator, you can find inside this library some samples with tabs like you want and works with sherlockactionbar.
Visit this question: Actionbarsherlock + tabs + multi fragments?.
Ask me if you got in trouble.
I have come across another prickly little problem.
I am writing an app with tabs, but I have a textbox (EditText) at the top of the screen that I want to be able to receive text data from any of the tabs. As each of the tabs has it's own activity and layout, this is proving difficult to achieve.
I want to be able to use:
editText1.setText("Hello World");//sample text
from any Tab/Activity.
Does anyone know how to make a textbox from one layout public and able to recieve text?
I am using TabActivity, yes I know it's deprecated but as this is my first app with tabs, it's easier to learn than Fragments. I will try them next time, unless they are the answer to my problem, in which case I have a lot of re-coding to do!!
ok, new part.
package com.epsilonitsystems.epecsandroid;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
import android.view.Menu;
import android.speech.tts.TextToSpeech;
public class MainActivity extends TabActivity {
public EditText editText1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
TabHost tabHost = getTabHost();
// Tab for Core
TabSpec corespec = tabHost.newTabSpec("Core");
corespec.setIndicator("", getResources().getDrawable(R.drawable.ic_i));
Intent coreIntent = new Intent(this, CoreActivity.class);
corespec.setContent(coreIntent);
// Tab for Drink
TabSpec drinkspec = tabHost.newTabSpec("Drink");
drinkspec.setIndicator("", getResources().getDrawable(R.drawable.ic_drink));
Intent drinkIntent = new Intent(this, DrinkActivity.class);
drinkspec.setContent(drinkIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(corespec); // Adding Core tab
tabHost.addTab(drinkspec); // Adding Drink tab
}
#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_main, menu);
return true;
}
}
That's the Main Activity, I'll just show the Core Activity as they will all be the same when I get it working.
package com.epsilonitsystems.epecsandroid;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.speech.tts.TextToSpeech;
public class CoreActivity extends Activity {
private TextToSpeech mTts;
// This code can be any value you want, its just a checksum.
private static final int MY_DATA_CHECK_CODE = 1234;
EditText editText1;
String Spch,Str;
ImageButton imageButton1,imageButton2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.core_layout);
//button1 = (Button) findViewById(R.id.button1);
editText1 = (EditText) findViewById(R.id.editText1);
mTts = new TextToSpeech(this, null);
}
public void onimageButton1Click(View view) {
//mTts.speak("I",TextToSpeech.QUEUE_FLUSH,null);
//Spch=editText1.toString();
//Spch=Spch+"I ";
editText1.setText("Hello World");
}
}//Activity End
I can't post a screenshot as I'm still a new user, sorry.
Any ideas please?
Gary,
What you are looking for probably is how to share data between different Activities.
You can do this in a couple of ways, but the cleanest is using the Intent you start the new Activity with.
With this you can set extra data via the Intent.putXXXXX methods.
You could use something as Intent.putExtra("my_private_key", mTextVar); and fetch that out in your other Activity with this.getIntent().getStringExtra("my_private_key");
ps. As tempting as it might seem to start with the TabActivity, you're actually making it a lot more difficult for yourself by making a bad start and learning classes which you should not use anymore. I'd advise you to pick up some good fragments tutorials which will be just as easy once correctly explained. You should take a look at per example http://www.vogella.com/android.html
I have a weird requirment to accomplish. Let say I have a tabHost with 3 tabs. And let say the second tab is curently displayed. What I need to do is - when clicking on the third tab to display a context menu. The context menu needs to be displayed in the activity from tab 2.
IMPORTANT So, when I click on the third tab I must not go to another activity, I must stay at the current activity(from tab 2) and display a context menu also.
Hope I made myself clear. Thank you!
simple demo example as given at below check it make some changes according to ur requirement
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
public class Tab_exActivity extends TabActivity {
/** 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
// this.myhost = (TabHost)this.findViewById(R.);
// this.myhost.setup();
final 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, tabactivity1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Home",
res.getDrawable(R.drawable.act1))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, tabactivity2.class);
spec = tabHost.newTabSpec("albums").setIndicator("Refresh",
res.getDrawable(R.drawable.act2))
.setContent(intent);
tabHost.addTab(spec);
// intent = new Intent().setClass(this, tabactivity3.class);
spec = tabHost.newTabSpec("songs").setIndicator("Search",
res.getDrawable(R.drawable.act3))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabId) {
if (tabId.equals("songs")){
System.out.println("44444");
View v = tabHost.getCurrentView();
registerForContextMenu(v);
v.showContextMenu();
}
}});
// openContextMenu(spec);
// tabHost.setCurrentTab(2);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Action 1");
menu.add(0, v.getId(), 0, "Action 2");
}
}
I have a CUSTOM tab host the uses the addTab() method instead of tabspec. one of my tabs in a MapViewthat shows a specific location. The problem I am encountering is an exception on the line in my tabhost that sets up the mapview tab.
Tab Activity (removed package for confidential reasons):
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
public class CustomTabHost extends TabActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);
setTabs() ;
}
private void setTabs()
{
addTab("Services", R.drawable.servicesicon, services.class);
addTab("Our Work", R.drawable.ourworkicon, ourwork.class);
addTab("RSS", R.drawable.rssicon, RSSReader.class);
addTab("Locate IML", R.drawable.locateicon, Locate.class);
addTab("Contact IML", R.drawable.contacticon, ContactUs.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tabs_bg, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title1);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon1);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
MapActivity:
import java.util.List;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class Locate extends MapActivity {
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.locateiml);
MapView mapview = (MapView) findViewById(R.id.locatemap);
mapview.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapview.getOverlays();
Drawable drawable =this.getResources().getDrawable(R.drawable.locateicon);
Locateoverlay itemoverlay = new Locateoverlay(drawable, this);
GeoPoint point = new GeoPoint(382571066,-85751392);
OverlayItem overlayitem = new OverlayItem(point, "Interactive", null);
itemoverlay.addOverlay(overlayitem);
mapOverlays.add(itemoverlay);
}
protected boolean isRouteDisplayed()
{
return false;
}
}
There is also an overlay, but that is not needed for this question. My question is, I am pretty sure I need to create a parent activity the handles the map activity, but I do not know how to do this.
I know that the Activity in TabHost is not considered as Activity. I think it is managed as View.
In my case, main Activity was inherited Activity and TabHost was implemented through xml configuration. Then I added MapActivity -as a TabSpec- to TabHost, but MapView shows gray background only.So, I make main Activity inherit MapActivity, and it worked.
I don't know whether this is best solution, whether your main Activity must inherit TabActivity, but if there is no obvious reason, I think it is better to inherit MapActivity rather then TabActivity.Maybe my answer is not the best, I wish I could help you in some way.Good luck!
TabActivity caused me a lot of issues in the past. This might be another issue related to it.
Right now TabActivity is deprecated. The doc says:
New applications should use Fragments instead of this class; to
continue to run on older devices, you can use the v4 support library
which provides a version of the Fragment API that is compatible down
to DONUT.
To use a MapView inside a Fragment you will need to use: petedoyle's android-support-v4-googlemaps.