fragment doesn't show in framelayout in linearlayout - java

I use fragments to display multiple views in one activity. I have 2 framelayouts in one linearlayout. The onCreate and onCreateView of the fragment gets called but the view of the fragment is not displayed. Is what i'm trying to do not possible? Or is there a way to fix issue?
Layout of the activity:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".view.activity.StandardFlowActivity">
<FrameLayout
android:id="#+id/standardFlowDownloadContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="#+id/standardFlowBaseContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
Layout of the fragment
<FrameLayout 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"
tools:context="com.example.sennevervaecke.crossexperience.view.fragment.WedstrijdFragment">
<ListView
android:id="#+id/wedstrijdListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Fragment Class
public class WedstrijdFragment extends Fragment implements AdapterView.OnItemClickListener{
private ArrayList<Wedstrijd> wedstrijden;
private WedstrijdFragmentCom communication;
public WedstrijdFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
Log.e("wedstrijdFragment", "onCreate is called");
super.onCreate(savedInstanceState);
wedstrijden = LocalDB.getWedstrijden();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e("wedstrijdFragment", "onCreateView is called");
View view = inflater.inflate(R.layout.fragment_wedstrijd, container, false);
ListView listView = view.findViewById(R.id.wedstrijdListView);
WedstrijdAdapter adapter = new WedstrijdAdapter(getContext(), wedstrijden);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
communication = (WedstrijdFragmentCom) activity;
} catch (ClassCastException e){
e.printStackTrace();
}
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
communication.onWedstrijdItemClick(wedstrijden.get(i));
}
}
Code in the onCreate of the activity to add the fragment:
wedstrijdFragment = new WedstrijdFragment();
getSupportFragmentManager().beginTransaction().add(R.id.standardFlowBaseContainer, wedstrijdFragment, "wedstrijd").commit();
Thanks in advance!

Your first layout standardFlowDownloadContainer width and height is match_parent , so the standardFlowBaseContainer FrameLayout is out of the screen. you can change your standardFlowDownloadContainer's height to 20dp and run your project , you will see your Fragment content.

Related

Android Recycler returns zero output despite reaching onBindViewHolder() for each row

I've put a RecyclerView in the 'Tutorial' activity in my application, to list tutorial versions, which come from the Room database my app has. It is hosted within a fragment, but that's of little consequence since I had the same problem before putting it in there. The RecyclerView adapter reaches onBindViewHolder() without any issues and I get logs from the Log.w I used for debugging there with correct data. The problem seems to be that Layout does not get inflated at all, and the buttons that each row is supposed to output never appear.
THE ADAPTER
public class VersionListAdapter extends RecyclerView.Adapter<VersionListAdapter.VersionViewHolder> {
private List<Version> mVersionList;
private final LayoutInflater mInflater;
private final OnViewClickListener mListener;
public VersionListAdapter(Context context)
{
this.mInflater = LayoutInflater.from(context);
this.mVersionList = null;
this.mListener = (OnViewClickListener) context;
}
#NonNull
#Override
public VersionViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int viewType) {
View row = mInflater.inflate(R.layout.row_versions_rv, viewGroup, false);
return new VersionViewHolder(row, mListener);
}
#Override
public void onBindViewHolder(#NonNull VersionViewHolder versionHolder, int rowNumber) {
versionHolder.thisVersion = mVersionList.get(rowNumber);
versionHolder.versionButton.setText((mVersionList.get(rowNumber).getText()));
Log.w("THIS IS A TAG", mVersionList.get(rowNumber).getText()); //correct output
}
#Override
public int getItemCount() {
if(mVersionList!=null) return mVersionList.size();
else return 0;
}
public void setElementList(List<Version> versions){
this.mVersionList = versions;
notifyDataSetChanged();
}
public static class VersionViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener
{
OnViewClickListener listenerForThisRow;
Version thisVersion;
Button versionButton;
public VersionViewHolder(
#NonNull View viewForThisRow, OnViewClickListener listenerFromActivity)
{
super(viewForThisRow);
this.listenerForThisRow = listenerFromActivity;
versionButton = viewForThisRow.findViewById(R.id.version_button);
viewForThisRow.setOnClickListener(this);
}
#Override
public void onClick(View v){
listenerForThisRow.onViewClick(thisVersion);
}
}
public interface OnViewClickListener{
void onViewClick(Version version);
}
}
THE FRAGMENT CLASS WHICH HOSTS THE RECYCLER VIEW
public class VersionRecycler extends Fragment {
protected RecyclerView mRecycler;
protected VersionListAdapter mAdapter;
public VersionRecycler(){
super(R.layout.fragment_recycler_versions);
}
#Override
public View onCreateView(
#NotNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
) {
VersionViewModel mVersionViewModel = new VersionViewModel(requireActivity().getApplication());
long tutorialId = requireArguments().getLong("tutorialId");
View view = inflater.inflate(R.layout.fragment_recycler_versions, container, false);
mAdapter = new VersionListAdapter(requireActivity());
mRecycler = view.findViewById(R.id.versions_rv);
mRecycler.setLayoutManager(new LinearLayoutManager(view.getContext(), LinearLayoutManager.VERTICAL, false));
mVersionViewModel.getByTutorialId(tutorialId).observe(requireActivity(), versions->mAdapter.setElementList(versions));
mRecycler.setAdapter(mAdapter);
return view;
}
}
HOW THE FRAGMENT IS EMBEDDED IN THE ACTIVITY
<LinearLayout
android:id="#+id/layout_versions_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
FRAGMENT LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".versionrecycler.VersionRecycler">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/versions_rv"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="#layout/row_versions_rv"/>
</androidx.constraintlayout.widget.ConstraintLayout>
LAYOUT FOR RECYCLERVIEW ROW
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<Button
android:id="#+id/version_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You should also add Start and Bottom constraints:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".versionrecycler.VersionRecycler">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/versions_rv"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:listitem="#layout/row_versions_rv"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You could also change RecyclerView layout_height to wrap_content and add Start constraint only

