android button only working at second time click - android

I am developing an android game and, strangely, in my game's main menu the buttons only work the second time they are clicked. I already tried putting the onClick in the button XML file(and the public method with the view atribute in the Activity), but I got the same result. Can anyone help me?
this is the XML:
<Button
android:id="#+id/botaoTeppo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/versaoDoJogo"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="40dp"
android:background="#drawable/botao_menu_principal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/botaoTreino"
android:textSize="40sp"
android:textColor="#FFFFFF" />
and here's the onCreate() for adding the buttonListener:
#Override
public void onCreate(Bundle savedInstanceState) {
super.getGameHelper().setMaxAutoSignInAttempts(0);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchToScreen(R.id.tela_inicial_sumo_sensei);
String fontpathChines = "fonts/Wonton.ttf";
Typeface tfChines = Typeface.createFromAsset(getAssets(), fontpathChines);
Button botaoTeppo = (Button)findViewById(R.id.botaoTeppo);
Button botaoJogarOnline = (Button) findViewById(R.id.botaoJogarOnline);
Button botaoModoCompeticao = (Button) findViewById(R.id.botaoModoCompeticao);
botaoTeppo.setTypeface(tfChines);
botaoJogarOnline.setTypeface(tfChines);
botaoModoCompeticao.setTypeface(tfChines);
botaoTeppo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "botão ir pra teppo acionado", Toast.LENGTH_SHORT).show();
ArmazenaMostrarRegrasTreinamento mostrarExplicacaoDoTeppoAntes = ArmazenaMostrarRegrasTreinamento.getInstance();
boolean mostrarExplicacaoDoTeppo = mostrarExplicacaoDoTeppoAntes.getMostrarRegrasDoTreinamento(getApplicationContext());
Intent iniciaTelaTreinoIndividual;
if(mostrarExplicacaoDoTeppo == true)
{
iniciaTelaTreinoIndividual = new Intent(MainActivity.this, ExplicacaoTeppo.class);
}
else
{
iniciaTelaTreinoIndividual = new Intent(MainActivity.this, EscolhaNivelActivity.class);
}
startActivity(iniciaTelaTreinoIndividual);
}
});
this.popupCarregandoSeUsuarioEstahNaVersaoAtual =
ProgressDialog.show(MainActivity.this, getResources().getString(R.string.checando_versao_atual),
getResources().getString(R.string.por_favor_aguarde));
ChecaVersaoAtualDoSistemaTask taskChecaVersaoAtualDoSistema = new ChecaVersaoAtualDoSistemaTask(this, this.popupCarregandoSeUsuarioEstahNaVersaoAtual);
taskChecaVersaoAtualDoSistema.execute("");
}

Add this line to button in xml and try:
android:focusable="false"

I had the same issue with an application which I developed a custom camera.
If you are using a fullscreen layout in this activity, try to put this code in your AndroidManifest.xml file under the activity you need:
<activity
android:name=".YOUR_ACTIVITY"
android:configChanges="orientation"
android:label="#string/title_activity_YOUR_ACTIVITY"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
The line that does the magic is: android:theme="#android:style/Theme.NoTitleBar.Fullscreen"

Related

Button doesn't redirect to activity but crashes app instead

My button doesnt open up a new activity, the app crashes instead. I've even copied the source code from http://www.androidbegin.com/tutorial/android-button-click-new-activity-example/ and try running it on my own but still the app crashes. I cant seem to find the problem.
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Locate the button in activity_main.xml
button = (Button) findViewById(R.id.MyButton);
// Capture button clicks
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
secondActivity.class);
startActivity(myIntent);
}
});
}
}
XML BUTTON
<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" >
<Button
android:id="#+id/MyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button" />
YOUR CODE IS FINE!
The most likely cause of this behavior is that secondActivity is not registered in your manifest. Check whether it is and try again. If it's not, you can simply register it by adding the line below inside the application tag of your manifest.xml file.
<activity android:name=".secondActivity" />
Re-run your code and try again. It'd most likely work this time.
I hope this helps.. Merry coding!
As per the tutorial link you provided, second activity's name is NewActivity.class. In your code, it looks like you modified it to secondActivity.class.
So ensure to have it manifest also
<activity android:name=".secondActivity" >
</activity>
And always use PascalCase for Classes and camelCase for methods
Place this code snippet:
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Locate the button in activity_main.xml
button = (Button) findViewById(R.id.MyButton);
// Capture button clicks
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
secondActivity.class);
startActivity(myIntent);
}
});
}
}
Register secondActivity in manifest.xml
<activity
android:name=".secondActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" />
This may help you

