Clickable TableRow on Android - java

I have a simple program i have made in java for android but am having difficulty in making the tablerow clickable. When the user clicks a row i wish for a certain picture to be displayed in the imageview below. Here is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF909090"
android:stretchColumns="1" >
<TableRow>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="I" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="A"
android:onClick="Achord" />
</TableRow>
<TableRow>
<TextView
android:layout_column="0"
android:layout_margin="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="II" />
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="Bm" />
</TableRow>
<TableRow>
<TextView
android:layout_column="0"
android:layout_margin="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="III" />
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="C#m" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow android:background="#0000ff" >
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="IV" />
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="D" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="V" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="E" />
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="VI" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="F#m" />
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="VII" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="G#dim" />
</TableRow>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#000000"
android:maxLines="10"
android:text="The relative minor of the Major key of A is F#m. The 12 bar blues progression in A is: \n A | A | A | A | D | D | A | A | E | D | A | E" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.03"
android:src="#drawable/android_focused" />
and here is my java code:
package com.coreservlets.widgets;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TableRow;
import com.welly.keychords.R;
public class keya extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keya);
}
}
what i want to happen is that imageview1 will display a different image depending on what row is clicked. Any help would be greatly appreciated
As sam suggested i have ammended my code to this:
public class keya extends Activity {
ImageView imagev = (ImageView) findViewById(R.id.imageView1);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keya);
}
public void rowClick(View view) {
switch(view.getId()) {
case R.id.one:
// Load image from Drawable folder
imagev.setImageResource(R.drawable.android_normal); // example file for now
break;
}
}
}
but now i have a null pointer exception on running that class. all compiles fine though

Simply give each TableRow element a unique id and define an onClick method:
<TableRow
android:id="#+id/one"
android:onClick="rowClick">
Add a class variable imageView that references the ImageView in your layout. Then load the image:
public void rowClick(View view) {
switch(view.getId()) {
case R.id.one:
// Load image from Drawable folder
imageView.setImageResource(R.id.imageOne);
break;
}
}
Understand that any elements inside a TableRow with their own onClick method will call its own method. For instance the TextView with android:onClick="Achord" will call Achord() not rowClick().
Addition
You need to call findViewById() after declaring your layout with setContentView() in onCreate().
ImageView imagev;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keya);
imagev = (ImageView) findViewById(R.id.imageView1);
}

Give each row an ID, then you can assign a listener to each one in your code:
TableRow row = (TableRow)findViewById( R.id.row1 );
row.setOnClickListener( new OnClickListener() {
#Override
public void onClick( View v ) {
//Do Stuff
}
} );

You need to call setOnClickListener() for each of your TableRow objects so that framework knows what to do when a row is clicked. In addition your Activity should implement View.OnClickListener() so that you can control what happens when a row gets clicked. Right now you aren't doing anything when the View gets clicked; at least according to your code you aren't registering a callback on any of the Views' onClickListeners

Assign id to Tablerow just like row1
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF909090"
android:stretchColumns="1" >
<TableRow
android:id="#+id/row1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="I" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="A"
android:onClick="Achord" />
</TableRow>
<TableRow>
<TextView
android:layout_column="0"
android:layout_margin="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="II" />
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="Bm" />
</TableRow>
<TableRow>
<TextView
android:layout_column="0"
android:layout_margin="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="III" />
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="C#m" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow android:background="#0000ff" >
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="IV" />
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="D" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="V" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="E" />
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="VI" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="F#m" />
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="VII" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="G#dim" />
</TableRow>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#000000"
android:maxLines="10"
android:text="The relative minor of the Major key of A is F#m. The 12 bar blues progression in A is: \n A | A | A | A | D | D | A | A | E | D | A | E" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.03" />
</TableLayout>
Call it in activity like this.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TableRow;
import com.welly.keychords.R;
public class keya extends Activity {
TableRow row1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keya);
row1 = (TableRow) findViewById(R.id.row1);
row1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.e("Click ", "Row 1");
}
});
}
}

i got it working by declaring the imageview variable within the public void rowclick

Assign an id to Tablerow in xml layout file:
<TableRow
android:id="#+id/rowL1"
...
</TableRow>
Add the following in your java file:
private TableRow rowL1;
//In onCreateView...
rowL1 = (TableRow) rootView.findViewById(R.id.rowL1);
rowL1.setOnClickListener(v -> {
//Your code here...
});

Related

Android studio checkbox gets unchecked when moved from one screen to next screen

I am new to android studio, I tried all possible options, but unable to understand why checkbox gets unchecked when moved from one screen to other when I come back to previous screen.
I am missing out something in onsavedInstanceState or onRestoreInstanceState
Below is my Java Code
package com.example.qualitycheckstation;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Qc2_1_1 extends AppCompatActivity {
CheckBox qc2_1cb1a,qc2_1cb1b,qc2_1cb1c, qc2_1cb2a,qc2_1cb2b,qc2_1cb2c ,qc2_1cb3a,qc2_1cb3b, qc2_1cb3c;
TextView date2_1_1, time2_1_1;
Button next2_1_1,back2_1_1;
Boolean myBoolean1,myBoolean2,myBoolean3,myBoolean4,myBoolean5,myBoolean6,myBoolean7,myBoolean8,myBoolean9;
private static String pr2_1_1,pr2_1_2,pr2_1_3;
private boolean myBoolean = false;
public static String getPr2_1_1 (){
return pr2_1_1;
}
public static String getPr2_1_2 (){
return pr2_1_2;
}
public static String getPr2_1_3(){
return pr2_1_3;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qc2_1_1);
date2_1_1= findViewById(R.id.date2_1_1);
time2_1_1 = findViewById(R.id.time2_1_1);
Calendar calendar= Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
String date = simpleDateFormat.format(calendar.getTime());
date2_1_1.setText(date);
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("hh:mm:ss a");
String time0 = simpleDateFormat1.format(calendar.getTime());
time2_1_1.setText(time0);
qc2_1cb1a = findViewById(R.id.qc2_1cb1a);
qc2_1cb1b = findViewById(R.id.qc2_1cb1b);
qc2_1cb1c = findViewById(R.id.qc2_1cb1c);
qc2_1cb2a = findViewById(R.id.qc2_1cb2a);
qc2_1cb2b = findViewById(R.id.qc2_1cb2b);
qc2_1cb2c = findViewById(R.id.qc2_1cb2c);
qc2_1cb3a = findViewById(R.id.qc2_1cb3a);
qc2_1cb3b = findViewById(R.id.qc2_1cb3b);
qc2_1cb3c = findViewById(R.id.qc2_1cb3c);
next2_1_1 = findViewById(R.id.next2_1_1);
back2_1_1 = findViewById(R.id.back2_1_1);
next2_1_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if((qc2_1cb1a.isChecked())){
pr2_1_1 = "O";
}
else if((qc2_1cb1b.isChecked())){
pr2_1_1 = "X";
}
else if((qc2_1cb1c.isChecked())){
pr2_1_1 = "N/A";
}
if((qc2_1cb2a.isChecked())){
pr2_1_2 = "O";
}
else if((qc2_1cb2b.isChecked())){
pr2_1_2 = "X";
}
else if((qc2_1cb2c.isChecked())){
pr2_1_2 = "N/A";
}
if((qc2_1cb3a.isChecked())){
pr2_1_3 = "O";
}
else if((qc2_1cb3b.isChecked())){
pr2_1_3 = "X";
}
else if((qc2_1cb3c.isChecked())){
pr2_1_3 = "N/A";
}
if (
((qc2_1cb1a.isChecked()) && (qc2_1cb1b.isChecked())) || ((qc2_1cb1b.isChecked()) && (qc2_1cb1c.isChecked())) || ((qc2_1cb1a.isChecked()) && (qc2_1cb1c.isChecked())) ||
((qc2_1cb2a.isChecked()) && (qc2_1cb2b.isChecked())) || ((qc2_1cb2b.isChecked()) && (qc2_1cb2c.isChecked())) || ((qc2_1cb2a.isChecked()) && (qc2_1cb2c.isChecked())) ||
((qc2_1cb3a.isChecked()) && (qc2_1cb3b.isChecked())) || ((qc2_1cb3b.isChecked()) && (qc2_1cb3c.isChecked())) || ((qc2_1cb3a.isChecked()) && (qc2_1cb3c.isChecked()))
)
{ { Toast.makeText(getApplicationContext(),"Select Either OK, NG or N/A", Toast.LENGTH_SHORT).show(); }}
else if (
((qc2_1cb1a.isChecked()) || (qc2_1cb1b.isChecked()) || (qc2_1cb1c.isChecked())) &&
((qc2_1cb2a.isChecked()) || (qc2_1cb2b.isChecked()) || (qc2_1cb2c.isChecked()))&&
((qc2_1cb3a.isChecked()) || (qc2_1cb3b.isChecked()) || (qc2_1cb3c.isChecked()))
)
{ Intent intent = new Intent(Qc2_1_1.this, Qc2_1_2.class);startActivity(intent); }
else { Toast.makeText(getApplicationContext(),"Select all parameters", Toast.LENGTH_SHORT).show(); }
}
});
back2_1_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Qc2_1_1.this, Qc2.class);
startActivity(intent);
}
});
}
#Override
protected void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("qc2_1cb1a", qc2_1cb1a.isChecked());
outState.putBoolean("qc2_1cb1b", qc2_1cb1b.isChecked());
outState.putBoolean("qc2_1cb1c", qc2_1cb1c.isChecked());
outState.putBoolean("qc2_1cb2a", qc2_1cb2a.isChecked());
outState.putBoolean("qc2_1cb2b", qc2_1cb2b.isChecked());
outState.putBoolean("qc2_1cb2c", qc2_1cb2c.isChecked());
outState.putBoolean("qc2_1cb3a", qc2_1cb3a.isChecked());
outState.putBoolean("qc2_1cb3b", qc2_1cb3b.isChecked());
outState.putBoolean("qc2_1cb3c", qc2_1cb3c.isChecked());
}
#Override
protected void onRestoreInstanceState(#NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
myBoolean1 = savedInstanceState.getBoolean("qc2_1cb1a");
myBoolean2 = savedInstanceState.getBoolean("qc2_1cb1b");
myBoolean3 = savedInstanceState.getBoolean("qc2_1cb1c");
myBoolean4 = savedInstanceState.getBoolean("qc2_1cb2a");
myBoolean5 = savedInstanceState.getBoolean("qc2_1cb2b");
myBoolean6 = savedInstanceState.getBoolean("qc2_1cb2c");
myBoolean7 = savedInstanceState.getBoolean("qc2_1cb3a");
myBoolean8 = savedInstanceState.getBoolean("qc2_1cb3b");
myBoolean9 = savedInstanceState.getBoolean("qc2_1cb3c");
qc2_1cb1a.setChecked(myBoolean1);
qc2_1cb1b.setChecked(myBoolean2);
qc2_1cb1c.setChecked(myBoolean3);
qc2_1cb2a.setChecked(myBoolean4);
qc2_1cb2b.setChecked(myBoolean5);
qc2_1cb2c.setChecked(myBoolean6);
qc2_1cb3a.setChecked(myBoolean7);
qc2_1cb3b.setChecked(myBoolean8);
qc2_1cb3c.setChecked(myBoolean9);
}
}
xml Code
<TableLayout
android:layout_width="match_parent"
android:layout_height="462dp"
android:layout_marginStart="15dp"
android:layout_marginTop="125dp"
android:layout_marginEnd="15dp"
android:background="#color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="Sl No"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Measured Item"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="199dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="OP No"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text="Gauge Used"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="Parameter"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="91dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="OK"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="92dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="NG"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="N/A"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="1."
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Groove Surface"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="25"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text=" Visual"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="131dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="Exist"
android:textColor="#color/black"
android:textSize="25dp" />
<CheckBox
android:id="#+id/qc2_1cb1a"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_green" />
<CheckBox
android:id="#+id/qc2_1cb1b"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
<CheckBox
android:id="#+id/qc2_1cb1c"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="2."
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="291dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Serial Engraving Surface"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="291dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="30"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="41dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text=" Visual"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="Numbers should be readable"
android:textColor="#color/black"
android:textSize="25dp" />
<CheckBox
android:id="#+id/qc2_1cb2a"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_green" />
<CheckBox
android:id="#+id/qc2_1cb2b"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
<CheckBox
android:id="#+id/qc2_1cb2c"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="3."
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="251dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Machining Surface"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="251dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="25"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="31dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text=" Visual"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="328dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="No rough surface, unwash surface,scratch, burr"
android:textColor="#color/black"
android:textSize="25dp" />
<CheckBox
android:id="#+id/qc2_1cb3a"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_green" />
<CheckBox
android:id="#+id/qc2_1cb3b"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
<CheckBox
android:id="#+id/qc2_1cb3c"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
</TableRow>
</TableLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="360dp"
android:layout_marginTop="32dp"
android:text="QC Station 2.1"
android:textColor="#color/black"
android:textSize="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="211dp"
android:layout_height="68dp"
android:layout_marginStart="25dp"
android:layout_marginTop="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo" />
<TextView
android:id="#+id/date2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:layout_marginEnd="50dp"
android:ems="6"
android:hint="DD/MM/YYYY"
android:inputType="date"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/time2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:layout_marginEnd="50dp"
android:ems="6"
android:hint="HH.MM.SS"
android:inputType="time"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/next2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="25dp"
android:layout_marginBottom="30dp"
android:background="#drawable/button"
android:text="next"
android:textColor="#color/white"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="#+id/back2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginBottom="30dp"
android:background="#drawable/button"
android:text="Back"
android:textColor="#color/white"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Any support to solve this would be of great help
You should get values from savedInstanceState in onCreate() method. Someting like:
if (savedInstanceState!=null){
myBoolean1 = savedInstanceState.getBoolean("qc2_1cb1a");
myBoolean2 = savedInstanceState.getBoolean("qc2_1cb1b");
myBoolean3 = savedInstanceState.getBoolean("qc2_1cb1c");
myBoolean4 = savedInstanceState.getBoolean("qc2_1cb2a");
myBoolean5 = savedInstanceState.getBoolean("qc2_1cb2b");
myBoolean6 = savedInstanceState.getBoolean("qc2_1cb2c");
myBoolean7 = savedInstanceState.getBoolean("qc2_1cb3a");
myBoolean8 = savedInstanceState.getBoolean("qc2_1cb3b");
myBoolean9 = savedInstanceState.getBoolean("qc2_1cb3c");
}
and then assign it to your check boxes

