I am trying to call a library in a fragment but dont know how to set it in a fragment I have done it in the main activity but I am getting an error in setting the setContentVie
On a fragment you don't call setContentView
explicitly, you return the view after inflating it, as you are. So instead of calling setContentView
consider adding the view aboutPage
to rootView
or one of its children views.
For example, say your layout R.layout.fragment_navigation
contains a LinearLayout
(or any other ViewGroup
for that matter) with an ID of content
. You would do this, before your return statement:
LinearLayout content = (LinearLayout) rootView.findViewById(R.id.content);
content.addView(aboutPage); //<-- Instead of setContentView(aboutPage)
You'll have to adjust this to your layout, I don't know what's inside it.
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/container">
</RelativeLayout>
public class FragmentExample extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment, container, false);
Element versionElement = new Element();
versionElement.setTitle("Version 6.2");
Element adsElement = new Element();
adsElement.setTitle("Advertise with us");
View aboutPage = new AboutPage(getActivity())
.isRTL(false)
.addItem(versionElement)
.addItem(adsElement)
.addGroup("Connect with us")
.addEmail("elmehdi.sakout@gmail.com")
.addFacebook("the.medy")
.addTwitter("medyo80")
.addYoutube("UCdPQtdWIsg7_pi4mrRu46vA")
.addPlayStore("com.ideashower.readitlater.pro")
.addInstagram("medyo80")
.addGitHub("medyo")
.create();
viewGroup.addView(aboutPage);
return viewGroup;
}
}
setContentView() is for Activities, for Fragments you have to return the inflated layout on the onCreateView() method like this:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.article_view, container, false);
}
I hope it helps.
in Android X you simply can first import
import androidx.fragment.app.Fragment
and then in constructor pass your layout id like R.layout.fragment_navigation and then just override onViewCreated methode (it,s safer too)and than you just implement onCreateView(). Example:
import android.view.View
import android.widget.*
import androidx.fragment.app.Fragment
class MahrInCoinFragment : Fragment(R.layout.fragment_navigation) {
private lateinit var mCoin: Coin
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Load coin saved data or use default value
val cost: Int = Hawk.get(Constant.COIN_COST_KEY, Constant.DEFAULT_COIN_PRICE)
val date: String = Hawk.get(Constant.COIN_DATE_KEY, Constant.DEFAULT_COIN_DATE)
updateCoin(Coin().apply { this.cost = cost; this.date = date })
// Handle listeners
view.findViewById<Button>(R.id.btn_calculate).setOnClickListener(this)
view.findViewById<ImageButton>(R.id.btn_update).setOnClickListener(this)
}
If you need just aboutPage than change the return statement to return aboutPage;