简体   繁体   中英

How to change Button drawable tint color on button click inside fragment

I am trying to change the button drawable tint color on button click to the color i want it is not possible on click to change please help I want it to be button not ImageButton.

here below i will post the codes I am using a tab layout where the two tabs are oneway and return fragments. I am having the problem in the onewayfragment . where when selecting the type from(economy,business and first class) i need when i select on economy button the drawable in the economy button should change to the color i want.

1) HomePage

2) HomeAdapter

3) OneWayFragment

4) ReturnFragment

当我选择经济按钮时,这就是我想要做的,经济按钮内的可绘制应将可绘制色调更改为我想要的颜色

here below is the coding of the files i have used

HomePage.java

package com.example.gomerry.Home_Page;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.TextView;
import androidx.viewpager.widget.ViewPager;
import com.example.gomerry.Menu;
import com.example.gomerry.R;
import com.google.android.material.tabs.TabLayout;

public class HomePage extends Menu {


TextView date;
TextView month;
TextView dayofWeek,day;
DatePickerDialog datePickerDialog1;
DatePickerDialog datePickerDialog2;
Adapter adapter;


TabLayout tabLayout;
ViewPager viewPager;

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

    tabLayout=(TabLayout)findViewById(R.id.tabLayout);
    viewPager=(ViewPager)findViewById(R.id.viewPager);

    tabLayout.addTab(tabLayout.newTab().setText("One Way"));
    tabLayout.addTab(tabLayout.newTab().setText("Return"));
    // tabLayout.addTab(tabLayout.newTab().setText("Movie"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final HomeAdapter adapter = new HomeAdapter(this,getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);

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



    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
}
}//end of code

This is the HomeAdapter.java

package com.example.gomerry.Home_Page;

import android.content.Context;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.example.gomerry.CancelBooking.CancellationFragment;
import com.example.gomerry.CancelBooking.ChargesFragment;

public class HomeAdapter extends FragmentPagerAdapter {

private Context myContext;
int totalTabs;

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

// this is for fragment tabs
@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            OneWayFragment oneWayFragment = new OneWayFragment();
            return oneWayFragment;
        case 1:
            ReturnFragment returnFragment = new ReturnFragment();
            return returnFragment;

        default:
            return null;
    }
}
// this counts total number of tabs
@Override
public int getCount() {
    return totalTabs;
}
}//end of code

This is the OneWayFragment.java

package com.example.gomerry.Home_Page;

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.example.gomerry.R;

public class OneWayFragment extends Fragment {


Button economy;

public OneWayFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
   // return inflater.inflate(R.layout.one_way_fragment, container, false);

    Log.d("link_fly",Data_URL);
    final View view = inflater.inflate(R.layout.one_way_fragment, container, false);

    economy =(Button) view.findViewById(R.id.button11);

    economy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            economy.setTextColor(R.color.cyan92a6);
            ImageButton button = (ImageButton) this.findViewById(R.id.button_i_want_to_modify);
            economy.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint
            economy.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.cyanBcD4), PorterDuff.Mode.MULTIPLY);
            economy.setCompoundDrawableTintMode();
            binding.myButton.compoundDrawables.first().setTint(Color.RED);
            view.economy.compoundDrawables.first().setTint(Color.RED);
            economy.compoundDrawables.first().setTint(Color.RED);
            economy.getBackground().setColorFilter();
        }
    });

    return view;
}
}//end of code

this is the button xml

这是按钮xml

this is the image of codes i have tried and i am getting errors and which is not working and i have not tried all at once i am trying one by one in this image i have uncommented all to show that they are showing errors.
在此处输入图片说明

I hope this is a clear explanation of the code :) and the problem i am having. please help me to solve this error. Thanks in advance

To change the drawable of your Button, you have to know a few things. Apparently, you can have more than one compound drawable on your button, but if you have only one you can do it like this

    Button economy = findViewById(R.id.button11)
    economy.setOnClickListener {
        economy.compoundDrawables.first().setTint(Color.RED)
    }

As You can see compundDrawables returns a list of drawables, so if You want to edit only one, you can just call first() and then you can set the tint accordingly to Your needs. Cheers!

您可以使用setBackgroundTintList()方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM