Reputation: 3928
I have a chain of 8 buttons I've positioned as a side menu, where on their right there is a fragment container. Clicking on each button opens a different fragment.
This is done with an onClick listener and a when statement.
What I want to do is that other than changing the fragment in the box, I want the background for each button to change when it is clicked, and return to default when another one is clicked. What is the best way to go around doing that?
Can I call multiple actions for each case in the when statement? Do I need for each button clicked to set its background and the background for the other 7? It seems like too much code.
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onClick(v: View?) {
when (v?.id){
R.id.recipeOneButton -> createRecipeOne()
R.id.recipeTwoButton -> createRecipeTwo()
R.id.recipeThreeButton -> createRecipeThree()
R.id.recipeFourButton -> createRecipeFour()
R.id.recipeFiveButton -> createRecipeFive()
R.id.recipeSixButton -> createRecipeSix()
R.id.recipeSevenButton -> createRecipeSeven()
R.id.recipeEightButton -> createRecipeEight()
}
}
private val manager = this.supportFragmentManager!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
createRecipeOne()
val recipeOneButton : Button = findViewById(R.id.recipeOneButton)
val recipeTwoFragment : Button = findViewById(R.id.recipeTwoButton)
val recipeThreeButton : Button = findViewById(R.id.recipeThreeButton)
val recipeFourButton : Button = findViewById(R.id.recipeFourButton)
val recipeFiveButton : Button = findViewById(R.id.recipeFiveButton)
val recipeSixButton : Button = findViewById(R.id.recipeSixButton)
val recipeSevenButton: Button = findViewById(R.id.recipeSevenButton)
val recipeEightButton : Button = findViewById(R.id.recipeEightButton)
recipeOneButton.setOnClickListener(this)
recipeTwoFragment.setOnClickListener(this)
recipeThreeButton.setOnClickListener(this)
recipeFourButton.setOnClickListener(this)
recipeFiveButton.setOnClickListener(this)
recipeSixButton.setOnClickListener(this)
recipeSevenButton.setOnClickListener(this)
recipeEightButton.setOnClickListener(this)
}
fun createRecipeOne(){
val transaction = manager.beginTransaction()
val fragment = RecipeOne()
transaction.replace(R.id.fragmentHolder, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
fun createRecipeTwo(){
val transaction = manager.beginTransaction()
val fragment = RecipeTwo()
transaction.replace(R.id.fragmentHolder, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
fun createRecipeThree(){
val transaction = manager.beginTransaction()
val fragment = RecipeThree()
transaction.replace(R.id.fragmentHolder, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
fun createRecipeFour(){
val transaction = manager.beginTransaction()
val fragment = RecipeFour()
transaction.replace(R.id.fragmentHolder, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
fun createRecipeFive(){
val transaction = manager.beginTransaction()
val fragment = RecipeFive()
transaction.replace(R.id.fragmentHolder, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
fun createRecipeSix(){
val transaction = manager.beginTransaction()
val fragment = RecipeSix()
transaction.replace(R.id.fragmentHolder, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
fun createRecipeSeven(){
val transaction = manager.beginTransaction()
val fragment = RecipeSeven()
transaction.replace(R.id.fragmentHolder, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
fun createRecipeEight(){
val transaction = manager.beginTransaction()
val fragment = RecipeEight()
transaction.replace(R.id.fragmentHolder, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
}
Upvotes: 2
Views: 1369
Reputation: 29844
Basically, you need to map button ids to Fragments.
To avoid the redundancy in your functions createRecipeX
, you should let your when
expression return the proper fragment, rather than calling a function:
override fun onClick(v: View) {
val fragment = when (v.id) {
R.id.recipeOneButton -> RecipeOne()
R.id.recipeTwoButton -> RecipeTwo()
// ...
else -> throw IllegalStateException()
}
val transaction = manager.beginTransaction()
transaction.replace(R.id.fragmentHolder, fragment)
transaction.addToBackStack(null)
transaction.commit()
// change button background
}
Then you can move all the code that they have in common below the when expression.
If the button background is unique for each button you would do it like this:
val fragment = when (v.id) {
R.id.recipeOneButton -> {
// change button background to specific color 1
RecipeOne()
}
R.id.recipeTwoButton -> {
// change button background to specific color 2
RecipeTwo()
}
// ...
else -> throw IllegalStateException()
}
Upvotes: 0
Reputation: 4087
you can do something like this in when
to change background of that button
when (v?.id){
R.id.recipeOneButton -> {
createRecipeOne()
changebackground(id)
}
R.id.recipeTwoButton -> {
createRecipeTwo()
changebackground(id)
}
}
in changebackground(id)
pass the id of that button for you want to change background .
For changing other buttons background . if you want to reduce the code . you have to do something like this .
private void changeColor(Button[] buttons){
for(int x=0; x < buttons.length; x++){
buttons[x].setBackgroundColor(Color.RED);
}
}
but for that , you have to initialise button like this
btns[0] = (Button) findViewById(R.id.recipeOneButton );
btns[1] = (Button) findViewById(R.id.recipeTwoButton );
Upvotes: 2