-2

Hi I am given an assignment to create contents in the fragment basic layout. However having create a swipe activity with tab action bar, i am unable to edit the fragment layout within each given tab. Here is the codes as follow :

MainActivity.java : method i use to refer to the fragment layout
(created a default swipe activity with action tab)

package com.example.clement.testingfragments;


public class MainActivity extends AppCompatActivity {


 private SectionsPagerAdapter mSectionsPagerAdapter;
 private ViewPager mViewPager;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    // action bar
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    //manage the fractions
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction =
            fragmentManager.beginTransaction();
    fragmentTransaction.replace(android.R.id.content, new BlankFragment());
    fragmentTransaction.commit();


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if(getArguments().getInt(ARG_SECTION_NUMBER) == 1) {

            View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
            return rootView;
        }else {

            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;

        }
    }
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "SECTION 1";
            case 1:
                return "SECTION 2";
            case 2:
                return "SECTION 3";
        }
        return null;
    }
  }
}

I then created the BlankFragment.java : (Default creation of a fragment basic layout)

public class BlankFragment extends Fragment {



TextView mtv;
public BlankFragment() {
    // Required empty public constructor
}





@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    Log.i("hello","hello");

    View viewRoot = inflater.inflate(R.layout.fragment_blank, container, false);

        mtv = (TextView)viewRoot.findViewById(R.id.testy);
        mtv.setText("hello");
        return viewRoot;


     }

   }

The problem with this is that i am a unable to change textview that is inside of the fragment_blank.xml. i have research all across the internet and the suggestions given were to create void methods, using getActivity(), onViewCreated(), Onstart(). All of these have seem to not work.

i have included a if else loop to return a null under the condition that the method works, but it still runs without returning a null and i have no clue as to why this is happening.

My fragment_blank.xml:

<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.clement.testingfragments.BlankFragment">

<TextView
    android:id="@+id/testy"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

</FrameLayout>

Do let me know if more information is needed. I sincerely seek your suggestions and advice. Thanks in advance!

Cosq
  • 155
  • 1
  • 2
  • 12

1 Answers1

0

As per your question your textview is inside basicfragmentlayout.xml, while you are inflating fragment_blank inside your onCreateView. You need to change your code as below:

MyActivity.java

public class MyActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction =
                fragmentManager.beginTransaction();
        fragmentTransaction.replace(android.R.id.content, new MyFragment());
        fragmentTransaction.commit();
    }
}

MyFragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment extends Fragment {
    View myView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.fragment_my, container, false);

        TextView mtv = (TextView) myView.findViewById(R.id.testy);
        mtv.setText("hello");

        return myView;
    }
}

fragment_my.java

<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">

    <TextView
        android:id="@+id/testy"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="TempText" />

</FrameLayout>
phoenix
  • 496
  • 3
  • 11
  • oh damn so sorry i wrote down the wrong xml file for the question. it has been edited. Please do read again for it – Cosq Feb 19 '17 at 10:09
  • post your xml and remove unnecessary code from the question. – phoenix Feb 19 '17 at 10:11
  • Your code seems to be Okay for the purpose you want. The issue seems to be somewhere else. I've edited my answer that contains sample of what you want. You can post more of your code for further help. – phoenix Feb 19 '17 at 10:37
  • Alright thanks man it seems promising(: as I am away for the next few days I'll give an update asap! – Cosq Feb 19 '17 at 10:44
  • hey man i managed to try the codes u given me and i added the fragment manager into the main activity. i hope i did it right. From the looks of it the codes seem errorless but some how its not showing up the changers in the AVD. What else do u think could be the problem? i tried linking it to the layout page to another java class to interact with it and still nothing happens. – Cosq Feb 20 '17 at 10:21
  • OK. put a debug point inside your onCreateView method and check if the method is getting called and then debug it step by step if you are getting any error. – phoenix Feb 20 '17 at 10:41