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..!!
Related
I'm trying to remove a progress indicator after loading my data in a fragment involving a ListView. Here is my completion handler:
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
dataSource = (ArrayList<Map<String, Object>>) task.getResult();
PostAdapter adapter = new PostAdapter(getActivity(), dataSource);
ListView list = (ListView) rootView.findViewById(R.id.listView);
list.setAdapter(adapter);
View indicator = getActivity().findViewById(R.id.indicator);
RelativeLayout layout = (RelativeLayout) getActivity().findViewById(R.id.layout);
layout.removeView(indicator);
}
});
The last 3 lines of code is the relevant part. Everything is called correctly, nothing is null etc. in debug everything works perfectly. The adapter also works correctly, populating my list, but the indicator is still on the screen. I've also tried setting it's visibility to GONE or HIDDEN but they also don't seem to hide it either. I've seen Android - Can't hide progress bar but it's answers involve setEmptyView() which I'm not using anyway. I am using the same fragment (of course, a different instance) in another tab, and it works correctly.
Here is my layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:background="#ffffffff"
android:id="#+id/layout">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_margin="0dp"
android:padding="0dp" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/indicator"
android:indeterminate="true"
android:layout_centerInParent="true" />
</RelativeLayout>
What am I doing wrong?
I've found out the problem (thanks to all the commenters). I was calling activity's methods to find and remove the view. The problem is that, I have multiple instances of the same fragment in the same activity, under different tabs. I've used my fragment's root view to find and remove the indicator, instead of the activity, and it worked.
Change the codes of last three lines in onClick(...) as:
View indicator = (ProgressBar)rootView.findViewById(R.id.indicator);
and use:
indicator.setVisibility(ProgressBar.GONE);
or codes:
RelativeLayout layout = (RelativeLayout) rootView.findViewById(R.id.layout);
layout.removeView(indicator);
I am working on a simple app, so that the main activity has two fragments that user can swipe to go to. On one of them, I want to have custom list. For example, something like this https://github.com/JakeWharton/SwipeToDismissNOA, where each item in the list can be deleted by swiping. I was able to get a regular list to work inside the ListFragment, but can't get this custom list to work. As I understand, ListFragment needs to have a simple .xml with one , but the one that I want to use, has a bit more stuff in it. Something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
android:padding="16dp">
<LinearLayout android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:orientation="vertical">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:listSeparatorTextViewStyle"
android:text="ListView" />
<ListView
android:id="#android:id/list"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
So when I am trying to getListView() using this .xml, it either returns null or when I am trying to do something like this:
View view = inflater.inflate(R.layout.fragment_section_dummy, null);
ListView ls = (ListView) view.findViewById(android.R.id.list);
It throws this error:
10-15 20:25:28.895: E/AndroidRuntime(8081):
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView.
Thanks.
For me, is easiest work with my own IDs, so I would change your ListView ID to another one. For my example, I choose android:id="#+id/myList".
In your fragment, in your onCreateView method, try inflating your layout and getting your view by ID.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.yourDummyLayout, container, false);
//You should get your ListView there
ListView yourListView = rootView.findViewById(R.id.myList);
[......]
return rootView;
}
Try with this ;) Good luck !
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 just learning about android development, and I am having some issues with getting this to work.
I have an activity that uses a relatively layout. I need it to have 2 buttons along the bottom, and then right above the bottoms, I want my custom view to take up the rest of the space.
viewer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<sketchViewer.AnimationPanelView
android:id="#+id/animationView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/homeFromViewerButton"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/homeFromViewerButton"
android:layout_width="640dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Replay" />
<Button
android:id="#+id/replayButton"
android:layout_width="640dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Home" />
</RelativeLayout>
The issue I am having is I that when I run my program, I need to pass a number of parameters into my custom view constructor so that my custom view decides what it should draw. So after creating an instance of my custom view (AnimationPanelView), I am not sure how I set this object into the space I provided for the view.
This is my activity class:
Viewer.java
public class Viewer extends Activity {
AnimationPanelView animationPanelView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewer);
animationPanelView = new AnimationPanelView(this, true /*, more parameters here */);
animationPanelView.setBackgroundColor(Color.GREEN);
RelativeLayout v = (RelativeLayout) findViewById(R.id.viewerLayout);
v.addView(animationPanelView);
}
Right now, with my v.addView command, the view takes up the entire page, covering up the buttons at the bottom. Can anyone shed some light on this? I feel like I am close, but I've been playing around with it for a while, and I just seem stuck.
Check out the implementing a custom view section here. You need to override onLayout and onMeasure so you can tell your container how big you are.
You are adding another custom view to your layout instead you should use
animationPanelView = (AnimationPanelView) findViewById(R.id.animationView);
animationPanelView.setBackgroundColor(Color.GREEN);
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.