setOnClickListener causes a crash of an APP

Hello I have this very simple code I'm trying to run via Android Studio
public class MainActivity extends ActionBarActivity {
Button random;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
random = (Button) findViewById(R.id.button);
display = (TextView) findViewById(R.id.TextView);
random.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
display.setText("I have changed");
}
});
I haven't really added very much, but whenever I use the setOnClickListener no matter what's inside of it crashes the app. I couldn't find a solution for this.
Thank you.
//edit: sorry. I added a wrong code, random is a button
It doesn't seem to be your case but I had the same problem and it was caused by having these lines
mButton = findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// ...
}
});
before
setContentView(R.layout.activity_main);
You are setting the setOnClickListener method on the random object which by your code is currently Null.
So you are getting a NullPointerException.
I think you intended it to be button instead.
What is "random"? I think correctly will be:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
display.setText("I have changed");
}
});
In your code you are setting click listener on a button. Please make sure you get right id and right object to set listener. The button you want to set clcik listener on should have same id to the id you defined in you activity
In Activity
Button button =(Button)findViewById(R.id.button1);
In XML
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
Firstly,check logcat by error level
Secondly,random is not initialized!!
You need
1.Create button in activity_main.xml
For example,
<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"
tools:context=".MainActivity" >
<Button
android:id="#+id/myCoolButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
2.Init that button in code
Button myCoolButton = (Button) findViewById(R.id.myCoolButton);
3.Attach listener to button
myCoolButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
display.setText("I have changed");
}});
And it works! Hope that help you

Keyboard doesn't appears and the buttons on the parent view are clickable after popup appears

After a specific button is clicked, a popup window appears on the screen. There is a edittext object on the popup, but I couldn't write int it, because keyboard doesn't appears. Secondly, the other buttons on parent view of popup are still clickable. How can I fix these? My code:
edit_text_popup.xml
<?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"
android:padding="10dip"
android:background="#color/conn_text" >
<EditText
android:id="#+id/editTextInPopup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hello_world"
android:padding="5dip"
android:background="#color/blue3">
</EditText>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="10dip">
<Button
android:id="#+id/popupcommitbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok"/>
<Button
android:id="#+id/popupcancelbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
where popup is called:
public class GroupConnections extends Activity implements OnClickListener {
Category thiscat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_connections);
View addMoreConnButton =findViewById(R.id.attachMorePeopleButton);
View editPropButton = findViewById(R.id.editCategoryPropertiesButton);
addMoreConnButton.setOnClickListener(this);
editPropButton.setOnClickListener(this);
//get selected category
int catId = getIntent().getExtras().getInt("categoryid");
thiscat = (Category)SocialRssModel.Instance().holders.get(catId);
//set text views
TableRow tabler = (TableRow)findViewById(R.id.tableRow1);
TextView cateNameTextView = (TextView)tabler.findViewById(R.id.numOfContentsTextView2);
cateNameTextView.setText(thiscat.getName());
tabler = (TableRow)findViewById(R.id.tableRow2);
CheckBox enable = (CheckBox)tabler.findViewById(R.id.notificationCheckBox);
enable.setChecked(thiscat.isSelected());
}
/**
* Button listeners are specified
*/
public void onClick(View v) {
switch (v.getId()) {
case R.id.attachMorePeopleButton:
Intent i = new Intent(this, AddMoreConnections.class);
startActivity(i);
break;
case R.id.editCategoryPropertiesButton:
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.edit_text_popup, null);
final PopupWindow editpopup = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//specify cancel button
View cancelbutton = popupView.findViewById(R.id.popupcancelbutton);
cancelbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
editpopup.dismiss();
}
});
//specify edittext
editpopup.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
editpopup.showAsDropDown(findViewById(R.id.editCategoryPropertiesButton), 0, -30);
break;
// More buttons go here (if any) ...
}
}
}
Activity tag in AndroidManifest to show softInputMode is not hidden:
<activity
android:name=".GroupConnections"
android:label="#string/app_name" >
</activity>
I found the solution on the following link : EditText On A Popup Window
Adding following command editpopup.setFocusable(true); solves both problems.
I have similar problem. Try to add this line in your activity tag in manifest.xml :
android:windowSoftInputMode="stateVisible"
Change your Popup Dialog to an activity, use this code in your manifest.xml, it's show your activity as Dialog, You can start it as a usual activity but it's showing such a dialog, this will be easier for show a custom dialog:
<activity
android:name=".view.MyDialog"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateVisible"
android:theme="#android:style/Theme.Dialog" >
</activity>