Android: Array loops return null elements

I have been trying to use getResources().getIdentifier() methods to assign ids to an array of images. However, when I consoled the array, I only got 2 valid elements and the rest was null. Could somebody please explain to me why after the onCreate method is executed, the program only consoles the values of the first 2 loops and returns null values afterward?
Currently: [ImageView0, ImageView1, null, null, null, null, null, null, null]
What should I do to make it fully loop through the array and return an array with valid values of [ImageView1, ImageView2, ImageView3, ImageView4, ImageView5, ImageView6, ImageView7, and ImageView8] instead ?
Many thanks! I am appreciated any help!
INPUT
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
ImageView[] computerImages;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
computerImages = new ImageView[9];
for (int i=0; i<computerImages.length; i++){
String computerImageID = "ImageView"+i;
int resID = getResources().getIdentifier(computerImageID,"id",getPackageName());
computerImages[i] = findViewById(resID);
} Log.i("ON CREATE", Arrays.toString(computerImages));
}
OUTPUT
I/ON CREATE: [android.support.v7.widget.AppCompatImageView{fe40fe0 VFED..C.. ......ID 0,0-0,0 #7f070003 app:id/ImageView0}, android.support.v7.widget.AppCompatImageView{8313299 VFED..C.. ......ID 0,0-0,0 #7f070004 app:id/ImageView1}, null, null, null, null, null, null, null]
XML LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<Button
android:id="#+id/button"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="Computer" />
<Button
android:id="#+id/button2"
android:layout_width="171dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="Player 02" />
</LinearLayout>
<GridLayout 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/gridLayout"
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/board"
android:columnCount="3"
android:rowCount="3">
<ImageView
android:id="#+id/imageView0"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="0" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="0dp"
android:layout_marginTop="15dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="1" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="15dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="2" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="3" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="1dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="4" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="5" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="6" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="1dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="7" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="8" />
</GridLayout>
<Button
android:id="#+id/playAgainButton"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:onClick="clickReset"
android:text="Play Again"
android:textSize="20sp" />
<TextView
android:id="#+id/winnerMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:background="#android:color/holo_orange_dark"
android:padding="5dp"
android:text="TextView"
android:textColor="#android:color/white"
android:textSize="25sp"
android:visibility="invisible" />
</RelativeLayout>
It returning null because "ImageView3, ImageView4, ImageView5, ImageView6, ImageView7, and ImageView8" views are not present in resource file. Only ImageView0, ImageView1 are present in resource file.

Why my android application has stopped?

My project won't open on my device, it say's "Unfortunately,myApp has stopped".
this is my manifest:
this is my activity java :
package bismillah.project;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Project2Activity extends Activity {
public static int a,b,c;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//coding start here!!
//input data
EditText pek=(EditText)findViewById(R.id.editText1);
String value=pek.getText().toString();
final int pekerja=Integer.parseInt(value);
EditText lam=(EditText)findViewById(R.id.editText2);
String value1=lam.getText().toString();
final int lama=Integer.parseInt(value1);
EditText up=(EditText)findViewById(R.id.editText3);
String value2=up.getText().toString();
final int upah=Integer.parseInt(value2);
EditText jum=(EditText)findViewById(R.id.editText4);
String value3=jum.getText().toString();
final int jumlah=Integer.parseInt(value3);
//button proses
Button button1=(Button)findViewById(R.id.Button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Spinner bibit = (Spinner)findViewById(R.id.spinner1);
TextView biaya = (TextView)findViewById(R.id.textView6);
if(bibit.getSelectedItem().toString().equals("Cabai Rp.100")){
a=pekerja*(lama*upah)+(jumlah*100);
biaya.setText("Biaya Rp." + a);
if(bibit.getSelectedItem().toString().equals("Tomat Rp.150")){
a=pekerja*(lama*upah)+(jumlah*150);
biaya.setText("Biaya Rp." + a);
if(bibit.getSelectedItem().toString().equals("Timun Rp.200")){
a=pekerja*(lama*upah)+(jumlah*200);
biaya.setText("Biaya Rp." + a);
}
}
}
}
});
//button reset
Button button2=(Button)findViewById(R.id.Button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView biaya = (TextView)findViewById(R.id.textView6);
EditText pek=(EditText)findViewById(R.id.editText1);
EditText lam=(EditText)findViewById(R.id.editText2);
EditText up=(EditText)findViewById(R.id.editText3);
EditText jum=(EditText)findViewById(R.id.editText4);
pek.setText("");
lam.setText("");
up.setText("");
jum.setText("");
biaya.setText("");
}
});
//button pindah activity
Button button3=(Button)findViewById(R.id.Button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i =new Intent(getApplicationContext(),activity2.class);
startActivity(i);
}
});
}
}
and this is my main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/bg"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="#string/hello"
android:textColor="#color/warna"
android:textSize="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Jumlah Pekerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Lama Kerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Upah :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jenis Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:entries="#array/list" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jumlah Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textColor="#color/warna2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Proses" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Reset" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hasil Panen" />
</LinearLayout>
</LinearLayout>
I tried your code and I had to delete some stuff from your XML file because it was creating some bugs. It might be related to me but here's your XML I was able to use:
But please do not change your XML file before trying the solution below. If it still doesn't work, you should consider the elements I removed.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="hello"
android:textSize="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Jumlah Pekerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Lama Kerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Upah :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jenis Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jumlah Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Proses" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Reset" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hasil Panen" />
</LinearLayout>
</LinearLayout>
Next I created your Project2Activity java class and activity2 (pretty much empty since you gave no info about it).
So when trying to run your application you get the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application.so/com.example.application.so.Project2Activity}: java.lang.NumberFormatException: For input string: ""
and it is at :
at com.example.application.so.Project2Activity.onCreate(Project2Activity.java:24)
now if we consider this lines 23 and 24:
String value=pek.getText().toString(); // line 23
final int pekerja=Integer.parseInt(value); // line 24
The error is that you are trying to read (line 23) your editText Value at the creation of the activity, at that time, your editText is still blank which means value will be equal to "" (empty string).
What you want to do instead is to put your lines from 22 to 33 (the ones below) in a button click event, I tried putting them in your button 1 click event and your app doesn't crash any more:
EditText pek=(EditText)findViewById(R.id.editText1);
String value=pek.getText().toString();
final int pekerja=Integer.parseInt(value);
EditText lam=(EditText)findViewById(R.id.editText2);
String value1=lam.getText().toString();
final int lama=Integer.parseInt(value1);
EditText up=(EditText)findViewById(R.id.editText3);
String value2=up.getText().toString();
final int upah=Integer.parseInt(value2);
EditText jum=(EditText)findViewById(R.id.editText4);
String value3=jum.getText().toString();
final int jumlah=Integer.parseInt(value3);
So to summarize, the problem is that you are extracting empty strings from your editTexts and trying to make integers out of them. Instead try making that all reading and integer extracting process on the click of a button where your really need these inputs. just like get the inputs and the final moment.
You should pay attention to your logcat (error messages), it is quite helpful.
What you can also do is to verify that your editTexts are not empty while trying to extract strings from them and raise exception or Toast if they are empty. Nothing too complicated just something like:
if (value.equals("")):
// editText is empty so do something
You can refer to this link for further understanding of how to know when an editText is empty.

