My scroll view isn't scrolling. I have a feeling it may be due to an "onTouch" method I have that instead of scrolling is incrementing the counter shown. I have tried to find some solutions but none seem to be directly related to mine and any attempt to change the scroll view has not worked.
I am not sure how to get around this and any help would be greatly appreciated.
Java Code
package com.alanlyne.tbm.Variables.varL;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.alanlyne.tbm.Menu.Menu;
import com.alanlyne.tbm.Menu.menuCounter;
import com.alanlyne.tbm.R;
import com.alanlyne.tbm.Variables.Var;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class varL2 extends AppCompatActivity {
TextView t0, t1, t2, t3, t4, t5, t6, t7, t8, x;
Button b1;
int counter = 0;
DatabaseReference databaseName;
private long mLastClickTime = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_var_l2);
databaseName = FirebaseDatabase.getInstance().getReference("counter");
t0 = findViewById(R.id.t0);
t1 = findViewById(R.id.t1);
t2 = findViewById(R.id.t2);
t3 = findViewById(R.id.t3);
t4 = findViewById(R.id.t4);
t5 = findViewById(R.id.t5);
t6 = findViewById(R.id.t6);
t7 = findViewById(R.id.t7);
t8 = findViewById(R.id.t8);
b1 = findViewById(R.id.b1);
x = findViewById(R.id.x);
x.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent x = new Intent(varL2.this, Var.class);
startActivity(x);
}
});
View view = findViewById(R.id.ScrollView01);
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
if (SystemClock.elapsedRealtime() - mLastClickTime < 500) {
System.out.println("Fun");
}
else{
mLastClickTime = SystemClock.elapsedRealtime();
counter++;
System.out.println(counter);
if (counter > 0) {
TextView tv = findViewById(R.id.t1);
tv.setVisibility(View.VISIBLE);
}
if (counter > 1) {
TextView tv = findViewById(R.id.t2);
tv.setVisibility(View.VISIBLE);
}
if (counter > 2) {
TextView tv = findViewById(R.id.t3);
tv.setVisibility(View.VISIBLE);
}
if (counter > 3) {
TextView tv = findViewById(R.id.t4);
tv.setVisibility(View.VISIBLE);
}
if (counter > 4) {
TextView tv = findViewById(R.id.t5);
tv.setVisibility(View.VISIBLE);
}
if (counter > 5) {
TextView tv = findViewById(R.id.t6);
tv.setVisibility(View.VISIBLE);
}
if (counter > 6) {
TextView tv = findViewById(R.id.t7);
tv.setVisibility(View.VISIBLE);
}
if (counter > 7) {
TextView tv = findViewById(R.id.t8);
tv.setVisibility(View.VISIBLE);
}
if (counter > 8) {
Button b1 = findViewById(R.id.b1);
b1.setVisibility(View.VISIBLE);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent Menu = new Intent(varL2.this, Var.class);
startActivity(Menu);
addCounter();
}
});
}}
return true;
}
});
}
private void addCounter(){
if(Menu.counter == 12) {
Menu.counter = 13;
menuCounter counter = new menuCounter(Menu.counter);
databaseName.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(counter);
System.out.println("AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH");
}
}
}
XML Code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bgcolour"
tools:context=".Variables.varL.varL2">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/t0"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="#drawable/edit_round"
android:gravity="center"
android:padding="10dp"
android:text="There are 8 primitive data types for creating variables in Java."
android:textColor="#android:color/black"
android:textColorHighlight="#FF000000"
android:textSize="15sp"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="#+id/t1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/t1"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/edit_round"
android:gravity="center"
android:padding="10dp"
android:text="byte\n8 bit integer\n-128 to 127\nDefault of 0"
android:textColor="#android:color/black"
android:textColorHighlight="#FF000000"
android:textSize="15sp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/t2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/t0" />
<TextView
android:id="#+id/t2"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/edit_round"
android:gravity="center"
android:padding="10dp"
android:text="short\n16 bit integer\n-32768 to 32767\nDefault of 0"
android:textColor="#android:color/black"
android:textColorHighlight="#FF000000"
android:textSize="15sp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/t3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/t1" />
<TextView
android:id="#+id/t3"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/edit_round"
android:gravity="center"
android:padding="10dp"
android:text="int\n32 bit integer\n-2147483648 to 2147483647\nDefault of 0"
android:textColor="#android:color/black"
android:textColorHighlight="#FF000000"
android:textSize="15sp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/t2" />
<TextView
android:id="#+id/t4"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/edit_round"
android:gravity="center"
android:padding="10dp"
android:text="long\n64 bit integer\n-9223372036854775808 to 9223372036854775807\nDefault of 0L"
android:textColor="#android:color/black"
android:textColorHighlight="#FF000000"
android:textSize="15sp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/t5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/t3" />
<TextView
android:id="#+id/t5"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/edit_round"
android:gravity="center"
android:padding="10dp"
android:text="float\nDecimal point number\n1.5*10^-45 to 3.4*10^38\nDefault of 0.0f"
android:textColor="#android:color/black"
android:textColorHighlight="#FF000000"
android:textSize="15sp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/t6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/t4" />
<TextView
android:id="#+id/t6"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/edit_round"
android:gravity="center"
android:padding="10dp"
android:text="double\nDecimal point number\n5.0*10^-324 to 1.7*10^308\nDefault of 0.0"
android:textColor="#android:color/black"
android:textColorHighlight="#FF000000"
android:textSize="15sp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/t7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/t5" />
<TextView
android:id="#+id/t7"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/edit_round"
android:gravity="center"
android:padding="10dp"
android:text="char\nA unicode character e.g 'a','b'\n(\u0000 to \uffff) or (0 to 65535)\nDefault of null"
android:textColor="#android:color/black"
android:textColorHighlight="#FF000000"
android:textSize="15sp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/t8"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/t6" />
<TextView
android:id="#+id/t8"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/edit_round"
android:gravity="center"
android:padding="10dp"
android:text="boolean\nLogical true or false\ntrue or false\nDefault of false"
android:textColor="#android:color/black"
android:textColorHighlight="#FF000000"
android:textSize="15sp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/b1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/t7" />
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="150dp"
android:layout_marginBottom="20dp"
android:background="#drawable/b_round"
android:text="Continue"
android:textColor="#android:color/white"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/t8" />
<TextView
android:id="#+id/x"
android:layout_width="15dp"
android:layout_height="35dp"
android:layout_marginStart="10dp"
android:text="x"
android:textColor="#android:color/white"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/t0"
app:layout_constraintEnd_toEndOf="#+id/t0"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Fixed the issue by having the
View view = findViewById(R.id.ScrollView01);
reference the id of the constraint layout instead of the scroll view.
Usually when you are dealing with ScrollView you need to set android:fillViewport to true
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
Related
I am creating my first app with Java for android in android studio and have some problems with the LayoutInflator.
The java code of the Activity looks like this:
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.io.Serializable;
import java.util.ArrayList;
public class MatchActivity extends MatchSettingsActivity implements View.OnClickListener, Serializable {
public ArrayList<Player> matchParticipants = new ArrayList<>();
public String matchType;
public int setsAmount;
public int legsAmount;
public String checkOutType;
public String atcMode;
public int matchFinished;
public final String matchType501 = "501";
public final String matchType301 = "301";
public ArrayList<View> playerRowViewsArrayList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match);
ImageButton backButton = findViewById(R.id.match_back_button);
backButton.setOnClickListener(this);
Match match = (Match) getIntent().getSerializableExtra("match_object");
setMatchSettings(match.matchParticipants, match.matchType,
Integer.parseInt(match.setsAmount), Integer.parseInt(match.legsAmount),
match.checkOut, match.atcMode, match.matchFinished);
createMatchUI(matchType, matchParticipants);
}
#Override
public void onClick(View v) {
super.onClick(v);
if (v.getId() == R.id.match_back_button) {
Intent matchIntent = new Intent(MatchActivity.this, MainActivity.class);
MatchActivity.this.startActivity(matchIntent);
setContentView(R.layout.activity_main);
}
}
public void createMatchUI(String matchType, ArrayList<Player> matchParticipants) {
LinearLayout mainMatchLayout = findViewById(R.id.main_match_layout);
LayoutInflater inflater = getLayoutInflater();
if (matchType.equals(matchType501) || matchType.equals(matchType301)) {
for (int i=0; i<matchParticipants.size(); i+=1) {
View playerRowView = inflater.inflate(R.layout.player_row_layout, mainMatchLayout, false);
TextView nameTextView = playerRowView.findViewById(R.id.name);
TextView pointsTextView = playerRowView.findViewById(R.id.points);
TextView averageTextView = playerRowView.findViewById(R.id.average);
TextView lastDart1 = playerRowView.findViewById(R.id.last_d1);
TextView lastDart2 = playerRowView.findViewById(R.id.last_d2);
TextView lastDart3 = playerRowView.findViewById(R.id.last_d3);
TextView lastDartsCombined = playerRowView.findViewById(R.id.last_darts_combined);
nameTextView.setText(matchParticipants.get(i).playerName);
if (matchType.equals(matchType501)) {
pointsTextView.setText(matchType501);
} else {
pointsTextView.setText(matchType301);
}
averageTextView.setText("");
lastDart1.setText("");
lastDart2.setText("");
lastDart3.setText("");
lastDartsCombined.setText("");
playerRowViewsArrayList.add(playerRowView);
}
for (int i1=0; i1<playerRowViewsArrayList.size(); i1+=1) {
mainMatchLayout.addView(playerRowViewsArrayList.get(i1));
}
}
}
public void setMatchSettings(ArrayList<Player> matchParticipants, String matchType,
int setsAmount, int legsAmount, String checkOutType,
String atcMode, int matchFinished) {
this.matchParticipants = matchParticipants;
this.matchType = matchType;
this.setsAmount = setsAmount;
this.legsAmount = legsAmount;
this.checkOutType = checkOutType;
this.atcMode = atcMode;
this.matchFinished = matchFinished;
}
}
In the method createMatchUI I am trying to display one view for each player Object in a list.
The layout that should be created for each player using the LayoutInflator looks like this
<?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="wrap_content">
<TextView
android:id="#+id/points"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="30sp"
android:fontFamily="#font/suit_bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#id/last_d1"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:fontFamily="#font/suit_bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/points"
app:layout_constraintEnd_toEndOf="#id/points"
app:layout_constraintBottom_toBottomOf="#id/last_darts_combined"
android:gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:background="#drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="#+id/last_d1"
android:layout_width="0dp"
android:layout_height="0dp"
android:textSize="16sp"
android:fontFamily="#font/suit_bold"
app:layout_constraintEnd_toStartOf="#id/last_d2"
app:layout_constraintTop_toTopOf="#id/points"
app:layout_constraintStart_toEndOf="#id/points"
app:layout_constraintBottom_toBottomOf="#id/points"
android:gravity="center"
app:layout_constraintBottom_toTopOf="#id/last_darts_combined"
android:background="#drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="#+id/last_d2"
android:layout_width="0dp"
android:layout_height="0dp"
android:textSize="16sp"
android:fontFamily="#font/suit_bold"
app:layout_constraintEnd_toStartOf="#id/last_d3"
app:layout_constraintTop_toTopOf="#id/points"
app:layout_constraintStart_toEndOf="#id/last_d1"
app:layout_constraintBottom_toBottomOf="#id/points"
android:gravity="center"
android:background="#drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="#+id/last_d3"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:textSize="16sp"
android:fontFamily="#font/suit_bold"
app:layout_constraintEnd_toStartOf="#id/sets"
app:layout_constraintStart_toEndOf="#id/last_d2"
app:layout_constraintTop_toTopOf="#id/points"
app:layout_constraintBottom_toBottomOf="#id/points"
android:background="#drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="#+id/last_darts_combined"
android:layout_width="0dp"
android:layout_height="0dp"
android:textSize="16sp"
android:fontFamily="#font/suit_bold"
app:layout_constraintEnd_toEndOf="#id/last_d3"
app:layout_constraintStart_toStartOf="#id/last_d1"
app:layout_constraintTop_toTopOf="#id/name"
app:layout_constraintBottom_toBottomOf="#id/name"
android:gravity="center"
android:background="#drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="#+id/sets"
android:layout_width="0dp"
android:layout_height="0dp"
android:textSize="16sp"
android:fontFamily="#font/suit_bold"
app:layout_constraintTop_toTopOf="#id/points"
app:layout_constraintEnd_toStartOf="#id/legs"
app:layout_constraintStart_toEndOf="#id/last_d3"
app:layout_constraintBottom_toBottomOf="#id/points"
android:gravity="center"
android:background="#drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="#+id/legs"
android:layout_width="0dp"
android:layout_height="0dp"
android:textSize="16sp"
android:fontFamily="#font/suit_bold"
app:layout_constraintTop_toTopOf="#id/points"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/sets"
app:layout_constraintBottom_toBottomOf="#id/points"
android:gravity="center"
android:background="#drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="#+id/average"
android:layout_width="0dp"
android:layout_height="0dp"
android:textSize="16sp"
android:fontFamily="#font/suit_bold"
app:layout_constraintStart_toEndOf="#id/last_darts_combined"
app:layout_constraintTop_toTopOf="#id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="#id/name"
android:gravity="center"
android:background="#drawable/custom_rl_button_blue_borderless"/>
<View
android:id="#+id/divider"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="#+id/name"
app:layout_constraintTop_toTopOf="#+id/points"
app:layout_constraintEnd_toStartOf="#id/points"
app:layout_constraintStart_toStartOf="#id/points"/>
<View
android:id="#+id/divider2"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="#+id/name"
app:layout_constraintTop_toTopOf="#+id/points"
app:layout_constraintEnd_toStartOf="#id/last_d1"
app:layout_constraintStart_toEndOf="#id/points"/>
<View
android:id="#+id/divider3"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="#+id/points"
app:layout_constraintTop_toTopOf="#+id/points"
app:layout_constraintEnd_toStartOf="#id/last_d2"
app:layout_constraintStart_toEndOf="#id/last_d1"/>
<View
android:id="#+id/divider4"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="#+id/points"
app:layout_constraintTop_toTopOf="#+id/points"
app:layout_constraintEnd_toStartOf="#id/last_d3"
app:layout_constraintStart_toEndOf="#id/last_d2"/>
<View
android:id="#+id/divider5"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="#+id/name"
app:layout_constraintTop_toTopOf="#+id/points"
app:layout_constraintEnd_toStartOf="#id/sets"
app:layout_constraintStart_toEndOf="#id/last_d3"/>
<View
android:id="#+id/divider6"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="#+id/points"
app:layout_constraintTop_toTopOf="#+id/points"
app:layout_constraintEnd_toStartOf="#id/legs"
app:layout_constraintStart_toEndOf="#id/sets"/>
<View
android:id="#+id/divider7"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="#+id/name"
app:layout_constraintTop_toTopOf="#+id/points"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/legs"/>
<View
android:id="#+id/divider8"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="?android:attr/listDivider"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="#id/points"
app:layout_constraintTop_toBottomOf="#id/points"/>
<View
android:id="#+id/divider9"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="?android:attr/listDivider"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#id/points"
app:layout_constraintTop_toTopOf="#id/points"/>
<View
android:id="#+id/divider10"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="?android:attr/listDivider"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="#id/name"
app:layout_constraintTop_toBottomOf="#id/name"/>
</androidx.constraintlayout.widget.ConstraintLayout>
and the root activity/layout looks like this:
<?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="#color/background_color">
<androidx.appcompat.widget.Toolbar
android:id="#+id/match_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/toolbar_color"
android:theme="#style/toolbarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.appcompat.widget.Toolbar>
<TextView
android:id="#+id/match_label"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center_vertical"
app:layout_constraintStart_toStartOf="#id/match_toolbar"
app:layout_constraintBottom_toBottomOf="#id/match_toolbar"
app:layout_constraintEnd_toEndOf="#id/match_toolbar"
app:layout_constraintTop_toTopOf="#id/match_toolbar"
android:text="#string/match_label"
android:textColor="#color/white"
android:fontFamily="#font/suit_light"
android:textSize="20sp"/>
<ImageButton
android:id="#+id/match_back_button"
android:layout_width="35dp"
android:layout_height="25dp"
android:background="#drawable/back_arrow2"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
app:layout_constraintBottom_toBottomOf="#id/match_toolbar"
app:layout_constraintStart_toStartOf="#id/match_toolbar"
app:layout_constraintTop_toTopOf="#id/match_toolbar" />
<LinearLayout
android:id="#+id/main_match_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/match_toolbar"
app:layout_constraintBottom_toTopOf="#id/input_layout"
android:layout_margin="15dp">
</LinearLayout>
<LinearLayout
android:id="#+id/input_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
[...]
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
The problem I am facing is, that always only one view is added to the root layout (mainMatchLayout) like this:
Instead I want one layout for each player object that exists in my ArrayList matchParticipants.
Can anyone figure out what I'm doing wrong?
Thank you.
Hello friends i am creating a mp3 player which have two activities how can i control media from both activities like play pause and progress bar here is
the first activity have list of the song when user click any songs it work fine and below this song listview i create progress and bar and media controll button like play pause and next and back its include spreate layoute ad in below list when user click on this layout it redirect another activity and which have also same feature like play pause and and progress bar how can i controll this media from both activities?
my code!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#1e1e2c"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/song_list"/>
<include
android:layout_below="#+id/song_title"
layout="#layout/mediacontroller"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout 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:background="#1e1e2c"
android:layout_height="match_parent">
<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:id="#+id/rotate"
android:layout_centerInParent="true"
android:src="#drawable/musiccirlce"/>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
android:focusable="true"
android:id="#+id/repeat"
android:visibility="visible"
android:scaleType="centerCrop"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/baseline_repeat_white_48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.023"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.811" />
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
android:focusable="true"
android:id="#+id/repeatenable"
android:visibility="gone"
android:scaleType="centerCrop"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/repeatenable"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.023"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.811" />
<ImageButton
android:id="#+id/shuffle"
android:layout_width="20dp"
android:layout_height="24dp"
android:visibility="visible"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="centerCrop"
android:src="#drawable/baseline_shuffle_white_48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.976"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.811" />
<ImageButton
android:layout_width="25dp"
android:id="#+id/shufflenable"
android:layout_height="25dp"
android:visibility="gone"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/shuffulenabled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.976"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.811" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="99dp"
android:layout_alignParentBottom="true">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:clickable="false"
android:background="#color/backgroundColor"
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="1.0">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="15dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="centerCrop"
android:src="#mipmap/outline_thumb_up_alt_black_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/songtime"
app:layout_constraintVertical_bias="0.56"
android:layout_marginRight="8dp"
android:layout_marginLeft="16dp" />
<ImageButton
android:id="#+id/imageButton2new"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="15dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:visibility="gone"
android:clickable="true"
android:focusable="true"
android:scaleType="centerCrop"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#mipmap/baseline_thumb_up_alt_black_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/songtime"
app:layout_constraintVertical_bias="0.56"
android:layout_marginRight="8dp"
android:layout_marginLeft="16dp" />
<ImageButton
android:id="#+id/button"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="15dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="8dp"
android:scaleType="centerCrop"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#mipmap/baseline_thumb_down_alt_black_18"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/endTime"
app:layout_constraintVertical_bias="0.56"
android:layout_marginRight="16dp"
android:layout_marginLeft="8dp" />
<ImageButton
android:id="#+id/buttontwo"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="15dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="8dp"
android:visibility="gone"
android:scaleType="centerCrop"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#mipmap/baseline_thumb_down_alt_black_18"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/endTime"
app:layout_constraintVertical_bias="0.56"
android:layout_marginRight="16dp"
android:layout_marginLeft="8dp" />
<TextView
android:id="#+id/songtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif"
android:text="1:05"
android:textSize="12sp"
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/endTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif"
android:text="3:06"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<LinearLayout
android:id="#+id/linearLayout5"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<ImageButton
android:id="#+id/play_button_main"
android:layout_width="55dp"
android:layout_height="55dp"
android:scaleType="centerCrop"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/play_button" />
<ImageButton
android:id="#+id/pause_button_main"
android:layout_width="55dp"
android:visibility="gone"
android:scaleType="centerCrop"
android:layout_height="55dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/pause_button" />
</LinearLayout>
<ImageButton
android:layout_width="55dp"
android:id="#+id/pervious"
android:layout_height="55dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:padding="15dp"
android:clickable="true"
android:focusable="true"
android:scaleType="fitCenter"
android:src="#drawable/backword_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/linearLayout5"
app:layout_constraintHorizontal_bias="0.754"
app:layout_constraintStart_toEndOf="#+id/imageButton2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<ImageButton
android:id="#+id/next"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:padding="15dp"
android:scaleType="fitCenter"
android:src="#drawable/forword_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintHorizontal_bias="0.25"
app:layout_constraintStart_toEndOf="#+id/linearLayout5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
/>
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
<SeekBar
android:id="#+id/seekBar3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/relativeLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/relativeLayout"
app:layout_constraintVertical_bias="0.75" />
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
package music.player.musicplayer;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class MainActivity extends AppCompatActivity {
private ArrayList<Song> songList;
private ListView songView;
LinearLayout nextactivity;
SeekBar seekBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
songView = findViewById(R.id.song_list);
songList = new ArrayList<Song>();
seekBar=findViewById(R.id.seekBar2);
getSongList();
Collections.sort(songList, new Comparator<Song>(){
public int compare(Song a, Song b){
return a.getTitle().compareTo(b.getTitle());
}
});
SongAdapter songAdt = new SongAdapter(this, songList);
songView.setAdapter(songAdt);
nextactivity=findViewById(R.id.mediacontroller);
nextactivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
songView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
//song info method retrive
public void getSongList() {
//retrieve song info
ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if (musicCursor != null && musicCursor.moveToFirst()) {
//get columns
int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
int dataColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA);
//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
String thisPath = musicCursor.getString(dataColumn);
songList.add(new Song(thisId, thisTitle, thisArtist, thisPath));
}
while (musicCursor.moveToNext());
}
}
}
package music.player.musicplayer;
public class Song {
private long id;
private String title;
private String artist;
private String getpath;
public long getId() {
return id;
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
public String getGetpath() {
return getpath;
}
public Song(long id, String title, String artist, String getpath ) {
this.id = id;
this.title = title;
this.artist = artist;
this.getpath = getpath;
}
}
package music.player.musicplayer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
class SongAdapter extends BaseAdapter{
private ArrayList<Song> songs;
private LayoutInflater songInf;
public SongAdapter(Context c, ArrayList<Song> theSongs){
songs=theSongs;
songInf=LayoutInflater.from(c);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return songs.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout songLay = (RelativeLayout)songInf.inflate(R.layout.song, parent, false);
//get title and artist views
TextView songView = (TextView)songLay.findViewById(R.id.song_title);
TextView artistView = (TextView)songLay.findViewById(R.id.song_artist);
//get song using position
Song currSong = songs.get(position);
//get title and artist strings
songView.setText(currSong.getTitle());
artistView.setText(currSong.getArtist());
//set position as tag
songLay.setTag(position);
return songLay;
}
}
Here is my activities picture you get idea what i actually i want
actity two:
So, I am trying to do an application which gives a user the ability to keep track of the score and number of fouls of two different teams playing football
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:text="Team A"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:id="#+id/team_a_goal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<TextView
android:id="#+id/team_a_foul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="8dp"
android:onClick="addGoalForTeamA"
android:text="GOAL" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="8dp"
android:onClick="addFoulForTeamA"
android:text="FOUL" />
</LinearLayout>
<view
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:text="Team B"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:id="#+id/team_b_goal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<TextView
android:id="#+id/team_b_foul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="8dp"
android:onClick="addGoalForTeamB"
android:text="GOAL" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="8dp"
android:onClick="addFoulForTeamB"
android:text="FOUL" />
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:onClick="Reset"
android:text="Reset" />
and java code
package com.example.android.scorekeeper;
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int goalTeamA = 0;
int foulTeamA = 0;
int goalTeamB = 0;
int foulTeamB = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addGoalForTeamA(View view) {
goalTeamA = +1;
displayGoalTeamA(goalTeamA);
}
public void addFoulForTeamA(View view) {
foulTeamA = +1;
displayFoulTeamA(foulTeamA);
}
public void addGoalForTeamB(View view) {
goalTeamB = +1;
displayGoalTeamB(goalTeamB);
}
public void addFoulForTeamB(View view) {
foulTeamB = +1;
displayFoulTeamB(foulTeamB);
}
public void Reset(View view) {
goalTeamA = 0;
foulTeamA = 0;
goalTeamB = 0;
foulTeamB = 0;
displayGoalTeamA(goalTeamA);
displayFoulTeamA(foulTeamA);
displayGoalTeamB(goalTeamB);
displayFoulTeamB(foulTeamB);
}
public void displayGoalTeamA(int score) {
TextView scoreView = findViewById(R.id.team_a_goal);
scoreView.setText(String.valueOf(score));
}
public void displayFoulTeamA(int score) {
TextView scoreView = findViewById(R.id.team_a_foul);
scoreView.setText(String.valueOf(score));
}
public void displayGoalTeamB(int score) {
TextView scoreView = findViewById(R.id.team_b_goal);
scoreView.setText(String.valueOf(score));
}
public void displayFoulTeamB(int score) {
TextView scoreView = findViewById(R.id.team_b_foul);
scoreView.setText(String.valueOf(score));
} }
The code seems fine to me and Android Studio doesn't report any errors but it did't launch on my android
make sure you have added your activity to your manifest file
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I have a void method synchronizeNum(String String String) Which will be trigger in onViewCreated , This Line.
new DisDepartmentSender(myView.getContext(), urlAddress, accountEmail, departmentName).execute();
It will get data from database and finally pass back and set the data into this method synchronizeNum (String String String).After i set the data i wish to display it using TextView. I think the only way is execute again onViewCreated method, since we cannot setText inside synchronizeNum method.I have tried many detach and attach method inside the method but the problem is it get NullPointer i donno how to solve i have been looking around for 1 day just for this little problem. I need you guys help please.
CurrentDepartmentView FragmentClass
package com.example.cheng.freequeue.DisplayDepartmentStatus;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.cheng.freequeue.R;
/**
* Created by cheng on 11/4/2018.
*/
public class CurrentStatusView extends Fragment {
DisDepartmentList disDepartmentList=new DisDepartmentList();
final static String urlAddress = "https://kokcheng95.000webhostapp.com/departmentStatus.php";
private String CompanyName, accountEmail, departmentName, imageUrl;
private String num1room1, num2room2, num3room3, num1, num2, num3, room1, room2, room3;
private final Handler handler = new Handler();
View myView;
Context c;
ViewGroup container;
private LayoutInflater inflater;
TextView company_name, department_name, num_1, num_2, num_3, room_1, room_2, room_3;
de.hdodenhof.circleimageview.CircleImageView image_department;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.current_department_view, container, false);
this.container=container;
company_name = (TextView) myView.findViewById(R.id.company_name);
department_name = (TextView) myView.findViewById(R.id.department_name);
image_department = (de.hdodenhof.circleimageview.CircleImageView) myView.findViewById(R.id.image_department);
num_1 = (TextView) myView.findViewById(R.id.number1);
num_1 = (TextView) myView.findViewById(R.id.number1);
num_2 = (TextView) myView.findViewById(R.id.number2);
num_3 = (TextView) myView.findViewById(R.id.number3);
room_1 = (TextView) myView.findViewById(R.id.room1);
room_2 = (TextView) myView.findViewById(R.id.room2);
room_3 = (TextView) myView.findViewById(R.id.room3);
new DisDepartmentSender(myView.getContext(), urlAddress, accountEmail, departmentName).execute();
//Set data
company_name.setText("Company Name : " + CompanyName);
department_name.setText("Department : " + departmentName);
PicassoClientDepartment.dowloadImage(myView.getContext(), imageUrl, image_department);
//Set Data
num_1.setText(num1);
num_2.setText(num1);
num_3.setText(num1);
room_1.setText(room1);
room_2.setText(room2);
room_3.setText(room3);
return myView;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
if (bundle != null) {
CompanyName = bundle.getString("CompanyName", "noText");
accountEmail = bundle.getString("accountEmail", "noText");
departmentName = bundle.getString("departmentName", "noText");
imageUrl = bundle.getString("imageUrl", "noText");
}
}
public void synchronizeNum(String num1room1, String num2room2, String num3room3) {
String path1[] = num1room1.split("_");
String path2[] = num2room2.split("_");
String path3[] = num3room3.split("_");
num1 = path1[0];
room1 = path1[1];
num2 = path2[0];
room2 = path2[1];
num3 = path3[0];
room3 = path3[1];
/* Fragment currentFragment = getActivity().getFragmentManager().findFragmentByTag("CurrentStatusView");
FragmentTransaction fragTransaction = (getActivity()).getFragmentManager().beginTransaction();
fragTransaction.detach(currentFragment);
fragTransaction.attach(currentFragment);
fragTransaction.commit();*/
}
public void setNum1room1(String num1room1) {
this.num1room1 = num1room1;
}
public String getNum1room1() {
return num1room1;
}
public void setNum2room2(String num2room2) {
this.num2room2 = num2room2;
}
public String getNum2room2() {
return num2room2;
}
public void setNum3room3(String num3room3) {
this.num3room3 = num3room3;
}
public String getNum3room3() {
return num3room3;
}
}
current_department_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.cheng.freequeue.LoginPage">
<TextView
android:id="#+id/department_name"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:text="Department : Orthopedic"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.163" />
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.458">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/textView6"
android:layout_width="200dp"
android:layout_height="80dp"
android:layout_margin="0.1sp"
android:background="#drawable/cell_shape"
android:text="Number"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="40dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView5"
android:layout_width="150dp"
android:layout_height="80dp"
android:layout_marginTop="0.1sp"
android:background="#drawable/cell_shape"
android:text="Room"
android:textAlignment="center"
android:textColor="#080808"
android:textSize="40dp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp">
<TextView
android:id="#+id/number1"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_margin="0.1sp"
android:background="#drawable/cell_shape_body"
android:text="1001"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/room1"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginTop="0.1sp"
android:background="#drawable/cell_shape_body"
android:text="2"
android:textAlignment="center"
android:textColor="#080808"
android:textSize="30dp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp">
<TextView
android:id="#+id/number2"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_margin="0.1sp"
android:background="#drawable/cell_shape_body"
android:text="1002"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/room2"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginTop="0.1sp"
android:background="#drawable/cell_shape_body"
android:text="2"
android:textAlignment="center"
android:textColor="#080808"
android:textSize="30dp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp">
<TextView
android:id="#+id/number3"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_margin="0.1sp"
android:background="#drawable/cell_shape_body"
android:text="1003"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/room3"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginTop="0.1sp"
android:background="#drawable/cell_shape_body"
android:text="2"
android:textAlignment="center"
android:textColor="#080808"
android:textSize="30dp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<Button
android:id="#+id/btn_booking"
android:layout_width="350dp"
android:layout_height="50dp"
android:background="#d7b9b9b9"
android:text="BOOKING"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold"
android:visibility="visible"
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.965" />
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/image_department"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.015"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.048"
app:civ_border_color="#ffffff"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/company_name"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Company Name: TNB"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.064" />
</android.support.constraint.ConstraintLayout>
This Line Error ,NullPointer
Fragment currentFragment=getActivity().getFragmentManager().findFragmentByTag("CurrentStatusView");
Logcat error
04-12 20:52:13.112 3021-3021/com.example.cheng.freequeue E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.cheng.freequeue.DisplayDepartmentStatus.CurrentStatusView.synchronizeNum(CurrentStatusView.java:104)
at com.example.cheng.freequeue.DisplayDepartmentStatus.DisDepartmentParse.onPostExecute(DisDepartmentParse.java:64)
at com.example.cheng.freequeue.DisplayDepartmentStatus.DisDepartmentParse.onPostExecute(DisDepartmentParse.java:18)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5166)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
at dalvik.system.NativeStart.main(Native Method)
I recommend to create an interface for updating data.
public interface SynchronizeCallback {
void synchronizeNum(String num1room1, String num2room2, String num3room3);
}
Your fragment should implement this interface.
Also set the text to the TextViews inside synchronizeNum():
num_1.setText(num1);
num_2.setText(num1);
num_3.setText(num1);
room_1.setText(room1);
room_2.setText(room2);
room_3.setText(room3);
Pass the interface to your task when you create it:
new DisDepartmentSender(myView.getContext(), urlAddress, accountEmail, departmentName, this).execute();
In onPostExecute of your AsyncTask call
syncronizeCallback.updateValues(newNum1room1, newNum2room2, newNum3room3);
Then you should see the new data.
I am working on an app to time and score basketball games, when I added the countdown part and a button to control the count to the MainActivity.java file, the app builds fine but crashes every time. Here are codes I used.
Here is the error generated every time i try to launch the app
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abdulkarim.courtcounter, PID: 19740
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.abdulkarim.courtcounter/com.example.abdulkarim.courtcounter.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:116)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:50)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.abdulkarim.courtcounter.MainActivity.<init>(MainActivity.java:14)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
MainActivity.java
package com.example.abdulkarim.courtcounter;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0, scoreTeamB = 0;
TextView minutes = (TextView) findViewById(R.id.min_counter);
TextView seconds = (TextView) findViewById(R.id.sec_counter);
long milliLeft, min, sec;
CountDownTimer gameTime;
final Button timeoutButton = (Button) findViewById(R.id.timeout_button);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the +3 points button is clicked
*/
public void threePointsA(View view){
scoreTeamA = scoreTeamA + 3;
displayScoreA(scoreTeamA);
}
/**
* This method is called when the +2 points button is clicked
*/
public void twoPointsA(View view){
scoreTeamA = scoreTeamA + 2;
displayScoreA(scoreTeamA);
}
/**
* This method is called when the Free Throw button is clicked
*/
public void freeThrowA(View view){
scoreTeamA = scoreTeamA + 1;
displayScoreA(scoreTeamA);
}
/**
* This method is called when the +3 points button is clicked
*/
public void threePointsB(View view){
scoreTeamB = scoreTeamB + 3;
displayScoreB(scoreTeamB);
}
/**
* This method is called when the +2 points button is clicked
*/
public void twoPointsB(View view){
scoreTeamB = scoreTeamB + 2;
displayScoreB(scoreTeamB);
}
/**
* This method is called when the Free Throw button is clicked
*/
public void freeThrowB(View view){
scoreTeamB = scoreTeamB + 1;
displayScoreB(scoreTeamB);
}
/**
* This method is called when the reset button is clicked
*/
public void reset(View view){
scoreTeamA = 0;
scoreTeamB = 0;
displayScoreA(scoreTeamA);
displayScoreB(scoreTeamB);
}
/**
* This method initializes the countdown timer
*/
public void gameTime(long timeLengthMilli) {
gameTime = new CountDownTimer(timeLengthMilli, 1000) {
#Override
public void onTick(long milliTillFinish) {
milliLeft = milliTillFinish;
min = (milliTillFinish / (1000 * 60));
sec = ((milliTillFinish / 1000) - min * 60);
minutes.setText(Long.toString(min));
seconds.setText(Long.toString(sec));
Log.i("Tick", "Tock");
}
public void onFinish() {
minutes.setText("00");
seconds.setText("00");
}
}.start();
}
public void timerPause() {
gameTime.cancel();
}
private void timerResume() {
Log.i("min", Long.toString(min));
Log.i("Sec", Long.toString(sec));
gameTime(milliLeft);
}
/**
*This method is called when the timeOut button is clicked
*/
public void timeOut(View view){
timeoutButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(timeoutButton.getText().equals("Start")){
Log.i("Started", timeoutButton.getText().toString());
timeoutButton.setText("Timeout");
gameTime(60*1000);
} else if (timeoutButton.getText().equals("Pause")){
Log.i("Timeout", timeoutButton.getText().toString());
timeoutButton.setText("Resume");
timerPause();
} else if (timeoutButton.getText().equals("Resume")){
timeoutButton.setText("Timeout");
timerResume();
}
}
});
}
/**
* This method handles the display of scores for team A
*/
private void displayScoreA(int score){
TextView scoreTextView = (TextView) findViewById(R.id.teamA_score_text_view);
scoreTextView.setText(String.valueOf(score));
}
/**
* This method handles the display of scores for team A
*/
private void displayScoreB(int score){
TextView scoreTextView = (TextView) findViewById(R.id.teamB_score_text_view);
scoreTextView.setText(String.valueOf(score));
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abdulkarim.courtcounter.MainActivity">
<RelativeLayout
android:id="#+id/r1_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/min_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="75dp"
android:paddingTop="16dp"
android:text="Mins"
android:textAllCaps="true"
android:textColor="#android:color/black"
android:textSize="10sp" />
<TextView
android:id="#+id/sec_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingTop="16dp"
android:layout_toRightOf="#id/min_text_view"
android:text="Secs"
android:textAllCaps="true"
android:textColor="#android:color/black"
android:textSize="10sp" />
<TextView
android:id="#+id/min_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/min_text_view"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif-light"
android:text="00"
android:textColor="#000000"
android:textSize="56sp" />
<TextView
android:id="#+id/blinker_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/min_text_view"
android:layout_marginTop="8dp"
android:layout_toRightOf="#id/min_counter"
android:fontFamily="sans-serif-light"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text=":"
android:textColor="#000000"
android:textSize="56sp" />
<TextView
android:id="#+id/sec_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/min_text_view"
android:layout_marginTop="8dp"
android:layout_toRightOf="#id/blinker_text_view"
android:fontFamily="sans-serif-light"
android:text="00"
android:textColor="#000000"
android:textSize="56sp" />
</RelativeLayout>
<LinearLayout
android:id="#+id/l1_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/r1_layout"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:text="Team A"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:id="#+id/teamA_score_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="24dp"
android:fontFamily="sans-serif-medium"
android:onClick="threePointsA"
android:text="+3 Points"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:fontFamily="sans-serif-medium"
android:onClick="twoPointsA"
android:text="+2 Points"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:fontFamily="sans-serif-medium"
android:onClick="freeThrowA"
android:text="Free Throw"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:background="#android:color/darker_gray"></View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:text="Team B"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:id="#+id/teamB_score_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="24dp"
android:fontFamily="sans-serif-medium"
android:onClick="threePointsB"
android:text="+3 Points"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:fontFamily="sans-serif-medium"
android:onClick="twoPointsB"
android:text="+2 Points"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:fontFamily="sans-serif-medium"
android:onClick="freeThrowB"
android:text="Free Throw"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/timeout_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginBottom="32dp"
android:layout_marginLeft="16dp"
android:text="Start"
android:fontFamily="sans-serif-medium"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp"
android:onClick="timeOut"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="8dp"
android:fontFamily="sans-serif-medium"
android:onClick="Reset"
android:text="Reset"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp" />
</LinearLayout>
</RelativeLayout>
You can't call findViewById before the onCreate method.
Move the initialization of your view references inside onCreate.
public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0, scoreTeamB = 0;
TextView minutes;
TextView seconds;
long milliLeft, min, sec;
CountDownTimer gameTime;
Button timeoutButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
minutes = (TextView) findViewById(R.id.min_counter);
seconds = (TextView) findViewById(R.id.sec_counter);
timeoutButton = (Button) findViewById(R.id.timeout_button);
}
Declare all the variable in global
Move findViewById in onCreate method. You should initialize them inside onCreate method, not in global.
public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0, scoreTeamB = 0;
TextView minutes;
TextView seconds;
long milliLeft, min, sec;
CountDownTimer gameTime;
final Button timeoutButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
minutes = (TextView) findViewById(R.id.min_counter);
seconds = (TextView) findViewById(R.id.sec_counter);
timeoutButton = (Button) findViewById(R.id.timeout_button);
}
// others code...
}