Different sound on each button - android

I have an array of the buttons and I want to play different sound by clicking on each button. When I click at some buttons everything is OK, but when I click on other buttons, application crashes, and logcat says:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
This is sample of my code:
public class HardActivity extends ActionBarActivity {
private Button BoardButtons[][];
private MediaPlayer[] playAudio = new MediaPlayer[49];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_medium);
BoardButtons = new Button[Game.getBOARD_SIZE_VERTICAL_MEDIUM()][Game.getBOARD_SIZE_HORIZONTAL_MEDIUM()];
BoardButtons[0][0] = (Button)findViewById(R.id.button1);
BoardButtons[0][1] = (Button)findViewById(R.id.button2);
BoardButtons[0][2] = (Button)findViewById(R.id.button3);
BoardButtons[0][3] = (Button)findViewById(R.id.button4);
BoardButtons[0][4] = (Button)findViewById(R.id.button5);
BoardButtons[0][5] = (Button)findViewById(R.id.button6);
BoardButtons[0][6] = (Button)findViewById(R.id.button7);
BoardButtons[1][0] = (Button)findViewById(R.id.button8);
BoardButtons[1][1] = (Button)findViewById(R.id.button9);
BoardButtons[1][2] = (Button)findViewById(R.id.button10);
BoardButtons[1][3] = (Button)findViewById(R.id.button11);
BoardButtons[1][4] = (Button)findViewById(R.id.button12);
BoardButtons[1][5] = (Button)findViewById(R.id.button13);
BoardButtons[1][6] = (Button)findViewById(R.id.button14);
BoardButtons[2][0] = (Button)findViewById(R.id.button15);
BoardButtons[2][1] = (Button)findViewById(R.id.button16);
BoardButtons[2][2] = (Button)findViewById(R.id.button17);
BoardButtons[2][3] = (Button)findViewById(R.id.button18);
BoardButtons[2][4] = (Button)findViewById(R.id.button19);
BoardButtons[2][5] = (Button)findViewById(R.id.button20);
BoardButtons[2][6] = (Button)findViewById(R.id.button21);
BoardButtons[3][0] = (Button)findViewById(R.id.button22);
BoardButtons[3][1] = (Button)findViewById(R.id.button23);
BoardButtons[3][2] = (Button)findViewById(R.id.button24);
BoardButtons[3][3] = (Button)findViewById(R.id.button25);
BoardButtons[3][4] = (Button)findViewById(R.id.button26);
BoardButtons[3][5] = (Button)findViewById(R.id.button27);
BoardButtons[3][6] = (Button)findViewById(R.id.button28);
BoardButtons[4][0] = (Button)findViewById(R.id.button29);
BoardButtons[4][1] = (Button)findViewById(R.id.button30);
BoardButtons[4][2] = (Button)findViewById(R.id.button31);
BoardButtons[4][3] = (Button)findViewById(R.id.button32);
BoardButtons[4][4] = (Button)findViewById(R.id.button33);
BoardButtons[4][5] = (Button)findViewById(R.id.button34);
BoardButtons[4][6] = (Button)findViewById(R.id.button35);
BoardButtons[5][0] = (Button)findViewById(R.id.button36);
BoardButtons[5][1] = (Button)findViewById(R.id.button37);
BoardButtons[5][2] = (Button)findViewById(R.id.button38);
BoardButtons[5][3] = (Button)findViewById(R.id.button39);
BoardButtons[5][4] = (Button)findViewById(R.id.button40);
BoardButtons[5][5] = (Button)findViewById(R.id.button41);
BoardButtons[5][6] = (Button)findViewById(R.id.button42);
BoardButtons[6][0] = (Button)findViewById(R.id.button43);
BoardButtons[6][1] = (Button)findViewById(R.id.button44);
BoardButtons[6][2] = (Button)findViewById(R.id.button45);
BoardButtons[6][3] = (Button)findViewById(R.id.button46);
BoardButtons[6][4] = (Button)findViewById(R.id.button47);
BoardButtons[6][5] = (Button)findViewById(R.id.button48);
BoardButtons[6][6] = (Button)findViewById(R.id.button49);
Game = new FieldsGame();
startNewGame();
setVolumeControlStream(AudioManager.STREAM_MUSIC);
playAudio[0] = MediaPlayer.create(this, R.raw.dry_kick);
playAudio[1] = MediaPlayer.create(this, R.raw.kick_drum);
playAudio[2] = MediaPlayer.create(this, R.raw.simplekick);
playAudio[3] = MediaPlayer.create(this, R.raw.kick_q_shot);
playAudio[4] = MediaPlayer.create(this, R.raw.kick_bass);
playAudio[5] = MediaPlayer.create(this, R.raw.kick_synth);
playAudio[6] = MediaPlayer.create(this, R.raw.kick_hot);
playAudio[7] = MediaPlayer.create(this, R.raw.simplesnare4);
playAudio[8] = MediaPlayer.create(this, R.raw.snare2);
playAudio[9] = MediaPlayer.create(this, R.raw.snare_cool);
playAudio[10] = MediaPlayer.create(this, R.raw.snare_soft);
playAudio[11] = MediaPlayer.create(this, R.raw.clap_bell);
playAudio[12] = MediaPlayer.create(this, R.raw.clap_hot);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void startNewGame() {
Game.clearBoard();
int[] randomVer = new int[6];
int[] randomHor = new int[6];
for(int i = 0;i < 6; i++ ) {
randomVer[i] = rand.nextInt(6) + 0;
randomHor[i] = rand.nextInt(6) + 0;
}
for (int i = 0; i < BoardButtons.length; i++) {
for(int j = 0; j < BoardButtons[i].length; j++) {
String buttonText = BoardButtons[i][j].getText().toString();
if (i == randomVer[0] && j == randomHor[0] && j != BoardButtons[i].length)
BoardButtons[i][j].setText(Game.DIRECTION_RIGHT);
else if (i == randomVer[1] && j == randomHor[1] && i != BoardButtons.length)
BoardButtons[i][j].setText(Game.DIRECTION_DOWN);
else if (i == randomVer[2] && j == randomHor[2] && j != BoardButtons[i].length)
BoardButtons[i][j].setText(Game.DIRECTION_RIGHT);
else if (i == randomVer[3] && j == randomHor[3] && i != BoardButtons.length)
BoardButtons[i][j].setText(Game.DIRECTION_DOWN);
else if (i == randomVer[4] && j == randomHor[4] && j != BoardButtons[i].length)
BoardButtons[i][j].setText(Game.DIRECTION_RIGHT);
else if (i == randomVer[5] && j == randomHor[5] && i != BoardButtons.length)
BoardButtons[i][j].setText(Game.DIRECTION_DOWN);
else
BoardButtons[i][j].setText(Game.EMPTY_SPACE);
BoardButtons[i][j].setEnabled(true);
BoardButtons[i][j].setOnClickListener(new ButtonClick(i, j));
}
}
gameOver = false;
}
public class ButtonClick implements View.OnClickListener{
int locationVer;
int locationHor;
public ButtonClick(int locationVer, int locationHor)
{
this.locationVer = locationVer;
this.locationHor = locationHor;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
//kicks
case R.id.button1:
playAudio[0].start();
break;
case R.id.button2:
playAudio[1].start();
break;
case R.id.button3:
playAudio[2].start();
break;
case R.id.button4:
playAudio[3].start();
break;
case R.id.button5:
playAudio[4].start();
break;
case R.id.button6:
playAudio[5].start();
break;
case R.id.button7:
playAudio[6].start();
break;
//snares
case R.id.button8:
playAudio[7].start();
break;
case R.id.button9:
playAudio[8].start();
break;
case R.id.button10:
playAudio[9].start();
break;
case R.id.button11:
playAudio[10].start();
break;
case R.id.button12:
playAudio[11].start();
break;
case R.id.button13:
playAudio[12].start();
break;
}
}
}
This is my layout activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#800008">
<TableLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentTop="false"
android:id="#+id/tableLayout"
android:layout_alignWithParentIfMissing="false"
android:gravity="center">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button1"
android:layout_column="1"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button2"
android:layout_column="2"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button3"
android:layout_column="3"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button4"
android:layout_column="4"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button5"
android:layout_column="5"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button6"
android:layout_column="6"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button7"
android:layout_column="7"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button8"
android:layout_column="1"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button9"
android:layout_column="2"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button10"
android:layout_column="3"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button11"
android:layout_column="4"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button12"
android:layout_column="5"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button13"
android:layout_column="6"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button14"
android:layout_column="7"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button15"
android:layout_column="1"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button16"
android:layout_column="2"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button17"
android:layout_column="3"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button18"
android:layout_column="4"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button19"
android:layout_column="5"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button20"
android:layout_column="6"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button21"
android:layout_column="7"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button22"
android:layout_column="1"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button23"
android:layout_column="2"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button24"
android:layout_column="3"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button25"
android:layout_column="4"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button26"
android:layout_column="5"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button27"
android:layout_column="6"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button28"
android:layout_column="7"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button29"
android:layout_column="1"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button30"
android:layout_column="2"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button31"
android:layout_column="3"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button32"
android:layout_column="4"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button33"
android:layout_column="5"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button34"
android:layout_column="6"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button35"
android:layout_column="7"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button36"
android:layout_column="1"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button37"
android:layout_column="2"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button38"
android:layout_column="3"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button39"
android:layout_column="4"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button40"
android:layout_column="5"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button41"
android:layout_column="6"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button42"
android:layout_column="7"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button43"
android:layout_column="1"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button44"
android:layout_column="2"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button45"
android:layout_column="3"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button46"
android:layout_column="4"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button47"
android:layout_column="5"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button48"
android:layout_column="6"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button49"
android:layout_column="7"
android:background="#drawable/custom_button_field"
android:textSize="10dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableRow>
</TableLayout>
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:id="#+id/textView2"
android:background="#aa0008"
android:textColor="#ffffff"
android:textSize="30dp"
android:gravity="center"
android:layout_below="#+id/tableLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" />
<Button
android:layout_width="120dp"
android:layout_height="120dp"
android:text=""
android:id="#+id/buttonRestart"
android:background="#drawable/custom_button_restart"
android:layout_below="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp" />
<Button
android:layout_width="120dp"
android:layout_height="120dp"
android:text=""
android:id="#+id/buttonExit"
android:background="#drawable/custom_back_button"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp" />
</RelativeLayout>
It have already set audio to 12/49 buttons. The app crashes when I click buttons 5, 6, 7, 10, 11, 12.

This is a copy of a code I have that runs, hope it helps.
public class MainActivity extends Activity {
MediaPlayer button_sound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);
Button b4 = (Button) findViewById(R.id.button4);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
button_sound = MediaPlayer.create(MainActivity.this,
R.raw.shooting);
button_sound.start();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
button_sound = MediaPlayer.create(MainActivity.this,
R.raw.rounds);
button_sound.start();
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
button_sound = MediaPlayer.create(MainActivity.this,
R.raw.shooting);
button_sound.start();
}
});
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
button_sound = MediaPlayer.create(MainActivity.this,
R.raw.rounds);
button_sound.start();
}
});
}

Related

Recycler view in a Fragment is showing up in emulator and some real time devices but not showing up in some real time devices

I am having a recycler view in one my fragment. The recycler view is showing up with the items in emulator and some real time devices but it is not showing up in some real time devices. I have kept this recycler view in a nested scroll view.
Devices I'm able to see the recycler view:One plus 6T(9), Realme
Devices I'm not able to see the recycler view: Vivo(8.1), Sony Xperia Z2(6.1)
My Fragment.xml Code
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/reminder_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginTop="18dp"
android:text="#string/refill_reminder"
android:textColor="#031B54"
android:textSize="13sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.cardview.widget.CardView
android:id="#+id/refillReminder_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginTop="9dp"
android:layout_marginEnd="14dp"
app:cardCornerRadius="3dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#id/reminder_label"
app:layout_constraintTop_toBottomOf="#id/reminder_label">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/refillName_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="18dp"
android:layout_marginTop="15dp"
android:text="#string/refill_name"
android:textColor="#090909"
android:textSize="15sp" />
<TextView
android:id="#+id/refillName_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/refillName_heading"
android:layout_marginStart="48dp"
android:layout_marginEnd="87dp"
android:layout_toEndOf="#id/refillName_heading"
android:text="#string/metamorphine"
android:textColor="#090909"
android:textSize="15sp"
android:textStyle="normal" />
<TextView
android:id="#+id/refillDueIn_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/refillName_heading"
android:layout_alignStart="#id/refillName_heading"
android:layout_marginTop="3dp"
android:text="#string/refill_due_in"
android:textColor="#090909"
android:textSize="15sp" />
<TextView
android:id="#+id/refillDueIn_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#id/refillName_label"
android:layout_alignBottom="#id/refillDueIn_heading"
android:text="#string/_28_days"
android:textColor="#090909"
android:textSize="15sp"
android:textStyle="normal" />
<TextView
android:id="#+id/nextRefillDate_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/refillDueIn_heading"
android:layout_alignStart="#id/refillDueIn_heading"
android:layout_marginTop="3dp"
android:layout_marginBottom="12dp"
android:text="#string/next_refill_date"
android:textColor="#090909"
android:textSize="15sp" />
<TextView
android:id="#+id/nextRefillDate_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#id/refillDueIn_label"
android:layout_alignBottom="#id/nextRefillDate_heading"
android:text="#string/_12_5_2019"
android:textColor="#090909"
android:textSize="15sp"
android:textStyle="normal" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/prescriptionHistory_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginTop="13dp"
android:layout_marginEnd="14dp"
app:cardCornerRadius="3dp"
app:layout_constraintEnd_toEndOf="#id/refillReminder_card"
app:layout_constraintStart_toStartOf="#id/refillReminder_card"
app:layout_constraintTop_toBottomOf="#id/refillReminder_card">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/history_image"
android:layout_width="30dp"
android:layout_height="32dp"
android:layout_marginStart="13dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/ic_history"
android:contentDescription="TODO" />
<TextView
android:id="#+id/history_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="20dp"
android:layout_toEndOf="#id/history_image"
android:text="#string/prescription_history"
android:textColor="#031B54"
android:textSize="17sp"
android:textStyle="bold" />
<Button
android:id="#+id/prescriptionHistory_button"
android:layout_width="20dp"
android:layout_height="25dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="#drawable/ic_arrow_right_b" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/myMedicines_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="19dp"
android:layout_marginStart="14dp"
android:text="#string/my_medicines"
android:textColor="#031B54"
android:textSize="13sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/prescriptionHistory_card" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myMedicines_RecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:elevation="5dp"
android:layout_marginStart="14dp"
android:layout_marginEnd="14dp"
android:scrollbarStyle="insideInset"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/myMedicines_label"
app:layout_constraintEnd_toEndOf="parent"
android:nestedScrollingEnabled="false">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
RecylerView CARD.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/myMedicines_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="3dp"
android:elevation="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/dosageForm_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="28dp"
android:layout_marginTop="16dp"
android:contentDescription="TODO" />
<TextView
android:id="#+id/dosage_startDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dosageForm_icon"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:text="#string/apr_22_2019"
android:textColor="#404040"
android:textStyle="bold"/>
<TextView
android:id="#+id/dosage_endDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dosage_startDate"
android:layout_alignStart="#id/dosage_startDate"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:text="#string/apr_30_2019"
android:textColor="#404040"
android:textStyle="bold"/>
<View
android:id="#+id/verticalSeperator_view"
android:layout_width="13dp"
android:layout_height="130dp"
android:layout_marginStart="30dp"
android:layout_toEndOf="#id/dosageForm_icon"
android:background="#drawable/ic_seperator" />
<TextView
android:id="#+id/refillName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:layout_toEndOf="#id/verticalSeperator_view"
android:text="#string/metamorphine"
android:textColor="#404040"
android:textSize="14dp"
android:textStyle="bold" />
<Button
android:id="#+id/button"
android:layout_width="22dp"
android:layout_height="18dp"
android:layout_alignParentEnd="true"
android:layout_marginEnd="7dp"
android:layout_marginTop="10dp"
android:background="#drawable/ic_more_horiz" />
<RelativeLayout
android:id="#+id/weekday_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/refillName"
android:layout_alignStart="#id/refillName"
android:layout_marginTop="17dp"
android:layout_toEndOf="#id/verticalSeperator_view">
<TextView
android:id="#+id/sunday_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/weekday_bg_grey"
android:text="#string/su"
android:gravity="center"
android:textSize="12sp"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/monday_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/sunday_label"
android:layout_marginStart="7dp"
android:layout_toEndOf="#id/sunday_label"
android:background="#drawable/weekday_bg_grey"
android:text="#string/mo"
android:gravity="center"
android:textSize="12sp"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/tuesday_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/sunday_label"
android:layout_marginStart="7dp"
android:layout_toEndOf="#id/monday_label"
android:background="#drawable/weekday_bg_grey"
android:text="#string/tu"
android:gravity="center"
android:textSize="12sp"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/wednesday_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/sunday_label"
android:layout_marginStart="7dp"
android:layout_toEndOf="#id/tuesday_label"
android:background="#drawable/weekday_bg_grey"
android:text="#string/we"
android:gravity="center"
android:textSize="12sp"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/thursday_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/sunday_label"
android:layout_marginStart="7dp"
android:layout_toEndOf="#id/wednesday_label"
android:background="#drawable/weekday_bg_grey"
android:text="#string/th"
android:gravity="center"
android:textSize="12sp"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/friday_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/sunday_label"
android:layout_marginStart="7dp"
android:layout_toEndOf="#id/thursday_label"
android:background="#drawable/weekday_bg_grey"
android:text="#string/fr"
android:gravity="center"
android:textSize="12sp"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/saturday_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/sunday_label"
android:layout_marginStart="7dp"
android:layout_toEndOf="#id/friday_label"
android:background="#drawable/weekday_bg_grey"
android:text="#string/sa"
android:gravity="center"
android:textSize="12sp"
android:textColor="#FFFFFF" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/timeline_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/weekday_view"
android:layout_alignStart="#id/weekday_view"
android:layout_marginTop="5dp">
<RelativeLayout
android:id="#+id/view1"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_marginStart="5dp">
<View
android:id="#+id/horizontalSeperator_view1"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_marginTop="34dp"
android:background="#FBFBFB" />
<TextView
android:id="#+id/time1_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textSize="10sp" />
<View
android:id="#+id/subview1"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_below="#id/time1_label"
android:layout_centerHorizontal="true"
android:layout_marginTop="6dp"
android:background="#drawable/timeline_bg_color" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/view2"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_toEndOf="#id/view1">
<View
android:id="#+id/horizontalSeperator_view2"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_marginTop="34dp"
android:background="#FBFBFB" />
<TextView
android:id="#+id/time2_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textSize="10sp" />
<View
android:id="#+id/subview2"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_below="#id/time2_label"
android:layout_centerHorizontal="true"
android:layout_marginTop="6dp"
android:background="#drawable/timeline_bg_color" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/view3"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_toEndOf="#id/view2">
<View
android:id="#+id/horizontalSeperator_view3"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_marginTop="34dp"
android:background="#FBFBFB" />
<TextView
android:id="#+id/time3_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textSize="10sp" />
<View
android:id="#+id/subview3"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_below="#id/time3_label"
android:layout_centerHorizontal="true"
android:layout_marginTop="6dp"
android:background="#drawable/timeline_bg_color" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/view4"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_toEndOf="#id/view3">
<View
android:id="#+id/horizontalSeperator_view4"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_marginTop="34dp"
android:background="#FBFBFB" />
<TextView
android:id="#+id/time4_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textSize="10sp" />
<View
android:id="#+id/subview4"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_below="#id/time4_label"
android:layout_centerHorizontal="true"
android:layout_marginTop="6dp"
android:background="#drawable/timeline_bg_color" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
Fragment Class
public class PrescriptionFragment extends Fragment {
Context thisContext;
private RecyclerView myMedicineRecyclerView;
private MyMedicinesAdapter myMedicinesAdapter;
List<MyMedicine> myMedicineList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("Inside prescription onCreateView");
thisContext = container.getContext();
View view = inflater.inflate(R.layout.prescription_fragment, container, false);
myMedicineRecyclerView = (RecyclerView) view.findViewById(R.id.myMedicines_RecyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(thisContext, RecyclerView.VERTICAL, false);
myMedicineRecyclerView.setLayoutManager(layoutManager);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
final Button presHistoryButton = (Button) view.findViewById(R.id.prescriptionHistory_button);
presHistoryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(thisContext, "Prescription History Button Tapped", Toast.LENGTH_SHORT).show();
}
});
}
// This refreshList() method is getting called from my Fragments main class
public void refreshList(List<TreatmentObject> data) {
System.out.println("Inside of refreshList of Pres Fragment");
System.out.println("Data size is" + data.size());
System.out.println("Inside refreshList(data)" + data);
myMedicineList = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
TreatmentObject treatmentObject = data.get(i);
System.out.println(treatmentObject);
int dosageImage;
switch (treatmentObject.getFORM()) {
case "Tablet":
dosageImage = R.drawable.ic_tablet_c;
break;
case "Capsule":
dosageImage = R.drawable.ic_pills_c;
break;
case "Bandage":
dosageImage = R.drawable.ic_bandage_c;
break;
case "Concent...":
dosageImage = R.drawable.ic_medicine_c;
break;
case "Drops":
dosageImage = R.drawable.ic_eyedrops_c;
break;
case "Elixir":
dosageImage = R.drawable.ic_elixir_c;
break;
case "Enema":
dosageImage = R.drawable.ic_enema_c;
break;
case "Gel":
dosageImage = R.drawable.ic_gel_c;
break;
case "Inhaler":
dosageImage = R.drawable.ic_inhaler_c;
break;
case "Injection":
dosageImage = R.drawable.ic_syringe_c;
break;
case "Kit":
dosageImage = R.drawable.ic_firsaidkit_c;
break;
case "Spray":
dosageImage = R.drawable.ic_paintspray_c;
break;
case "Syrup":
dosageImage = R.drawable.ic_syrup_c;
break;
case "Tape":
dosageImage = R.drawable.ic_tape_c;
break;
case "Tincture":
dosageImage = R.drawable.ic_tincture_c;
break;
case "Wafer":
dosageImage = R.drawable.ic_wafer_c;
break;
default:
dosageImage = R.drawable.ic_pills_c;
}
String startDate = null, endDate = null;
if(treatmentObject.getStartDate() != null && treatmentObject.getStartDate() != "") {
startDate = toISO8601UTCRepeat(fromISO8601UTCRepeat(treatmentObject.getStartDate()));
}
if(treatmentObject.getEndDate() != null && treatmentObject.getEndDate() != "") {
endDate = toISO8601UTCRepeat(fromISO8601UTCRepeat(treatmentObject.getEndDate()));
}
String daysArray[] = new String[0];
if(treatmentObject.getSCHEDULE() != null && treatmentObject.getSCHEDULE() != "") {
if (treatmentObject.getSCHEDULE().equalsIgnoreCase("Daily")) {
daysArray = new String[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
} else if (treatmentObject.getSCHEDULE().equalsIgnoreCase("Weekly")) {
if(treatmentObject.getDAYS() != null && treatmentObject.getDAYS() != "") {
String days = treatmentObject.getDAYS();
daysArray = days.split(", ");
System.out.println("Weekly Days Array is" + Arrays.toString(daysArray));
}
}
}
String time1 = null, time2 = null, time3 = null, time4 = null;
if (treatmentObject.getTIME1() != null && treatmentObject.getTIME1() != "" ) {
time1 = toISO8601UTC(fromISO8601UTC(treatmentObject.getTIME1()));
}
if (treatmentObject.getTIME2() != null && treatmentObject.getTIME2() != "" ) {
time2 = toISO8601UTC(fromISO8601UTC(treatmentObject.getTIME2()));
}
if (treatmentObject.getTIME3() != null && treatmentObject.getTIME3() != "" ) {
time3 = toISO8601UTC(fromISO8601UTC(treatmentObject.getTIME3()));
}
if (treatmentObject.getTIME4() != null && treatmentObject.getTIME4() != "" ) {
time4 = toISO8601UTC(fromISO8601UTC(treatmentObject.getTIME4()));
}
MyMedicine myMedicine = new MyMedicine(treatmentObject.getMedicineName(), dosageImage, startDate, endDate, daysArray, time1, time2, time3, time4, treatmentObject.getNOTE());
myMedicineList.add(myMedicine);
myMedicinesAdapter = new MyMedicinesAdapter(thisContext, myMedicineList);
myMedicineRecyclerView.setAdapter(myMedicinesAdapter);
}
}
public static String toISO8601UTC(Date date) {
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("hh:mma");
df.setTimeZone(tz);
return df.format(date);
}
public static Date fromISO8601UTC(String dateStr) {
TimeZone tz = TimeZone.getTimeZone("UTC");
System.out.println(tz);
DateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");
df.setTimeZone(tz);
try {
return df.parse(dateStr);
} catch (ParseException e) {
System.out.println("Inside catch exception");
e.printStackTrace();
}
return null;
}
public static String toISO8601UTCRepeat(Date date) {
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("MMM dd, yyyy");
df.setTimeZone(tz);
return df.format(date);
}
public static Date fromISO8601UTCRepeat(String dateStr) {
TimeZone tz = TimeZone.getTimeZone("UTC");
System.out.println(tz);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setTimeZone(tz);
try {
return df.parse(dateStr);
} catch (ParseException e) {
System.out.println("Inside catch exception");
e.printStackTrace();
}
return null;
}
}

Why can't I access the layout of a class for testing

UPDATE: I've expanded the code, my app works as expected but still getting a NullPointerException when trying to test the activity_main.xml.
I'm trying to test the views in a layout for a calculator but I keep getting NullPointerException even after calling onCreate in the test file, I've also tried setting content view inside the test file directly to the activity_main but no luck.
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
Calculator calculator = new Calculator();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
void onCreateHelper() {
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
}
public void onNumberClick(View view) {
Button button = (Button) view;
String result = this.getButtonText(button);
this.setValueView(result);
int number = Integer.parseInt(result);
calculator.numberClick(number);
}
public void onOperatorClick(View view) {
Button button = (Button) view;
String value = this.getButtonText(button);
calculator.operatorClick(value);
String result = String.valueOf(calculator.getRunningTotal());
binding.editText.setText(result);
}
public void onClearClick (View view) {
Button button = (Button) view;
String value = this.getButtonText(button);
binding.editText.setText("");
calculator.clearClick();
}
String getButtonText(Button button) {
String result = button.getText().toString();
return result;
}
void setValueView(String value){
binding.editText.setText(binding.editText.getText() + value);
}
}
Test:
public class MainActivityTest<T extends MainActivity> {
private MainActivity mainActivity;
#Before
public void before() {
mainActivity = new MainActivity();
mainActivity.onCreateHelper();
}
#Test
public void OnNumberClickTest() throws Exception {
mainActivity.onNumberClick(mainActivity.binding.buttonSeven);
assertEquals(7, mainActivity.calculator.getRunningTotal());
}
#Test
public void OnOperatorClickTest() throws Exception {
mainActivity.onOperatorClick(mainActivity.binding.buttonAdd);
assertEquals('+', mainActivity.calculator.getPreviousOperator());
}
#Test
public void onClearClickTest() throws Exception {
mainActivity.onClearClick(mainActivity.binding.buttonClear);
assertEquals(0, mainActivity.calculator.getRunningTotal());
}
#Test
public void getButtonTextTest() throws Exception {
String buttonText = mainActivity.getButtonText(mainActivity.binding.buttonEight);
assertEquals(8, buttonText);
}
#Test
public void setValueViewTest() throws Exception {
mainActivity.setValueView("TEST");
assertEquals("TEST", mainActivity.binding.editText.getText());
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="99dp"
android:orientation="vertical" android:layout_weight="0.67">
<TextView
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:gravity="right"
android:paddingRight="10sp"
android:paddingTop="5sp"
android:textSize="15pt"
android:textStyle="bold" />
<TextView
android:id="#+id/txtInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:gravity="right"
android:paddingRight="10sp"
android:paddingTop="5sp"
android:textSize="15pt"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="75dp"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonClear"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="5"
android:textStyle="bold"
android:textSize="40sp"
android:text="Clear" />
<Button
android:id="#+id/buttonBack"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight=".51"
android:textStyle="bold"
android:textSize="40sp"
android:text="BS" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="75dp"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonSeven"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="7" />
<Button
android:id="#+id/buttonEight"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="8" />
<Button
android:id="#+id/buttonNine"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="9" />
<Button
android:id="#+id/buttonDivide"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1.15"
android:textStyle="bold"
android:textSize="40sp"
android:text="/" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="75dp"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonFour"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textSize="40sp"
android:textStyle="bold"
android:text="4" />
<Button
android:id="#+id/buttonFive"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="5" />
<Button
android:id="#+id/buttonSix"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="6" />
<Button
android:id="#+id/buttonMultiply"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight=".98"
android:textStyle="bold"
android:textSize="40sp"
android:text="x" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="75dp"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="1" />
<Button
android:id="#+id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="2" />
<Button
android:id="#+id/buttonThree"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="3" />
<Button
android:id="#+id/buttonSubtract"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1.27"
android:textStyle="bold"
android:textSize="40sp"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="75dp"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonDot"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1.22"
android:textStyle="bold"
android:textSize="40sp"
android:text="." />
<Button
android:id="#+id/buttonZero"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight=".98"
android:textStyle="bold"
android:textSize="40sp"
android:text="0" />
<Button
android:id="#+id/buttonEqual"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight=".95"
android:textStyle="bold"
android:textSize="40sp"
android:text="=" />
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight=".93"
android:textStyle="bold"
android:textSize="40sp"
android:text="+" />
</LinearLayout>
</LinearLayout>

Toggle button inside linear layout not showing on off text

I have toggle buttons inside linear layout and on click they do not show the ontext and offtext.I also have listener for all toggle buttons and inside onclick function i tried to set the ontext but it didn't work.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.sharmila.eeeeeee.tryfragment"
android:id="#+id/fragment"
android:layout_centerHorizontal="true"
tools:layout="#layout/try_fragment" />
<LinearLayout android:id="#+id/linearlayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/fragment"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:weightSum="1">
<ToggleButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/wholenote"
android:layout_row="0"
android:layout_column="0"
android:background="#drawable/a"
android:textOff=""
android:textOn="S"
android:textSize="700dp"
android:textColor="#ff0000"
/>
<ToggleButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/halfnote"
android:layout_row="0"
android:layout_column="1"
android:background="#drawable/b"
android:textOff=""
android:textOn="S"
android:textSize="700dp"
android:textColor="#ff0000"/>
<ToggleButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/quarternote"
android:layout_row="0"
android:layout_column="2"
android:background="#drawable/c"
android:textOff=""
android:textOn="S"
android:textSize="700dp"
android:textColor="#ff0000"
/>
<ToggleButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/eightnote"
android:layout_row="0"
android:layout_column="3"
android:background="#drawable/d"
android:textOn="S"
android:textSize="700dp"
android:textColor="#ff0000"
android:textOff=""/>
<ToggleButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/sixteennote"
android:textOn="S"
android:textSize="700dp"
android:textColor="#ff0000"
android:textOff=""
android:layout_row="0"
android:layout_column="4"
android:background="#drawable/e"
/>
<ToggleButton
android:layout_width="49dp"
android:layout_height="40dp"
android:textOn="S"
android:textSize="700dp"
android:textColor="#ff0000"
android:id="#+id/thirtytwonote"
android:layout_row="0"
android:layout_column="5"
android:background="#drawable/g"
/>
<ToggleButton
android:layout_width="49dp"
android:layout_height="40dp"
android:textOn="S"
android:textSize="700dp"
android:textColor="#ff0000"
android:id="#+id/sixyfournote"
android:layout_row="0"
android:layout_column="6"
android:background="#drawable/f"
/>
</LinearLayout>
<TableRow android:id="#+id/Linearlayout2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/linearlayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:weightSum="1">
<View
android:layout_width="10dp"
android:layout_height="40dp"
android:layout_weight="0.34" />
<ImageButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/pause"
android:layout_row="0"
android:layout_column="1"
android:src="#drawable/pause" />
<View
android:layout_width="10dp"
android:layout_height="40dp"
android:layout_weight="0.18" />
<ImageButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/play"
android:layout_row="0"
android:layout_column="3"
android:src="#drawable/play" />
<View
android:layout_width="10dp"
android:layout_height="40dp"
android:layout_weight="0.18" />
<ImageButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/stop"
android:src="#drawable/stop"
android:layout_row="0"
android:layout_column="3"/>
</TableRow>
<GridLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gridView"
android:layout_below="#+id/Linearlayout2"
>
<Button
android:layout_width="78dp"
android:layout_height="wrap_content"
android:text="Prev"
android:id="#+id/previous"
android:layout_row="0"
android:layout_column="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/next"
android:layout_row="0"
android:layout_column="19" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:id="#+id/save"
android:layout_row="1"
android:layout_column="1" />
<Button
android:layout_width="99dp"
android:layout_height="wrap_content"
android:text="Finish"
android:id="#+id/finish"
android:layout_row="1"
android:layout_column="21" />
</GridLayout>
Activity code:
private static ToggleButton wholenote;
private static ToggleButton halfnote;
private static ToggleButton quarternote;
private static ToggleButton eighthnote;
private static ToggleButton sixteenthnote;
private static ToggleButton thirtytwonote;
private static ToggleButton sixtyfourthnote;
private static String selected = "01";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wholenote = (ToggleButton) findViewById(R.id.wholenote);
wholenote.setOnClickListener(this);
halfnote = (ToggleButton) findViewById(R.id.halfnote);
halfnote.setOnClickListener(this);
quarternote = (ToggleButton) findViewById(R.id.quarternote);
quarternote.setOnClickListener(this);
eighthnote = (ToggleButton) findViewById(R.id.eightnote);
eighthnote.setOnClickListener(this);
sixteenthnote = (ToggleButton) findViewById(R.id.sixteennote);
sixteenthnote.setOnClickListener(this);
thirtytwonote = (ToggleButton) findViewById(R.id.thirtytwonote);
thirtytwonote.setOnClickListener(this);
sixtyfourthnote = (ToggleButton) findViewById(R.id.sixyfournote);
sixtyfourthnote.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.wholenote:
wholenote.setTextOn("S");
break;
case R.id.halfnote:
halfnote.setTextOn("S");
break;
case R.id.quarternote:
quarternote.setTextOn("S");
break;
case R.id.eightnote:
eightnote.setTextOn("S");
break;
case R.id.sixteennote:
sixteennote.setTextOn("S");
break;
case R.id.thirtytwonote:
thirtytwonote.setTextOn("S");
break;
case R.id.sixyfournote:
sixtyfournote.setTextOn("S");
break;
}
}
android:textSize="700dp"
It's not showing on off text,it is because you text size is too large to show it on screen.
Set all togglebuttons textSize 10dp.
android:textSize="10dp"

