Why LinearLayout inside ScrollView of a Fragment is NULL object? - android

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

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

ListView addHeaderView glitching around after onResuming Activity

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;
}

Android: Unable to inflate EditText in the layout

I am trying to inflate a EditText view inside a RelativeLayout in my Activity.
But getting an error says:
The specified child already has a parent. You must call removeView() on the child's parent first.
My code:
RelativeLayout relLayout= new RelativeLayout(insideActivity);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relLayout.setLayoutParams(params);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.holo_edit_text, null);
EditText searchBox = (EditText)view.findViewById(R.id.holo_text);
relLayout.addView(searchBox);
And here is holo_edit_text.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="wrap_content">
<EditText
android:id="#+id/holo_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
</RelativeLayout>
your holo_edit_text.xml should be
<?xml version="1.0" encoding="utf-8"?>
<EditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/holo_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
And the way you add it to the view should be like this
EDIT (use getLayoutInflater() instead)
View view = getLayoutInflater().inflate(R.layout.holo_edit_text, null);
//EditText searchBox = (EditText)view.findViewById(R.id.holo_text);
//The last sentence is not neccesary
relLayout.addView(view);
Have you tried this while adding to relative layout
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
relLayout.addView(view, params);
Try this..
View child_view = LayoutInflater.from(this).inflate(R.layout.holo_edit_text, null);
relLayout.addView(child_view);
Use a try/catch mechanism.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (rootView != null) {
((ViewGroup) rootView.getParent()).removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.your_layout_file_here, container,false);
EditText searchBox = (EditText)rootView.findViewById(R.id.holo_text);
relLayout.addView(searchBox);
} catch (InflateException e) {
}
return rootView;
}
I have fixed my issue by simply setting the LayoutParams to searchBox before adding it to the RelativeLayout. And its working fine.
Thanks all for your response.

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;
}
}

Categories

Resources