I'm using Actionbarsherlock and want to use a custom actionbar layout, but still want a navigation list. As a result I am including an IcsSpinner widget. However, the width is always as big as the largest item, and this is not desired. I broke it out of the actionbar to debug and still got the same results:
Fragment Code
public class TestFragment extends SherlockFragment {
private final String[] testStrings = {"Short","Not so long text"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.test_layout, container, false);
IcsSpinner spinner = (IcsSpinner)view.findViewById(R.id.test_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.spiner_test_row, R.id.spinner_text, testStrings);
spinner.setAdapter(adapter);
return(view);
}
}
test_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.actionbarsherlock.internal.widget.IcsSpinner
android:id="#+id/test_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
R.layout.spinner_test_row
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
Changing to standard Spinner (on gingerbread emulator) works fine:
Any Suggestions?
This appears to be the default behavior of the IcsSpinner component within ActionBarSherlock, even when running Honeycomb+ version of Android - e.g. Square Wallet on my ICS device.
Jake has suggested not using the IcsSpinner component, but without it there is no way simple way to use a custom actionbar layout and maintain the Navigation List UI. Unless Jake can suggest and alternative I will continue to use it, making sure do full testing upon future releases of his great library.
Related
I'm having a problem with the bottom of my ListView and ImageButton being cut off when I load my app onto a phone or when using an emulator. I could just use margins or padding, but wouldn't that be device specific? I'd like my app to look as I want it to regardless of screen size. Here is my code:
The Class:
public class Cookbook extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String[] items = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),
R.layout.row_layout,
items);
View view = inflater.inflate(R.layout.frag_cookbook, container, false);
ListView list = (ListView) view.findViewById(R.id.listView);
list.setAdapter(adapter);
return view;
}
}
The Layout XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<ListView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="fill"
android:id="#+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:background="#f9e48f" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_add"
android:background="#null"
android:layout_gravity="end|bottom"
android:layout_alignParentBottom="true"
android:baselineAligned="false"
android:layout_alignParentEnd="true"
android:layout_marginBottom="1dp" />
</RelativeLayout>
The Android Studio preview looks like this:
But when emulating it or loading it onto a phone, it looks like this:
This is because you are using CoordinatorLayout with ListView. You can change your implementation to RecyclerView to achieve correct scroll.
or If you are tagetting above 5.0, you can use the following piece of code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
listView.setNestedScrollingEnabled(true);
}
I think CoordinatorLayout only works with the children of NestedScrollingChild.
I've had this happen to me before recently. I recommend posting your styles.xml as well.
The culprit for me was this line of code or something similar:
<item name="android:windowTranslucentStatus">true</item>
Here's the Question I posted with a similar issue:
Layout is under StatusBar and Soft Keys
In Android Design Library, well, the FloatingActionButton supposed to be within the CoordinatorLayout, not within any other views ex: a Fragment view in one of the tabs above. so try adding your FloatingActionButton to your main_layout and then just communicate with the activity to show/hide the FAB and do the necessary clicks.
Put the below code in onCreateView, I think it will work.
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
decorView.setSystemUiVisibility(uiOptions);
Changing the layout to RelativeLayout worked for me. Then added this line in viewPager tag :
android:layout_below="#id/appbar"
Voilla..!!
I tried to create a full screen layout to display a imageview. In the lower end devices (below 4.0) i achieved this, unfortunately when i using the same code in higher end devices the system bar won't be hide. When I used this feature rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); (rootView - my fragment view), the system bar is hidden at the moment of application launch, once i touch the screen the system bar is enabled. How can i disable the system bar appearing on user touch until the current activity getting closed?
I think the trick is just to call the native style from Android allowing you to hide the ActionBar by default. Then just select the theme you need to use Holo Light, Holo Dark, etc.).
For example:
public class PhotoDialogFragment extends DialogFragment {
(...)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
//HERE IS THE TRICK
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Light_NoActionBar_Fullscreen);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fullImageView = inflater.inflate(R.layout.photo_fragment, null);
(...)
return fullImageView;
}
(...)
Here is my "photo_fragment" XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/fullscreen_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/your_image" />
</LinearLayout>
My app for phones has a pager with the main view and the premium features view when swiping right, in landscape mode it has a slightly modified version to fit the landscape orientation.
Now i have to make the tablet UI, and I'd like to show both views on one screen, I've already managed to show them, but the tablet in landscape mode is picking up the fragments from layout-land instead of the portrait layout, and I need it to pick the portrait ones to show side by side.
How can i make the fragment pick the portrait version even if the tablet is in landscape without creating duplicate layouts?
My fragments look like this
public class PremiumFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup premiumView = (ViewGroup) inflater.inflate(
R.layout.premium_features, container, false);
return premiumView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "onView created ran");
Intent intent = new Intent("setup");
LocalBroadcastManager.getInstance(getBaseContext()).sendBroadcast(intent);
}
}
It's been a while but for anyone interested in this matter:
If I understand correctly, you need to have a portrait layout, a landscape layout (for mobile for example) and a different landscape layout for tablets which matches the portrait one. There's not that much duplicity involved. You can include an independent non-qualified layout in your qualified layouts.
Lets say you have these layouts:
layout-land/my_fragment.xml
layout-port/my_fragment.xml
layout-large-land/my_fragment.xml
layout/my_fragment_content.xml
layout-port/my_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/my_fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout >
layout-land/my_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
//something completely different
</FrameLayout >
and layout-large-land/my_fragment.xml (contents are the same as portrait one):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/my_fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout >
There is some minimum duplicity involved but the only important thing is that you have no reason to ever touch layout-port/my_fragment.xml or layout-large-land/my_fragment.xml because all vital information is contained in a single file layout/my_fragment_content.xml.
This topic is more thoroughly analysed in this article that the author probably read aleady:
Android Developers: Building a Flexible UI
I am very new to android! I've created a vertical linearlayout with two listviews inside my listfragment and I just want every of this listviews show simple array of strings which I can update during runtime. I it seems very difficult for me to realize how to do this. Here are my files:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="horizontal" >
<ListView
android:id="#+id/trainslist"
android:layout_width="240dp"
android:layout_height="wrap_content" >
</ListView>
<ListView
android:id="#+id/timelist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
This is a fragment XML file, and here is a fragment file:
public class ToFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String[] countries = {
"Japan",
"USA"
};
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1, countries);
setListAdapter(adapter1);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
Look, here I try to make one of listviews show what I need and it works. But I don't understand how to thange it's content during runtime (not from onCreateView?) from Activity. It's also unclear for me what is android.R.layout.simple_list_item_1? Why, when I write it, android sends my array to the first listview, not second?
The second issue is I can't change second listview content even in this way, even not during runtime! Please help me deal with this! I really find it so difficult...
I am using actionbarsherlock library to have action-bar in my application,
I am also using view-pager to switch between the page,
Now the problem is i want only one tab with two fragment to display data regarding each other.
What could be the possible way to do this?
I have been going through like the one below but unsuccessful
In main fragment i am inflating a xml like this
<code>
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragView = inflater.inflate(R.layout.album_view_main, container, false);
return fragView;
}
</code>
xml fie is look like this
<code>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="#+id/fragment_album_view"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="3"
class="com.nix.music.activity.fragments.AlbumFragMent" />
<FrameLayout
android:id="#+id/frame_list_song"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="2" >
</FrameLayout>
</LinearLayout>
</code>
but the problem is i cannot find "frame_list_song" in AlbumFragment class it appears null
what could be the way around ?
thank you
This layout should be set in the parent activity using setContentView(). Then, you would be able to do something like getActivity().findViewById(R.id.frame_list_song) from your AlbumFragment. The better way would be to use an interface and transfer control to the main activity, and not access other fragments directly.