How to add an integer variable with radio buttons in Android Studio? - android-studio

I am new in using android studio, I'm having trouble on how to add an integer variable with radio buttons. First I have to get the text of the radio button and put them in an if else statement for a condition then I'll use a basic arithmetic operator. But I always get the problem of "Not a statement" whenever I declare the variable inside the if else.
Here is XML code:
<RadioGroup
android:id="#+id/radioGroup2"
android:layout_width="197dp"
android:layout_height="241dp"
android:layout_marginStart="119dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="10dp" />
<RadioButton
android:id="#+id/stronglyDisagree"
android:tag="Strongly Disagree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="#000000"
android:minHeight="48dp"
android:textSize="18sp"
android:text="Strongly Disagree"
android:textColor="#color/black"
android:textStyle="bold" />
<RadioButton
android:id="#+id/disagree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="#000000"
android:minHeight="48dp"
android:text="Disagree"
android:textSize="18sp"
android:textColor="#color/black"
android:textStyle="bold" />
<RadioButton
android:id="#+id/neutral"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="#000000"
android:minHeight="48dp"
android:text="Neutral"
android:textSize="18sp"
android:textColor="#color/black"
android:textStyle="bold" />
<RadioButton
android:id="#+id/agree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="#000000"
android:minHeight="48dp"
android:textSize="18sp"
android:text="Agree"
android:textColor="#color/black"
android:textStyle="bold" />
<RadioButton
android:id="#+id/stronglyAgree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="#000000"
android:minHeight="48dp"
android:textSize="18sp"
android:text="Strongly Agree"
android:textColor="#color/black"
android:textStyle="bold" />
Here is the java code:
// Intelligence Integer Data (Linguistics)
int linguistics = 0;
next.setOnClickListener(view -> {
// RadioGroup Function (Selected Radio Button)
String radiovalue ((RadioButton)this.findViewById(likert.getCheckedRadioButtonId())).getText().toString();
if(radiovalue.equals("Strongly Disagree")){
linguistics + 1;
}else if(radiovalue.equals("Disagree")){
linguistics + 2;
}else if(radiovalue.equals("Neutral")){
linguistics + 3;
}else if(radiovalue.equals("Agree")){
linguistics + 4;
}else if(radiovalue.equals("Strongly Agree")){
linguistics + 5;
}
// next question
NextQuestion(random.nextInt(questionLength)); });
I expected the integer variable I've declared will change its value and add up on each click of the submit button after selecting a radio button.

Related

Converting an old style tablelayout to a constraint layout

