fragments basics 2
activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="200dp">
</FrameLayout>
<LinearLayout
android:layout_below="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="3">
<Button
android:id="@+id/bt_first_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Red"/>
<Button
android:id="@+id/bt_second_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Green"/>
<Button
android:id="@+id/bt_third_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Blue"/>
</LinearLayout>
</RelativeLayout>
fragment_blue_layout.xml
fragment_blue_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="@+id/fl_blue_fragment"
tools:context=".views.fragments.RedFragment">
</FrameLayout>
fragment_green_layout.xml
fragment_green_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="@+id/fl_green_fragment"
tools:context=".views.fragments.RedFragment">
</FrameLayout>
fragment_red_layout.xml
fragment_red_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="@+id/fl_red_fragment"
tools:context=".views.fragments.RedFragment">
</FrameLayout>
BlueFragment
BlueFragment
public class BlueFragment extends Fragment {
private FrameLayout mFLRootLayout;
public BlueFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View redFragmentView = inflater.inflate(R.layout.fragment_blue_layout, container, false);
mFLRootLayout = redFragmentView.findViewById(R.id.fl_blue_fragment);
mFLRootLayout.setBackgroundColor(getResources().getColor(R.color.blue));
return redFragmentView;
}
}
GreenFragment
GreenFragment
public class GreenFragment extends Fragment {
private FrameLayout mFLRootLayout;
public GreenFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View redFragmentView = inflater.inflate(R.layout.fragment_green_layout, container, false);
mFLRootLayout = redFragmentView.findViewById(R.id.fl_green_fragment);
mFLRootLayout.setBackgroundColor(getResources().getColor(R.color.green));
return redFragmentView;
}
}
RedFragment
RedFragment
public class RedFragment extends Fragment {
private FrameLayout mFLRootLayout;
public RedFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View redFragmentView = inflater.inflate(R.layout.fragment_red_layout, container, false);
mFLRootLayout = redFragmentView.findViewById(R.id.fl_red_fragment);
mFLRootLayout.setBackgroundColor(getResources().getColor(R.color.red));
return redFragmentView;
}
}
MainActivity
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@BindView(R.id.bt_first_button) Button mBTFirstButton;
@BindView(R.id.bt_second_button) Button mBTSecondButton;
@BindView(R.id.bt_third_button) Button mBTThirdButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mBTFirstButton.setOnClickListener(this);
mBTSecondButton.setOnClickListener(this);
mBTThirdButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_first_button:
firstButtonClicked();
break;
case R.id.bt_second_button:
secondButtonClicked();
break;
case R.id.bt_third_button:
thirdButtonClicked();
break;
}
}
private void firstButtonClicked() {
FragmentUtility.showFragmentInContainer(this, new RedFragment(), R.id.fl_container);
}
private void secondButtonClicked() {
FragmentUtility.showFragmentInContainer(this, new GreenFragment(), R.id.fl_container);
}
private void thirdButtonClicked() {
FragmentUtility.showFragmentInContainer(this, new BlueFragment(), R.id.fl_container);
}
}
build.gradle(Moduel: app)
build.gradle(Moduel: app)
dependencies {
//----------
//--------
// butter knife
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}