Issue
I'm currently working on a mobile app as a final project in my course. I have this screen that has TabLayout inside in it. My problem is they have the same tab name. I want them to have different names. Where should I change the
ADAPTER
package com.example.biowit;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
public class LoginAdapter extends FragmentStateAdapter {
public LoginAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
public LoginAdapter(@NonNull Fragment fragment) {
super(fragment);
}
public LoginAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch (position){
case 0:
LoginTabFragment loginTab = new LoginTabFragment();
return loginTab;
case 1:
SignupTabFragment signupTab = new SignupTabFragment();
return signupTab;
default:
return null;
}
}
@Override
public int getItemCount() { return 2; }
}
SCREEN
package com.example.biowit;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
public class
LoginScreen extends AppCompatActivity {
TabLayout tabLayout;
ViewPager2 viewPager;
float v=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);
tabLayout = findViewById(R.id.tab_layout);
viewPager = findViewById(R.id.view_pager);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
LoginAdapter login_adapter = new LoginAdapter(this);
viewPager.setAdapter(login_adapter);
new TabLayoutMediator(tabLayout, viewPager,
new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText("LOGIN");
}
}).attach();
tabLayout.setAlpha(v);
}
}
Solution
Add your desired tab name in onConfigureTab
method:
new TabLayoutMediator(tabLayout, viewPager,
new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
switch(position) {
case 0:
tab.setText("LOGIN");
break;
case 1:
tab.setText("SIGNUP");
break;
}
}
}).attach();
Answered By - Sdghasemi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.