1

I am following a youtube tutorial on a login/signup screen using ViewPager and tablayout. Unfortunately, both aren't working. I would very much appreciate it if you could help me find the solution for this.

activity_log_in.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"
tools:context=".LogIn">

<ImageView
    android:id="@+id/imageview2"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:scaleType="centerCrop"
    android:src="@drawable/login_background"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_percent=".27"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:background="@drawable/vie_bg"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_percent=".78"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent=".7"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tab_layout"
        app:layout_constraintVertical_bias="0" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0"
        />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_google"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/white"
        android:elevation="35dp"
        android:src="@drawable/google"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/view_pager"
        app:tint="@null" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_fb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:backgroundTint="@color/white"
        android:elevation="35dp"
        android:src="@drawable/facebook"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/fab_google"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/view_pager"
        app:tint="@null" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_twitter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:backgroundTint="@color/white"
        android:elevation="35dp"
        android:src="@drawable/twitter"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toEndOf="@id/fab_google"
        app:layout_constraintTop_toBottomOf="@id/view_pager"
        app:tint="@null" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />





</androidx.constraintlayout.widget.ConstraintLayout>

<ImageView
    android:id="@+id/logo"
    android:layout_width="70dp"
    android:layout_height="70dp"
    app:layout_constraintVertical_bias=".3"
    android:src="@drawable/logo"
    app:layout_constraintBottom_toTopOf="@+id/constraintLayout"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@id/imageview2" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Welcome to KoralGel!"
    android:textColor="@color/black"
    android:fontFamily="@font/advent_pro_semibold"
    android:textSize="30sp"
    app:layout_constraintVertical_bias=".2"
    app:layout_constraintBottom_toTopOf="@id/constraintLayout"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/logo"/>


</androidx.constraintlayout.widget.ConstraintLayout>

Login.java:

package com.example.koralgel;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;

public class LogIn extends AppCompatActivity {

TabLayout tabLayout;
ViewPager viewPager;
FloatingActionButton fb, google, twitter;
float v=0;

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

    tabLayout=findViewById(R.id.tab_layout);
    viewPager= findViewById(R.id.view_pager);
    fb= findViewById(R.id.fab_fb);
    google= findViewById(R.id.fab_google);
    twitter= findViewById(R.id.fab_twitter);

    tabLayout.addTab(tabLayout.newTab().setText("Login"));
    tabLayout.addTab(tabLayout.newTab().setText("Signup"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final LoginAdapter adapter= new LoginAdapter(getSupportFragmentManager(),this,tabLayout.getTabCount());

    viewPager.setAdapter(adapter);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    fb.setTranslationY(300);
    google.setTranslationY(300);
    twitter.setTranslationY(300);
    tabLayout.setTranslationY(300);

    fb.setAlpha(v);
    google.setAlpha(v);
    twitter.setAlpha(v);
    tabLayout.setAlpha(v);

    fb.animate().translationY(0).setDuration(1000).setStartDelay(400).start();
    google.animate().translationY(0).setDuration(1000).setStartDelay(600).start();
    twitter.animate().translationY(0).setDuration(1000).setStartDelay(800).start();
    tabLayout.animate().translationY(0).setDuration(1000).setStartDelay(100).start();


}
}

LoginAdapter.jave:

package com.example.koralgel;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;

public class LoginAdapter extends FragmentStatePagerAdapter {

private Context context;
int totalTabs;

public LoginAdapter(FragmentManager fm, Context context, int totalTabs) {
    super(fm);
    this.context = context;
    this.totalTabs = totalTabs;
}

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

public Fragment getItem(int position) {
    switch (position) {
        case 0:
            login_tab_fragment LogInFragment = new login_tab_fragment();
            return LogInFragment;
        case 1:
            signup_tab_fragment SignupFragment = new signup_tab_fragment();
            return SignupFragment;
        default:
            return null;

    }
}

}

login_tab_fragment.java:

public class login_tab_fragment extends Fragment {

EditText email,pass;
Button login;
TextView forgotPass;
float v=0;

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

    email= root.findViewById(R.id.email);
    login= root.findViewById(R.id.button);
    pass= root.findViewById(R.id.password);
    forgotPass= root.findViewById(R.id.forgotPass);


    email.setTranslationY(300);
    login.setTranslationY(300);
    pass.setTranslationY(300);
    forgotPass.setTranslationY(300);

    email.setAlpha(v);
    login.setAlpha(v);
    pass.setAlpha(v);
    forgotPass.setAlpha(v);

    email.animate().translationY(0).setDuration(800).setStartDelay(300).start();
    pass.animate().translationY(0).setDuration(800).setStartDelay(500).start();
    forgotPass.animate().translationY(0).setDuration(800).setStartDelay(500).start();
    login.animate().translationY(0).setDuration(800).setStartDelay(700).start();


    return root;
}
}

signup_tab_fragment.java:

public class signup_tab_fragment extends Fragment {

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

    return root;
}
}

The outcome is just a blank ViewPager and tablayout. Please help me!

rita
  • 11
  • 1

0 Answers0