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

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);

Related

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"

Different sound on each button

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();
}
});
}

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.

Trying to generate a simple pop-up

I am trying to generate a simple popup on click of a button, but due to some reason it is not working. Below is my code:
public class Product extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.product);
String fontPath = "fonts/georgia.ttf";
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
final Animation animScale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
Button Particle =(Button)findViewById(R.id.button1);
Button MDF=(Button)findViewById(R.id.button2);
Button Laminates=(Button)findViewById(R.id.button3);
Button Ply =(Button)findViewById(R.id.button4);
Button Floor=(Button)findViewById(R.id.button5);
Button Door=(Button)findViewById(R.id.button6);
Button EdgeBand=(Button)findViewById(R.id.button7);
Button ModFur=(Button)findViewById(R.id.button8);
Particle.setTypeface(tf);
MDF.setTypeface(tf);
Laminates.setTypeface(tf);
Ply.setTypeface(tf);
Floor.setTypeface(tf);
Door.setTypeface(tf);
EdgeBand.setTypeface(tf);
ModFur.setTypeface(tf);
/* Particle.startAnimation(animScale);
MDF.startAnimation(animScale);
Laminates.startAnimation(animScale);
Ply.startAnimation(animScale);
Floor.startAnimation(animScale);
Door.startAnimation(animScale);
EdgeBand.startAnimation(animScale);
ModFur.startAnimation(animScale); */
Particle.setOnClickListener(Par);
}
private View.OnClickListener Par = new View.OnClickListener(){
public void onClick(View v){
openNewDialog();
}
};
private void openNewDialog() {
new AlertDialog.Builder(this).setItems(R.array.pop,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialoginterface,int i){
if(i==0){
Intent about = new Intent(Product.this,AboutUs.class);
startActivity(about);
}
if(i==1){
Intent about = new Intent(Product.this,AboutUs.class);
startActivity(about);
}
}});
}
In the Strings.xml file:
<array name="pop">
<item name="easy_label">Key Features</item>
<item name="medium_label">Advantages</item>
</array>
The layout file for the said activity in xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/universalbg"
android:orientation="vertical" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:text="#string/btn2"
android:textColor="#FFFFFF"
android:textSize="24dp"
android:textStyle="bold" />
</RelativeLayout>
<ScrollView
android:layout_marginTop="10dip"
android:layout_marginBottom="03dip"
android:id="#+id/Scroll"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="20dp"
android:background="#drawable/insidebut"
android:text="#string/BTn2"
android:textColor="#900606" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_below="#+id/button2"
android:layout_marginTop="20dp"
android:background="#drawable/insidebut"
android:text="#string/BTn3"
android:textColor="#900606" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button3"
android:layout_below="#+id/button3"
android:layout_marginTop="20dp"
android:background="#drawable/insidebut"
android:text="#string/BTn4"
android:textColor="#900606" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button4"
android:layout_below="#+id/button4"
android:layout_marginTop="20dp"
android:background="#drawable/insidebut"
android:text="#string/BTn5"
android:textColor="#900606" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button5"
android:layout_below="#+id/button5"
android:layout_marginTop="20dp"
android:background="#drawable/insidebut"
android:text="#string/BTn6"
android:textColor="#900606" />
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button6"
android:layout_below="#+id/button6"
android:layout_marginTop="20dp"
android:background="#drawable/insidebut"
android:text="#string/BTn7"
android:textColor="#900606" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button7"
android:layout_below="#+id/button7"
android:layout_marginTop="20dp"
android:background="#drawable/insidebut"
android:text="#string/BTn8"
android:textColor="#900606" />
</ScrollView>
You need to call show() in order to actually display the dialog.
private void openNewDialog() {
new AlertDialog.Builder(this).setItems(R.array.pop,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialoginterface,int i){
if(i==0){
Intent about = new Intent(Product.this,AboutUs.class);
startActivity(about);
}
if(i==1){
Intent about = new Intent(Product.this,AboutUs.class);
startActivity(about);
}
}}).show(); // Here
}

Categories

Resources