0

screenshot of how it appearsSo i'm trying to create a quiz application on android but while working on it, i noticed that the setText i used to set first question and answers is not working, it would be great if you help me find out what's not working here. Notice that the problems occurs only when trying to set text programmatically

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;       
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Play extends AppCompatActivity {
private Button a1,a2,a3,a4;
private TextView question;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);

    a1 = (Button)findViewById(R.id.a1);
    a2 = (Button)findViewById(R.id.a2);
    a3 = (Button)findViewById(R.id.a3);
    a4 = (Button)findViewById(R.id.a4);
    question = findViewById(R.id.question);
    question.setText("Pick");
    a1.setText("A");
    a2.setText("B");
    a3.setText("C");
    a4.setText("D");
}




<?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"
android:background="@drawable/background"
tools:context=".Play">

<Button
    android:id="@+id/a2"
    android:layout_width="250dp"
    android:layout_height="50dp"
    android:layout_marginTop="30dp"
    android:text=""
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/a1" />

<Button
    android:id="@+id/a3"
    android:layout_width="250dp"
    android:layout_height="50dp"
    android:layout_marginTop="30dp"
    android:text=""
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/a2"
/>

<Button
    android:id="@+id/a4"
    android:layout_width="250dp"
    android:layout_height="50dp"
    android:layout_marginTop="30dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.515"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/answer3"
    app:layout_constraintVertical_bias="0.0"
/>

<Button
    android:id="@+id/a1"
    android:layout_width="245dp"
    android:layout_height="50dp"
    android:text=""
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/question"
/>

<TextView
    android:id="@+id/question"
    android:layout_width="364dp"
    android:layout_height="264dp"
    android:layout_marginTop="30dp"
    android:layout_marginBottom="30dp"
    android:text=""
    app:layout_constraintBottom_toTopOf="@+id/a1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.487"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
/>

Demha
  • 29
  • 6

2 Answers2

1

I have tested your code, its showing result as expected. Try to change text color of your button text. or by setting background color of your views to check which view is appearing first. Sometimes code is working but due to some view over other, it does not reflect output

CodeX
  • 81
  • 1
  • 2
  • 6
0

Try to add

import android.widget.View;

at the top of your java file.

Android widget: How to change the text of a button

prushh
  • 21
  • 3