I'm facing an odd problem here. I'm just trying to set a value to a TextView in a Fragment. Problem is that it doesn't get updated at all. The color doesn't change too. The predefined text "test" is shown though, so I can rule out any layout related issues.
Any ideas on this?
Thanks !!
fragment_class.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:orientation="vertical" >
<TextView
android:id="#+id/classdetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test"
android:textColor="#000" />
</RelativeLayout>
Fragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//datasource = new ClassesDataSource(getActivity());
//datasource.open();
//Classes value = datasource.getClasses(this.getArguments().getString("id"));
View v = inflater.inflate(R.layout.fragment_class, container, false);
TextView tv = (TextView) v.findViewById(R.id.classdetail);
tv.setText("test_IDE");
tv.setTextColor(Color.BLUE);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_class, container, false);
}
Your problem is that you're inflating the view twice.
Replace:
return inflater.inflate(R.layout.fragment_class, container, false);
with:
return v;
To explain what the problem is, you are inflating the view, setting the text and text color, and then inflating a different view and assigning that one as the one that will be used for the fragment.
Try using
View v = getActivity().getLayoutInflater().inflate(R.layout.fragment_class, container, false);
Instead of
View v = inflater.inflate(R.layout.fragment_class, container, false);
Related
I'm trying to figure out why my call to findViewById on my layout is returning the generic type "View" instead of the type "ScrollView" that the view in question is declared as.
If you look below, my xml file clearly lists the view with id "scrollview" as a <ScrollView>, yet findViewById on this element only returns a View, not a ScrollView. Why is this happening?
Note: the parent container to this xml layout is a Fragment, not sure if this changes things.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.menu_fragment_capo, container, false);
mScrollview = v.findViewById(R.id.scrollview); //ERROR HERE
mScrollview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//mScrollview.smoothScrollTo();
return true; //true = not scrollable
}
});
}
scroller_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">
<ScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonLayoutParent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
Change mScrollview = v.findViewById(R.id.scrollview); to
mScrollview = (ScrollView)v.findViewById(R.id.scrollview);
FindViewById returns View which is super class of all ui elements therefore u need to cast it to specific type.
findViewById return a View. You need to cast it to a ScrollView as follows,
mScrollview = (ScrollView) v.findViewById(R.id.scrollview);
please cast
mScrollview = (ScrollView)v.findViewById(R.id.scrollview);
Always in the case of fragment you need to cast your layout or widgets to view for eg:mscroll=(Scrollview)yourview.findviewbyid(R.id.scroll) and I also ask you to take a look at this. findViewById in Fragment
This question already has an answer here:
Set textView text from a Fragment won't work [closed]
(1 answer)
Closed 6 years ago.
It drives me crazy. I have made research for one day, although I tried everything, nothing work.
this is my xml code:
<?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">
<TextView
android:id="#+id/testo_tab_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#000000"
android:textSize="80dp"/>
</RelativeLayout>
My java's code is:
public class SocietaTabOne extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.societa_tab_one, container, false);
TextView titolo = (TextView)view.findViewById(R.id.testo_tab_one);
titolo.setText("wow");
return inflater.inflate(R.layout.societa_tab_one,null);
}
}
I should see the text in my fragment, but I don't see anything.
Why?
because here you are recreating the layout (erasing the text you set)
return inflater.inflate(R.layout.societa_tab_one,null);
just return view you inflated at the beginning
return view;
Your Fragment should look Somthing like this
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.societa_tab_one, container, false);
TextView titolo = (TextView)view.findViewById(R.id.testo_tab_one);
titolo.setText("wow");
return view;
}
Just return view instead of this line inflater.inflate(R.layout.societa_tab_one,null)
I have a listfragment class:
public class activityfeedScreen extends ListFragment implements OnItemClickListener {
private ListView activityfeed_feedList_listView;
private ActivityFeedBaseAdapter adapter;
public static final activityfeedScreen newInstance()
{
activityfeedScreen fragment = new activityfeedScreen();
Bundle bdl = new Bundle(1);
fragment.setArguments(bdl);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_activityfeed_screen, container, false);
activityfeed_feedList_listView = (ListView)v.findViewById(R.id.activityfeed_feedList_listView);
activityfeed_feedList_listView.setAdapter(adapter);
activityfeed_feedList_listView.setOnItemClickListener(this);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onItemClick(AdapterView<?> activityfeedlistview, View view, int position, long itemID) {
Intent intent_activityfeed_showitemdescription = new Intent(view.getContext(), activityfeedItemScreen.class);
startActivity(intent_activityfeed_showitemdescription);
}
}
In this class I am getting an error at the line:
activityfeed_feedList_listView = (ListView)v.findViewById(R.id.activityfeed_feedList_listView);
I initially thought it was a problem due to the getView() being null so I moved that from the onCreate method to the onCreateView() method. But I am getting the runtime error as stated in the title.
Here is my layout file:
<?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:background="#E5E5E5"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<ImageView
android:id="#+id/activityfeed_profilePicture_imageView"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:src="#drawable/default_profile"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B9B9B9"
android:layout_marginTop="20dp">
<ListView
android:id="#+id/activityfeed_feedList_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
I understand that I must change the listview id to
android:id="#android:id/list"
but I am not sure then how to call that in my onCreateView method.
Thanks for all your help!
Yes. First of all you do have to change the ID of your ListView like so:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
In your onCreateView you can reference the ID by android.R.id.list. This is because Android built-in resource reside in android.R. You can also find there drawables, styles, etc. (you can call them in a similar maneer).
Taking the above into consideration, your onCreateView method should look like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_activityfeed_screen, container, false);
activityfeed_feedList_listView = (ListView)v.findViewById(android.R.id.list);
activityfeed_feedList_listView.setAdapter(adapter);
activityfeed_feedList_listView.setOnItemClickListener(this);
return v;
}
That's all ;)
In onCreateView() you need to provide a layout that has a ListView with and id of #android:id/list.
ListFragment provides some helper methods (e.g setListAdapter()) that require the layout to have a ListView with that id so it can reference it and set the adapter for you.
Yes, I realize there are questions VERY similar to this one, but unlike the others, my onCreateView() method inflates the view for the fragment. When I don't use findViewById(), everything inflates and my app works as expected (except for the functionality for the views I want to get in findViewById().
Here is my code:
#Override
public View onCreateView(LayoutInflater p_inflater, ViewGroup p_container, Bundle p_prev_state)
{
return p_inflater.inflate(R.layout.fragment_main, p_container, false);
}
Whenever I want to access a ListView within my layout, I do this:
private ListView getListView()
{
return (ListView)getView().findViewById(R.id.main_events);
}
but this is always null. Why? My fragment_main.xml looks like so:
<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:background="#color/white"
tools:context="me.k.cal.MainActivity$PlaceholderFragment" >
<ListView android:id="#+id/main_events"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1dp"
android:divider="#color/background_color" />
</RelativeLayout>
My full fragment code can be found here.
Use Below Code to inflate your view and get id of listview.
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View _view = inflater.inflate(R.layout.fragment1, container, false);
ListView _list= (ListView) _view.findViewById(R.id.main_events);
return _view;
}
Try this,hope this helps
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
View view = inflater.inflate(R.layout.fragment_main, container,
false);
ListView eventsList= (ListView) view.findViewById(R.id.main_events);
return view;
}
I've been trying to solve this for a while now, I've looked through quite a number of questions but still nothing works.
I have a MainActivity with a SlideMenu, all items open a Fragment. One of these Fragments extends to a ListFragment. What I'm trying to do is once a ListItem is selected another Fragment is called and displays some data...for now just a title.
When a ListItem is selected, I am creating an instance of the new Fragment, save the data in the Bundle and call the new Fragment to be displayed.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// do something with the data
Article a = articleList.get(position);
Fragment articleFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putString("title", a.title);
articleFragment.setArguments(args);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.frame_container, articleFragment);
transaction.commit();
}
The onCreateView of the new Fragment attempts to set the text of the TextView with the Bundle's data.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
TextView titleTextView = (TextView) rootView.findViewById(R.id.article_title);
String title = getArguments().getString("title");
titleTextView.setText(title);
return rootView;
}
The layout xmls are these two files:
activity_article_fragment.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.androidhive.slidingmenu.ArticleFragment"
tools:ignore="MergeRootFrame" />
fragment_article.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="info.androidhive.slidingmenu.ArticleFragment$PlaceholderFragment" >
<TextView
android:id="#+android:id/article_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
I've tried to do as many suggested...using the View to get the id from the xml (accessing textview within a fragment in activity, Android : How do I update my textView in a Fragment) but it's still null.
Whilst debugging everything is working but obviously stops when attempting to set the text of the TextView as it is null.
UPDATE
Just did as suggested by Karakuri...text being displayed but it looks like this: