I want to create a custom tabhost, which contains an icon also. For the purpose I need to use a method. This method works fine, but the tab indicator is disappeared from the tab.
The code for the tab indicator is:
private View getTabIndicator(Context context, String title, int icon) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
ImageView iv = (ImageView) view.findViewById(R.id.imageView);
iv.setImageResource(icon);
TextView tv = (TextView) view.findViewById(R.id.textView);
tv.setText(title);
return view;
}
Main Activity is :
public class MainActivity extends AppCompatActivity {
private FragmentTabHost mTabHost;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setting up a toolbar for the navigation purpose.
toolbar = (Toolbar)findViewById(R.id.app_bar);
toolbar.setTitle(" Call History Control");
toolbar.setLogo(R.mipmap.ic_launcher);
setSupportActionBar(toolbar);
// The fragments management is done here
// mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
// mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//
// mTabHost.addTab(
// mTabHost.newTabSpec("home").setIndicator(" HOME"),
// Home.class, null);
// mTabHost.addTab(mTabHost.newTabSpec("settings").setIndicator(" SETTINGS"),
// Settings.class, null);
//
// mTabHost.addTab(mTabHost.newTabSpec("about").setIndicator(" ABOUT"),
// About.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("home").setIndicator(getTabIndicator(getApplicationContext(),"HOME",R.drawable.ic_home)),
Home.class, null);
mTabHost.addTab(mTabHost.newTabSpec("settings").setIndicator(getTabIndicator(getApplicationContext(),"SETTINGS",R.drawable.ic_settings)),
Settings.class, null);
mTabHost.addTab(mTabHost.newTabSpec("about").setIndicator(getTabIndicator(getApplicationContext(),"ABOUT",R.drawable.ic_account)),
About.class, null);
}
The screeen shot is:
Related
I have one fragment in which I am setting tabhost widget with three tabs each have fragment.
When ever first time I come to this fragment first tab not loading first child fragment but after click on another tab and return on first tab then it is loading first child fragment.
Also I have little confusion that what will be the third parameter in tabhost.setup() method.It should be the layout of parent fragment which handles tabs or the layout of first child fragment.
Here is my code for parent fragment
public class MyStoreFragment extends Fragment implements TabHost.OnTabChangeListener {
private FragmentTabHost mTabHost;
View rootView;
public MyStoreFragment() {
// Empty constructor required for fragment subclasses
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// rootView = inflater.inflate(R.layout.fragment_my_store,container, false);
// mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost = new FragmentTabHost(getActivity());
// mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
mTabHost.setup(getActivity(), getChildFragmentManager(),R.layout.fragment_my_store);
mTabHost.addTab(mTabHost.newTabSpec("new").setIndicator("NEW"), NewBookFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("trending").setIndicator("TRENDING"),FragmentTab.class, null);
mTabHost.addTab(mTabHost.newTabSpec("explore").setIndicator("EXPLORE"),FragmentTab.class, null);
mTabHost.setCurrentTab(0);
mTabHost.setOnTabChangedListener(this);
mTabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.WHITE);
mTabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#FCF4E7"));
mTabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#FCF4E7"));
mTabHost.getTabWidget().getChildAt(0)
.setBackgroundColor(Color.parseColor("#DF4426"));
TextView tv1 = (TextView) mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
tv1.setTextColor(Color.parseColor("#F6CFC2"));
TextView tv2 = (TextView) mTabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.title);
tv2.setTextColor(Color.parseColor("#E03C20"));
TextView tv3 = (TextView) mTabHost.getTabWidget().getChildAt(2).findViewById(android.R.id.title);
tv3.setTextColor(Color.parseColor("#E03C20"));
// TODO To set height and width properly of tab widget
ViewGroup.LayoutParams params = mTabHost.getTabWidget().getChildAt(0).getLayoutParams();
// params.width = 128;
params.height = 50;
mTabHost.getTabWidget().getChildAt(0).setLayoutParams(params );
mTabHost.getTabWidget().getChildAt(1).setLayoutParams(params );
mTabHost.getTabWidget().getChildAt(2).setLayoutParams(params );
// for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
// TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
// tv.setTextColor(Color.parseColor("#DA4426"));
// }
return mTabHost;
}
I have Activity that contains custom ActionBar and ViewPager with three tabs.
I am not able to read text from EditText inside tab when click on button in ActionBar.
EDITED:
Contains TABS...
public class TranAdd extends FragmentActivity...{
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
EditText et;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
et = (EditText) mViewPager.findViewById(R.id.editText);
//...reference null...
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
TextView textViewTitle = (TextView) mCustomView.findViewById(R.id.textViewTitle);
textViewTitle.setText("New Transaction");
ImageView imageViewOk = (ImageView) mCustomView.findViewById(R.id.imageViewOk);
imageViewOk.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String txt = et.getText().toString();
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
FRAGMENT:
public class Fragment extends Fragment...{
...
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
}
Try calling setHasOptionsMenu(true) inside your onCreate() method of the Fragment (tab) which needs to interact with the ActionBar.
In my Android application, I am using FragmentTabHost. I have added 3 tabs in the tabhost now my problem is I could not change the tab icon dynamically when choose each tab.
My requirement is if I select a tab other tabs icon should be changed, I got null pointer exception when try to change the tab icon. This is the line I got null pointer exception. the following code under onTabChanged method
ImageView v1 = (ImageView) mTabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.icon);
v1 is return null (checked by if condition)
public class MainActivity extends FragmentActivity implements
OnTabChangeListener {
private static final String TAB_1_TAG = "FindTab";
private static final String TAB_2_TAG = "AddPopUpTab";
private static final String TAB_3_TAG = "MyAccoutTab";
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InitView();
}
private void InitView() {
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(
setIndicator1(MainActivity.this,
mTabHost.newTabSpec(TAB_1_TAG), R.drawable.tab1),
Tab1Container.class, null);
mTabHost.addTab(
setIndicator1(MainActivity.this,
mTabHost.newTabSpec(TAB_2_TAG), R.drawable.tab2),
Tab2Container.class, null);
mTabHost.addTab(
setIndicator1(MainActivity.this,
mTabHost.newTabSpec(TAB_3_TAG), R.drawable.tab3),
Tab3Container.class, null);
mTabHost.getTabWidget().setDividerDrawable(null);
mTabHost.setOnTabChangedListener(MainActivity.this);
}
private TabSpec setIndicator1(Context ctx, TabSpec spec, int genresIcon) {
View v = LayoutInflater.from(ctx).inflate(R.layout.tab_item, null);
ImageView img = (ImageView) v.findViewById(R.id.img_tabtxt);
img.setBackgroundResource(genresIcon);
return spec.setIndicator(v);
}
#Override
public void onTabChanged(String arg0) {
Log.e("tab change string", arg0);
ImageView v1 = (ImageView) mTabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.icon);
if (v1 == null) {
Log.e("v1", "null"); // Line
} else {
Log.e("v1", " Not null");
}
if (getResources().getDrawable(R.drawable.tab3) == null) {
Log.e("resource", "null");
} else {
Log.e("resource", "not null");
}
v1.setImageDrawable(getResources().getDrawable(R.drawable.tab3));
ImageView v2 = (ImageView) mTabHost.getTabWidget().getChildAt(1)
.findViewById(android.R.id.icon);
v2.setImageDrawable(getResources().getDrawable(R.drawable.tab1));
ImageView v3 = (ImageView) mTabHost.getTabWidget().getChildAt(2)
.findViewById(android.R.id.icon);
v3.setImageDrawable(getResources().getDrawable(R.drawable.tab2));
}
}
Layout for tab item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_tabtxt"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I think you need to you Actionbar is good.
If you want using Action bar then.
You need to try this
Change Tab icon in Action bar
I really need to access the images of a tab at runtime for scaling.
Therefore I made a custom tab layout with an imageview and a textview.
But if i want to add the custom tab to my tabhost I a "did you forget to call 'public void setup(localactivitymanager activitygroup)'" exception.
Thx in advance for any solutions ;D
Ps. I cannot use
spec = tabHost.newTabSpec("2").setIndicator(res.getString(R.string.tabname2),
res.getDrawable(R.drawable.tabimage2));
tabHost.addTab(spec, FragmentTwo.class, null);
because i need to scale the image before adding.
Here is my class:
public class MyTabActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabmanager_layout);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
{
mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#3b6c8d")); //unselected
}
}
private void addCustomTab(Context context,String labelId, Drawable drawable, Class<?> c, FragmentTabHost fth ) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_customtab, null);
ImageView image = (ImageView) view.findViewById(R.id.icon);
TextView text = (TextView) view.findViewById(R.id.tabtitle);
image.setImageDrawable(drawable);
text.setText(labelId);
TabHost.TabSpec spec = fth.newTabSpec(labelId);
Intent i = new Intent(this,c);
spec.setContent(i);
spec.setIndicator(view);
fth.addTab(spec);
}
You need to use FragmentTabHost's custom addTab() which will also take your Fragment class as parameter.
Replace
TabHost.TabSpec spec = fth.newTabSpec(labelId);
Intent i = new Intent(this,c);
spec.setContent(i);
spec.setIndicator(view);
with
TabHost.TabSpec spec = fth.newTabSpec(labelId);
spec.setIndicator(view);
fth.addTab(spec, c, null);
Is it possible to update the TabWidget icons/indicators? If so, how, when you are creating TabContent through intents?
Edits below with answer thanks to #Bart
Generic code:
MainActivity:
public class TestActivity extends TabActivity {
public int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab1").setContent(
new Intent(this, OneActivity.class)).setIndicator("One"));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setContent(
new Intent(this, TwoActivity.class)).setIndicator("Two"));
changeTitle(0, 20);
}
public void changeTitle(int cnt, int i){
View v = getTabWidget().getChildAt(cnt);
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setText("One ("+i+")");
}
}
OneActivity (ChildActivity):
public class OneActivity extends Activity {
TextView tv;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
tv = (TextView) findViewById(R.id.tv);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int i = Integer.parseInt((String) tv.getText());
i++;
tv.setText(""+i);
TestActivity parent = OneActivity.this.getParent();
parent.changeTitle(0,i);
}
});
}
}
I'd like to show the number in the Tab's title
It is possible but it is a bit of tricky:
ViewGroup vg = (ViewGroup) getTabHost().getTabWidget().getChildAt(0);
TextView tv = (TextView) vg.getChildAt(1);
You should pay attention to indices while calling getChildAt(). The best way is always to debug and check.
Since 1.6 TabWidget has a method getChildTabViewAt thanks to which you could skip the first line and write:
ViewGroup vg = (ViewGroup) getTabHost().getTabWidget().getChildTabViewAt(0);
I attach also source code of Android SDK to show how TabWidget icons are built:
View tabIndicator = inflater.inflate(R.layout.tab_indicator,
mTabWidget, // tab widget is the parent
false); // no inflate params
final TextView tv = (TextView) tabIndicator.findViewById(R.id.title);
tv.setText(mLabel);
final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.icon);
iconView.setImageDrawable(mIcon);
As you can see, it contains a TextView and ImageView.
Try get the imageView like this
ImageView v = (ImageView)tabActivity.getTabHost().getTabWidget().getChildTabViewAt(i).findViewById(android.R.id.icon);
v.setImageDrawable(getResources().getDrawable(R.drawable.newIcon));
Given a arrays for titles and icons
for (int i = 0; i < numTabs; i++ ) {
TabSpec spec = host.newTabSpec(String.valueOf(i)); //sets tab index
Drawable icon = res.getDrawable(icons[i]); //sets the icon
// set tab text and icon
spec.setIndicator(titles[i], icon); //sets the title
host.addTab(spec);
}