I'm trying to convert this layout made using table layout to a constraint layout.
The second column and the third column could be hidden.
The price should be aligned to right.
The header in the first column should aligned to the left.
It's an exercise to learn constraints better, but I'm failing
<?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:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:shrinkColumns="*"
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.0"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
<TableRow android:layout_marginBottom="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:gravity="start"
android:text="Price"
android:textColor="#android:color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_marginStart="10dp"
android:gravity="end"
android:text="-"
android:textColor="#android:color/black"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_marginStart="10dp"
android:gravity="end"
android:text="-"
android:textColor="#android:color/black"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_marginStart="10dp"
android:gravity="end"
android:text="2000$"
android:textColor="#android:color/black"
android:textSize="12sp" />
</TableRow>
<TableRow
android:layout_marginTop="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:gravity="start"
android:text="Price2\n(test)"
android:textColor="#android:color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_marginStart="10dp"
android:gravity="end"
android:text="8000$"
android:textColor="#android:color/black"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_marginStart="10dp"
android:gravity="end"
android:text="-5000$"
android:textColor="#android:color/black"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_marginStart="10dp"
android:gravity="end"
android:text="3000"
android:textColor="#android:color/black"
android:textSize="12sp" />
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I have try different ways: barriers, flow, but I didn't get the goal.
I find it difficult to vertically align columns 2, 3 and 4 and at the same time align rows 1 and 2 horizontally
I think you are looking for Barriers to complete your exercise. You would set a barrier to the left of each column and constrain each column to the barrier to the right.
Here is an example of using ConstraintLayout in place of a TableLayout. It is complex enough that I think staying with TableLayout for its functionality is warranted. Here is the result followed by the code. The code has some comments that explain what is going on.
<!--
This LinearLayout is here just to provide a means to center the ConstraintLayout.
-->
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="32dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--
A barrier is set at the start of each column.
-->
<androidx.constraintlayout.widget.Barrier
android:id="#+id/barrier1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="start"
app:constraint_referenced_ids="r1c1,r2c1" />
<androidx.constraintlayout.widget.Barrier
android:id="#+id/barrier2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="start"
app:constraint_referenced_ids="r1c2,r2c2" />
<androidx.constraintlayout.widget.Barrier
android:id="#+id/barrier3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="start"
app:constraint_referenced_ids="r1c3,r2c3" />
<androidx.constraintlayout.widget.Barrier
android:id="#+id/barrier4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="start"
app:constraint_referenced_ids="r1c4,r2c4" />
<!--
Each cell is constrained horizontally to the barrier to the right and to its own barrier to
the left. app:layout_constraintWidth_min="wrap" is specified to stop the cells from collapsing.
This is a hack IMO but effective.
-->
<TextView
android:id="#+id/r1c1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:gravity="start"
android:text="Price"
android:textColor="#android:color/black"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#id/barrier2"
app:layout_constraintStart_toStartOf="#id/barrier1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_min="wrap" />
<TextView
android:id="#+id/r2c1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:gravity="start"
android:text="Price2\n(test)"
android:textColor="#android:color/black"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#id/barrier2"
app:layout_constraintStart_toStartOf="#id/barrier1"
app:layout_constraintTop_toBottomOf="#id/divider"
app:layout_constraintWidth_min="wrap" />
<TextView
android:id="#+id/r1c2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:gravity="end"
android:text="-"
android:textColor="#android:color/black"
android:textSize="12sp"
android:visibility="visible"
app:layout_constraintEnd_toStartOf="#id/barrier3"
app:layout_constraintStart_toStartOf="#id/barrier2"
app:layout_constraintTop_toTopOf="#id/r1c1"
app:layout_constraintWidth_min="wrap" />
<TextView
android:id="#+id/r2c2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:gravity="end"
android:text="8000$"
android:textColor="#android:color/black"
android:textSize="12sp"
android:visibility="visible"
app:layout_constraintEnd_toStartOf="#id/barrier3"
app:layout_constraintStart_toStartOf="#id/barrier2"
app:layout_constraintTop_toBottomOf="#id/divider"
app:layout_constraintWidth_min="wrap" />
<TextView
android:id="#+id/r1c3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:gravity="end"
android:text="-"
android:textColor="#android:color/black"
android:textSize="12sp"
app:layout_constraintEnd_toStartOf="#id/barrier4"
app:layout_constraintStart_toStartOf="#id/barrier3"
app:layout_constraintTop_toTopOf="#id/r1c1"
app:layout_constraintWidth_min="wrap" />
<TextView
android:id="#+id/r2c3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:gravity="end"
android:text="-5000$"
android:textColor="#android:color/black"
android:textSize="12sp"
app:layout_constraintEnd_toStartOf="#id/barrier4"
app:layout_constraintStart_toStartOf="#id/barrier3"
app:layout_constraintTop_toBottomOf="#id/divider"
app:layout_constraintWidth_min="wrap" />
<TextView
android:id="#+id/r1c4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="end"
android:text="2000$"
android:textColor="#android:color/black"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="#id/r1c1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#id/barrier4"
app:layout_constraintWidth_min="wrap" />
<TextView
android:id="#+id/r2c4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="end"
android:text="3000"
android:textColor="#android:color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#id/barrier4"
app:layout_constraintTop_toBottomOf="#id/divider"
app:layout_constraintWidth_min="wrap" />
<View
android:id="#+id/divider"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#android:color/black"
app:layout_constraintEnd_toEndOf="#id/r1c4"
app:layout_constraintStart_toStartOf="#id/r1c1"
app:layout_constraintTop_toBottomOf="#id/r1c1" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
TableLayout will make the width of each cell in a column the width of the widest cell content. This is tricky to do with ConstraintLayout and is accomplished by setting the width of each cell to 0dp and constraining the start and end to the barriers that bracket the column. See this Stack Overflow question for an explanation. I consider this a hack, but it works.
The other difference between this implementation and TableLayout is how the table behaves when a single cell is collapsed. TableLayout will shift the remaining cells over while this implementation will maintain the original columns. If all the cells in a column are collapsed (made gone) then this works the same as TableLayout.

