heyaa heyaa - 21 days ago 10
Android Question

dynamically set text in textView android

I have radio button group and spinner in my android activity.First, I choose a radio button, then I choose spinner item. I want to see selected spinner item in textView. However, when I change spinner item, textView does not update. It updates only when I change radio button. How can I update textView dynamically? I am trying to product my radio button and spinner and display on textView.

public class GenelBagis extends AppCompatActivity {
Spinner spinnerGenelBagis;
ArrayAdapter<String> dataAdapterForGenelBagis;
String[] aylar;
TextView mText;
int amount=1;

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

aylar = getResources().getStringArray(R.array.month);
spinnerGenelBagis = (Spinner)findViewById(R.id.spinner2);
dataAdapterForGenelBagis = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,aylar);
spinnerGenelBagis.setAdapter(dataAdapterForGenelBagis);
mText = (TextView)findViewById(R.id.textView16);






spinnerGenelBagis.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
RadioButton meslekLisesiButton = (RadioButton) findViewById(R.id.meslekLisesi);
RadioButton yuksekOgrenimButton = (RadioButton) findViewById(R.id.yuksekOgrenim);
RadioButton yuksekLisansButton = (RadioButton) findViewById(R.id.meslekLisesi);
RadioButton doktoraButton = (RadioButton) findViewById(R.id.meslekLisesi);

String text = spinnerGenelBagis.getSelectedItem().toString();
int month = Integer.parseInt(text);

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {



if (meslekLisesiButton.isChecked() == true) {
String amount = Integer.toString(month * 200);
mText.setText(amount);
} else if (yuksekOgrenimButton.isChecked() == true) {
//
} else if (yuksekLisansButton.isChecked() == true) {
//
} else if (doktoraButton.isChecked() == true) {
//

}

}
});
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
});




}}


xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.merve.tev.GenelBagis"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">



<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Burs Vermek İstediğiniz Toplam Tutar"
tools:layout_constraintTop_creator="1"
android:layout_marginTop="22dp"
app:layout_constraintTop_toBottomOf="@+id/spinner2"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="279dp"
android:layout_height="130dp"
android:checkedButton="@+id/meslekLisesi"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="@+id/spinner2"
android:layout_marginTop="92dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="92dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp">

<RadioButton
android:id="@+id/meslekLisesi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Meslek Lisesi (Aylık) 200 TL" />

<RadioButton
android:id="@+id/yuksekOgrenim"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

android:text="Yüksek Öğrenim (Aylık) 510 TL"
tools:layout_editor_absoluteX="42dp"
tools:layout_editor_absoluteY="169dp" />



<RadioButton
android:id="@+id/yuksekLisans"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

android:text="Yüksek Lisans (Aylık) 850 TL" />

<RadioButton
android:id="@+id/doktora"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

android:text="Doktora (Aylık) 1150 TL" />

</RadioGroup>

<Spinner
android:id="@+id/spinner2"
android:layout_width="310dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
tools:layout_constraintTop_creator="1"
android:layout_marginTop="26dp"
app:layout_constraintTop_toBottomOf="@+id/textView15"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />

<Button
android:id="@+id/devamEt2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:textSize="15sp"
android:text="Devam Et"
style="@style/Widget.AppCompat.Button.Colored"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="@+id/textView16"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="43dp"
app:layout_constraintLeft_toLeftOf="@+id/textView16" />

<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Öğrenci Eğitim Düzeyine Göre Bağış"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/radioGroup1"
android:layout_marginStart="16dp"
android:layout_marginTop="46dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="29dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp" />

<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kaç Aylık Burs Vermek İstersiniz?"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp" />

<TextView
android:id="@+id/textView16"
android:layout_width="89dp"
android:layout_height="29dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="@+id/devamEt2"
android:layout_marginTop="64dp"
app:layout_constraintTop_toBottomOf="@+id/spinner2"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="61dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />


</android.support.constraint.ConstraintLayout>


My activity looks like this:
activity

Answer Source

I've totally changed the approach. Have a look.

public class Test extends AppCompatActivity {
 Spinner spinnerGenelBagis;
 RadioGroup radioGroup;
 ArrayAdapter<String> dataAdapterForGenelBagis;
 String[] aylar;
 TextView mText;

 private static int spinnerValue = 0;
 private static int radioValue = 0;

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

    aylar = getResources().getStringArray(R.array.month);
    spinnerGenelBagis = (Spinner) findViewById(R.id.spinner2);
    dataAdapterForGenelBagis = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, aylar);
    spinnerGenelBagis.setAdapter(dataAdapterForGenelBagis);
    mText = (TextView) findViewById(R.id.textView16);
    radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {


            if (checkedId == R.id.meslekLisesi) {

                radioValue = 200;
                updateTextView();

            } else if (checkedId == R.id.yuksekOgrenim) {

                radioValue = 300;
                updateTextView();

            } else if (checkedId == R.id.yuksekLisans) {
                //
            } else if (checkedId == R.id.doktora) {
                //

            }

        }
    });

    spinnerGenelBagis.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            String text = spinnerGenelBagis.getSelectedItem().toString();
            spinnerValue = Integer.parseInt(text);
            updateTextView();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}

private void updateTextView() {
    String totalAmount = Integer.toString(spinnerValue * radioValue);
    mText.setText(totalAmount);
}

}

Hope it helps.
And, you don't need to write yuksekOgrenimButton.isChecked() = true if you used it. yuksekOgrenimButton.isChecked() returns boolean by itself.