[go: up one dir, main page]

0% found this document useful (0 votes)
46 views7 pages

Demo Viewer Paper N Navigation

The document outlines the XML layout and Java code for an Android application featuring a ViewPager with three fragments: Person, Home, and Settings. It includes the creation of vector assets for icons and a bottom navigation menu to switch between fragments. The MainActivity manages fragment transitions and updates the action bar title based on the selected fragment.

Uploaded by

Kaito Chan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views7 pages

Demo Viewer Paper N Navigation

The document outlines the XML layout and Java code for an Android application featuring a ViewPager with three fragments: Person, Home, and Settings. It includes the creation of vector assets for icons and a bottom navigation menu to switch between fragments. The MainActivity manages fragment transitions and updates the action bar title based on the selected fragment.

Uploaded by

Kaito Chan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

<?xml version="1.0" encoding="utf-8"?

>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_checked="false" />
<item android:color="@color/yellow" android:state_checked="true"/>
</selector>

Tạo 3 biểu tượng vector asset: home_white.xml, person_white.xml và setting_white.xml

<?xml version="1.0" encoding="utf-8"?>


<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/person"
android:title="Person"
android:icon="@drawable/person_white"/>
<item
android:id="@+id/home"
android:title="Home"
android:icon="@drawable/home_white"/>
<item
android:id="@+id/settings"
android:title="Settings"
android:icon="@drawable/settings_white"/>
</menu>
Fragment_first.xml
<?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"
android:background="#FFFFFF"
tools:context=".MainActivity">

<TextView
android:id="@+id/firstFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Person"
android:textColor="#43a047"
android:textSize="40sp"
android:textStyle="italic|bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Fragment_second.xml
<?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"
android:background="#FFFFFF"
tools:context=".MainActivity">

<TextView
android:id="@+id/secondFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:textColor="#43a047"
android:textSize="40sp"
android:textStyle="italic|bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Fragment_third.xml
<?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"
android:background="#DC6C6C"
tools:context=".MainActivity">

<TextView
android:id="@+id/secondFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Setting"
android:textColor="#43a047"
android:textSize="40sp"
android:textStyle="italic|bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/main"
android:theme="@style/Base.Theme.ViewPagerNNavigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation"/>

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#009688"
android:layout_alignParentBottom="true"
app:itemTextColor="@color/color_navigation"
app:itemIconTint="@color/color_navigation"
app:labelVisibilityMode="labeled"
app:menu="@menu/menu_navigation"
/>

</RelativeLayout>

ViewpageAdapter.java
package com.ptithcm.viewpagernnavigation;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

public class ViewpagerAdater extends FragmentStatePagerAdapter {

public ViewpagerAdater(@NonNull FragmentManager fm, int behavior) {


super(fm, behavior);
}

@NonNull
@Override
public Fragment getItem(int position) {

switch (position) {
case 0:
return new FirstFragment();
case 1:
return new SecondFragment();
case 2:
return new ThirdFragment();
}
return null;
}

@Override
public int getCount() {
return 3;
}
}

Mainactivity.java
package com.ptithcm.viewpagernnavigation;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

ViewPager mViewPager;
BottomNavigationView mBottomNavigationView;

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

mViewPager = findViewById(R.id.view_pager);
mBottomNavigationView = findViewById(R.id.bottom_navigation);

//getSupportActionBar().setBackgroundDrawable(new
ColorDrawable(getResources().getColor(R.color.colorAccent)));
ViewpagerAdater viewpagerAdater = new
ViewpagerAdater(getSupportFragmentManager(),

FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
mViewPager.setAdapter(viewpagerAdater);
mViewPager.setCurrentItem(0);

getSupportActionBar().setTitle("Person");

mViewPager.addOnPageChangeListener(new
ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position){
case 0:

mBottomNavigationView.getMenu().findItem(R.id.person).setChecked(true);
getSupportActionBar().setTitle("Person");
break;
case 1:

mBottomNavigationView.getMenu().findItem(R.id.home).setChecked(true);
getSupportActionBar().setTitle("Home");
break;
case 2:

mBottomNavigationView.getMenu().findItem(R.id.settings).setChecked(true);
getSupportActionBar().setTitle("Setting");
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {

}
});
mBottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.person:
mViewPager.setCurrentItem(0);
getSupportActionBar().setTitle("Person");
break;
case R.id.home:
mViewPager.setCurrentItem(1);
getSupportActionBar().setTitle("Home");
break;
case R.id.settings:
mViewPager.setCurrentItem(2);
getSupportActionBar().setTitle("Setting");
break;
}
return true;
}
});
}

You might also like