Here is my activity in which i set fragment tabs:
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_tabs);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
Bundle b = new Bundle();
b.putString("key", "Simple");
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),this.getResources().getDrawable(R.drawable.iconxml)
Fragment1.class, b);
iconxml.xml file
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/ic_launcher" />
<item android:drawable="#drawable/icontab" />
my app crashes with this.. plz help
![exceptions][logcat outout]
Try to change this:
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple",getResources().getDrawable(R.drawable.iconxml)
Fragment1.class, b);
Replace this to
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),this.getResources().getDrawable(R.drawable.iconxml) Fragment1.class, b);
this
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple",this.getResources().getDrawable(R.drawable.iconxml)) Fragment1.class, b);
See there TabHost.TabSpec
http://wptrafficanalyzer.in/blog/creating-navigation-tabs-using-tabhost-and-fragments-in-android/
i tried modyfing above example and finally solved my problem :)
Related
I tried to combine leanback with xml, but I had some problem - padding the leanback row is different from padding other elements. How can I change padding to RowsSupportFragment?
In xml
<RelativeLayout
android:id="#+id/tags_container"
android:layout_below="#+id/guidance_icon"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="50dp"/>
And in java code
RelativeLayout tags = view.findViewById(R.id.tags_container);
ScreenshotFragment = FragmentScreenshots.newInstance(mFilm);
getChildFragmentManager()
.beginTransaction()
.replace(tags.getId(), ScreenshotFragment)
.commit();
class FragmentScreenshots
public static class FragmentScreenshots extends RowsSupportFragment {
private Film mFilm = null;
public static FragmentScreenshots newInstance(Film film) {
FragmentScreenshots fragment = new FragmentScreenshots();
Bundle args = new Bundle();
args.putSerializable(ActivityFilm.PARAM_FILM, film);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListRowPresenter presenter = new CompatibleListRowPresenter(false, false, true);
ArrayObjectAdapter mRowsAdapter = new ArrayObjectAdapter(presenter);
List<AbstractCard> list = new ArrayList<>();
setAdapter(mRowsAdapter);
...
}
}
}
I found where I can edit the padding on RowsSupportFragment.
In your activity theme add the item
<item name="rowHorizontalGridStyle">#style/CustomHorizontalGridView</item>
And in CustomHorizontalGridView u can edit padding
<style name="CustomHorizontalGridView">
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:paddingStart">#dimen/your_padding</item>
<item name="android:paddingEnd">#dimen/your_padding</item>
<item name="android:paddingBottom">#dimen/lb_browse_item_vertical_spacing</item>
<item name="android:paddingTop">#dimen/lb_browse_item_vertical_spacing</item>
<item name="android:horizontalSpacing">#dimen/lb_browse_item_horizontal_spacing</item>
<item name="android:verticalSpacing">#dimen/lb_browse_item_vertical_spacing</item>
<item name="focusOutFront">true</item>
</style>
I am trying to add a custom divider between my tabs. but it doesn't work. here is my java code:
String tabschedule = getString(R.string.myschedule);
mTabHost = getTabHost();
intent = new Intent(this, Schedule.class);
spec = mTabHost
.newTabSpec("Schedule")
.setIndicator(tabschedule,
this.getResources().getDrawable(R.drawable.schedule))
.setContent(intent);
mTabHost.addTab(spec);
String filteration = getString(R.string.filteration);
intent = new Intent(this, Filteration.class);
spec = mTabHost
.newTabSpec("Filteration")
.setIndicator(filteration,
this.getResources().getDrawable(R.drawable.filteration))
.setContent(intent);
mTabHost.addTab(spec);
String history = getString(R.string.history);
intent = new Intent(this, MyHistory.class);
spec = mTabHost
.newTabSpec("History")
.setIndicator(history,
this.getResources().getDrawable(R.drawable.historylay))
.setContent(intent);
mTabHost.addTab(spec);
and here is my xml code:
<style name="tabtheme" parent="#android:style/Theme.Light.NoTitleBar">
<item name="android:actionBarTabBarStyle">#style/customTabBar</item>
</style>
<style name="customTabBar" parent="#style/Widget.AppCompat.ActionBar.TabBar">
<item name="android:showDividers">middle</item>
<!-- give your divider here -->
<item name="android:actionBarDivider">#drawable/tabdivider</item>
<item name="android:dividerPadding">0dp</item>
</style>
This is my screen shot
Any suggestion will be appreciated, thanks in advance.
I want to hide the divider between preferences in fragment.
The code is below:
1.SettingsActivity.java
public class SettingsActivity extends PreferenceActivity {
super.onCreate(savedInstanceState);
SettingsFragment settingsFragement = new SettingsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(android.R.id.content, settingsFragement, "settings");
transaction.commitAllowingStateLoss();
}
2.SettingsFragment.java
public class SettingsFragment extends PreferenceFragment{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
3.settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory>
<Preference
android:key="preference_a"
android:title="preferenceA"/>
<Preference
android:key="preference_b"
android:title="preferenceB"/>
<ListPreference
android:key="list_preference_c"
android:title="list_preferenceC"/>
</PreferenceCategory>
</PreferenceScreen>
There are system default dividers amoung the preferences.
I want to hide the dividers.
How to hide the dividers? Thanks a lot.
Thanks for everyone`s answer. But,the question is that I cannot get the listview from the activity and fragment.
ListView list = (ListView) findViewById(android.R.id.list);
if you are using PreferenceFragmentCompat , it is using Recycle view so you have to use this code . This automatically hides the divider
#Nullable
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
setDivider(new ColorDrawable(Color.TRANSPARENT));
setDividerHeight(0);
}
These solutions didn't work for me. Here's what I did in PreferenceFragmentCompat:
#Override
public void setDivider(Drawable divider) {
super.setDivider(new ColorDrawable(Color.TRANSPARENT));
}
#Override
public void setDividerHeight(int height) {
super.setDividerHeight(0);
}
Define custom preference theme without dividers in your style.xml:
<style name="PrefsTheme" parent="PreferenceThemeOverlay.v14.Material">
<item name="android:divider">#null</item>
<item name="android:dividerHeight">0dp</item>
</style>
and don't forget to use it in your main app theme which is also in style.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- ... -->
<item name="preferenceTheme">#style/PrefsTheme</item>
</style>
You can remove your divider by style.
<style name="PreferenceFragment.Material">
<item name="android:divider">#null</item>
</style>
First: if used the listview in xml
set the android:dividerHeight="0dp" in listview in xml file ,
second:
ListView list = (ListView) findViewById(android.R.id.list);
list.setDivider(new ColorDrawable(Color.TRANSPARENT)); // or some other color int
list.setDividerHeight((int) getResources().getDisplayMetrics().density);
I'm setting a image hover in my Tab Host using xml file. But its not working for Me. Please take a look at my codes below to see if I forgotten something or if there is something wrong in my codes.
Main Activity Class
public class MainActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Photos");
// setting Title and Icon for the Tab
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Songs");
songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
Intent songsIntent = new Intent(this, SongsActivity.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Videos");
videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));
Intent videosIntent = new Intent(this, VideosActivity.class);
videospec.setContent(videosIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
}
}
My XML file for Hover
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- When selected, use grey -->
<item android:drawable="#drawable/photohover"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/photo" />
</selector>
i checked your code it is working fine. I don't know what's your problem. Please check this link. It will helps you to understand the Tabwidget concept. http://www.rdcworld-android.blogspot.in/2011/11/tabwidget-in-android-advance.html
Let me know still you are facing a problem with tabwiget, with clear information.
Problem Solved. I just change getResources() to context.getResources() and it works fine.
try this :-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- selected state -->
<item android:drawable="#drawable/normal_pressed"
android:state_selected="true"
android:state_pressed="false" />
<!-- unselected state (default) -->
<item android:drawable="#drawable/normal" />
</selector>
I saw this question here. The solution was to make a xml for a icon and then getResources().getDrawable(R.drawable.tabicon);
on in. So this is in my java:
TabHost tabhost = (TabHost)findViewById(R.id.tabhost);
tabhost.setup();
TabSpec tabspecs1 = th.newTabSpec("example");
tabspecs1.setContent(R.id.tab1);
tabspecs1.setIndicator("Example");
getResources().getDrawable(R.drawable.tabicon);
th.addTab(tabspecs1);
listView = getListView();
My tabicon.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use icon1 -->
<item android:drawable="#drawable/ic_action_line_chart"
android:state_selected="true" />
<!-- When not selected, use icon2-->
<item android:drawable="#drawable/ic_action_calculator" />
</selector>
line chart and calculator are in drawable-folders.
Anything I missed? Tabs are working fine, but no icons...?
Okay I did it like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setTitle("MyApp");
listView = getListView();
TabHost th = (TabHost)findViewById(R.id.tabhost);
th.setup();
TabSpec tabspecs1 = th.newTabSpec("tag01");
tabspecs1.setContent(R.id.tab1);
tabspecs1.setIndicator ("exlample"),getResources().getDrawable(R.drawable.tabicon));
th.addTab(tabspecs1);
TabSpec tabspecs2 = th.newTabSpec("tag02");
tabspecs2.setContent(R.id.tab2);
tabspecs2.setIndicator ("lululu", getResources().getDrawable(R.drawable.tabicon));
th.addTab(tabspecs2);
}
I used tabicon.xml for both, just 4 testing.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use icon1 -->
<item android:drawable="#drawable/ic_icon1"
android:state_selected="true" />
<!-- When not selected, use icon2-->
<item android:drawable="#drawable/ic_icon2" />
</selector>
You're getting the drawable, but not setting it. Try this method:
http://developer.android.com/reference/android/widget/TabHost.TabSpec.html#setIndicator(java.lang.CharSequence, android.graphics.drawable.Drawable)
setIndicator ("Example", getResources().getDrawable(R.drawable.tabicon))
This
getResources().getDrawable(R.drawable.tabicon);
doesn't do anything. You're not setting the retrieved Drawable anywhere.