Where should I put logic in the Java tab layouts - android

I have 2 fragments each fragments has a different java logic. Where should i put the java logic? if i did put it in the fragments,then error message displays :
cannot resolve method findViewById, Cannont resolve method getApplicationContext

it appear that you did not inflate the view correctly, change your code to be something like this :
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.your_fragment, container, false);
mButton = (Button) view.findViewById(R.id.mButton );
list_content = (ListView) view.findViewById(R.id.list_content);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
return view;
}
and you should use 'getActivity()' method for context not 'This' .

You can find other view's Id's in your fragment like this.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_layout,container,false);
tabLayout = (TabLayout)view.findViewById(R.id.tabs);
return view;
}

When you inflate the view on the oncreateview make a view field like this :
View view = inflater.inflate(R.layout..., container, false);
and return with this view.
After that with this variable you can reach the findViewById, like
view.findViewById
Instead of getApplicationContext you can use getContext in fragment.
Good luck.

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

getting force close when using findviewbyId()

I'm starting to learn android app development and currently practicing with simple apps. I'm writing this app that includes a listview and i want to fill it with fake data but, when i use findViewbyId() in my code the app gives me force close BUT when i set the content view to my fragment instead of activity_main i get no force close, but the app shows me an empty list.
This is my code :
package com.example.karen.please;
public class MainActivityFragment extends Fragment {
private ArrayAdapter<String> mForecastAdapter;
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] forecastArray =
{
"sunny",
"cloudy",
"asteroids",
"rainy",
"sunny"
};
List<String> weekforecast = new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter =
new ArrayAdapter<String>(
getActivity(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textview,
weekforecast
);
ListView mylistview = (ListView)container.findViewById(R.id.listview_forecast); //screws upp in this line
mylistview.setAdapter(mForecastAdapter);
return inflater.inflate(R.layout.fragment_main, container, true);
}}
Any help would be appreciated.
You're trying to find a view before you inflate your layout... Try inflating at the top of your method, like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_main, container, true);
<all the other init code you have>
return myView;
}
First you need inflate your layout...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
...
ListView mylistview = (ListView)rootView.findViewById(R.id.listview_forecast);
...
Before using findViewById you should Create a View variable, use the inflate.. line, use findViewById on the View variable you created, and then return that.
This is what your code should look like:
package com.example.karen.please;
public class MainActivityFragment extends Fragment {
private ArrayAdapter<String> mForecastAdapter;
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Instead of inflate and return instantly, store it in a variable,
// Then return at the end of the method.
View view = inflater.inflate(R.layout.fragment_main, container, true);
[...]
// Here, use findViewById on the View you have just inflated.
ListView mylistview = (ListView) view.findViewById(R.id.listview_forecast); //screws upp in this line
mylistview.setAdapter(mForecastAdapter);
// Return the view you created on the beginning.
return view;
}}
You should start with a proper tutorial not chaotically adding code that you don't understand. Start reading here:
Creating and Using Fragments
After you understood how it goes then you will see that first you need to inflate the layout that contains the stuff you want to find by id and then by using the view(that defines the layout) you can find the desired view(listview, etc).
ListView my listview = (ListView).findViewById(R.id.listview_forcast);
I think this is how it should be, no container in the code

JSONPARSING error in findViewById

Here's my code in the main activity don'tn know why error in findViewById occurs
.. this is just my first time in JSON paarsing .
public class HomeFragment extends Fragment{
public HomeFragment(){}
View rootView;
ListView list;
ActorsAdapter adapt;
ArrayList<Actors> actorslist;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_fragment, container, false);
list = (ListView)findViewById(R.id.listFeeds);
actorslist = new ArrayList<Actors>();
return rootView;
new ActorsAsynTask().execute("http://jcasim.5gbfree.com/Project/login.php");
}
listFeeds It's a component I'm pretty sure is a component that lives in your fragment. Since you haven't return the root view yet, the findViewById is not going to find it. you have to wait until it's on screen to ask for it using findViewById (in onStart() method) or ask it form the root view before you return it.
it would be list = (ListView)rootView.findViewById(R.id.listFeeds);
The View of the Fragment is not set before the onCreateView() returns. Therefore the findViewById() tries to find the specified R.id.listFeedsfrom a null reference.
Try this instead list = (ListView)rootView.findViewById(R.id.listFeeds);

Android fragment illegalstateexception

i searched and found custom listview's library and i try to use it (QuickReturnHeaderHelper)
in this library's example used activity and this library can load like this
QuickReturnHeaderHelper helper = new QuickReturnHeaderHelper(getActivity(),
R.layout.activity_main, R.layout.header);
View view = helper.createView();
getActivity().setContentView(view);
but in my project i use fragment but i do not know how i can use this source code
View rootView = inflater.inflate(R.layout.activity_main,
container, false);
QuickReturnHeaderHelper helper = new QuickReturnHeaderHelper(getActivity(),
R.layout.activity_main, R.layout.header);
View view = helper.createView();
getActivity().setContentView(view);
i wrote my code like this but i can not recive result same as a activity.
how i can change my code if anyone knows solution please help me
thanks
You should override onCreateView() of your Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
QuickReturnHeaderHelper helper = new QuickReturnHeaderHelper(getActivity(),
R.layout.activity_main, R.layout.header);
return helper.createView();
}

Categories

Resources