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>
Related
I am trying have my android app work on multiple versions and have ran into a jam with API 19. I have a toolbar that is not clickable. The toolbar shows but clicking is not firing. The app works fine for API 21 and 23. I also notice that the view pager is also not clickable but the swipe works. Not sure what I am doing wrong...
Toolbar:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbarGeneral"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/AppTheme.AppBarOverlay"
app:popupTheme="#style/AppTheme.PopupOverlay" />
Style:
<resources>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBarOverlay">false</item>
</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBarOverlay">true</item>
</style>
</resources>
MainActivity:
public class MasterActivity : AppCompatActivity, View.IOnClickListener//, Android.Support.V4.App.FragmentActivity
{
//private XmppClient _xmppClient;
private ViewPager _mViewPager;
//private bool _shouldImportContact;
//private bool _shouldInitialiseSystem;
private AppCompatActivity _context;
private MasterPageChangeListener _pageChangeListener;
private IMenuItem _editMenu;
private SQLiteConnection _dbConnection;
private int _fragmentPos;
private Android.Support.V4.App.Fragment[] _masterFragments;
private CRUDRepository<FacebookInfo> _facebookCRUD;
private FacebookInfo _facebookInfo;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_facebookInfo = new FacebookInfo();
_facebookCRUD = new CRUDRepository<FacebookInfo>();
_fragmentPos = Intent.GetIntExtra("FragmentPos", 0);
_dbConnection = SQLiteHelper.GetConnection();
try
{
_context = this;
SetContentView(Resource.Layout.Master); //Set view the Master view
Toolbar.Toolbar toolbar = FindViewById<Toolbar.Toolbar>(Resource.Id.toolbar);
this.SetSupportActionBar(toolbar);
_mViewPager = FindViewById<ViewPager>(Resource.Id.viewPager);
_mViewPager.Adapter = new MasterAdapter(base.SupportFragmentManager, _masterFragments, titleLabels);
_mViewPager.SetCurrentItem(_fragmentPos, true);
TabLayout tabLayout = FindViewById<TabLayout>(Resource.Id.tabs);
tabLayout.SetupWithViewPager(_mViewPager);
FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
fab.SetOnClickListener(this);
//Create the pageChangeListener class so that the page selected is known
_pageChangeListener = new MasterPageChangeListener(this, _mViewPager);
_mViewPager.AddOnPageChangeListener(_pageChangeListener);
}
catch (Exception Ex)
{
var data = Ex.Message;
}
}
OnPrepareOptionsMenu and OnCreateOptionsMenu:
public override bool OnPrepareOptionsMenu(IMenu menu)
{
if (_pageChangeListener.tabPosition == 0)
_editMenu.SetVisible(true);
else if (_pageChangeListener.tabPosition == 1 || _pageChangeListener.tabPosition == 2)
_editMenu.SetVisible(false);
return base.OnPrepareOptionsMenu(menu);
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
base.OnCreateOptionsMenu(menu);
var inflater = MenuInflater;
inflater.Inflate(Resource.Menu.menu, menu);
_editMenu = menu.FindItem(Resource.Id.action_add);
return true;
}
The issue directly relates to this:
1) You need to define app: correctly to xmlns:app="http://schemas.android.com/apk/res-auto" instead of xmlns:app="http://schemas.android.com/apk/lib-auto"
2) You need to change the word app:layout_behaviour to app:layout_behavior
Feel free to edit your original question with the respective code so this answer makes more sense! (As more source code was revealed in a PM)
I have a tab based application which has only one activity. I would like to apply the android material transition effects when moving from one fragment to other. I have followed this samples. Here is what I am doing,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_contact, container, false);
ButterKnife.bind(this,view);
setupWindowAnimations();
return view;
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setupWindowAnimations() {
Fade fade = new Fade();
fade.setDuration(1000);
getActivity().getWindow().setEnterTransition(fade);
Slide slide = new Slide();
slide.setDuration(1000);
getActivity().getWindow().setReturnTransition(slide);
}
But this has no effect when I enter and exit the fragment which has this code added.
In my styles(V21).xml I have this added
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowActivityTransitions">true</item>
<item name="android:windowContentTransitions">true</item>
</style>
How to make this work?
I have two different styles of TabLayout in my app:
<style name="TabLayoutPower" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextColor">#android:color/white</item>
</style>
<style name="TabLayoutFree" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/black</item>
<item name="tabTextColor">#android:color/black</item>
</style>
How can I define the default TabLayout style for a theme? I cannot find any info which item name should I use to build my theme. I'd like to add the TabLayout the same way I added my listview:
<style name="Free" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/black</item>
<item name="colorPrimaryDark">#color/black</item>
<item name="colorAccent">#color/main_red</item>
<item name="android:windowBackground">#color/main_bg_light</item>
<item name="android:listViewStyle">#style/MyListView</item>
</style>
For Support Library TabLayout, you can set tabStyle attribute in your theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- ... -->
<item name="tabStyle">#style/AppTheme.TabLayout</item>
</style>
<style name="AppTheme.TabLayout" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextColor">#android:color/white</item>
</style>
If you wan to use 2 different styles for your TabLayout based on theme of your application then you should define your style inside attrs.xml
here is the Sample code for that
First you create an attrs.xml file ( Like this )
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="textColor" format="reference|color" />
<attr name="backColor" format="reference|color" />
<attr name="myTabStyle" format="reference" />
</resources>
define various theme as per your requirements inside styles.xml
Note: i have used 3 different themes for demo
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="tabStyle">#style/TabLayoutPower</item>
<item name="textColor">#android:color/white</item>
<item name="backColor">#color/colorPrimary</item>
</style>
<style name="AppTheme2" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#android:color/holo_green_dark</item>
<item name="colorPrimaryDark">#android:color/white</item>
<item name="colorAccent">#android:color/holo_blue_dark</item>
<item name="textColor">#android:color/white</item>
<item name="tabStyle">#style/TabLayoutFree</item>
<item name="backColor">#FF00</item>
</style>
<style name="AppTheme3" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#ff00</item>
<item name="colorPrimaryDark">#ff0</item>
<item name="colorAccent">#0a91d4</item>
<item name="tabStyle">#style/TabLayoutNew</item>
<item name="textColor">#FF00</item>
<item name="backColor">#android:color/white</item>
</style>
<style name="TabLayoutPower" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextColor">#android:color/white</item>
</style>
<style name="TabLayoutFree" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/holo_blue_bright</item>
<item name="tabTextColor">#android:color/holo_blue_bright</item>
</style>
<style name="TabLayoutNew" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/holo_green_dark</item>
<item name="tabTextColor">#android:color/holo_green_dark</item>
</style>
</resources>
Now use this style in your TabLayout like this inside layout.xml files
Use like this style="?myTabStyle" it will select style based on current theme of your application
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btnThemeOne"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Theme One" />
<Button
android:id="#+id/btnThemeTwo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Theme Two" />
<Button
android:id="#+id/btnThemeThree"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Theme Three" />
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
style="?myTabStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
android:background="#color/colorAccent"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
sample code of TabsActivity to change theme
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class TabsActivity extends AppCompatActivity {
PrefManager prefManager;
private TabLayout tabLayout;
private ViewPager viewPager;
private Button btnThemeOne, btnThemeTwo, btnThemeThree;
#Override
protected void onCreate(Bundle savedInstanceState) {
prefManager = new PrefManager(this);
getTheme().applyStyle(prefManager.getTheme(), true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs);
btnThemeOne = findViewById(R.id.btnThemeOne);
btnThemeOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(TabsActivity.this, "Applying theme one", Toast.LENGTH_SHORT).show();
prefManager.setTheme(R.style.AppTheme);
recreate();
}
});
btnThemeTwo = findViewById(R.id.btnThemeTwo);
btnThemeTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(TabsActivity.this, "Applying theme two", Toast.LENGTH_SHORT).show();
prefManager.setTheme(R.style.AppTheme2);
recreate();
}
});
btnThemeThree = findViewById(R.id.btnThemeThree);
btnThemeThree.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(TabsActivity.this, "Applying theme three", Toast.LENGTH_SHORT).show();
prefManager.setTheme(R.style.AppTheme3);
recreate();
}
});
viewPager = findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new OneFragment(), "TWO");
adapter.addFragment(new OneFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
PrefManager class to save theme info
import android.content.Context;
import android.content.SharedPreferences;
public class PrefManager {
// Shared preferences file name
private final String PREF_NAME = "theme-pref";
private final String THEME = "theme";
SharedPreferences pref;
SharedPreferences.Editor editor;
public PrefManager(Context context) {
pref = context.getSharedPreferences(PREF_NAME, 0);
editor = pref.edit();
}
public int getTheme() {
return pref.getInt(THEME, R.style.AppTheme);
}
public void setTheme(int themeId) {
editor.putInt(THEME, themeId);
editor.commit();
}
}
you can check the output video of above code
https://www.youtube.com/watch?v=uup072IDGd0
You can find that name in Android theme -
<item name="android:tabWidgetStyle">#style/Your.Style</item>
<style name="Base.Widget.Design.TabLayout" parent="">
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextColor">#android:color/white</item>
</style>
<style name="Free" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/black</item>
<item name="colorPrimaryDark">#color/black</item>
<item name="colorAccent">#color/main_red</item>
<item name="android:windowBackground">#color/main_bg_light</item>
<item name="android:listViewStyle">#style/MyListView</item>
<item name="android:tabWidgetStyle">#style/Base.Widget.Design.TabLayout</item>
</style>
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 want get the effects like this picture. I am using Action bar Sherlock for backward compatibility.
I am use NAVIGATION_MODE_TABS mode in action bar. I follow this blog to achieve below screen. My code is here.
HomeActivity extends SherlockFragmentActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
setTitle(R.string.app_name);
mViewPager = (ViewPager) findViewById(R.id.pager);
// Add the tabs
mTabsAdapter = new TabsAdapter(this, actionBar, mViewPager);
mTabsAdapter.addTab(actionBar.newTab().setText("General"),
GeneralFragment.class, null);
mTabsAdapter.addTab(actionBar.newTab().setText("Memory"),
MemoryFragment.class, null);
// more tabs here; seems no need those
if (savedInstanceState != null) {
actionBar.setSelectedNavigationItem(savedInstanceState
.getInt("tab"));
}
int titleId = Resources.getSystem().getIdentifier("action_bar_title",
"id", "android");
// If you're using sherlock, in older versions of android you are not
// supposed to get a reference to android.R.id.action_bar_title, So
// here's little hack for that.
if (titleId == 0) {
titleId = com.actionbarsherlock.R.id.abs__action_bar_title;
}
View titleView = findViewById(titleId);
// attach listener to this spinnerView for handling spinner selection
// change
spinnerView = (Spinner) getLayoutInflater().inflate(
R.layout.spinner_layout, null);
//
// source of ViewGroupUtils class is given at the end of this post.
ViewGroupUtils.replaceView(titleView, spinnerView);
}
I manage to build this .
How i build my desired screen in ActionbarSherlock? Any link or suggestion will be greatly appreciated .
Edited: My ViewGroupUtils class
`public class ViewGroupUtils {
public static ViewGroup getParent(View view) {
return (ViewGroup) view.getParent();
}
public static void removeView(View view) {
ViewGroup parent = getParent(view);
if (parent != null) {
parent.removeView(view);
}
}
public static void replaceView(View currentView, View newView) {
ViewGroup parent = getParent(currentView);
if (parent == null) {
return;
}
final int index = parent.indexOfChild(currentView);
removeView(currentView);
removeView(newView);
parent.addView(newView, index);
}
}
`
Just change your menu XML with this
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_share"
android:icon="#android:drawable/ic_menu_share"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="Share"/>
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="Search"/>
<item
android:id="#+id/more"
android:orderInCategory="100"
android:icon="#android:drawable/ic_menu_more"
android:showAsAction="always"
android:title="">
<menu>
<item
android:id="#+id/action_call"
android:icon="#android:drawable/ic_menu_call"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Call"/>
<item
android:id="#+id/action_camera"
android:icon="#android:drawable/ic_menu_camera"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Camera"/>
<item
android:id="#+id/action_compass"
android:icon="#android:drawable/ic_menu_compass"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Compass"/>
<item
android:id="#+id/action_help"
android:icon="#android:drawable/ic_menu_help"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Help"/>
</menu>
</item>
No need to change in your Java file.
Output is....