-2

I had a problem I didn't know how to change colors in Java
I want to change the text color At the end of the code in the Calculation result The app is an application that tries to calculate body mass(bmi) I don't know what codes to use to change the color of writing this is my java code and xml code can I get help plz

java code

package com.example.bmicalculator;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


//Main activity class start here
public class MainActivity extends Activity {

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

// Get the references to the widgets
        final EditText e1 = (EditText) findViewById(R.id.et1);
        final EditText e2 = (EditText) findViewById(R.id.et2);
        final TextView tv4 = (TextView) findViewById(R.id.tv4);

        findViewById(R.id.ib1).setOnClickListener(new View.OnClickListener() {

            // Logic for validation, input can't be empty
            @Override
            public void onClick(View v) {

                String str1 = e1.getText().toString();
                String str2 = e2.getText().toString();

                if(TextUtils.isEmpty(str1)){
                    e1.setError("Please enter your weight");
                    e1.requestFocus();
                    return;
                }

                if(TextUtils.isEmpty(str2)){
                    e2.setError("Please enter your height");
                    e2.requestFocus();
                    return;
                }

//Get the user values from the widget reference
                float weight = Float.parseFloat(str1);
                float height = Float.parseFloat(str2)/1;

//Calculate BMI value
                float bmiValue = calculateBMI(weight, height);

//Define the meaning of the bmi value
                String bmiInterpretation = interpretBMI(bmiValue);

                tv4.setText(String.valueOf(bmiValue + "-" + bmiInterpretation));

            }
        });

    }

    //Calculate BMI
    private float calculateBMI (float weight, float height)
    {
        return (float) (weight / (height * height));
    }

    // her I want change color
    private String interpretBMI(float bmiValue)
    {

        if (bmiValue < 16) {
            return "Severely underweight" ;


        } else if (bmiValue < 18.5) {

            return "Underweight" ;
        } else if (bmiValue < 25) {

            return "Normal";
        } else if (bmiValue < 30) {

            return "Overweight";
        } else {
            return "Obese";
        }
    }

    }

xml code

<!-- Linear layout start here -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/pic"
    tools:context=".MainActivity">
    <!-- Text view for BMI Text -->

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="187dp">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="305dp"
            android:layout_height="93dp"
            android:layout_gravity="center"
            android:layout_marginStart="53dp"
            android:layout_marginLeft="53dp"
            android:layout_marginEnd="53dp"
            android:layout_marginRight="53dp"
            android:layout_marginBottom="30dp"
            android:fontFamily="@font/sss"
            android:paddingLeft="15dp"
            android:paddingTop="40dp"
            android:shadowColor="@android:color/black"
            android:shadowDx="4"
            android:shadowDy="4"
            android:text="1_MAC_Community"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            android:typeface="serif"
            app:layout_constraintBottom_toTopOf="@+id/textView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="MissingConstraints" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="64dp"
            android:layout_marginLeft="64dp"
            android:layout_marginTop="30dp"
            android:layout_marginEnd="33dp"
            android:layout_marginRight="33dp"
            android:text="plz Fill YOUR  information"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            android:typeface="serif"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv1"
            tools:ignore="MissingConstraints" />

    </android.support.constraint.ConstraintLayout>

    <TextView
        android:id="@+id/tv9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingTop="30dp"
        android:text="WEIGHT (KG)"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/white"
        android:textStyle="bold|italic"
        android:typeface="serif" />

    <!-- Edit text for entering weight with hint in kgs -->
    <EditText
        android:id="@+id/et1"
        android:layout_width="314dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ems="10"
        android:fadingEdgeLength="10dp"
        android:hint="IN KGs"
        android:inputType="numberDecimal"
        android:scrollbarTrackHorizontal="@android:color/background_dark"
        android:textAlignment="center"
        android:textColorHighlight="#000000"
        android:textColorLink="#00FFFFFF"
        android:textCursorDrawable="@android:color/background_dark">

        <requestFocus />
    </EditText>

    <!-- Text view for HEIGHT(CM)text -->
    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingTop="30dp"
        android:text="HEIGHT (Meter)"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/white"
        android:textStyle="bold|italic"
        android:typeface="serif" />

    <!-- Edit text for entering height with hint in cm -->
    <EditText
        android:id="@+id/et2"
        android:layout_width="281dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ems="10"
        android:hint="IN Meter"
        android:textAlignment="center"
        android:inputType="numberDecimal"></EditText>

    <!-- Button for calculating the formula, when pressed, with calculate written over it -->
    <Button
        android:id="@+id/ib1"
        android:layout_width="158dp"
        android:layout_height="51dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:fadingEdge="vertical"
        android:longClickable="true"
        android:nextFocusRight="@color/colorPrimaryDark"
        android:text="Calculate MY BMI"
        android:visibility="visible" />

    <!-- Text view for showing result -->
    <TextView
        android:id="@+id/tv4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingTop="20dp"
        android:text=""
        android:textSize="20dp"
        android:textStyle="bold"
        android:textColor="@android:color/holo_orange_dark"/>

</LinearLayout>
    <!-- Linear layout ends here -->
MohamedDev
  • 19
  • 1
  • 9

3 Answers3

3

first remove final keyword from your TextView Object

this code

final TextView tv4 = (TextView) findViewById(R.id.tv4);

Replace

TextView tv4 = (TextView) findViewById(R.id.tv4);

then you can set color

you can write hex code

tv4.setTextColor("#ffffff");

or yuu can write color name

tv4.setTextColor(Color.WHITE);

or you can call it from color.xml file

tv4.setTextColor(getResources().getColor(R.color.your_colour));
Kamesh kumar
  • 114
  • 2
  • 10
  • you need to remove final keyword from TextView object if you want to change something in textview, i update my answer please check it – Kamesh kumar Sep 08 '19 at 05:08
1

Quite simple

tv4.setTextColor(Color.RED);

Or if you have a speific colour resource

tv4.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.your_colour));

tv4.setTextColor(getResources().getColor(R.color.your_colour));

Update:

Decare your view elements globally outside of onCreate

public class MainActivity extends Activity {

private EditText e1;
private EditText e2;
private TextView tv4;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...
    e1 = (EditText) findViewById(R.id.et1);
    e2 = (EditText) findViewById(R.id.et2);
    tv4 = (TextView) findViewById(R.id.tv4);
}
JakeB
  • 2,043
  • 3
  • 12
  • 19
0

Its Quite easy

1)Find Reference to Text View of which you want to change color

2)Use Method

MytextView.setColor(Color.BLUE);

Or if you have defined your custom color in colors.xml file

MytextView.setColor(getResourses().getColor(R.color.Mycolor));
Yousaf Raza
  • 703
  • 3
  • 11
  • textview here means tv4 and if it says can't resolve symbol so how come your setText function is working tv4.setText(String.valueOf(bmiValue + "-" + bmiInterpretation)); – Yousaf Raza Sep 08 '19 at 21:06