Android ImageButton not firing the onclick event, what is wrong with my code?

I have very simple code that includes ImageButton with OnClickListener that points to another Activity, the click on the ImageButton doesn't fire the onClick (The same problem was with Button) :
public class ToolsActivity extends Activity {
private ImageButton btnCamera;
final Context context = ToolsActivity.this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tools);
this.btnCamera = (ImageButton) findViewById(R.id.cameraButton);
this.btnCamera.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_tools, menu);
return true;
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<ImageButton
android:id="#+id/cameraButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/btncamera"
android:contentDescription="#string/desc" />
I can't see anything wrong with the code. If it works with a regular button my guess is that you maybe have to set android:clickable="true" in the xml (you can also do it in code).
I opened new Android project and copy paste may code, and it works, I guess something with the project was damaged.

Layouts in Android [ id or field cannot be resolved]

public class Page1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final Button button = (Button) findViewById(R.id.welcome);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = null;
myIntent = new Intent(view.getContext(), Page1.class);
startActivity(myIntent);
}
});
}
}
I want to load contents from another XML file named welcome.xml, but i do get an error welcome cannot be resolved or is not a field
This Page1.java class is next screen of my Android Application.
My Welcome.xml
<Button android:text="#+id/Button01" android:id="#+id/welcome"
android:layout_width="wrap_content" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
</Button>
It should be working.
If you don't set the handler, do you see the button in the screen?
Is the file actually named «*W*elcome.xml»? Try to remove the capital letter (rename it to welcome.xml). Then do a clean, rebuild and check if it works now...
Could you paste your complete xml file and log? my first guess is you have a case issue , your layout file is named "Welcome" and you have setContentView to "welcome" . Also dont have same names for layouts and controls , it will get confusing.
firend you are making silly mistake:see this
public class Page1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final Button button = (Button) findViewById(**R.id.Button01**);//use id of button here not layout name
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = null;
myIntent = new Intent(view.getContext(), Page1.class);
startActivity(myIntent);
}
});
}
}
Is this all what your welcome.xml has?
Your button isn't under a layout. thus, the layout file itself will be throwing out exceptions.
secondly, android:text is not correct. the entry you have made there, should be under android:id
and it shouldn't be:
final Button button = (Button) findViewById(R.id.welcome);
but:
final Button button = (Button) findViewById(R.id.Button01);
The Welcome.xml contains Button with id welcome which is not an layout to setContentView
Views can be List, relative, absolute table etc .. in which you can add a button.
And also check for the Case in filename and specified R.layout.*
Sample xml file with linearlayout and a button. Save it as welcome.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/linearlayoutmain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="#+id/ButtonWelcome"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button"
>
</Button>
</LinearLayout>
in Your Code
public class Page1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final Button button = (Button) findViewById(R.id.ButtonWelcome);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = null;
**//You have called Page1.class again which is the name of this class //again**
myIntent = new Intent(view.getContext(), Page1.class);
startActivity(myIntent);
}
});
}
}
Create another activity similarly and the call that class in the intent marked bold.
Your Welcome.xml is not complete, should be something like this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="3dip"
android:orientation="vertical">
<Button android:text="#+id/Button01" android:id="#+id/welcome"
android:layout_width="wrap_content" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
</Button>
</LinearLayout>
Also, if your are still having problemas, try to clean your project so the R.java get updated with new id values like welcome id (R.id.welcome),because if R.java does not contain welcome id, you will get errors like that.

Categories

Resources