Android Error using findViewById - android

I am new to this android application development. I am trying to find textfield using findViewById Eclipse keep asking me to create a new method for findViewById. Here is my code.
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle SavedInstanceState) {
return inflater.inflate(R.layout.myFragment, container,false);
//find textarea
TextView textArea = (TextView) findViewById(R.id.txtField_curDateTime);
}
Please note i have myFragment.java and myFragment.xml files.
Appreciate your help.
Thanks

Do it like this:
View v = inflater.inflate(R.layout.myFragment, container,false);
TextView textArea = (TextView) v.findViewById(R.id.txtField_curDateTime);
return v;
Moreover, define TextView textArea outside the onCreateView so that its available to other methods too.

Try this:
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle SavedInstanceState) {
View vObject = inflater.inflate(R.layout.myFragment, container,false);
//find textarea
TextView textArea = (TextView)vObject.findViewById(R.id.txtField_curDateTime);
return vObject;
}

Related

how to Bind dynamic views using butterknife

i am creating the multiple dynamic views in android and my fragment using the layout of parent fragment and in child fragment i am creating the dynamic views but i am not able to bind those dynamic views using Butterknife.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this is the on create
// Inflate the layout for this fragment
View view = super.onCreateView(inflater, container, savedInstanceState); this.container.addView(inflater.inflate(R.layout.overall_sport_performance_row, null), 5);
i have one textview in the overall_sport_performance layout and i want to use that using butterknife
I don't really understand your solution, if your Fragment's view is overall_spot_performance.xml, do like this:
#BindView(R.id.your_text_view_id)
protected TextView textView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = View.inflate(getContext(), R.layout.overall_sport_performance_row, null);
Butterknife.bind(this, view);
return view;
}
No need to bind the TextView dynamically. However if you still need to bind it dynamically, you can use findById method. I did not test it but should work like this:
View dynamicView = inflater.inflate(R.layout.overall_sport_performance_row, null);
this.container.addView(dynamicView, 5);
TextView textView = ButterKnife.findById(dynamicView, R.id.your_text_view_id);

getting a view using getResources()

I want to set text of a TextView that is inside a layout file,
I tried setContentView() but it isnt working since i am using fragments.
I tried using getResources().getLayout(R.layout.abc);
It returns null
I tried setContentView() but it isnt working since i am using
fragments
That's wrong. You have to override onCreateView and inflate and return the layout you want to show, and you can use onCreateView, and use its first parameter, View view, to call findViewById and access the widgets in your layout. You can read more here and here
inside fragment you can set view inside function onCreateView(), use below code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b) {
View view = (LinearLayout) inflater.inflate(R.layout.my_layout, container, false);
return view;
}
here my_layout should be the name of layout file,
now you can get view of it inside function onviewcreated()
public void onViewCreated(View view, Bundle savedInstanceState){
// here you can get your textview and set its value
}
thumbs up, if you find my answer correct
You can inflate your layout like this:
ViewGroup group = LayouInflate.from(context).inflate(R.layout.abc,null);
TextView tv = group.findViewById(R.id.xxx);
you have to inflate the layout through the onCreateView method and then return the View.
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.your_layout, container, false);
TextView yourTextView = (TextView)rootView.findViewById(R.id.yourTextViewId);
return rootView;
}

xml file not shown in android.R.layout

I'm creating a fragment for navigation drawer but when I'm using inflater.inflate...
my xml file is not among the options available in android.R.layout...
CODE
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(android.R.layout.fragment_navigation_drawer, container, false);
}
Use it like this:
return inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
Thanks a lot everyone for answering..
but I got it correctly this way
int view = R.layout.fragment_navigation_drawer;
return inflater.inflate(view, container, false);

Font change is not updating to the pager

I have three fonts in a listView, whichever the user selects it has to effect the textView in the Pager. But, it is not updating the font in that position of the ViewPager. It is getting updated after moving to one or two slides/pages. Please help me, Thanks.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_main, container, false);
// Set Slide Object Value
TextView mDesc = (TextView)rootView.findViewById(R.id.textTitle);
m_txTitle.setTypeface(SettingsABC.getTypeface(fontStyle));
fontStyle = Typeface.createFromAsset(getAssets(), "cookies.ttf");
mDesc.setTypeface(fontStyle);
}

Adding TextView object dynamically to a Fragment

I have been able to work with TextView elements in a Fragment when they are statically defined in my XML. But when I try to create them dynamically/programatically, I don't see anything displayed. I've created a simple example of the problem I'm having.
This one works, I see the screen updated.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.hello_world, container, false);
TextView tv = (TextView)v.findViewById(R.id.text);
tv.setText("Fragment #" + mNum);
tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
return v;
}
This one doesn't work. The XML file diagtextvertical.xml is simply this:
<?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"
>
</LinearLayout>
The code is as follows.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
//good View v = inflater.inflate(R.layout.diaglist, container, false);
View v = inflater.inflate(R.layout.diagverticaltext, container, false);
mycontainer = container;
LinearLayout l = (LinearLayout)inflater.inflate(R.layout.diagverticaltext, container, false);
TextView tv = new TextView(container.getContext());
tv.setText("Testing...");
l.addView(tv);
tv = new TextView(container.getContext());
tv.setText("Testing1...");
l.addView(tv);
tv = new TextView(container.getContext());
tv.setText("Testing2...");
l.addView(tv);
return v;
}
I have tried using getActivity() in place of container.getContext(), but no luck. There is something I'm not clear on on how to map the layout to the actual view instance - something different than doing this in an Activity.
There is a second part to my question. My actual goal is to update the view asyncronously based on polled data, so one question I have is how to get my hands on the ViewGroup and the inflater when I'm calling in on my own callback.
I'm new to android development, but i think you should return l instead of v in your onCreateView or you should add TextViews to v.

Categories

Resources