Selecting a fragment to start with in ViewPager - Android

I am trying to make a Login screen where the Register, Login and Forgot Password screens are all fragments, placed like this:
Forgot Password | Login | Register
I found a simple video on YouTube showing me how to do this. The only issue I have is that when I start the Activity, it opens on the Forgot Password fragment since it is the first one, but I am wanting it to start in the middle, the Login fragment. Here is the code for the main activity. What should I change or add to this code or am I taking the wrong approach for what I am trying to achieve
Access.java
public class Access extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.access);
initializePaging();
}
private void initializePaging() {
List<Fragment> fragments = new Vector<>();
fragments.add(Fragment.instantiate(this, AccessForgotPassword.class.getName()));
fragments.add(Fragment.instantiate(this, AccessLogin.class.getName()));
fragments.add(Fragment.instantiate(this, AccessRegister.class.getName()));
PagerAdapter mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);
ViewPager accessViewPager = (ViewPager) findViewById(R.id.accessViewPager);
accessViewPager.setAdapter(mPagerAdapter);
}
}
access.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/accessMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".access.Access">
<android.support.v4.view.ViewPager
android:id="#+id/accessViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
AccessForgotPassword.java
public class AccessForgotPassword extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.access_forgot_password, container, false);
}
}
access_forgot_password.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/accessForgotPasswordMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FF0000">
</LinearLayout>
AccessLogin.java
public class AccessLogin extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.access_login, container, false);
}
}
access_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/accessLoginMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00">
</LinearLayout>
AccessRegister.java
public class AccessRegister extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.infalte(R.layout.access_register, container, false);
}
}
access_register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/accessRegisterMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#0000FF">
</LinearLayout>
Set
accessViewPager.setCurrentItem(1);
Try adding accessViewPager.setCurrentItem(1) in end of onCreate()
Try to set accessViewPager.setCurrentItem(1); in the onCreate() of your activity.
Just after setting adapter you can set manually set the second fragment to view pager
accessViewPager.setCurrentItem(1); // 0= ForgotPassword, 1=LoginScreen,...so on

Viewpager with listview inside

i want to create a viewpager with one layout that contains a listview but i'm always getting this error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.myapp/com.example.myapp.Join_activity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a
null object reference
The viewpager works fine without the listview, and the listview works fine without the viewpager. I know the problem, i'm using "setContentView" with a different layout where my listview is, but i don't know how to solve it, please help, here is the code:
public class Join_activity extends AppCompatActivity {
ListView lv_zona;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.join_pager);//here is the problem
//my listview is in join3.xml
//ViewPager
viewPager = (ViewPager) findViewById(R.id.viewpaginador);
viewPager.setAdapter(new Join_Adapter(this));
//ListView
lv_zona = (ListView)findViewById(R.id.lv_zona);
lv_zona.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,new String[]{"A1","B1","C1","D1"}));
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
#Override
public void onPageSelected(int position) {}
#Override
public void onPageScrollStateChanged(int state){}
});
}//end oncreate
}//end class
*
public class Join_Adapter extends PagerAdapter {
private Context mContext;
public Join_Adapter(Context context) {
mContext = context;
}
#Override
public Object instantiateItem(ViewGroup coleccion, int posicion) {
ModelObject modelObject = ModelObject.values()[posicion];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup mlayout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), coleccion, false);
coleccion.addView(mlayout);
return mlayout;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view)
{collection.removeView((View) view);}
#Override
public int getCount()
{return ModelObject.values().length;}
#Override
public boolean isViewFromObject(View view, Object object)
{return view == object;}
#Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
}
My files:
My_files.jpg
My xmls:
***join3.xml - Here is my ListView***
<?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
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_zona"
android:choiceMode="singleChoice" />
</LinearLayout> </LinearLayout>
*
***join_pager.xml - my main layout***
<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"
tools:context=".Paginador">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpaginador"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
You should use fragments for this. You are using a view pager. Now you should display the list inside a fragment that you will load in the activity. This answer shows how to do that. Your fragment xml will contain the listView. And the getItem of the adapter will return the fragment.
The reason of NullPointerException is the list activity is not in the main layout, so, findViewById returns null.
To put a listview in a ViewPager, see: https://stackoverflow.com/a/12535150/189961

