Change the text color of the menu when selecting an Android fragment

Asked 1 years ago, Updated 1 years ago, 106 views

https://github.com/neokree/MaterialTabs

What I want to implement is like the commonly used library Materialtabs.

It would be convenient to use the library, but I'm thinking about learning to implement it myself.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button btn[] = new Button[4];
    ViewPager viewPager = null;
    Thread thread = null;
    Handler handler = null;
    intp=0; //page number
    int v=1; //Screen Switching Bangee

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //viewPager
        viewPager = (ViewPager)findViewById(R.id.viewPager);
        MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(adapter);

        btn[0] = (Button)findViewById(R.id.btn_a);
        btn[1] = (Button)findViewById(R.id.btn_b);
        btn[2] = (Button)findViewById(R.id.btn_c);
        btn[3] = (Button)findViewById(R.id.btn_d);

        for(int i=0;i<btn.length; i++){
            btn[i].setOnClickListener(this);
        }
    }


    @Override
    public void onClick(View v) {

        switch(v.getId()){
            case R.id.btn_a:
                viewPager.setCurrentItem(0);
                Toast.makeText (this "A Button", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_b:
                viewPager.setCurrentItem(1);
                Toast.makeText (this "B button", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_c:
                viewPager.setCurrentItem(2);
                Toast.makeText (this "C Button", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_d:
                viewPager.setCurrentItem(3);
                break;
            default:
                break;

        }

    }
}

A simple menu was implemented using fragments and viewpagers.

But if you look at commercial applications, aren't there signs such as changing the color of letters when entering the fragment?

I'm trying to implement this, but I'm not sure.

I think I should use an animation, but how to check the status of entering the fragment is

I'm curious. Let's say setOnClickListener, I think there will be an animation only when I click...

I don't know much about Android because I'm a student studying.

If it takes a long time or is quite difficult

I'd appreciate it if you could tell me to use the library boldly

Have a nice day

android menu fragment

2022-09-22 20:43

1 Answers

I remember using TabLayout, ViewPager, Fragment.

If you want to do it the same way as above, I think the button will be the menu (tab).

Using selector on the button, I think you just need to change the drawble at a specific event.

Below is taken from the Android reference document.

H, Fragments also have a life cycle like activities, so you can check the entry status.

XML file saved at res/drawable/button.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

This layout XML applies the state list drawable to a Button:

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button" />


2022-09-22 20:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.