unable to go from one activity to another

Whenever I try to go from any activity to this particular activity,
the application goes back to the login screen.
Activity (I could not reach) Java source file:
package com.example.studentresultmaangementsystem;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
importandroid.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class Main7Activity extends AppCompatActivity {
Button button,button1;
TableLayout layout1;
TableLayout layout2;
TableRow table1row1;
TableRow table1row2;
TableRow table1row3;
TableRow table2row1;
TableRow table2row2;
TableRow table2row3;
TableRow table2row4;
TableRow table2row5;
TableRow table2row7;
TableRow table2row6;
EditText name;
EditText rollNumber;
EditText subject1;
EditText subject2;
EditText subject3;
EditText subject4;
EditText subject5;
EditText subject6;
EditText marks1,marks2,marks3,marks4,marks5,marks6;
DatabaseReference drf;
Student stu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adding);
layout1 = (TableLayout) findViewById(R.id.tb);
layout2 = (TableLayout)findViewById(R.id.tb2);
table1row1 = (TableRow)findViewById(R.id.r1);
table1row2 = (TableRow)findViewById(R.id.r3);
table1row3 = (TableRow)findViewById(R.id.row);
table2row1 = (TableRow)findViewById(R.id.row1);
table2row2 = (TableRow)findViewById(R.id.row2);
table2row3 = (TableRow)findViewById(R.id.row3);
table2row4 = (TableRow)findViewById(R.id.row4);
table2row5 = (TableRow)findViewById(R.id.row5);
table2row6 = (TableRow)findViewById(R.id.row6);
table2row7 = (TableRow)findViewById(R.id.row7);
button = (Button) findViewById(R.id.bu);
button1 = (Button) findViewById(R.id.bu1);
name = (EditText) findViewById(R.id.en);
rollNumber = (EditText) findViewById(R.id.er);
subject1 = (EditText) findViewById(R.id.es1);
subject2 = (EditText) findViewById(R.id.es2);
subject3 = (EditText) findViewById(R.id.es3);
subject4 = (EditText) findViewById(R.id.es4);
subject5 = (EditText) findViewById(R.id.es5);
subject6 = (EditText) findViewById(R.id.es6);
marks1 = (EditText) findViewById(R.id.em1);
marks2 = (EditText) findViewById(R.id.em2);
marks3 = (EditText) findViewById(R.id.em3);
marks4 = (EditText) findViewById(R.id.em4);
marks5 = (EditText) findViewById(R.id.em5);
marks6 = (EditText) findViewById(R.id.em6);
stu = new Student();
drf = FirebaseDatabase.getInstance().getReference().child("student");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String n = name.getText().toString();
int rollnumber = Integer.parseInt(rollNumber.getText().toString().trim());
String s1 = subject1.getText().toString();
String s2 = subject2.getText().toString();
String s3 = subject3.getText().toString();
String s4 = subject4.getText().toString();
String s5 = subject5.getText().toString();
String s6 = subject6.getText().toString();
float m1 = Float.parseFloat(marks1.getText().toString().trim());
float m2 = Float.parseFloat(marks2.getText().toString().trim());
float m3 = Float.parseFloat(marks3.getText().toString().trim());
float m4 = Float.parseFloat(marks4.getText().toString().trim());
float m5 = Float.parseFloat(marks5.getText().toString().trim());
float m6 = Float.parseFloat(marks6.getText().toString().trim());
stu.setSubject1(s1);
stu.setSubject2(s2);
stu.setSubject3(s3);
stu.setSubject4(s4);
stu.setSubject5(s5);
stu.setSubject6(s6);
stu.setMarks1(m1);
stu.setMarks2(m2);
stu.setMarks3(m3);
stu.setMarks4(m4);
stu.setMarks5(m5);
stu.setMarks6(m6);
drf.push().setValue(stu);
Toast.makeText(Main7Activity.this, "the data has been inserted into the database", Toast.LENGTH_SHORT).show();
}
});
}
}
XML layout file tied to the above Java file
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main7Activity"
android:background="#E0314C">
<TableLayout
android:id="#+id/tb" android:layout_width="367dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginVertical="40dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:background="#f1f1f1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
<TableRow android:id="#+id/r1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#74B7D5"
android:gravity="center"
android:text="ENTER DETAILS OF STUDENT " android:textStyle="bold" />
</TableRow>
<TableRow android:id="#+id/r3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#74B7D5"
android:gravity="center"
android:text="Name ="
android:textStyle="bold" />
<EditText android:id="#+id/en"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:background="#ffffff"
android:gravity="center"
android:hint="enter name"
android:inputType="text"
android:maxLength="10"
android:textStyle="bold" />
</TableRow>
<TableRow android:id="#+id/row">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#74B7D5"
android:gravity="center"
android:text="RollNo ="
android:textStyle="bold" />
<EditText
android:id="#+id/er"
android:layout_width="204dp"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="5"
android:background="#ffffff"
android:gravity="center"
android:hint="enter rollno"
android:inputType="number"
android:maxLength="6"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/tb2"
android:layout_width="382dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginVertical="1dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#f1f1f1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tb"
tools:ignore="MissingConstraints">
<TableRow android:id="#+id/row1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#74B7D5"
android:gravity="center"
android:text="Subjects "
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#74B7D5"
android:gravity="center"
android:text="Marks "
android:textStyle="bold" />
</TableRow>
<TableRow android:id="#+id/row2">
<EditText
android:id="#+id/es1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" ----------
android:background="#ffffff"
android:gravity="center"
android:hint="subject_name"
android:inputType="text"
android:maxLength="8"
android:textStyle="bold" />
<EditText
android:id="#+id/em1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="center"
android:hint="enter marks"
android:inputType="number"
android:maxLength="3"
android:textStyle="bold" />
</TableRow>
<TableRow android:id="#+id/row3">
<EditText
android:id="#+id/es2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="center"
android:hint="subject_name"
android:inputType="text"
android:maxLength="8"
android:textStyle="bold" />
<EditText
android:id="#+id/em2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="center"
android:hint="enter marks"
android:inputType="number"
android:maxLength="3"
android:textStyle="bold" />
</TableRow>
<TableRow android:id="#+id/row4">
<EditText
android:id="#+id/es3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="center"
android:hint="subject_name"
android:inputType="text"
android:maxLength="8"
android:textStyle="bold" />
<EditText
android:id="#+id/em3"
android:layout_width="114dp"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="center"
android:hint="enter marks"
android:inputType="number"
android:maxLength="3"
android:textStyle="bold" />
</TableRow>
<TableRow android:id="#+id/row5">
<EditText
android:id="#+id/es4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="center"
android:hint="subject_name"
android:inputType="text"
android:maxLength="8"
android:textStyle="bold" />
<EditText
android:id="#+id/em4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="center"
android:hint="enter marks"
android:inputType="number"
android:maxLength="3"
android:textStyle="bold" />
</TableRow>
<TableRow android:id="#+id/row6">
<EditText
android:id="#+id/es5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="center"
android:hint="subject_name"
android:inputType="text"
android:maxLength="8"
android:textStyle="bold" />
<EditText
android:id="#+id/em5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="center"
android:hint="enter marks"
android:inputType="number
android:maxLength="3"
android:textStyle="bold" />
</TableRow>
<TableRow android:id="#+id/row7">
<EditText
android:id="#+id/es6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="center"
android:hint="subject_name"
android:inputType="text"
android:maxLength="8"
android:textStyle="bold" />
<EditText
android:id="#+id/em6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="center"
android:hint="enter marks"
android:inputType="number"
android:maxLength="3"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<Button
android:id="#+id/bu"
android:layout_width="106dp"
android:layout_height="39dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:onClick="addata"
android:text="Add data"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.947"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="631dp"
/>
<Button
android:id="#+id/bu1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:onClick="back"
android:text="Go back"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.25"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints,OnClick"
tools:layout_editor_absoluteY="631dp" />
</LinearLayout>

