ListView addHeaderView glitching around after onResuming Activity - android

In my ListFragment I want a headerView to make the app look like its website counterpart.
onCreateView():
View headerView = inflater.inflate(R.layout.header, null, false);
onStart():
if(headerView != null) {
getListView().addHeaderView(headerView);
}
After pausing and resuming the activity, the headerView has a large top margin is glitching around: https://www.youtube.com/watch?v=Q_zhr8w5Yzg
header.xml
<?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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/news_bubble"
android:paddingTop="5dp"
android:paddingRight="10dp"
android:paddingBottom="20dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:text="#string/str_header"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</LinearLayout>
Edit: This question is solved. If anybody stumbles upon this error, this is my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container, false);
listView = (ListView)rootView.findViewById(android.R.id.list);
headerView = inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(headerView);
[...]
return rootView;
}

You need to make the list view as the container for your header view. So inflate the listView first and then inflate the header view as follows
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_layout,container,false);
ListView listView = (ListView)view.findViewById(R.id.my_list);
View headerView = inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(headerView);
return view;
}

Related

Accessing views outside of onCreateView() method

I am trying to make a particular nested FrameLayout visible dynamically. However, i am getting NullPointerException when i try to access the view outside of my fragment's onCreateView() method. So here is my implementation.
private View myView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragmentView = inflater.inflate(R.layout.listView, container, false);
this.myView = fragmentView;
...
return fragmentView;
}
#Override
public void apiCompleted(ApiResult apiResult, HttpRequest httpRequest) {
if(myLocationManager.hasLocation()){
FrameLayout flayout = (FrameLayout) myView.findViewById(R.id.ldrawlayout);
flayout.setVisibility(View.VISIBLE);
}
}
listView.xml
<LinearLayout 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:background="#color/background_gray"
tools:context=".fragment.MallListFragment"
android:orientation="vertical">
<ListView
android:id="#+id/lv_malls"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
rowlist.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row_mallx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:paddingBottom="7dp">
<FrameLayout
android:id="#+id/ldrawlayout"
>
...
</FrameLayout>
</RelativeLayout>
LOGCAT (onCreateView method is called first)
01-15 18:40:16.905 7633-7633/? V/onCreateView METHOD CALLEDīš• onCreateView
01-15 18:40:17.280 7633-7633/? V/APICOMPLETED METHOD CALLEDīš• apiCompleted
Ok, I see the problem. You want to access the row-layout, but your listview has multiple versions of this layout, that mean you can not access the row layout from the activity, you have to do it in your listadapter or you give your single rows in the listadapter an unique id such as row.setId(row.getId + positionOfRow)
LayoutInflater Inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = Inflater.inflate(R.layout.row_mallx, null);
FrameLayout flayout = (FrameLayout) view .findViewById(R.id.ldrawlayout);
flayout.setVisibility(View.VISIBLE);
try this code in on apiCompleted
Are your sure that the onCreateView() method is called before the apiCompleted() method.
If yes, you should be able to access the view any where in your code as long as you hold the reference to it.
You can add some logs to see the order that the functions get called

How to display a message when listview is empty?

I am new to android programming. Here is my code. Kindly help.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
ListView view = (ListView) inflater.inflate(R.layout.fragment_alarms,container, false);
String[] titles = getResources().getStringArray(R.array.drawer_menu_list);
view.setAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.drawer_list_item, titles));
view.setAdapter(mAdapter.setCurrentSeverityAlarm(0));
mListView = view;
mAdapter.refresh(mSpinnerAdapter);
return view;
}
xml file:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_alarms"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/alarm_list_vertical_margin"
android:paddingLeft="#dimen/alarm_list_horizontal_margin"
android:paddingRight="#dimen/alarm_list_horizontal_margin"
android:paddingTop="#dimen/alarm_list_vertical_margin"
android:divider="#android:color/transparent"
android:dividerHeight="#dimen/alarm_list_divider_height"
android:scrollbars="none">
</ListView>
ListView has a setEmptyView(View) method. The View you pass into this method will automatically be shown if your list is empty.
The easiest way to do this is to include the empty View in your layout. Your XML and Java will end up looking something like this:
XML
<ListView ... />
<TextView
android:id="#+id/empty_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nothing to show" />
Java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_alarms,container, false);
ListView view = (ListView) layout.findViewById(R.id.list_arlams);
View emptyView = layout.findViewById(R.id.empty_view);
// ...
view.setEmptyView(emptyView);
return layout;
}
You can of course also programmatically instantiate a View if you want to keep it out of your XML, or you could use a separate layout file for your empty View.

Changing Background color of a view inside a Fragment

Ok, this is my new problem;
I am trying to change the background color of a View inside a Fragment on touch Event.
It is already setup and working fine. But it seems the background just won't change of color.
I do suspect this happens because this view has also other views on it on fill parent So even if it changes color I won't be able to see it.
Anyhow I have already set up those views color to Color.TRANSPARENT but it still does not work...
Here is my xml file:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:id = "#+id/lay_1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/position"
android:id="#+id/pos"/>
<ViewFlipper
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/pos"
android:id="#+id/flipper_1"
>
<include layout="#layout/cart"/>
<include layout="#layout/ang"/>
<include layout="#layout/data_graph"/>
</ViewFlipper>
If you need more information don't mind asking for it.
Thanks a lot.
Luis.
I had a similar issue and you should try to create a method called at the oncreate of the fragment to update the inside of the fragment view (background color, text...)
ex:
public static LinearLayout getcolor(LayoutInflater inflater, ViewGroup container, boolean bf){
int i;
View v;
TextView t;
LinearLayout ll;
int r = (int)Math.round(Math.random()*255+1);
int g = (int)Math.round(Math.random()*255+1);
int b = (int)Math.round(Math.random()*255+1);
i=Color.argb(200,r,g,b);
if(bf){
ll = (LinearLayout )inflater.inflate(R.layout.fragment_card_front, container, false);
v=ll.findViewById(R.id.frag_card_front);
v.setBackgroundColor(i);
t=(TextView) ll.findViewById(R.id.textf);
t.setText(""+i);
}
else{
ll = (LinearLayout )inflater.inflate(R.layout.fragment_card_back, container, false);
v=ll.findViewById(R.id.frag_card_back);
v.setBackgroundColor(i);
t=(TextView) ll.findViewById(R.id.textb);
t.setText(""+i);
}
return ll;
}
public static class CardFrontFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout ll = (LinearLayout ) getcolor(inflater, container,true);
return ll;
}
}
public static class CardBackFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout ll = (LinearLayout ) getcolor(inflater, container,false);
return ll;
}
}

Android, findViewById returns null

I'm writing some code for Android, the matter is that, when I call findViewById it returns me null, and I cannot understand why! I've been wasting my brain since yesterday, but I cannot figure out the solution. The goal is to set a layout as a header for a listView. Here is my code, respectively my header and my page:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/header"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp">
<TextView
android:text="#string/bydistance"
android:layout_gravity="left"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="13dp"
android:gravity="center_horizontal"/>
<TextView
android:text="#string/byactivity"
android:layout_gravity="right"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="13dp"
android:gravity="center_horizontal"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
>
<ListView
android:id="#+id/parkList"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:divider="#android:color/transparent"
android:dividerHeight="5.0dp">
</ListView>
</LinearLayout>
And here my code where I call the addheaderView:
public class NearMeFragment extends Fragment implements View.OnClickListener{
private FragmentManager fragmentManager;
#Override
public void onCreate(Bundle savedBundle){
super.onCreate(savedBundle);
}
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {
View v = inflater.inflate(R.layout.park_list, container, false);
//TODO remove
make_test(v, container);
return v;
}
public void openTest(View view){
new LiveComment().show(getChildFragmentManager(), "dialog");
}
public void onClick(View view){
if (fragmentManager == null)
fragmentManager = getChildFragmentManager();
//if (view.getId() == R.id.test){
fragmentManager.beginTransaction().add(new LiveComment(), "livecomment").commit();
}
private void make_test(View v, ViewGroup container) {
ListView listView = (ListView) v.findViewById(R.id.parkList);
Park[] list = new Park[3];
list[0] = new Park("parco a", "acquasparta", false, false, 1, 2, 3);
list[1]=new Park("parco b", "perugia", false, false, 1, 2, 3);
list[2]=new Park("parco b", "perugia", false, false, 1, 2, 3);
ParkListAdapter adapter = new ParkListAdapter(v.getContext(), R.layout.small_park_container,list);
LinearLayout header = (LinearLayout) container.findViewById(R.id.header);
listView.addHeaderView(header);
listView.setAdapter(adapter);
}
}
The error is given at
listView.addHeaderView(header)
R.id.header is inside v not inside container.
Change
LinearLayout header = (LinearLayout) container.findViewById(R.id.header);
with
LinearLayout header = (LinearLayout) v.findViewById(R.id.header);
about the ViewGroup container to doc says:
If non-null, this is the parent view that the fragment's UI should be
attached to. The fragment should not add the view itself, but this can
be used to generate the LayoutParams of the view.
Edit: Since your header view is in another Layout you have to inflate it too:
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {
View v = inflater.inflate(R.layout.park_list, container, false);
View header = inflater.inflate(R.layout.headerlayout, null);
//TODO remove
make_test(v, header);
return v;
}
and
private void make_test(View v, View header) {
// your code
// you have to remove LinearLayout header = (LinearLayout) container.findViewById(R.id.header);
listView.addHeaderView(header);
listView.setAdapter(adapter);
}

Why LinearLayout inside ScrollView of a Fragment is NULL object?

I am trying to add multiple views to the Fragment dynamically:
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.fragment1, container, false);
ViewGroup parent = (ViewGroup) getActivity().findViewById(R.id.linearLayoutOfFirstFragment);
if (parent==null)
Toast.makeText(getActivity(),
"Null object " ,
Toast.LENGTH_SHORT).show(); //findViewById(R.id.linearLayoutOfFirstFragment);
myFirstView= (TextView)inflater.inflate(R.layout.mytextview, container, false);
vg.addView(myFirstView);
/*mySecondView= (TextView)inflater.inflate(R.layout.mytextview, container, false);
parent.addView(mySecondView);
......
XML for Fragment 1 - fragment1.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="#+id/linearLayoutOfFirstFragment"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00FF00"/>
</ScrollView>
I could add it to the fragment when the top layout was Linear Layout.
However when tried to add it to the Linear Layout inside scroll View after inflating it, get Linear Layout object NULL.
Should I inflate Linear Layout as well, to be able to add to it ? HOW TO DO IT?
Thank you in advance.
Change
ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.fragment1, container, false);
ViewGroup parent = (ViewGroup) getActivity().findViewById(R.id.linearLayoutOfFirstFragment);
with
ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.fragment1, container, false);
ViewGroup parent = (ViewGroup) vg.findViewById(R.id.linearLayoutOfFirstFragment);
Since linearLayoutOfFirstFragment is inside fragment1 you have to retrieve it through vg

Categories

Resources