MainActivity.java
package com.jamesvuong.footballscorekeeper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
final int TOUCHDOWN_POINTS = 6;
final int FIELD_GOAL_POINTS = 3;
final int EXTRA_POINT_1_POINT = 1;
final int EXTRA_POINT_2_POINTS = 2;
final int SAFETY_POINTS = 2;
String score_a , score_b;
String TEXT_A , TEXT_B;
int team_a_score;
int team_b_score;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
team_a_score = savedInstanceState.getInt(TEXT_A);
team_b_score = savedInstanceState.getInt(TEXT_B);
}
else {
// Probably initialize members with default values for a new instance
team_a_score = 0;
team_b_score = 0;
}
setContentView(R.layout.activity_main);
displayTeamAScore(team_a_score);
displayTeamBScore(team_b_score);
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(TEXT_A,team_a_score);
savedInstanceState.putInt(TEXT_B, team_b_score);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
// Restore state members from saved instance
team_a_score = savedInstanceState.getInt(TEXT_A);
team_b_score = savedInstanceState.getInt(TEXT_B);
}
/**
* Update Scores
*/
public void resetScores(View v) {
team_a_score = 0;
team_b_score = 0;
displayTeamAScore(team_a_score);
displayTeamBScore(team_b_score);
}
public void updateTeamScore(View v) {
switch(v.getId()) {
case R.id.team_a_touchdown:
team_a_score += TOUCHDOWN_POINTS;
break;
case R.id.team_a_field_goal:
team_a_score += FIELD_GOAL_POINTS;
break;
case R.id.team_a_extra_point_1:
team_a_score += EXTRA_POINT_1_POINT;
break;
case R.id.team_a_extra_point_2:
team_a_score += EXTRA_POINT_2_POINTS;
break;
case R.id.team_a_safety:
team_a_score += SAFETY_POINTS;
break;
case R.id.team_b_touchdown:
team_b_score += TOUCHDOWN_POINTS;
break;
case R.id.team_b_field_goal:
team_b_score += FIELD_GOAL_POINTS;
break;
case R.id.team_b_extra_point_1:
team_b_score += EXTRA_POINT_1_POINT;
break;
case R.id.team_b_extra_point_2:
team_b_score += EXTRA_POINT_2_POINTS;
break;
case R.id.team_b_safety:
team_b_score += SAFETY_POINTS;
break;
default:
break;
}
displayTeamAScore(team_a_score);
displayTeamBScore(team_b_score);
}
/**
* Display Scores
*/
public void displayTeamAScore(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
public void displayTeamBScore(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_b_score);
scoreView.setText(String.valueOf(score));
}
}
Activity_main.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:padding="10dp"
tools:context="com.jamesvuong.footballscorekeeper.MainActivity">
<LinearLayout
android:id="#+id/score_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/team_a_score_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:orientation="vertical">
<TextView
android:id="#+id/team_a_label"
android:text="Team A"
style="#style/TeamLabelStyle"/>
<TextView
android:id="#+id/team_a_score"
android:text="0"
android:saveEnabled="true"
style="#style/ScoreTextStyle"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="-"
android:gravity="center"
android:textStyle="bold"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/team_b_score_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:orientation="vertical">
<TextView
android:id="#+id/team_b_label"
android:text="Team B"
style="#style/TeamLabelStyle"/>
<TextView
android:id="#+id/team_b_score"
android:text="0"
android:saveEnabled="true"
style="#style/ScoreTextStyle"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/scoring_buttons_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="#+id/touchdown_container"
style="#style/ScoreTypeButtonContainerStyle">
<Button
android:id="#+id/team_a_touchdown"
style="#style/ScoreButtonStyle"
android:text="+6"
android:onClick="updateTeamScore"/>
<TextView
android:text="Touchdown"
style="#style/ScoreTypeLabelStyle"/>
<Button
android:id="#+id/team_b_touchdown"
style="#style/ScoreButtonStyle"
android:text="+6"
android:onClick="updateTeamScore"/>
</LinearLayout>
<LinearLayout
android:id="#+id/field_goal_container"
style="#style/ScoreTypeButtonContainerStyle">
<Button
android:id="#+id/team_a_field_goal"
style="#style/ScoreButtonStyle"
android:text="+3"
android:onClick="updateTeamScore"/>
<TextView
android:text="Field Goal"
style="#style/ScoreTypeLabelStyle"/>
<Button
android:id="#+id/team_b_field_goal"
style="#style/ScoreButtonStyle"
android:text="+3"
android:onClick="updateTeamScore"/>
</LinearLayout>
<LinearLayout
android:id="#+id/extra_point_container"
style="#style/ScoreTypeButtonContainerStyle">
<Button
android:id="#+id/team_a_extra_point_1"
style="#style/ScoreButtonStyle"
android:text="+1"
android:onClick="updateTeamScore"/>
<Button
android:id="#+id/team_a_extra_point_2"
style="#style/ScoreButtonStyle"
android:text="+2"
android:onClick="updateTeamScore"/>
<TextView
style="#style/ScoreTypeLabelStyle"
android:text="Extra Point" />
<Button
android:id="#+id/team_b_extra_point_1"
style="#style/ScoreButtonStyle"
android:text="+1"
android:onClick="updateTeamScore"/>
<Button
android:id="#+id/team_b_extra_point_2"
style="#style/ScoreButtonStyle"
android:text="+2"
android:onClick="updateTeamScore"/>
</LinearLayout>
<LinearLayout
android:id="#+id/safety_container"
style="#style/ScoreTypeButtonContainerStyle">
<Button
android:id="#+id/team_a_safety"
style="#style/ScoreButtonStyle"
android:text="+2"
android:onClick="updateTeamScore"/>
<TextView
style="#style/ScoreTypeLabelStyle"
android:text="Safety"/>
<Button
android:id="#+id/team_b_safety"
style="#style/ScoreButtonStyle"
android:text="+2"
android:onClick="updateTeamScore"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#333333"
android:onClick="resetScores"
android:text="Reset"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
I want to save some values, like score of both the team, so that even after the app is killed(not running anymore in background) the scores are saved and can be retained back when the user again opens the app.I have tried using 'savedInstanceState' but its not working. Please help. I am a beginner in android development.
savedInstanceState is used to save the current state of the app during screen rotations, not to save data permanently. SharedPreferences is what you are looking for. Here is an example :
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
// save
sharedPreferences.edit()
.putString("key1", "value")
.putInt("key2", 10)
.apply();
// retrieve
String stringValue = sharedPreferences.getString("key1", null);
int intValue = sharedPreferences.getInt("key2", -1);
// null and -1 are default values if not found
if your app has not been killed then u can store values using saveInstanceState/Application class else U have to use Shared-preference or Sqlite/Other ORM.
Related
I would like to create a countdown where the initial number is given by the user. I created this application but when the application starts shut down. I do not understand what the mistake is because there are no errors during the compilation. What am I doing wrong?
this is the code:
public class Main2Activity extends AppCompatActivity {
Button bstart, bStopReset;
EditText editTimer;
CountDownTimer timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
editTimer = (EditText) findViewById(R.id.contatore);
bstart = (Button) findViewById(R.id.start);
bStopReset = (Button) findViewById(R.id.stop_reset);
}
String valore = editTimer.getText().toString();
int ValoreIntero = Integer.parseInt(valore);
public void startOnClick(View view) {
timer = new CountDownTimer(ValoreIntero, 1000) {
#Override
public void onTick(final long millSecondsLeftToFinish) {
String time = String.valueOf(millSecondsLeftToFinish / 1000);
editTimer.setText(time);
}
#Override
public void onFinish() {
editTimer.setText("Done!");
}
};
timer.start();
}
public void stopOnClick(View view) {
timer.cancel();
editTimer.setText("0");
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_bright"
android:orientation="vertical"
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:weightSum="1"
tools:context="com.example.gabrypacor.orologio.Main2Activity">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.39"
android:gravity="center"
android:text="countdown"
android:textAlignment="center"
android:textSize="36sp" />
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.27"
android:gravity="center"
android:text="inserisci il tempo del countdown nel riquadro blu"
android:textAlignment="center"
android:textSize="20sp"
tools:textAlignment="center" />
<EditText
android:id="#+id/contatore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.21"
android:background="#android:color/holo_blue_light"
android:ems="10"
android:inputType="numberDecimal"
android:text="0"
android:textAlignment="center"
android:textColorLink="?android:attr/textColorPrimaryDisableOnly"
android:textSize="60sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.18"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_weight="1"
android:onClick="startOnClick"
android:text="inizio" />
<Button
android:id="#+id/stop_reset"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_weight="1"
android:onClick="stopOnClick"
android:text="stop/reset" />
</LinearLayout>
You are setting these two values when your activity is initialized rather than when the user clicks the start button:
String valore = editTimer.getText().toString();
int ValoreIntero = Integer.parseInt(valore);
Move them into your startOnClick method and that should fix the exception you are getting.
I created a list of checkboxes, and I was trying to increase an int value proportionally to the number of the selected ones. Instead, every time I try to check them (through .isChecked()) the app crashes. I've tried to search everywhere for some solutions, but nothing worked. I really can't understand where is the problem.
Here's the code
XML file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.deadlybanana.myfirstapp.DisplayPicture">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="0dp">
<LinearLayout
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/unicorn" />
<TextView
android:id="#+id/frasenome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<CheckBox
android:id="#+id/checkbox1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Beatiful?" />
<CheckBox
android:id="#+id/checkbox2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Fabolous?" />
<CheckBox
android:id="#+id/checkbox3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Unique?" />
<CheckBox
android:id="#+id/checkbox4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you a horse?" />
<CheckBox
android:id="#+id/checkbox5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Do you eat grass?" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="checkbutton"
android:text="Check if you are a unicorn" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Java file
package com.example.deadlybanana.myfirstapp;
public class DisplayPicture extends AppCompatActivity {
List<Boolean> checkboxes = new ArrayList<Boolean>();
CheckBox press1;
CheckBox press2;
CheckBox press3;
CheckBox press4;
CheckBox press5;
int points = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_picture);
Intent intent = getIntent();
String name = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
CheckBox press1 = (CheckBox)findViewById(R.id.checkbox1);
CheckBox press2 = (CheckBox)findViewById(R.id.checkbox2);
CheckBox press3 = (CheckBox)findViewById(R.id.checkbox3);
CheckBox press4 = (CheckBox)findViewById(R.id.checkbox4);
CheckBox press5 = (CheckBox)findViewById(R.id.checkbox5);
TextView NameString = (TextView) findViewById(R.id.frasenome);
String phrase = name + ", are you some of these things?";
NameString.setText(phrase);
}
public void checkbutton(View view){
if (press1.isChecked()){
points += 4;
}
if (points > 3){
Toast.makeText(this, "You are a unicorn", Toast.LENGTH_LONG).show();
} else{
Toast.makeText(this, "You are not a unicorn", Toast.LENGTH_LONG).show();
}
}
}
(right now it is being examined just the first checkbox to simplify the code)
Try this: in onCreate()
press1 = (CheckBox)findViewById(R.id.checkbox1);
press2 = (CheckBox)findViewById(R.id.checkbox2);
press3 = (CheckBox)findViewById(R.id.checkbox3);
press4 = (CheckBox)findViewById(R.id.checkbox4);
press5 = (CheckBox)findViewById(R.id.checkbox5);
error occurs as press1.isChecked() is refers to the global CheckBox press1;
which is not yet initialized..
AS you are initializing the checkbox like:
CheckBox press1 = (CheckBox)findViewById(R.id.checkbox1); inside onCreate() which only gives it a local instance which is not accessable in your checkbutton(View view) function
Solution
remove the local initialization just use: press1 = (CheckBox)findViewById(R.id.checkbox1);
Set id for your button lets assume it be button and add the following code
Button btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
checkbutton();
}
});
Note:remove the view parameter
Implementing Listeners is the best way to handle the events
I never use the method isChecked() because it never works for me. Do you want to increase the points when you select a checkbutton and maybe show the points when you press a button?
Here is the class code:
public class ButtonActivity extends AppCompatActivity
{
int points=0;
boolean check1=false;
boolean check2=false;
boolean check3=false;
boolean check4=false;
boolean check5=false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),points+"",Toast.LENGTH_SHORT).show();
}
});
}
public void onClick(View view)
{
switch(view.getId())
{
case R.id.checkBox:
if(check1)
{
check1=false;
points--;
}
else
{
check1=true;
points++;
}
break;
case R.id.checkBox2:
if(check2)
{
check2=false;
points--;
}
else
{
check2=true;
points++;
}
break;
case R.id.checkBox3:
if(check3)
{
check3=false;
points--;
}
else
{
check3=true;
points++;
}
break;
case R.id.checkBox4:
if(check4)
{
check4=false;
points--;
}
else
{
check4=true;
points++;
}
break;
case R.id.checkBox5:
if(check5)
{
check5=false;
points--;
}
else
{
check5=true;
points++;
}
break;
}
}
}
Here is the layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_button"
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"
tools:context="com.example.utente.stackoverflow.ButtonActivity">
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox2"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox3"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox4"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox5"
android:onClick="onClick"/>
<Button
android:text="Points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button" />
I tested it and it works. I hope this helps you.
I don't understand why the app doesn't work when I launch it.
First of all, this is what I want:
Spaceship appears perfectly
And this is what appears when I launch the game:
Spaceship disappears!
I dont know what is wrong. I haven't an atribute which makes invisible the ImageView... Here is my code:
game_activity.xml (game design)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.spaceinvaders.GameActivity">
<ImageView
android:id="#+id/fondo_juego"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/fondo3" />
<ImageView
android:id="#+id/enemigo"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:src="#drawable/enemigodiseno11"/>
<Button
android:id="#+id/disparo"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:onClick="dispara"
android:visibility="visible" />
<ImageView
android:id="#+id/municion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/disparo"
android:layout_centerHorizontal="true"
android:src="#drawable/municion"
android:visibility="invisible" />
<ImageView
android:id="#+id/nave"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:src="#drawable/diseno11" />
<Button
android:id="#+id/control_izquierda"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#android:color/transparent"
android:onClick="actualizaPosicion"
android:visibility="visible" />
<Button
android:id="#+id/control_derecha"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:onClick="actualizaPosicion"
android:visibility="visible" />
</RelativeLayout>
activity_main.xml (This is the principal screen)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/principal_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.spaceinvaders.MainActivity">
<ImageView
android:id="#+id/fondo_pantalla_principal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/fondo2" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:adjustViewBounds="true"
android:src="#drawable/iconotitulo"
android:layout_centerHorizontal="true"/>
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/iconotitulo1"/>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical">
<ImageButton
android:id="#+id/play_boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
android:onClick="iniciaJuego"
android:src="#drawable/boton1" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_margin="10dp">
<ImageButton
android:id="#+id/opcion_boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
android:src="#drawable/boton2" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
popup_activity.xml (a simple popup which allow the user to change some parameters)
<?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="#839ceaac"
android:orientation="vertical">
<ImageButton
android:id="#+id/volver_boton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="2dp"
android:layout_marginTop="3dp"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
android:src="#drawable/cerrar" />
<TextView
android:id="#+id/titulo_opciones"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="6dp"
android:layout_marginTop="5dp"
android:text="Opciones gráficas"
android:textSize="20dp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="#id/titulo_opciones"
android:background="#9dc8a6" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/titulo_opciones"
android:layout_marginBottom="8dp"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/fondo_titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:text="Fondo"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/fondo_1"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/fondo_titulo"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="actualizaFondo"
android:scaleType="centerCrop"
android:src="#drawable/fondo" />
<ImageView
android:id="#+id/fondo_2"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/fondo_1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="actualizaFondo"
android:scaleType="centerCrop"
android:src="#drawable/fondo1" />
<ImageView
android:id="#+id/fondo_3"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/fondo_2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="actualizaFondo"
android:scaleType="centerCrop"
android:src="#drawable/fondo3" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/nave_titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:text="Nave"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/nave_1"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/nave_titulo"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="actualizaNave"
android:src="#drawable/diseno11" />
<ImageView
android:id="#+id/nave_2"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/nave_1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="actualizaNave"
android:src="#drawable/diseno21" />
<ImageView
android:id="#+id/nave_3"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/nave_2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="actualizaNave"
android:src="#drawable/diseno31" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/enemigos_titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:text="Enemigos"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/enemigo_1"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/enemigos_titulo"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="actualizaEnemigo"
android:rotation="180"
android:src="#drawable/enemigodiseno11" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
GameActivity.java (game_activity.xml functionality)
package com.example.android.spaceinvaders;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class GameActivity extends AppCompatActivity {
ImageView municion;
ImageView nave;
ImageView fondoJuego;
ImageView enemigo;
Button botonDisparo;
Handler manejaDisparo = new Handler();
Handler manejaEnemigo = new Handler();
final int movimiento = 30;
final int movimientoEnemigo = 20;
boolean inicioAFin = false;
int ladeadoIzq, ladeadoDer, frontal, disparo;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_activity);
municion = (ImageView) findViewById(R.id.municion);
nave = (ImageView) findViewById(R.id.nave);
enemigo = (ImageView) findViewById(R.id.enemigo);
fondoJuego = (ImageView) findViewById(R.id.fondo_juego);
botonDisparo = (Button) findViewById(R.id.disparo);
Intent i = getIntent();
if (i != null) {
String data = i.getStringExtra("arg");
introduceCambios(data);
}
manejaEnemigo.postDelayed(accionMovimiento, 0);
}
private void introduceCambios(String data) {
String[] info = data.split(" ");
int idFondo = getResources().getIdentifier(info[0], "drawable", getPackageName());
fondoJuego.setImageResource(idFondo);
int idNave = getResources().getIdentifier(info[1], "drawable", getPackageName());
cambiosMovilidad(idNave);
nave.setImageResource(frontal);
int idEnemigo = getResources().getIdentifier(info[2], "drawable", getPackageName());
enemigo.setImageResource(idEnemigo);
}
public void actualizaPosicion(View v) {
switch (v.getId()) {
case (R.id.control_derecha):
if (!seSale("der", "CU")) {
nave.setImageResource(ladeadoIzq);
nave.setX(nave.getX() - movimiento);
}
break;
case R.id.control_izquierda:
if (!seSale("izq", "CU")) {
nave.setImageResource(ladeadoDer);
nave.setX(nave.getX() + movimiento);
}
break;
}
}
private void cambiosMovilidad(int idNave) {
switch (idNave) {
case 2130837589:
frontal = R.drawable.diseno11;
ladeadoDer = R.drawable.diseno13;
ladeadoIzq = R.drawable.diseno12;
disparo = R.drawable.municion;
break;
case 2130837592:
frontal = R.drawable.diseno21;
ladeadoDer = R.drawable.diseno23;
ladeadoIzq = R.drawable.diseno22;
disparo = R.drawable.municion1;
break;
case 2130837595:
frontal = R.drawable.diseno31;
ladeadoDer = R.drawable.diseno33;
ladeadoIzq = R.drawable.diseno32;
break;
}
}
public void dispara(View v) {
nave.setImageResource(frontal);
municion.setImageResource(disparo);
municion.setX(nave.getX() + (((nave.getWidth()) / 2) - 5));
municion.setY(nave.getY());
municion.setVisibility(View.VISIBLE);
botonDisparo.setEnabled(false);
manejaDisparo.postDelayed(accionDisparo, 0);
}
Runnable accionDisparo = new Runnable() {
#Override
public void run() {
municion.setY(municion.getY() - 50);
if (llegaAlFinal()) {
municion.setVisibility(View.INVISIBLE);
manejaDisparo.removeCallbacks(accionDisparo);
botonDisparo.setEnabled(true);
}
manejaDisparo.postDelayed(this, 80);
}
};
Runnable accionMovimiento = new Runnable() {
#Override
public void run() {
if (inicioAFin) {
enemigo.setImageResource(R.drawable.enemigodiseno12);
enemigo.setX(enemigo.getX() + movimientoEnemigo);
} else {
enemigo.setImageResource(R.drawable.enemigodiseno13);
enemigo.setX(enemigo.getX() - movimientoEnemigo);
}
if (seSale("izq", "IA") || seSale("der", "IA"))
inicioAFin = !inicioAFin;
manejaEnemigo.postDelayed(this, 80);
}
};
private boolean llegaAlFinal() {
return municion.getY() <= 20;
}
private boolean seSale(String direccion, String jugador) {
switch (direccion) {
case "izq":
switch (jugador) {
case "CU":
return (nave.getX() + movimiento + nave.getWidth()) > findViewById(R.id.activity_main).getWidth();
case "IA":
return (enemigo.getX() + movimiento + enemigo.getWidth()) > findViewById(R.id.activity_main).getWidth();
}
case "der":
switch (jugador) {
case "CU":
return (nave.getX() - movimiento) < 0;
case "IA":
return (enemigo.getX() - movimiento) < 0;
}
}
return true;
}
}
MainActivity.java (activity_main.xml and popup_activity.xml functionality)
package com.example.android.spaceinvaders;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
ImageButton opcionBoton;
private PopupWindow popup;
private RelativeLayout layoutPrincipal;
String[] resultados;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultados = new String[3];
resultados[0] = "fondo3";
resultados[1] = "diseno11";
resultados[2] = "enemigodiseno11";
opcionBoton = (ImageButton) findViewById(R.id.opcion_boton);
layoutPrincipal = (RelativeLayout) findViewById(R.id.principal_screen);
opcionBoton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View vistaPopup = inflater.inflate(R.layout.popup_activity, null);
popup = new PopupWindow(
vistaPopup,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
ImageButton cerrarPop = (ImageButton) vistaPopup.findViewById(R.id.volver_boton);
cerrarPop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
popup.dismiss();
opcionBoton.setVisibility(View.VISIBLE);
}
});
popup.showAtLocation(layoutPrincipal, Gravity.BOTTOM, 0, 0);
opcionBoton.setVisibility(View.INVISIBLE);
}
});
}
public void iniciaJuego(View view) {
Intent juego = new Intent(view.getContext(), GameActivity.class);
juego.putExtra("arg", resultados[0] + " " + resultados[1] + " " + resultados[2]);
startActivity(juego);
}
public void actualizaFondo(View vista) {
switch (vista.getId()) {
case R.id.fondo_1:
resultados[0] = "fondo";
break;
case R.id.fondo_2:
resultados[0] = "fondo1";
break;
case R.id.fondo_3:
resultados[0] = "fondo3";
break;
}
}
public void actualizaNave(View vista) {
switch (vista.getId()) {
case R.id.nave_1:
resultados[1] = "diseno11";
break;
case R.id.nave_2:
resultados[1] = "diseno21";
break;
case R.id.nave_3:
resultados[1] = "diseno31";
break;
}
}
public void actualizaEnemigo(View vista) {
switch (vista.getId()) {
case R.id.enemigo_1:
resultados[2] = "enemigodiseno11";
break;
}
}
}
I hope you could help me, please... I don't know where is the error.
The project repository is: github.com/cvazquezlos/Space-Invaders-Android
Thank you so much!
I'm trying to implement a Slinding Menu by using Pager Sliding Tab Strip (com.astuetz:pagerslidingtabstrip).
I set up my xml layout of the main screen:
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".97">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/logo_b"
android:src="#drawable/logo_b"
android:layout_weight=".05"
android:layout_gravity="right"
android:layout_marginRight="-15dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollMe"
android:layout_weight=".95"
android:fillViewport="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
app:pstsIndicatorColor="#19B5FE"
app:pstsShouldExpand="true"
app:pstsTextAllCaps="false"
app:pstsIndicatorHeight="1dp"
/>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
</LinearLayout>
This xml is included into another View, but it handle the View Pager.
To handle the init of ViewPager and PagerSlidingTapStrip i have a FragmentActivity where during onCreate i have:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding_menu);
pager = (ViewPager) findViewById(R.id.pager);
CustomPagerAdapteradapter = new CustomPagerAdapter(getSupportFragmentManager());
adapter.setContext(getApplicationContext());
pager.setAdapter(adapter);
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
}
And the adapter is:
public static class CustomPagerAdapteradapter extends FragmentPagerAdapter implements IconTabProvider {
private int[] ICONS = {
R.drawable.icon_menu0,
R.drawable.icon_menu1,
R.drawable.icon_menu2,
R.drawable.icon_menu3,
R.drawable.icon_menu4
};
private static Context CONTEXT;
private Fragment fragment[] = new Fragment[5];
public CustomPagerAdapteradapter (FragmentManager fm) {
super(fm);
for(int i = 0; i < 5; ++i) {
fragment[i] = Profile.newInstance();
}
}
public void setContext(Context c) { CONTEXT = c; }
#Override
public int getCount() {
return ICONS.length;
}
#Override
public Fragment getItem(int position) {
Toast.makeText(CONTEXT, "" + position, Toast.LENGTH_SHORT).show();
switch (position) {
case 0:
return fragment[position];
case 1:
return fragment[position];
case 2:
return fragment[position];
case 3:
return fragment[position];
case 4:
return fragment[position];
case 5:
return fragment[position];
default:
return fragment[position];
}
}
#Override
public int getPageIconResId(int position) {
return ICONS[position];
}
}
The Profile Fragment xml is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".90">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight=".20">
<!-- Not filled yet -->
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".70"
android:background="#drawable/radius_background"
android:layout_margin="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:textColor="#FFF"
android:textSize="16dp"
android:text="info#example.com"
android:id="#+id/user_mail" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:textColor="#FFF"
android:textSize="16dp"
android:text="15/10/54"
android:id="#+id/user_birth" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:textColor="#FFF"
android:textSize="16dp"
android:text="Italy"
android:id="#+id/user_country" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:textColor="#FFF"
android:textSize="16dp"
android:text="Pa"
android:id="#+id/user_town_short" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:textColor="#FFF"
android:textSize="16dp"
android:text="Palermo"
android:id="#+id/user_town" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:textColor="#FFF"
android:textSize="16dp"
android:text="Male"
android:id="#+id/user_sex" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight=".05">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="#FFF"
android:background="#18333E"
android:text="Edit/Update Data"
android:id="#+id/updateData"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Profile fragment onCreateView just inflate the xml:
return inflater.inflate(R.layout.fragment_profile, container, false);
Even if the code seems to be ok, I never see profile on the View Pager. What's wrong?
Final answer: remove the ScrollView as it is blocking with ViewPager.
public class AddActivity extends Activity implements OnClickListener{
String[] info = new String[11];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView keyString = (TextView)findViewById(R.id.keyString);
TextView site1 = (TextView)findViewById(R.id.site1);
TextView site2 = (TextView)findViewById(R.id.site2);
TextView site3 = (TextView)findViewById(R.id.site3);
ImageButton submit = (ImageButton)findViewById(R.id.submit);
ImageButton add1 = (ImageButton)findViewById(R.id.add1);
ImageButton add2 = (ImageButton)findViewById(R.id.add2);
ImageButton add3 = (ImageButton)findViewById(R.id.add3);
submit.setOnClickListener((OnClickListener) this);
add1.setOnClickListener((OnClickListener) this);
add2.setOnClickListener((OnClickListener) this);
add3.setOnClickListener((OnClickListener) this);
int id = v.getId();
switch(id){
case R.id.submit:{
submitEntry(info);
break;
}
case R.id.add1:{
add2.setVisibility(View.VISIBLE);
site2.setVisibility(View.VISIBLE);
break;
}
}
}
}
This is the code.
<?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" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/key_string"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<EditText
android:id="#+id/keyString"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="invisible" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/site_string"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new" />
<EditText
android:id="#+id/site1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/url_hint"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new"
android:visibility="invisible" />
<EditText
android:id="#+id/site2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="#string/url_hint"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="invisible" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new"
android:visibility="invisible" />
<EditText
android:id="#+id/site3"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="#string/url_hint"
android:visibility="invisible"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/submit_buttom" />
</LinearLayout>
</ScrollView>
</LinearLayout>
And this is the XML. The add1, add2, add3 and submit ImageButtons are all in a ScrollView.
When I press the add1 ImageButton, I want the add2 and site2 ImageButtons to become visible but instead, it throws the following error.
Motion event has invalid pointer count 0; value must be between 1 and 16.
What am I doing wrong?
PS: All the findViewById() calls are in the onClick() method because a NullPointerExeption is thrown if I call them in the onCreate().
Those findViewByIdcalls in onClickdon't make sense. Not sure why you are getting a null pointer exception calling them in onCreate.onClick is never called in this instance because nothing in the creation of the Activity is assigning the buttons to look at your onClick method; the buttons will default to having no listener assigned. It also doesn't look like a good idea to use the Activity as the onClickListener as well.
Your code should look something like this:
public class AddActivity extends Activity {
// https://source.android.com/source/code-style.html
// info -> mInfo; non-public, non-static field!
String[] mInfo = new String[11];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
TextView keyString = (TextView)findViewById(R.id.keyString);
TextView site1 = (TextView)findViewById(R.id.site1);
TextView site2 = (TextView)findViewById(R.id.site2);
TextView site3 = (TextView)findViewById(R.id.site3);
Button submit = (Button)findViewById(R.id.submit);
ImageButton add1 = (ImageButton)findViewById(R.id.add1);
ImageButton add2 = (ImageButton)findViewById(R.id.add2);
ImageButton add3 = (ImageButton)findViewById(R.id.add3);
add1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
add2.setVisibility(View.VISIBLE);
site2.setVisibility(View.VISIBLE);
}
});
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
submitEntry(mInfo);
}
});
}
The findViewById() calls and especially the setOnClickListener() calls should have been inside the onCreate(). With setOnClickListener() inside the onClick() i doubt the onClick was ever called.
We would need more logs to find the exact issue.