Я пытаюсь отобразить свою кнопку Dynami c в макете сетки.
ПРОБЛЕМА: Раньше я использовал линейный макет, но все же у меня возникли ошибки при получении строки и column, то я пытаюсь использовать gridlayout в моем файле xml, но ожидание все равно не соответствует действительности. Я использую adpater также для своих столбцов и строк, но у меня есть некоторые ошибки.
Итак, у меня проблема с отображением моего кода, он показывает только одну кнопку вместо длины строки, которую я хочу.
public class MainActivity extends AppCompatActivity{
private String result="";
String sampleText = "The red fox jumps over the lazy dog.";
String[] words = sampleText.split("\\s");
private GridLayout ll;
Button button;
private final ArrayList<Button> buttonList = new ArrayList<>();
GridLayout.LayoutParams lp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int noOfBtns = words.length;
ll =(GridLayout) findViewById(R.id.btnlay);
final TextView tvtext = findViewById(R.id.result);
final TextView text_quiz = findViewById(R.id.text_quiz);
button = findViewById(R.id.validate);
tvtext.setText(sampleText);
final Button[] btns = new Button[noOfBtns];
lp = new GridLayout.LayoutParams();
for (int i = 0; i < noOfBtns; i++) {
final int j = i;
/*words[i] = words[i].replaceAll("[^\\w]", "");*/
btns[i] = new Button(this);
btns[i].setBackgroundResource(R.drawable.custom_button);
btns[i].setWidth(0);
btns[i].setText(words[i]);
btns[i].setTag(i);
btns[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result = result + btns[j].getText().toString();
if (j < noOfBtns) {
text_quiz.append(words[j] + " ");
btns[j].setEnabled(false);
}
}
});
/* buttonList.add(btns[i]);*/
ll.removeAllViews();
ll.setRowCount(10);
ll.setColumnCount(7);
Collections.shuffle(buttonList);
ll.addView(btns[i],lp);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String answer = text_quiz.getText().toString().trim();
if (sampleText.compareTo(answer) == 0) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Correct")
.setMessage("please click ok to continue until the end")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("Incorrect");
builder.setMessage("Your Answer is Incorrect.");
builder.setCancelable(true);
final AlertDialog dlg = builder.create();
dlg.show();
Handler mHandler = new Handler();
Runnable mRunnable = new Runnable() {
public void run() {
if (dlg != null && dlg.isShowing()) dlg.dismiss();
}
};
mHandler.postDelayed(mRunnable, 2000);
text_quiz.setText("");
for (int a = 0; a < noOfBtns; a++) {
btns[a].setEnabled(true);
}
}
}
});
}
Вот мой xml файл:
<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">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/round_corners"
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.144">
<ImageButton
android:id="@+id/icon1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="@drawable/round_corners"
android:src="@drawable/ic_image_play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="TextView"
android:textColor="@color/p10"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.425"
app:layout_constraintStart_toEndOf="@+id/icon1"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/text_quiz"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/round_corners2"
android:gravity="center"
android:textColor="@color/p10"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/btnlay"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout2"
app:layout_constraintVertical_bias="0.563" />
<GridLayout
android:id="@+id/btnlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50sp"
android:gravity="center"
android:background="@drawable/round_corners2"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />
<Button
android:id="@+id/validate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnlay"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
, поэтому результат моего ожидания должен быть таким: