i want clickable buttons in zoomview .but my problem is that when i set the view in
zoomview then buttons are not responding while click,i am stuck at this point any help
is apreciated
my code is
public class SongsActivity extends Activity {
ImageView imvw;
DrawView drawView;
ImageView imageView;
View dv;
RelativeLayout rdown;
private static final float MIN_ZOOM = 1f, MAX_ZOOM = 1f;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
private static final RelativeLayout New = null;
int mode = NONE;
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
RelativeLayout main_container;
private ZoomView zoomView;
Button BtnWelcome;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs_layout);
try{
imvw = (ImageView) findViewById(R.id.imageView1);
View v = ((LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.songs_layout, null, false);
BtnWelcome = (Button) findViewById(R.id.button4);
v.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
main_container = (RelativeLayout) findViewById(R.id.Map);
zoomView = new ZoomView(this);
zoomView.addView(v);
main_container.addView(zoomView);
//BtnWelcome.bringToFront();
BtnWelcome.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(SongsActivity.this,
Wavepointsactivity.class);
intent.putExtra("Welcome", "Welcome");
startActivity(intent);
}
});
}catch (Exception e) {
// TODO: handle exception
}
}
}
and my xml is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Screen Design for the SONGS -->
<RelativeLayout
android:id="#+id/tabconteffnt"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top"
android:background="#2E2E2E"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Cedar Breaks-Map"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/Map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="70dp"
android:scaleType="fitXY"
android:src="#drawable/map" />
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/button3"
android:layout_marginRight="37dp" />
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#+id/button4"
android:layout_toLeftOf="#+id/button4"
android:text="Button" />
<Button
android:id="#+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignLeft="#+id/button5"
android:layout_below="#+id/button5"
android:layout_marginTop="18dp"
android:text="Button" />
<Button
android:id="#+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#+id/button6"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="Button" />
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="#+id/button2" />
<Button
android:id="#+id/button11"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/button9"
android:text="Button" />
<Button
android:id="#+id/button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="#+id/button9"
android:layout_alignRight="#+id/button11"
android:text="Button" />
<Button
android:id="#+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBaseline="#+id/button10"
android:layout_alignBottom="#+id/button10"
android:text="Button" />
<Button
android:id="#+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBaseline="#+id/button7"
android:layout_alignBottom="#+id/button7"
android:layout_alignLeft="#+id/button8"
android:text="Button" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignLeft="#+id/button3" />
<RelativeLayout
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignLeft="#+id/view1" >
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
There is some problem in initialising your id.button1 is not declared in your xml.So it is a null pointer exception.
BtnWelcome = (Button) findViewById(R.id.button1);
Give proper id of the button.It may resolve the error..
Related
I have a horizontal scroll bar that holds 12 buttons, I want to scroll to a specific button to be the middle of the screen,
Currently I am using
monthScrollView.smoothScrollBy((int) buttons.get(arrayNumber).getX(),0);
Which scrolls the button just off the screen, so how to I find the value of half the screen to subtract?
Try
int[] loc = new int[2];
buttons.get(arrayNumber).getLocationInWindow(loc);
horizontalScrollView.scrollTo(loc[0], 0);
Example Code -
activity_scroll.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:background="#android:color/black">
<HorizontalScrollView
android:id="#+id/horizon"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="1"
android:layout_margin="5dp" />
<Button
android:id="#+id/button_2"
android:layout_width="100dp"
android:text="2"
android:layout_height="100dp"
android:layout_margin="5dp" />
<Button
android:id="#+id/button_3"
android:layout_width="100dp"
android:text="3"
android:layout_height="100dp"
android:layout_margin="5dp" />
<Button
android:id="#+id/button_4"
android:layout_width="100dp"
android:text="4"
android:layout_height="100dp"
android:layout_margin="5dp" />
<Button
android:id="#+id/button_5"
android:layout_width="100dp"
android:text="5"
android:layout_height="100dp"
android:layout_margin="5dp" />
<Button
android:id="#+id/button_6"
android:text="6"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp" />
<Button
android:id="#+id/button_7"
android:layout_width="100dp"
android:text="7"
android:layout_height="100dp"
android:layout_margin="5dp" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
TestActivity.java
public class TestActivity extends AppCompatActivity implements View.OnClickListener {
View[] views = new View[7];
int res[] = {R.id.button_1, R.id.button_2, R.id.button_3, R.id.button_4, R.id.button_5, R.id.button_6, R.id.button_7};
HorizontalScrollView horizontalScrollView;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
for (int i = 0; i < res.length; i++) {
views[i] = findViewById(res[i]);
views[i].setOnClickListener(this);
}
horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizon);
}
#Override
public void onClick(View v) {
int[] loc = new int[2];
views[3].getLocationInWindow(loc);
horizontalScrollView.scrollTo(loc[0], 0);
}
}
My working example.
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();
}
});
}
I want to create dynamic layout but getting null poniter exception when I add control into view by addview() method.how I solve this error?
this is my xml file
select_theator.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" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="100dp"
android:layout_height="100dp" >
<ImageView
android:id="#+id/iv1"
android:layout_width="115dp"
android:layout_height="128dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="175dp"
android:layout_height="175dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/linearLayout2"
android:layout_marginTop="25dp"
android:background="#drawable/round">
<TextView
android:id="#+id/tvtheator"
android:layout_width="100dp"
android:layout_height="38dp"
android:layout_marginTop="10dp"
android:layout_weight="0.02"
android:text="Copperas Cove"
android:textColor="#000000" />
<ImageView
android:id="#+id/myimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="90dp"
android:layout_marginTop="15dp"
android:layout_weight="0.00"
android:layout_marginRight="20dp"
android:background="#drawable/arrow" />
</LinearLayout>
<Button
android:id="#+id/seat"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:layout_marginTop="30dp"
android:background="#drawable/green"
android:text="SELECT SEATS" />
<Button
android:id="#+id/share"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/seat"
android:layout_marginTop="18dp"
android:background="#drawable/round"
android:text="Share" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/linearLayout2"
android:layout_alignParentRight="true"
android:layout_marginLeft="110dp" >
</RelativeLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_above="#+id/linearLayout1"
android:layout_alignParentRight="true"
android:background="#drawable/gray" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvname"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="22 JUMP STREET" />
<TextView
android:id="#+id/tvdate"
android:layout_width="50dp"
android:layout_height="34dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/tvname"
android:maxLines="1"
android:text="Thu,Jul 10th 11:05 AM"
android:textColor="#000000"/>
<Button
android:id="#+id/br"
style="?android:attr/buttonStyleSmall"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="#+id/tvdate"
android:layout_centerHorizontal="true"
android:background="#drawable/round"
android:text="R" />
<Button
android:id="#+id/btime"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvdate"
android:background="#drawable/round"
android:text="1 hr 50 min." />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
this is my edited java file
Theator.java
public class Theator extends Activity{
Bitmap bitmap2;
ImageView iv,iv1;
TextView tv1,tv2,tv3;
Button bt1,bt2,bt3,bt4;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
setContentView(sv);
View view = LayoutInflater.from(this).inflate(R.layout.select_theator, ll, false);
iv = (ImageView)findViewById(R.id.iv1);
ll.addView(iv);
tv1=(TextView)findViewById(R.id.tvname);
ll.addView(tv1);
tv2=(TextView)findViewById(R.id.tvdate);
ll.addView(tv2);
bt1=(Button)findViewById(R.id.br);
ll.addView(bt1);
bt2=(Button)findViewById(R.id.btime);
ll.addView(bt2);
tv3=(TextView)findViewById(R.id.tvtheator);
ll.addView(tv3);
iv1=(ImageView)findViewById(R.id.myimg);
ll.addView(iv1);
bt3=(Button)findViewById(R.id.seat);
ll.addView(bt3);
bt4=(Button)findViewById(R.id.share);
ll.addView(bt4);
bitmap2 = getBitmapFromUrl("http://content7.flixster.com/movie/11/17/82/11178261_pro.jpg");
iv=(ImageView)findViewById(R.id.iv1);
iv.setImageBitmap(bitmap2);
}
private Bitmap getBitmapFromUrl(String src) {
// TODO Auto-generated method stub
try {
URL url =new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input=connection.getInputStream();
Bitmap mybiBitmap=BitmapFactory.decodeStream(input);
return mybiBitmap;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
}
You put the line
setContentView(sv);
after all view has been added.
iv = (ImageView)findViewById(R.id.iv1);
findViewById looks for that view in the current view hierarchy, which, in your case, consists only of the ScrollView and the LinearLayout. Use an inflater to retrieve an object of the view that contains the ids you are looking for
View view = LayoutInflater.from(this).inflate(R.layout.select_theator, ll, false);
and than use
iv = (ImageView)view.findViewById(R.id.iv1);
or you can use directly
View view = LayoutInflater.from(this).inflate(R.layout.select_theator, ll);
That will add the inflated view inside ll and, after you call setContentView, you can use findViewById, because now, that layout is part of the Activity view hierarchy
There is a page in my android application which has a slide out menu and some data that needs to be displayed in the main screen. The slide out menu has the required menu options.
the main page is in ListView format. I want a TableLayout format layout inside this ListView.
The problem is when i change the layout to TableLayout, it messes up with my java activity file.
I want to display data extracted from an external source on the main screen of this page in table format.
The XML and Java Activity files are enclosed.
Thanks in advance.
XML Code
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/galaxy">
<!-- Menu Panel -->
<RelativeLayout
android:id="#+id/menuPanel"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:gravity="right"
android:background="#drawable/grey"
android:orientation="vertical" >
<TextView
android:id="#+id/menu_title_1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:paddingLeft="15dp"
android:gravity="center_vertical"
android:background="#drawable/grey"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black"
android:text="MAIN MENU">
</TextView>
<View
android:id="#+id/menu_item_divider_1"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_below="#+id/menu_title_1"
android:background="#000000"/>
<Button
android:id="#+id/readall"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="0dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_below="#+id/menu_item_divider_1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="All Parameters"/>
<Button
android:id="#+id/param1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_below="#+id/readall"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Parameter 1"/>
<Button
android:id="#+id/param2"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_below="#+id/param1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Parameter 2"/>
<Button
android:id="#+id/param3"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_below="#+id/param2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Parameter 3"/>
<Button
android:id="#+id/dtc"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="0dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_below="#+id/param3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="D.T.C."/>
<Button
android:id="#+id/logout"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="0dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_below="#+id/dtc"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Log Out"/>
</RelativeLayout>
<!-- Sliding Panel -->
<LinearLayout
android:id="#+id/slidingPanel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:orientation="vertical"
android:background="#drawable/grey" >
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/black" >
<View
android:id="#+id/header_vertical_divider_1"
android:layout_width="2dp"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/menuViewButton"
android:background="#drawable/grey" />
<ImageView
android:id="#+id/menuViewButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:clickable="true"
android:contentDescription="Main Menu"
android:textColor="#000000"
android:src="#drawable/icon"
android:visibility="visible" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="22dp"
android:layout_toRightOf="#+id/header_vertical_divider_1"
android:text="ECU Description"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
</RelativeLayout>
<View
android:id="#+id/dividerHeaderBottom"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#414141" ></View>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:divider="#000000"
android:dividerHeight="0.5dp"
android:background="#drawable/galaxy" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#+id/footer"
style="#android:style/ButtonBar"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom|center"
android:background="#drawable/black"
android:orientation="horizontal" >
<EditText
android:id="#+id/DefUid"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_weight="0.50"
android:digits="0123456789."
android:hint="IP Address"
android:maxLength="15"
android:maxWidth="350dip"
android:singleLine="true"
android:windowSoftInputMode="adjustNothing" >
</EditText>
<EditText
android:id="#+id/DefUid"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_weight="0.20"
android:maxWidth="80dip"
android:hint="Port"
android:digits="0123456789"
android:maxLength="4"
android:singleLine="true" >
</EditText>
<Button
android:id="#+id/conn"
android:layout_width="0px"
android:layout_height="40dp"
android:layout_weight="0.15"
android:text="C"/>
<Button
android:id="#+id/disc"
android:layout_width="0px"
android:layout_height="40dp"
android:layout_weight="0.15"
android:text="D"/>
</LinearLayout>
</FrameLayout>
Java Activity File
public class LayerStack extends Activity
{
//Declare
private LinearLayout slidingPanel;
private boolean isExpanded;
private DisplayMetrics metrics;
private ListView listView;
private RelativeLayout headerPanel;
private RelativeLayout menuPanel;
private int panelWidth;
private ImageView menuViewButton;
private static final int HIDE_TOOLBARS = 0;
private static final int SHOW_TOOLBARS = 1;
private static final int TOGGLE_TOOLBARS = 2;
Handler handler;
LinearLayout toolbarTop, toolbarBottom;
FrameLayout.LayoutParams menuPanelParameters;
FrameLayout.LayoutParams slidingPanelParameters;
LinearLayout.LayoutParams headerPanelParameters ;
LinearLayout.LayoutParams listViewParameters;
//#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.page7);
//ImageView myImage = (ImageView) findViewById(R.id.menuPanel);
//myImage.setAlpha(100);
// Get references to the elements in the layout
//Initialize
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
panelWidth = (int) ((metrics.widthPixels)*0.50);
headerPanel = (RelativeLayout) findViewById(R.id.header);
headerPanelParameters = (LinearLayout.LayoutParams) headerPanel.getLayoutParams();
headerPanelParameters.width = metrics.widthPixels;
headerPanel.setLayoutParams(headerPanelParameters);
menuPanel = (RelativeLayout) findViewById(R.id.menuPanel);
menuPanelParameters = (FrameLayout.LayoutParams) menuPanel.getLayoutParams();
menuPanelParameters.width = panelWidth;
menuPanel.setLayoutParams(menuPanelParameters);
slidingPanel = (LinearLayout) findViewById(R.id.slidingPanel);
slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel.getLayoutParams();
slidingPanelParameters.width = metrics.widthPixels;
slidingPanel.setLayoutParams(slidingPanelParameters);
listView = (ListView) findViewById(R.id.list);
listViewParameters = (LinearLayout.LayoutParams) listView.getLayoutParams();
listViewParameters.width = metrics.widthPixels;
listView.setLayoutParams(listViewParameters);
//Slide the Panel
menuViewButton = (ImageView) findViewById(R.id.menuViewButton);
menuViewButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!isExpanded)
{
isExpanded = true;
//Expand
new ExpandAnimation(slidingPanel, panelWidth,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.50f, 0, 0.0f, 0, 0.0f);
Button b2 = (Button) findViewById(R.id.readall);
b2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent(LayerStack.this,AppActivity8.class);
startActivity(i);
finish();
}
});
Button b1 = (Button) findViewById(R.id.logout);
b1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
new AlertDialog.Builder(LayerStack.this)
.setTitle("Log Out Confirmation")
.setMessage("Are you sure you want to log out?")
.setNegativeButton("No", null)
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface arg0,int arg1)
{
Intent i = new Intent(LayerStack.this,AppActivity4.class);
startActivity(i);
finish();
}
}).create().show();
}
});
}
else
{
isExpanded = false;
//Collapse
new CollapseAnimation(slidingPanel,panelWidth,
TranslateAnimation.RELATIVE_TO_SELF, 0.50f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);
}
}
});
}
}
I Used MultiColumn ListView Instead.
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);