Android ListActivity doesn't center the items

I have a ListActivity which displays an image and some text.
The imageView has a fixed size and I want to center the list horizontally.
After endless tries, I set any gravity to center_horinzontal, but nothing works.
The list is shown over the whole screen, as I can see by the scrollbar on the right side. But my frame remains on the left side.
In the adapter class, which entends ArrayAdapter, I even tried to set the gravity during runtime, but when I try to manipulate then basic LinearLayout, I get the error:
"java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams"
My attempt to change gravity in the method getView() which throws the error:
LinearLayout layout = (LinearLayout) rowView.findViewById(R.id.bootsFrameLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER_HORIZONTAL;
layout.setLayoutParams(params);
The list row is designed in a layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bootsFrameLayout"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:background="#drawable/card_blank2"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/bootsName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="TextView"
android:textAppearance="#style/BootNameStyle" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginStart="30dp" >
<ImageView
android:id="#+id/bootImage"
android:layout_width="240dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop"
android:src="#drawable/test_boot" />
<TextView
android:id="#+id/textWatchList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/gray_transparent"
android:gravity="end"
android:lines="2"
android:text="#string/watchlist"
android:textAppearance="#style/BootWatchListStyle" />
<ProgressBar
android:id="#+id/bootProgressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="invisible" />
</FrameLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="25dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginStart="25dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/bootMasse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="70"
android:text="TextView"
android:textAppearance="#style/BootInfoStyle" />
<TextView
android:id="#+id/bootBaujahr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="30"
android:gravity="right"
android:text="TextView"
android:textAppearance="#style/BootInfoStyle" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/bootLand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="70"
android:text="TextView"
android:textAppearance="#style/BootInfoStyle" />
<TextView
android:id="#+id/bootPreis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="30"
android:gravity="right"
android:text="TextView"
android:textAppearance="#style/BootPreisStyle" />
</TableRow>
</TableLayout>
The ListView is defined here:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="12dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginStart="12dp"
android:layout_weight="8" >
</ListView>
The activity occupies the whole screen, as it should do.
But although there is place enough, the frame won't center.
Any ideas?

How to configure GridLayout to take most of the horizontal screen space

I am trying to mimic this sort of design on android using GridLayout:
I think I have most of it done using the below code:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="8"
android:rowCount="4"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:background="#drawable/rounded">
<TextView
android:layout_gravity="center_horizontal"
android:text="Some Text Here"
android:textStyle="bold"
android:textColor="#000000"
android:textSize="30dp"
android:textAlignment="center"
android:layout_columnSpan="8"
android:paddingBottom="10dp"
/>
<TextView
android:text="16"
android:textStyle="bold"
android:paddingRight="70dp"
android:textColor="#000000"
android:textSize="18dp"
android:paddingLeft="10dp"
android:paddingBottom="5dp"/>
<TextView
android:text="27"
android:textStyle="bold"
android:paddingRight="70dp"
android:textSize="18dp"
android:textColor="#000000"/>
<TextView
android:text="52"
android:textStyle="bold"
android:layout_columnSpan="6"
android:textSize="18dp"
android:paddingRight="10dp"
android:textColor="#000000"/>
<TextView
android:text="Text 1"
android:paddingRight="20dp"
android:paddingLeft="10dp"
android:textSize="18dp"
android:textColor="#ff565656"
android:paddingBottom="10dp"/>
<TextView
android:text="Text 2"
android:paddingRight="20dp"
android:textSize="18dp"
android:textColor="#ff565656"/>
<TextView
android:text="Text 3"
android:textColor="#ff565656"
android:textSize="18dp"
android:paddingRight="10dp"/>
</GridLayout>
However, when I run it in my emulator it looks like this:
Notice that it is leaving too much space on the right and the left.
Question
How can I configure the grid layout so it takes up most of the screen on right and left of it (just like in the sample above).
How can I change the screen background color - Right now it is black, I'd like to change it to another color.
For GridLayout, replace this...
android:layout_width="wrap_content"
with...
android:layout_width="fill_parent"
as follows...
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnCount="8"
android:rowCount="4"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:background="#drawable/rounded">
#Anthony - You are not using the power and the essence of GridLayout in the code written. As you have manually allotted paddings to the views in order to make them appear in the first screen, which is very much possible with any other layout as well. Like below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2"
android:text="Some Text Here"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="16"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="17"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="18"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Text1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Text2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Text3"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
And, GridLayout is about rows and columns. So if you want your Views to be placed such that and to use it to fuller, you can supply the row as well as column position where you view should be placed in which order and its other properties will add up to it. Here is a link.
Try this out and I am sure you will understand it much better. Happy coding. :)