How can I add a ViewPager to a Navigation Drawer Item in Android application?

I want to add a viewPager for main fragment in my android app.
This is the xml file for main fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
This is the Java code for Main Fragment
public class MainFragment extends ListFragment {
private static final String TAG = MainFragment.class.getSimpleName();
private StudentsDBHandler studentsDBHandler;
public MainFragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
studentsDBHandler = new StudentsDBHandler(getActivity());
View rootView = inflater.inflate(R.layout.main_fragment, container, false);
displayStudents();
return rootView;
}
private void displayStudents() {
List<Student> studentList = studentsDBHandler.getAll();
ArrayAdapter<Student> list = new ArrayAdapter<>(getActivity(), R.layout.item_student, studentList);
setListAdapter(list);
}
}
You have a DrawerLayout page. It's about your drawer contains.
İn the DrawerLayout XML
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
add this code below
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
write this code and setup your View pager like example
viewPager=(ViewPager)findViewById(R.id.viewPager);
FragmentManager fm=getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fm));
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if(position==0)
fragment= new Home1();
if(position==1)
fragment=new Home2();
return fragment;
}
#Override
public int getCount() {
return 2;
}
}
}
I hope Help you

Fragments don't work in app

I have question, because I do not know what is wrong. Does not display the activity of activity_main_fragment.xml me and I do not know why. Do you have any suggestions what to change?
I created two fragments fragment_a and fragment_b and I want to display them connected to the fragment_a when you click on an item in a listview information came to me in fragment_b.
Communicator.java
public interface Communicator {
public void respond(int i);
}
strings.xml
<string-array name="titles">
<item>Zmiany</item>
<item>Ilę</item>
<item>Trudne pytania</item>
</string-array>
<string-array name="descriptions">
<item>Zmianysadas</item>
<item>Ilęvcxcvcx</item>
<item>Trudne pytaxcvcxvxcnia</item>
</string-array>
fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFBB00" >
<ListView
android:id="#+id/listView22"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
fragment_b.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent"
android:background="#99CC00">
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
activity_main_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/fragment1"
android:name="com.odpad.odpadygdansk.FragmentA"
android:layout_width="150dp"
android:layout_height="match_parent" />
<fragment
android:id="#+id/fragment2"
android:name="com.odpad.odpadygdansk.FragmentB"
android:layout_width="470dp"
android:layout_height="match_parent" />
</LinearLayout>
FragmentA.java
public class FragmentA extends Fragment implements AdapterView.OnItemClickListener{
ListView list;
Communicator communicator;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
communicator = (Communicator) getActivity();
list = (ListView) getActivity().findViewById(R.id.listView22);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.titles, android.R.layout.simple_list_item_1);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
return inflater.inflate(
R.layout.fragment_a, container, false);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
communicator.respond(i);
}
}
FragmentB.java
public class FragmentB extends Fragment{
TextView text;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
text = (TextView) getActivity().findViewById(R.id.textView);
return inflater.inflate(
R.layout.fragment_b, container, false);
}
public void changeData(int i)
{
Resources res = getResources();
String[] descriptions = res.getStringArray(R.array.descriptions);
text.setText(descriptions[i]);
}
}
EcologicalInformationActivity.java
public class EcologicalInformationActivity extends FragmentActivity implements Communicator{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_fragment);
}
#Override
public void respond(int i){
FragmentManager manager = getFragmentManager();
FragmentB f2 = (FragmentB) manager.findFragmentById(R.id.fragment2);
f2.changeData(i);
}
}
EcologicalInformationActivity extends FragmentActivity so you should call getSupportFragmentManager() in place of getFragmentManager(). See documentation here. If your fragments are not already instances of the support library version, you'll need to change that too.

Categories

Resources