How to inflate xml layout in android?

Hi Iam inflating a layout into another one. But it is overwriting the existing layout. Please do let me know to inflate a layout into another layout if anybody knows. When the plus button is pressed it should inflate a layout.
My xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginTop="14dp"
android:text="Daily Call Report"
android:textColor="#008000"
android:textSize="35dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="253dp"
android:text="Date"
android:textColor="#008000"
android:textSize="35dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/dayworkcategory"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="28dp"
android:layout_marginTop="10dp"
android:text="Day Work Plan:"
android:textSize="15dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="101dp"
android:layout_toRightOf="#+id/spinner1" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/spinner2"
android:layout_alignLeft="#+id/spinner2"
android:text="Category:"
android:textSize="15dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/placeworkstation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/dayworkcategory"
android:layout_marginTop="12dp"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignLeft="#+id/textView5"
android:layout_below="#+id/textView5"
android:ems="10"
android:hint="STATION NAME" >
</AutoCompleteTextView>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_marginLeft="27dp"
android:layout_toRightOf="#+id/textView4"
android:text="Actual Working :"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="16dp"
android:text="Place of work(as per TP) :"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/placeworkstation"
android:layout_marginLeft="28dp"
android:text="Stockists Visited :"
android:textSize="20dp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/stocklist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView6"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="Stocklist Name" >
</AutoCompleteTextView>
<Spinner
android:id="#+id/spinner3"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignBottom="#+id/autoCompleteTextView2"
android:layout_marginLeft="33dp"
android:layout_toRightOf="#+id/autoCompleteTextView2" />
<EditText
android:id="#+id/editText1"
android:layout_width="60dp"
android:layout_height="45dp"
android:layout_alignBaseline="#+id/autoCompleteTextView2"
android:layout_alignBottom="#+id/autoCompleteTextView2"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/spinner3"
android:ems="10"
android:hint="Qty" />
<EditText
android:id="#+id/editText2"
android:layout_width="145dp"
android:layout_height="45dp"
android:layout_alignTop="#+id/autoCompleteTextView2"
android:layout_marginLeft="31dp"
android:layout_toRightOf="#+id/editText1"
android:ems="10"
android:hint="Remarks" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="45dp"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignBottom="#+id/editText2"
android:layout_marginLeft="13dp"
android:layout_toRightOf="#+id/editText2"
android:text="+" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/chemistlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView7"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView3"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="Chemist Name" />
<EditText
android:id="#+id/editText4"
android:layout_width="145dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button2"
android:layout_marginRight="63dp"
android:ems="10"
android:hint="Remarks" >
</EditText>
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="45dp"
android:layout_alignBaseline="#+id/editText4"
android:layout_alignBottom="#+id/editText4"
android:layout_marginLeft="472dp"
android:layout_toRightOf="#+id/autoCompleteTextView3"
android:text="+" />
<EditText
android:id="#+id/editText3"
android:layout_width="60dp"
android:layout_height="45dp"
android:layout_alignTop="#+id/button2"
android:layout_marginRight="16dp"
android:layout_toLeftOf="#+id/editText4"
android:ems="10"
android:hint="Qty" />
<Spinner
android:id="#+id/spinner4"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignTop="#+id/button2"
android:layout_marginRight="19dp"
android:layout_toLeftOf="#+id/editText3" />
</RelativeLayout>
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView7"
android:layout_below="#+id/chemistlist"
android:layout_marginTop="12dp"
android:text="Travel Expenses Detail :"
android:textSize="20dp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/travelexpenses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView8"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView4"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="21dp"
android:ems="10"
android:hint="FROM" />
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="43dp"
android:layout_alignBaseline="#+id/editText6"
android:layout_alignBottom="#+id/editText6"
android:layout_alignLeft="#+id/editText6"
android:layout_marginLeft="125dp"
android:text="+" />
<Spinner
android:id="#+id/spinner5"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignBottom="#+id/autoCompleteTextView5"
android:layout_marginLeft="13dp"
android:layout_toRightOf="#+id/autoCompleteTextView5" />
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView5"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/autoCompleteTextView4"
android:layout_alignBottom="#+id/autoCompleteTextView4"
android:layout_marginLeft="37dp"
android:layout_toRightOf="#+id/autoCompleteTextView4"
android:ems="10"
android:hint="TO" >
</AutoCompleteTextView>
<EditText
android:id="#+id/editText6"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/editText5"
android:layout_alignBottom="#+id/editText5"
android:layout_marginLeft="42dp"
android:layout_toRightOf="#+id/editText5"
android:ems="10"
android:hint="Km" />
<EditText
android:id="#+id/editText5"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignBottom="#+id/spinner5"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/spinner5"
android:ems="10"
android:hint="AMOUNT" >
</EditText>
</RelativeLayout>
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView8"
android:layout_below="#+id/travelexpenses"
android:layout_marginTop="14dp"
android:text="Miscelleneous Expenses Detail :"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/chemistlist"
android:layout_centerVertical="true"
android:text="Chemists Visited :"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView9"
android:layout_alignBottom="#+id/textView9"
android:layout_alignRight="#+id/travelexpenses"
android:layout_marginRight="34dp"
android:text="Daily Allowance Expenses Detail :"
android:textSize="20dp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/miscelleneous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView9"
android:orientation="vertical" >
<EditText
android:id="#+id/editText7"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="21dp"
android:ems="10"
android:hint="Amount" />
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="45dp"
android:layout_alignTop="#+id/editText7"
android:layout_marginLeft="276dp"
android:layout_toRightOf="#+id/editText7"
android:text="+" />
<EditText
android:id="#+id/editText8"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignTop="#+id/button4"
android:layout_marginLeft="40dp"
android:layout_toRightOf="#+id/editText7"
android:ems="10"
android:hint="Remarks" />
</RelativeLayout>
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignBottom="#+id/miscelleneous"
android:layout_alignLeft="#+id/textView10"
android:layout_marginBottom="10dp"
android:layout_marginLeft="35dp"
android:text="Total(INR):"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignBaseline="#+id/textView11"
android:layout_alignBottom="#+id/textView11"
android:layout_marginLeft="23dp"
android:layout_toRightOf="#+id/textView11"
android:text="TOTAL"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/dayworkcategory"
android:layout_marginRight="33dp"
android:text="UPGRADE DRAFT" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/placeworkstation"
android:text="SUBMIT LATER" />
</RelativeLayout>
My code for inflating the layout:
package abts.medismo.e_detailing;
import abts.medismo.e_detailing.Model.Spinmodel;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;
public class DCR extends Activity {
Spinner spinworkplan, spincategory, spinstockproduct, spinchemistproduct,
spintravelmode;
Spinner autostation, autostock, autochemist;
String dbcatid, dbcatname, strcatname, spcatid, spcatname, strcatid;
RelativeLayout stocklist;
int i = 1;
int count = 0;
View rowView;
private View mExclusiveEmptyView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dcrdummo);
spinworkplan = (Spinner) findViewById(R.id.spinner1);
spincategory = (Spinner) findViewById(R.id.spinner2);
spinstockproduct = (Spinner) findViewById(R.id.spinner3);
spinchemistproduct = (Spinner) findViewById(R.id.spinner4);
spintravelmode = (Spinner) findViewById(R.id.spinner5);
stocklist = (RelativeLayout) findViewById(R.id.stocklist);
DatabaseHandler dbh = new DatabaseHandler(DCR.this);
SQLiteDatabase db = dbh.getWritableDatabase();
final ArrayAdapter<Spinmodel> adapterWorkPlan = new ArrayAdapter<Spinmodel>(
this, android.R.layout.simple_spinner_item);
adapterWorkPlan
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterWorkPlan.add(new Spinmodel("Doctor Visit", "Doctor Visit"));
adapterWorkPlan.add(new Spinmodel("Transit", "Transit"));
adapterWorkPlan.add(new Spinmodel("Meeting", "Meeting"));
adapterWorkPlan.add(new Spinmodel("Strike", "Strike"));
adapterWorkPlan.add(new Spinmodel("Head Quarters", "Head Quarters"));
adapterWorkPlan.add(new Spinmodel("Sales Closing", "Sales Closing"));
adapterWorkPlan.add(new Spinmodel("Others", "Others"));
spinworkplan.setAdapter(adapterWorkPlan);
final ArrayAdapter<Spinmodel> adapterCategory = new ArrayAdapter<Spinmodel>(
this, android.R.layout.simple_spinner_item);
String strquery1 = "SELECT * FROM category";
Cursor cursor = db.rawQuery(strquery1, null);
if (cursor.getCount() != 0) {
cursor.moveToFirst();
do {
dbcatid += "," + cursor.getString(0);
dbcatname += "," + cursor.getString(1);
} while (cursor.moveToNext());
}
System.out.println("dbCatid-->>" + dbcatid);
System.out.println("dbCatName-->>" + dbcatname);
if (dbcatname != null) {
int pos = dbcatname.indexOf(",");
int pos1 = dbcatid.indexOf(",");
if (pos == pos1) {
strcatid = dbcatid.substring(pos1 + 1);
strcatname = dbcatname.substring(pos + 1);
}
String delimiter = "\\,";
String[] catid = strcatid.split(delimiter);
String[] catname = strcatname.split(delimiter);
for (int i = 0; i < catid.length; i++) {
spcatid = catid[i];
spcatname = catname[i];
adapterCategory.add(new Spinmodel(spcatname, spcatid));
}
spincategory.setAdapter(adapterCategory);
}
dbcatid = null;
dbcatname = null;
final ArrayAdapter<Spinmodel> adapterStockProduct = new ArrayAdapter<Spinmodel>(
this, android.R.layout.simple_spinner_item);
String strproductquery = "SELECT * FROM product";
Cursor productcursor = db.rawQuery(strproductquery, null);
if (productcursor.getCount() != 0) {
productcursor.moveToFirst();
do {
dbcatid += "," + productcursor.getString(0);
dbcatname += "," + productcursor.getString(1);
} while (productcursor.moveToNext());
}
if (dbcatname != null) {
int pos = dbcatname.indexOf(",");
int pos1 = dbcatid.indexOf(",");
if (pos == pos1) {
strcatid = dbcatid.substring(pos1 + 1);
strcatname = dbcatname.substring(pos + 1);
}
String delimiter = "\\,";
String[] catid = strcatid.split(delimiter);
String[] catname = strcatname.split(delimiter);
for (int i = 0; i < catid.length; i++) {
spcatid = catid[i];
spcatname = catname[i];
adapterStockProduct.add(new Spinmodel(spcatname, spcatid));
}
spinstockproduct.setAdapter(adapterStockProduct);
spinchemistproduct.setAdapter(adapterStockProduct);
}
final ArrayAdapter<Spinmodel> adaptertravelmode = new ArrayAdapter<Spinmodel>(
this, android.R.layout.simple_spinner_item);
adaptertravelmode
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adaptertravelmode.add(new Spinmodel("TwoWheeler", "TwoWheeler"));
adaptertravelmode.add(new Spinmodel("Bus", "Bus"));
adaptertravelmode.add(new Spinmodel("Car", "Car"));
adaptertravelmode.add(new Spinmodel("Train", "Train"));
adaptertravelmode.add(new Spinmodel("Airlines", "Airlines"));
spintravelmode.setAdapter(adaptertravelmode);
}
public void onAddNewClicked(View v) {
count++;
System.out.println("value of i: " + i);
inflateEditRow(count);
v.setVisibility(View.VISIBLE);
System.out.println("value of count " + count);
}
private void inflateEditRow(int id) {
i = id - 1;
for (int j = i; j < id; j++) {
System.out.println("value of j " + j);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.dummaa, null);
mExclusiveEmptyView = rowView;
rowView.setId(id);
stocklist.addView(rowView);
// stocklist.addView(rowView, stocklist.getChildCount());
}
}
public void onDeleteClicked(View v) {
stocklist.removeView((View) v.getParent());
}
}
I got a bit lost in your code, but if I understood right you may need to have the main view as a parameter in rowView = inflater.inflate(R.layout.dummaa, null); instead of passing null.

