1

I am trying to make layout like in the picture. Project picture I can only link it because I am new. When I run the code with just one LinearLayout it works fine. But when I make nested linear layout it shows an only white screen. And there are no errors. The code adds a score to teams according to their throw s.Thank you.

Java code:

public class MainActivity extends AppCompatActivity {
    int scorea=0;
    int scoreb=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void displayforpointb3(View view){
        TextView scoreView=(TextView)findViewById(R.id.team_b_score);
        scoreb+=3;
        scoreView.setText(""+scoreb);
    }
    public void displayforpointb2(View view){
        TextView scoreView=(TextView)findViewById(R.id.team_b_score);
        scoreb+=3;
        scoreView.setText(""+scoreb);
    }
    public void displayforfreethrowb(View view){
        TextView scoreView=(TextView)findViewById(R.id.team_b_score);
        scoreb++;
        scoreView.setText(""+scoreb);
    }
    public void displayforpoint3(View view){
        TextView scoreView=(TextView)findViewById(R.id.team_a_score);
        scorea+=3;
        scoreView.setText(""+scorea);
    }
    public void displayforpoint2(View view){
        TextView scoreView=(TextView)findViewById(R.id.team_a_score);
        scorea+=2;
        scoreView.setText(""+scorea);
    }
    public void displayforfreethrow(View view){
        TextView scoreView=(TextView)findViewById(R.id.team_a_score);
        scorea++;
        scoreView.setText(""+scorea);
    }

}

XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.android.courtcounter.MainActivity"
    android:orientation="horizontal">

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:titleTextColor="@android:color/white"
    android:background="?attr/colorPrimary"/>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Teama"
            android:textSize="14dp"
            android:layout_gravity="center_horizontal"
            android:padding="8dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="14dp"
            android:text="@string/zero"
            android:layout_gravity="center_horizontal"
            android:padding="8dp"
            android:id="@+id/team_a_score" />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="8dp"
            android:text="@string/point3"
            android:onClick="displayforpoint3"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="8dp"
            android:text="@string/point2"
            android:onClick="displayforpoint2"/>
    <Button
        android:layout_width="match_parent"
        android:layout_gravity="center_horizontal"
        android:padding="8dp"
        android:layout_height="wrap_content"
        android:text="@string/freethrow"
        android:onClick="displayforfreethrow"/>
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Teama"
            android:textSize="14dp"
            android:layout_gravity="center_horizontal"
            android:padding="8dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="14dp"
            android:layout_gravity="center_horizontal"
            android:padding="8dp"
            android:text="@string/zero"
            android:id="@+id/team_b_score" />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="8dp"
            android:text="@string/point3"
            android:onClick="displayforpointb3"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="@string/point2"
        android:layout_weight="1"/>
        android:onClick="displayforpointb2"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="8dp"
        android:text="@string/freethrow"
        android:onClick="displayforfreethrowb"/>
    </LinearLayout>
</LinearLayout>
Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105
wozverine
  • 34
  • 6

2 Answers2

0

First of all you should define your TextView as a private attributes in your class as follows

private TextView scoreViewA;
private TextView scoreViewB;

Then in your onCreate() method, you should find the view of those TextView as follows

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        scoreViewA = findViewById(R.id.team_a_score);
        scoreViewB = findViewById(R.id.team_b_score);
    }

Then in your onClick methods, you should update those TextView

public void displayforpointb3(View view){
        scoreb+=3;
        scoreViewB.setText(""+scoreb);
    }
    public void displayforpointb2(View view){
        scoreb+=2; //You had scoreb+=3 I changed it to 2
        scoreViewB.setText(""+scoreb);
    }
    public void displayforfreethrowb(View view){
        scoreb++;
        scoreViewB.setText(""+scoreb);
    }
    public void displayforpoint3(View view){
        scorea+=3;
        scoreViewA.setText(""+scorea);
    }
    public void displayforpoint2(View view){
        scorea+=2;
        scoreViewA.setText(""+scorea);
    }
    public void displayforfreethrow(View view){
        scorea++;
        scoreViewA.setText(""+scorea);
    }

Also I see something wrong in your activity_main.xml I guess is a misstype

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="@string/point2"
        android:layout_weight="1"/>  <--- Remove this "/>"
        android:onClick="displayforpointb2"/>

Happy coding

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
0

Problem

The orientation of the root LinearLayout is horizontal. This results in child views appearing to the right of the Toolbar instead of underneath it.

Solution

Make the root LinearLayout vertical and wrap the two inner view groups in a horizontal LinearLayout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <!-- Team A -->
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical">
            
            <!-- Text & Buttons -->
            
        </LinearLayout>
        
        <!-- Team B -->
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- Text & Buttons -->

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

It's not pretty, but it gets the job done. Consider using ConstraintLayout as an alternative to nested view groups if you encounter any performance issues.

Community
  • 1
  • 1