'How can I set the 'text' value of a TextView to be based off an EditText value or variable?
I'm currently trying to make a DnD character sheet app using Android Studio for my University Final Year Project, and I'm having some issues with creating my layout. I am currently tinkering with having a TextView present it's text field as a specfic value i.e in this case I wish to have it copy the value of the EditText component so that later I can try and have a TextView present the modifier based on the overall stat value (i.e if the EditText has the value 14 then the TextView will show +2).
The issue I am having is that I don't know how to assign the text value of my TextView to be equal to what I wish it to be. I have tried in the java file to set a String to be equal to the component, but all I am getting back is an integer value and not the String.
Any advice or input would be helpful as I am very new to using Android Studio and app development.
Main Activity Java File
package com.finalproject.app;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.finalproject.app.databinding.ActivityDiceRollerBinding;
import com.finalproject.app.databinding.ActivityMainBinding;
public class MainActivity extends DrawerBaseActivity {
ActivityMainBinding activityMainBinding;
String test = String.valueOf(R.id.editTextCharacterName);
TextView textView = (TextView) findViewById(R.id.textViewTest) ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(activityMainBinding.getRoot());
allocateActivityTitle("Character Sheet");
}
public void onClick(View v){
}
}
Main Activity xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<View
android:id="@+id/divider2"
android:layout_width="409dp"
android:layout_height="1dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.08"
tools:visibility="invisible" />
<TextView
android:id="@+id/CharName_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Character Name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/editTextCharacterName"
app:layout_constraintStart_toStartOf="@+id/editTextCharacterName"
app:layout_constraintTop_toBottomOf="@+id/editTextCharacterName"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/editTextCharacterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="John"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.03"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider2"
app:layout_constraintVertical_bias="0.003" />
<TextView
android:id="@+id/textViewTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="155dp"
android:layout_marginTop="208dp"
android:layout_marginEnd="198dp"
android:text=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Solution 1:[1]
To get the text in an EditText we use getText so the code should be like this
EditText editText = (EditText) findViewById(R.id.editTextCharacterName);
String text = editText.getText().toString();
and here you can use the text in the editText
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hello |