TextView text animation is restarted if the text is changed in another TextView

In TextView1 text animation runs from right to left, and in TextView2 constantly changing text. The problem is that when I perform TextView2.setText ( "...") in the text animation TextView1 restarted. Is it possible to prevent the restart of the animation?
XML code TextView1:
<TextView
android:id="#+id/artistAlbum"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingTop="2dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="text text text text text text text"
android:textSize="14sp" />
Java code initialization TextView1:
final TextView textView1 = (TextView) rootView.findViewById(R.id.textView1);
textView1.setSelected(true);
XML code TextView2:
<TextView
android:id="#+id/seekSongDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:clickable="true"
android:paddingBottom="12dp"
android:paddingEnd="2dp"
android:paddingStart="10dp"
android:paddingTop="12dp"
android:text="0%"
android:textSize="20sp" />
XML code:
<TextView
android:id="#+id/fullSongDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="#color/background"
android:clickable="true"
android:paddingBottom="12dp"
android:paddingEnd="10dp"
android:paddingStart="2dp"
android:paddingTop="12dp"
android:text="3:15"
android:textSize="20sp" />
<org.adw.library.widgets.discreteseekbar.DiscreteSeekBar
android:id="#+id/seekDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignTop="#id/seekSongDuration"
android:layout_toEndOf="#id/seekSongDuration"
android:layout_toStartOf="#id/fullSongDuration"
android:paddingBottom="10dp"
android:paddingTop="10dp"
app:dsb_trackHeight="3dp" />
<com.andexert.library.RippleView
android:id="#id/butPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_centerHorizontal="true"
android:background="#color/background"
app:rv_alpha="100"
app:rv_centered="true"
app:rv_color="#android:color/white"
app:rv_framerate="15"
app:rv_rippleDuration="300">
<ImageView
android:id="#+id/imgPlayPause"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/ic_play_circle_outline_black_48dp" />
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:id="#+id/butShufle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_alignTop="#id/butPlayPause"
android:background="#color/background"
app:rv_alpha="150"
app:rv_color="#android:color/white"
app:rv_framerate="15"
app:rv_rippleDuration="300">
<ImageView
android:id="#+id/imgShufle"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:src="#drawable/ic_shuffle_black_36dp" />
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:id="#+id/butPreviosSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_alignTop="#id/butPlayPause"
android:layout_toEndOf="#id/butShufle"
android:layout_toStartOf="#id/butPlayPause"
android:background="#color/background"
app:rv_alpha="100"
app:rv_color="#android:color/white"
app:rv_framerate="20"
app:rv_rippleDuration="300"
app:rv_type="rectangle">
<ImageView
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:src="#drawable/ic_fast_rewind_black_48dp" />
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:id="#+id/butLooping"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_alignParentEnd="true"
android:layout_alignTop="#id/butPlayPause"
android:background="#color/background"
app:rv_alpha="150"
app:rv_color="#android:color/white"
app:rv_framerate="20"
app:rv_rippleDuration="300">
<ImageView
android:id="#+id/imgLoopReaped"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:src="#drawable/ic_repeat_black_36dp" />
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:id="#+id/butNextSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_alignTop="#+id/butPlayPause"
android:layout_toEndOf="#id/butPlayPause"
android:layout_toStartOf="#id/butLooping"
android:background="#color/background"
app:rv_alpha="100"
app:rv_color="#android:color/white"
app:rv_framerate="20"
app:rv_rippleDuration="300"
app:rv_type="rectangle">
<ImageView
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:src="#drawable/ic_fast_forward_black_48dp" />
</com.andexert.library.RippleView>
<RelativeLayout
android:id="#id/currentSongLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_above="#+id/butPlayPause"
android:background="#color/backgroundCurrentTrack"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<com.andexert.library.RippleView
android:id="#+id/butSettingCurrentSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
app:rv_alpha="100"
app:rv_centered="true"
app:rv_color="#android:color/black"
app:rv_framerate="15"
app:rv_rippleDuration="300">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="8dp"
android:src="#drawable/ic_more_circle_vert_black_36dp" />
</com.andexert.library.RippleView>
<ImageView
android:id="#+id/iconCurrentPlaylist"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:src="#drawable/ic_playlist_play_black_36dp" />
<TextView
android:id="#+id/txtNumberSongs"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:layout_toEndOf="#id/iconCurrentPlaylist"
android:background="#drawable/shape_ellipse"
android:paddingEnd="6dp"
android:paddingStart="6dp"
android:paddingTop="3dp"
android:text="1/1"
android:textAlignment="center"
android:textColor="#color/grayLite"
android:textSize="12sp" />
<TextView
android:id="#+id/songTitle"
android:layout_width="wrap_content"
android:layout_height="26dp"
android:layout_toEndOf="#id/txtNumberSongs"
android:layout_toStartOf="#id/butSettingCurrentSong"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingLeft="5dp"
android:paddingTop="2dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Title"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/artistAlbum"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_below="#id/songTitle"
android:layout_toEndOf="#id/iconCurrentPlaylist"
android:layout_toStartOf="#id/butSettingCurrentSong"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingTop="2dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Artist - Album"
android:textSize="14sp" />
</RelativeLayout>
Java code:
private TextView songArtistAlbum;
private TextView txtSeek;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
songArtistAlbum = (TextView) rootView.findViewById(R.id.artistAlbum);
songArtistAlbum.setSelected(true);
txtSeek = (TextView) rootView.findViewById(R.id.seekSongDuration);
...
}
private DiscreteSeekBar.OnProgressChangeListener onDurationProgressChangeListener = new DiscreteSeekBar.OnProgressChangeListener() {
#Override
public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
if (!fromUser) {
txtSeek.setText(Total.msecTo_MM_SS(seekBar.getProgress()));
}
}
#Override
public void onStartTrackingTouch(DiscreteSeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(DiscreteSeekBar seekBar) {
txtSeek.setText(Total.msecTo_MM_SS(seekBar.getProgress()));
}
};
Thanks in advance.
Try to wrap your TextView1 with LinearLayout.
Check Activity class TextView object and its assign id.