Button Click is not working.View is posting message on buttons

Now i encountered problem while debugging my code,that the Buttons in my application is not responding to click action.I kept break point on Click view method but on click view method is not responding.
What i am doing in on Click method is, if i clicked the first button then swapping the value with empty button value.But on click is not responding.
Here is my full code to check.
public class Play extends Activity implements OnClickListener
{
private ArrayList<Integer> m_buildButtonsIDs;
private ArrayList<Integer> m_gameButtonsIDs;
public static Character UserWords;
public static Character RemainingWords;
public static Character OpenCard;
static ArrayList<Character> ShuffledCards = new ArrayList<Character>();
static ArrayList<Character> UserBuildWords = new ArrayList<Character>();
static ArrayList<Character> RemainingBuildWords = new ArrayList<Character>();
static ArrayList<Character> RemainingBuildWordsAfterShowCard = new ArrayList<Character>();
static ArrayList<Character> DroppedCards = new ArrayList<Character>();
static Stack<Character>UserDroppedCards = new Stack<Character>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
InitializeBoard();
}
public void InitializeBoard()
{
m_buildButtonsIDs = new ArrayList<Integer>();
m_buildButtonsIDs.add(R.id.Button1);
m_buildButtonsIDs.add(R.id.Button2);
m_buildButtonsIDs.add(R.id.Button3);
m_buildButtonsIDs.add(R.id.Button4);
m_buildButtonsIDs.add(R.id.Button5);
m_buildButtonsIDs.add(R.id.Button6);
m_buildButtonsIDs.add(R.id.Button7);
m_buildButtonsIDs.add(R.id.Button8);
m_buildButtonsIDs.add(R.id.Button9);
m_gameButtonsIDs = new ArrayList<Integer>();
m_gameButtonsIDs.add(R.id.pickButton);
m_gameButtonsIDs.add(R.id.showButton);
m_gameButtonsIDs.add(R.id.emptyButton);
String random = RandomAlphabetGenerator.Random();
for(int i = 0;i<random.length();i++)
{
char randomcards = random.charAt(i);
ShuffledCards.add(randomcards);
}
for(int i = 0;i < 9;i++)
{
UserWords = ShuffledCards.get(i);
UserBuildWords.add(UserWords);
if(i == 8)
{
for(int j = 9;j < 52;j++)
{
RemainingWords = ShuffledCards.get(j);
RemainingBuildWords.add(RemainingWords);
}
}
}
OpenCard = RemainingBuildWords.get(0);
DroppedCards.add(OpenCard);
RemainingBuildWords.remove(0);
RemainingBuildWordsAfterShowCard.addAll(RemainingBuildWords);
FillUserBuildButtons(ShuffledCards);
StackingDroppedButtons(DroppedCards);
StackingPickButtons(RemainingBuildWordsAfterShowCard);
}
private void FillUserBuildButtons(ArrayList<Character> shuffledCards)
{
for (int i=0 ; i<m_buildButtonsIDs.size() ; i++)
{
Button BuildButton = (Button)findViewById(m_buildButtonsIDs.get(i));
BuildButton.setText(UserBuildWords.get(i).toString());
}
}
private void StackingDroppedButtons(ArrayList<Character> droppedCards)
{
Button ShowButton = (Button)findViewById(m_gameButtonsIDs.get(1));
ShowButton.setText(DroppedCards.get(0).toString());
}
private void StackingPickButtons(ArrayList<Character> remainingBuildWordsAfterShowCard)
{
Button ShowButton = (Button)findViewById(m_gameButtonsIDs.get(0));
ShowButton.setText(RemainingBuildWordsAfterShowCard.get(0).toString());
}
public void onClick(View v)
{
CharSequence text;
switch(v.getId())
{
case R.id.Button1:
Button FirstButton = (Button)findViewById(m_buildButtonsIDs.get(0));
text = FirstButton.getText().toString();
FirstButton.setText(SwapValue(text));
break;
case R.id.emptyButton:
break;
}
}
private CharSequence SwapValue(CharSequence k)
{
CharSequence empty = null;
Button EmptyButton = (Button)findViewById(m_buildButtonsIDs.get(0));
empty = EmptyButton.getText().toString();
EmptyButton.setText(k);
return empty;
}
}
Here is my xml code for checking:
<TableLayout
android:id="#+id/MainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginTop="50dp"
android:layout_marginLeft="115dp">
<LinearLayout
android:layout_width="40dp"
android:layout_height="40dp" >
<Button
android:id="#+id/Button1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button3"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Dummy"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
<Button
android:id="#+id/Button4"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button5"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button6"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="30dp">
<Button
android:id="#+id/Dummy2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
<Button
android:id="#+id/Dummy3"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
<Button
android:id="#+id/Button7"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button8"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button9"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp"/>
<Button
android:id="#+id/Dummy4"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
<Button
android:id="#+id/Dummy5"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
</LinearLayout>
<LinearLayout
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp">
<Button
android:id="#+id/pickButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp"/>
<Button
android:id="#+id/showButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp"/>
<Button
android:id="#+id/emptyButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/empty"
android:textSize="15dp" />
<Button
android:id="#+id/emptyButton1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text=""
android:textSize="15dp" />
<Button
android:id="#+id/dropButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/drop"
android:textSize="15dp"/>
<Button
android:id="#+id/declareButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/declare"
android:textSize="15dp"/>
</LinearLayout>
</TableLayout>
you should add an onClickListener to your Button :
btn.setOnClickListener(Play.this);

Categories

Resources