How do I make the titles of my viewpager multi-language?

0

I have a tabbed activity with 3 sections or viewpagers, and since the data type is charsequence it does not let me take the name from the string like this: @ string / conversionunidades so that it is multi-language. How do I make it multi-language? Here is a portion of my mainactivity code:

private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    /*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });*/

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    Bundle datos=getIntent().getExtras();
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.cambiar_color){

        return  true;
    }else if (id == R.id.acerca_de_la_app){
            ejecutarinfo();
        return  true;
    }else if (id == R.id.acerca_del_autor){
        ejecutarautor();
        return  true;
    }

    return super.onOptionsItemSelected(item);
}

public void ejecutarinfo(){

    Intent i=new Intent(this, info.class);
    startActivity(i);
}
public void ejecutarautor(){
    Intent i2=new Intent(this, autor.class);
    startActivity(i2);

}


/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        //return PlaceholderFragment.newInstance(position + 1);
        switch (position) {
            case 0:
                unidades unidades=new unidades();
                return unidades;
            case 1:
                bases bases=new bases();
                return bases;
            case 2:
                romanos romanos=new romanos();
                return romanos;
        }
        return null;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Conversor de Unidades";
            case 1:
                return "Conversor de Bases";
            case 2:
                return "Conversor Romano";
        }
        return null;
    }
}

}

    
asked by Johan Berrio 29.04.2018 в 04:30
source

2 answers

0

A string is a simple resource that is referenced by the value provided in the name attribute (not the name of the XML file).

  

Resource reference:

  • In Java: R.string.string_name
  • In XML: @ string / string_name
  

This application code retrieves a string:

String string = getString(R.string.hello);

You can use getString(int) or getText(int) to get a string. getText(int) will retain all rich text style applied to the string.

  

Link to documentation: String Resources

Capture:

    
answered by 29.04.2018 / 08:39
source
1

In the CharSequence getPageTitle method change return "Conversor de Unidades"; by return getString(R.string.tu_string); or by return getResources().getString(R.string.tu_string); and so on the others case .

In java you can access the string in ress values-strings.xml

The way you mention @string/conversionunidades is for layouts ( xml ).

    
answered by 29.04.2018 в 05:49