Could not execute method for android:onClick [duplicate]

This question already has answers here:
Resource not found TextView
(2 answers)
Closed 7 years ago.
Hi i am new to android development and i couldn't execute method when a button is clicked.I retyped the code as in the tutorial but it ended up lot of errors. Check the code below,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Basketball Score Game"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:paddingLeft="70dp"
android:id="#+id/header"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Team A"
android:layout_marginLeft="50dp"
android:layout_marginTop="80dp"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="50sp"
android:id="#+id/score_a"
android:layout_marginLeft="90dp"
android:layout_marginTop="10dp"
android:onClick="display_score_a"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff6000"
android:text="Outside The Ring"
android:onClick="three_pts_a"
android:padding="10dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:layout_height="wrap_content"
android:text="Inside the Ring"
android:layout_marginLeft="40dp"
android:padding="10dp"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:text="Free Throw"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:padding="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Team B"
android:layout_marginLeft="250dp"
android:layout_marginTop="80dp"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="50sp"
android:layout_marginLeft="290dp"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff6000"
android:text="Outside The Ring"
android:layout_marginLeft="230dp"
android:padding="10dp"
android:layout_marginTop="40dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:text="Inside the Ring"
android:layout_height="wrap_content"
android:layout_marginLeft="230dp"
android:layout_marginTop="10dp"
android:padding="10dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:layout_height="wrap_content"
android:layout_marginLeft="230dp"
android:layout_marginTop="10dp"
android:text="Free Throw"
android:padding="10dp"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:background="#ff6000"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
****Mainactivity.java****
package android.mytest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int total_pts1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void three_pts_a(View view)
{
total_pts1 = total_pts1 + 3;
display_score_a(total_pts1);
}
private void display_score_a(int number) {
TextView num = (TextView) findViewById(
R.id.score_a);
num.setText(number);
}
}
its because of the integer variable.you have to parse it to a string or change that line to num.setText(String.valueOf(number));

Categories