0

So, I'm following the steps in the Android API to add login to my app. My app is already fairly developed, and of course I'm no to android, so I'm having issues at this point.

the step reads:

Then set up the button in your UI by adding it to a fragment and update your activity to use your fragment.

I've searched for how to add a fragment and anything about that, watched youtube videos, but can't find out exactly what this means. Can anyone dumb this down for me?

https://developers.facebook.com/docs/facebook-login/android

ntgCleaner
  • 5,865
  • 9
  • 48
  • 86

1 Answers1

0

First, you need a hosting activity, extending AppCompatActivity that has a FrameLayout in its related layout file. Then you inflate the layout in the onCreate method and add the needed Fragment:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        if (findViewById(R.id.fragment_placeholder) != null) {
            if (savedInstanceState != null) {
                 return;
            }

            // we are extending AppCompatActivity, so we need supportFragmentManager 
            getSupportFragmentManager().beginTransaction()
                 .add(R.id.fragment_placeholder, new ContentFragment()).commit();
        }
    }
}

The according layout file activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_placeholder"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>`

Now you need to define ContentFragment:

public class RecordFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragment_content, container, false);
    }
}

The last step is the layout file for our fragment, fragment_content, containing a Button:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

For more information why we use AppCompatActivity, read this answer.

Community
  • 1
  • 1
marktani
  • 7,578
  • 6
  • 37
  • 60