Try to make TabLayout in Android - java

I followed some tutorial to make a Tablayout in android,
but I have a problem.
FirstActivity.java :
package info.mandai.animegallery;
import com.example.animegallery.R;
import info.mandai.animegallery.adapter.TabsPagerAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
public class FirstActivity extends FragmentActivity implements -----> Error Here
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "Registrasi", "Login", "A" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
}
}
On the word "FirstActivity" I have this error:
they said "The type FirstActivity must implement the inherited abstract method ActionBar.TabListener.onTabSelected(ActionBar$Tab, FragmentTransaction)
What do I do to fix it?

add following code to your class
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// do something
}

Related

Add an Icon to tabs inside viewPager

I am trying to add icons to my 3 tabs inside ViewPager.
I could not find any solutions for that.
If any other solutions are available I am ready to change completly my code
Thanks a lot for your answers.
Hello everybody,
I am trying to add icons to my 3 tabs inside ViewPager.
I could not find any solutions for that.
If any other solutions are available I am ready to change completly my code
Thanks a lot for your answers.
package com.example.septime.locumatix.Activities;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.example.septime.locumatix.Fragments.GrosBonnetAnalyseFragment;
import com.example.septime.locumatix.Fragments.GrosBonnetExercicesFragment;
import com.example.septime.locumatix.Fragments.GrosBonnetPlusLoinFragment;
import com.example.septime.locumatix.R;
import java.util.ArrayList;
import java.util.List;
public class GrosBonnetActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grosbonnet);
Toolbar toolbar = findViewById(R.id.mytoolbar);
ViewPager viewPager = findViewById(R.id.viewpager);
setViewPager(viewPager);
TabLayout tabLayout = findViewById(R.id.mytabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setViewPager(ViewPager viewPager) {
GrosBonnetActivity.ViewPagerAdapter adapter = new GrosBonnetActivity.ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new GrosBonnetAnalyseFragment(), "Analyse");
adapter.addFragment(new GrosBonnetExercicesFragment(), "Exercices");
adapter.addFragment(new GrosBonnetPlusLoinFragment(), "Plus loin");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
It is already described here. All you need is method setCustomView.

When I call next fragment form current the tab selection will not change on Button click

I am trying to develop an android app in which I am going to include tab with fragment. Please find below code for Activity :
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class EnterActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(3).select();
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new EnterCodeFragment(), "ENTER CODE");
adapter.addFragment(new HistoryFragment(), "HISTORY");
adapter.addFragment(new HelpFragment(), "HELP");
adapter.addFragment(new LogoutFragment(), "LOGOUT");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Below logic I used to call other fragment on button click.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_main, container, false);
Button loginButton = (Button)v.findViewById(R.id.button);
loginButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Fragment fragment = new EnterCodeFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.activity_main,fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
// ViewPagerAdapter.get
}
});
return v;
}
When I click on button it will move to the next fragment but tab selection will not change. I think it is creating a new fragment. Any help would be appreciated.
I think you not initialize fragment properly update like above code
Button loginButton = (Button)v.findViewById(R.id.button);
loginButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
EnterCodeFragment fragment = new EnterCodeFragment(); // check this
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.activity_main,fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
// ViewPagerAdapter.get
}
});

Addfrag is not accepting for more than one

import java.util.ArrayList;
import java.util.List;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.hrm.toolbar.AddressToolbar;
import com.hrm.toolbar.EmergencyToolbar;
public class ToolbarMenu extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toolbar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new AddressToolbar(), "Address");
adapter.addFrag(new EmergencyToolbar(), "Emergency");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
I am getting the following error
The method addFrag(Fragment, String) in the type ToolbarMenu.ViewPagerAdapter is not applicable for the arguments (AddressToolbar, String)
AddressToolbar
public class AddressToolbar extends Fragment {
public AddressToolbar() {
// Required empty public constructor
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.address_toolbar, container, false);
}
}

pager Cannot resolved as a variable

I'm trying to make action bar tabs, but I got a error:"pager Cannot resolved as a variable..."
Thansk for your help.
Java code;
package olcay.akgn.fibonaccicalculator;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity implements TabListener {
android.app.ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
actionBar=getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab1=actionBar.newTab();
tab1.setText("Hesaplayıcı");
tab1.setTabListener(this);
Tab tab2=actionBar.newTab();
tab2.setText("Bölünme");
tab2.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Log.d("AKGN","onTabReselected at "+" position"+tab.getPosition()+"name"+tab.getText());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Log.d("AKGN","onTabReselected at "+" position"+tab.getPosition()+"name"+tab.getText());
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Log.d("AKGN","onTabReselected at "+" position"+tab.getPosition()+"name"+tab.getText());
}
}
class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int arg0) {
Fragment fragment=null;
if(arg0==0){
fragment=new Hesapla();
}
if(arg0==0){
fragment=new Bolunme ();
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
Note; I begin this project with this video tutorial;
https://www.youtube.com/watch?v=iEl0ylVvZho
I tried to "Create "pager" in type id " but it did not solved my issue. Please help me.
You need to declare a ViewPager widget on your activity_main layout.
Never edit the R class - it is automatically generated.

Animation on Main Activity

I want to make animation on my main activity..
when my app is opened i want to open with animation ..
I have posted my main activity code and i want bounce animation while opening my pager xml.
import android.app.ActionBar;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class DesignMainActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_design_main);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
requestWindowFeature(Window.FEATURE_NO_TITLE);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
#Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Login").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Register").setTabListener(tabListener));
}
}
**My Questions :
1.I want bounce Animation While opening my app.
2.I want to hide title bar.**

Categories