How can i simplify this java Statement

java 8 multiple if conditions
java 8 if else alternative
simplify boolean expression java
if statement can be simplified
refactor switch statement design pattern java
replace if/else with strategy pattern
conditional statement in java
best way to format multiple 'or' conditions in an if statement (java)
private void clean(View v) {
    ColorDrawable color = new ColorDrawable(ContextCompat.getColor(this,R.color.white));
    if (v.getId() == R.id.nav_news){
        nav_feed_bg.setBackground(color);
        nav_profile_bg.setBackground(color);
        nav_chat_bg.setBackground(color);
        nav_books_bg.setBackground(color);
    } else if (v.getId() == R.id.nav_feed){
        nav_news_bg.setBackground(color);
        nav_profile_bg.setBackground(color);
        nav_chat_bg.setBackground(color);
        nav_books_bg.setBackground(color);
    } else if (v.getId() == R.id.nav_profile){
        nav_feed_bg.setBackground(color);
        nav_news_bg.setBackground(color);
        nav_chat_bg.setBackground(color);
        nav_books_bg.setBackground(color);
    } else if (v.getId() == R.id.nav_chat){
        nav_feed_bg.setBackground(color);
        nav_profile_bg.setBackground(color);
        nav_news_bg.setBackground(color);
        nav_books_bg.setBackground(color);
    } else if (v.getId() == R.id.nav_books){
        nav_feed_bg.setBackground(color);
        nav_profile_bg.setBackground(color);
        nav_chat_bg.setBackground(color);
        nav_news_bg.setBackground(color);
    }
}

How can i Simplify this Statement there are 5 Views If One View is Called We have to set white Background to Other Views with White Color Bg

Edit :

    @Override
    public void onClick(View v) {
        Drawable shadow;
        switch (v.getId()) {
            case R.id.nav_news:
                Utils.sToast(this, "NavBar Item Clicked 1");
                shadow = getDrawable(R.drawable.shadow_bg_nav);
                clean(v);
                nav_news_bg.setBackground(shadow);
                break;
            case R.id.nav_feed:
                Utils.sToast(this, "NavBar Item Clicked 2");
                shadow = getDrawable(R.drawable.shadow_bg_nav);
                clean(v);
                nav_feed_bg.setBackground(shadow);
                break;
            ... Other Statements Here
}

These May be Helpful for Answering I always try to Optimize my code but Some Statements like these makes Ugly Repetitive Blocks

Where do You set color for the selected item? is it possible to set all colors to white and then use switch to set only the selected one to desired color? It would make it at least more pleasant to look at.

nav_news_bg.setBackground(color);
nav_feed_bg.setBackground(color);
nav_profile_bg.setBackground(color);
nav_chat_bg.setBackground(color);
nav_books_bg.setBackground(color);

switch(v.getId()){
    case R.id.nav_news:
        nav_news_bg.setBackground(desiredColor);
        break;
    case R.id.nav_feed:
    .
    .
    .
}

After Your edit it would be best to drop any logic from clean and leave it only as

private void clean(View v) {
    ColorDrawable color = new ColorDrawable(ContextCompat.getColor(this,R.color.white));
    nav_news_bg.setBackground(color);
    nav_feed_bg.setBackground(color);
    nav_profile_bg.setBackground(color);
    nav_chat_bg.setBackground(color);
    nav_books_bg.setBackground(color);
}

because background color is set after this method is executed.

Simplifying Boolean Expressions and Conditionals, So the above statement is equivalent to the single line. etc., it is possible to simplify boolean expressions and conditional statements by using rules. Here we​  Another of the Java conditional statements, the switch statement, can be useful when a variety of different conditions might be true based on the value of a single char, integer or a String. With the roshambo application, the clientGesture String can take on the values of rock, paper or scissors, and depending upon which is selected, the

Since the crux of your logic is "set the background of all views except the one being called" Why not try

if (v.getId() != R.id.nav_news) nav_feed_news.setBackground(color);
if (v.getId() != nav_feed) nav_feed.setBackground(color);
// and so on for the rest.

How to Replace Many if Statements in Java, In this tutorial, we'll walk through the various ways of replacing nested if statements. Let's explore different options how we can simplify the code  For-each loop. If we’re using generics, then we’re using Java 5+, which means the for-each loop is also supported. We should use the enhanced for loop to eliminate the boring boilerplate code of using an explicit iterator. This makes the import statement for java.util.Iterator no longer needed.

Map<Integer, View> views = new HashMap();
public void MyClass() {
   init();
   views.put(R.id.nav_news, nav_news_bg);
   views.put(R.id.nav_profile, nav_profile_bg);
   // etc
}
private void clean(View v) {
    Colour colour = ... 
    for (Map.Entry entry : views) {
       if (!entry.getKey().equals(v.getId()) {
           entry.getValue().setbackground(color);
       } 
   } 
} 

Example of simplifying and improving code, This makes the import statement for java.util.Iterator no longer needed. Also, we take advantage of auto-unboxing to use the int type rather than  Java’s Selection statements: if; if-else; nested-if; if-else-if; switch-case; jump – break, continue, return; These statements allow you to control the flow of your program’s execution based upon conditions known only during run time. if: if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.

Simplification, It introduces no new Java programming features, but instead steps back to Obviously, changing the order of two statements doesn't simplify anything; but we​  We will see how to write such type of conditions in the java program using control statements. In this tutorial, we will see four types of control statements that you can use in java programs based on the requirement: In this tutorial we will cover following conditional statements: a) if statement b) nested if statement c) if-else statement

Unnecessarily complex boolean expression - Java queries, If A and B are expressions of boolean type, you can simplify them using the rewrites the value, for example by occurring as the condition in an if statement. Just as it is possible to simplify algebraic expressions by using rules like cancellation, commutativity, associativity, distributivity, etc., it is possible to simplify boolean expressions and conditional statements by using rules. Here we present the rules that you need to know.

Writing clean if else statements, Writing clean if else statements · Java | How to create your own Helper Class? Below shown example shows how to handle error cases and simplify the if else  if-then-else conditional statements let a Java program make simple decisions about what to do next. They work in the same logical way as we do when making decisions in real life.

Comments
  • would be better on codereview SE as there isn't actually an issue with it working
  • is your all views in same layout??
  • Is cleanV a typo and should refer to clean(View v), or is it other not relevant call?
  • sorry for that its a typo corrected it now it is because i was trying various answers so various methods created for that issue in java file without changing old method
  • May Be Not Recommended This can reduce the Complexity and Optimize my Code is there any possibility Optimize it some more with memory concent
  • From code You provided and assumin that cleanV() is in fact clean(), you cant drop the switch from clean and just use it for resetting everything to white, as setting the proper background is next call in line. Also think about making white color static, there is no need to create and garbage an instance for every click.
  • the above code is for custom navBar views here are linear layouts so when i click it holds backgound while selecting other menu can you get it