How to define column and row in a LinearLayout dynamically?

I'm trying to create dynamically this XML:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/topleft"
android:layout_width="160dp"
android:layout_height="200dp"
android:layout_column="0"
android:layout_row="0"
>
<ImageView
android:id="#+id/myimage1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/tetona"
/>
></LinearLayout>
<LinearLayout
android:id="#+id/topRigth"
android:layout_width="160dp"
android:layout_height="200dp"
android:layout_column="1"
android:layout_row="0"
>
<ImageView
android:id="#+id/myimage2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/tetona" />
></LinearLayout>
<LinearLayout
android:id="#+id/bottomRight"
android:layout_width="160dp"
android:layout_height="200dp"
android:layout_column="1"
android:layout_row="1"
>
<ImageView
android:id="#+id/myimage3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/tetona" />
></LinearLayout>
<LinearLayout
android:id="#+id/BottonLeft"
android:layout_width="160dp"
android:layout_height="200dp"
android:layout_column="0"
android:layout_row="1"
>
<ImageView
android:id="#+id/myimage4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/tetona" />
></LinearLayout>
</GridLayout>
I can create LinearLayouts inside a loop such as:
GridLayout gridlay = (GridLayout)findViewById(R.id.gridpadre);
gridlay.setColumnCount(2);
gridlay.setRowCount(2);
for (int i=0; i<(gridlay.getRowCount()); ++i){
for(int j=0; j<gridlay.getColumnCount(); ++j){
LinearLayout layout = new LinearLayout(this);
layout.setId(id);
layout.setLayoutParams(new LinearLayout.LayoutParams(160,200));
gridlay.addView(layout);
But... how can I set LinearLayout android:layout_column="i" and android:layout_row="j" dynamically in order to put it in the position i and j relative to the gridlayout?
Is it possible?
Thanks
try doing the following: layout.setOrientation(VERTICAL); then try layout.setHorizontalGravity(j) since I don't have a computer equipped with any way to test it I'm not sure it will work, but its worth a shot.

Resources