Button not working in layout - java

Am new to Android so just practising on small stuff, what am trying to do is a button(B1) in the (mainactivity) layout and when I click it , it changes to another layout(main) which has another button (B2)which changes back to mainactivity layout out on-click , B1 is working okey , but B2 not working any ideas?
package com.example.android;
import java.util.Random;
import java.awt.*;
import com.example.android.R;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.clickme);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.main);
/*Random rand = new Random();
btn.setBackgroundColor(Color.argb(255, rand.nextInt(255),
rand.nextInt(255), rand.nextInt(255)));*/
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
this is (main) layout
package com.example.android;
import com.example.android.R;
import java.util.Random;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
Button btn;
TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.ChangeColor);
text = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.activity_main);
/*Random rand = new Random();
btn.setBackgroundColor(Color.argb(255, rand.nextInt(255),
rand.nextInt(255), rand.nextInt(255)));
text.setTextColor(Color.argb(255, rand.nextInt(255),
rand.nextInt(255), rand.nextInt(255)));*/
}
});
}
}

setContentView(R.layout.activity_main); is not doing what you think.
To naviguate to activities you have to use intents like the others sugested in the comments
First decalre your context:
final Context context = this;
then pass it to your intent:
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
}
});

Related

I would like to create a sound and move the next activity after the ImageButton is clicked

I have the code for the sound and the next activity. I do not know how to merge the two codes in order to execute them at the same time or in sequence.
package com.example.testmessages;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Add sound
final MediaPlayer gunSoundMP = MediaPlayer.create(this, R.raw.gunsound);
ImageButton GunSound=(ImageButton)this.findViewById(R.id.imageButton3);
GunSound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
gunSoundMP.start();
}
});
//Image button to execute new Activity
imageButton=(ImageButton) findViewById(R.id.imageButton3);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentLoadMain2Activity = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intentLoadMain2Activity);
gunSoundMP.start();
}
});
}
}

trouble compiling app to add activity

as posts are limited in size on this platform I will post in parts first my mainactivity.java file package com.cancunsteve.aboutcancunsteve;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.MainActivity;
import android.NewActivity2;
public class MainActivity extends AppCompatActivity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.MyButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(MainActivity.this,
NewActivity2.class);
startActivity(myIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
You have to create a NewActivity2 class/activity
Your import is wrong for the NewActivity2, I doubt it is part of the android package.

Perserve value in text field in screen 1 while pressing back from the screen 2 using variables and intents

I have 2 screen on first i have Text field and when i pressed the button it takes me to second screen with data from the Text Field but while pressing the back button from screen 2 to screen 1.
I want that the text field(screen 1) will show the previously added data through variables.
////////////////////////////////////Screen 1 code://///////////////////////////
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button button;
EditText name;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.buttonNext1);
name=(EditText)findViewById(R.id.editTextName);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String namevalue= name.getText().toString();
savedInstanceState.putString("MyString", "Welcome back to Android");
Intent intent=new Intent(MainActivity.this,Main3Activity.class);
intent.putExtra("Name",namevalue);
startActivity(intent);
}
});
}
}
////////////////**Screen 2 code:**////////////////////////////////////////////
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class Main3Activity extends AppCompatActivity {
TextView t1;
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
b1=(Button) findViewById(R.id.button);
t1=(TextView)findViewById(R.id.textView2);
t1=(TextView)findViewById(R.id.textView2);
getIntent().getStringExtra("Name");
t1.setText("Name :" +getIntent().getStringExtra("Name"));
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent= new Intent(Main3Activity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
Screen 2
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class Main3Activity extends AppCompatActivity {
TextView t1;
Button b1;
String valueOfName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
b1=(Button) findViewById(R.id.button);
t1=(TextView)findViewById(R.id.textView2);
t1=(TextView)findViewById(R.id.textView2);
getIntent().getStringExtra("Name");
valueOfName = getIntent().getStringExtra("Name");
t1.setText("Name :" +getIntent().getStringExtra("Name"));
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
screen2Done();
}
});
}
public void screen2Done() {
Intent intent=new Intent();
intent.putExtra("RESULT_STRING", valueOfName);
setResult(RESULT_OK, intent);
finish();
}
#Override
public void onBackPressed() {
screen2Done();
}
On screen 1, capture the value in onActivityResult() method, similarly as in screen 2.

Android Java implement 2 buttons in 1 page

I finally learned about adding a button to a page and actually making it navigate to another activity "XML Page". Anyway, I have been trying to add 2 buttons in the same page which navigate each to a different XML's Pages. All I did was copy the first button which worked and then change the button name and all other things the first button works but the second isn't. It shows a click but nothing happens after.
Back1 Button works. TMode Button does the trouble.
Eclipse is not showing errors.
Here is my code -
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GameMode extends Activity {
/** Called when the activity is first created.*/
Button btn;
Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn=(Button)findViewById(R.id.Back1);
btn.setOnClickListener(btn2Listener);
}
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent2=new Intent(GameMode.this,MainActivity.class);
startActivity(intent2);
}
};
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn=(Button)findViewById(R.id.TMode);
btn.setOnClickListener(btn3Listener);
}
private OnClickListener btn3Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent3=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent3);
}
};
}
Try something like this:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GameMode extends Activity {
Button btn1;
Button btn2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn1=(Button)findViewById(R.id.Back1);
btn1.setOnClickListener(btn1Listener);
btn2=(Button)findViewById(R.id.TMode);
btn2.setOnClickListener(btn2Listener);
}
private OnClickListener btn1Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,MainActivity.class);
startActivity(intent2);
}
};
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent2);
}
};
}
You should in your XML file define two buttons
<Button
android:id="#+id/button1"
... />
<Button
android:id="#+id/button2"
... />
And then in your Activity in onCreate() method you do
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
...
})
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
...
});
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GameMode extends Activity {
Button btn1;
Button btn2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn1=(Button)findViewById(R.id.Back1);
btn1.setOnClickListener(btn1Listener);
btn2=(Button)findViewById(R.id.TMode);
btn2.setOnClickListener(btn2Listener);
}
private OnClickListener btn1Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,MainActivity.class);
startActivity(intent1);
}
};
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent2=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent2);
}
};
}

Program will not open new windows

I'm trying to make a game for adroid and am having some problems with my windows.
I can open the "newgame" window from the "mainactivity", I'm trying to work on the "back" button, but I can't get it to work.
I also can't open new windows from the second "newgame" window (was testing if it would start to "load")
This may be a stupid mistake but I have no idea why its not working. Most app tutorials deal with one window and thus don't help me
My code:
mainactivity.java:
package dream.o.eternaty;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button NewGame = (Button) findViewById(R.id.button1);
NewGame.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.newgame);
}
});
final Button Load = (Button) findViewById(R.id.button2);
Load.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.loadgame);
}});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
newgame.java:
package dream.o.eternaty;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class NewGame extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newgame);
final Button Back = (Button) findViewById(R.id.newgameback);
Back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.loadgame);
}
});
};
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}});
}
You shouldn't be transitioning the views, but actually starting a new activity for NewGame, as that's what you've declared it as, you can do it like this
Intent newIntent = new Intent(MainActivity.this,NewGame.class);
startActivityForResult(newIntent, 0);
in your MainActivity's onClick listener

Categories

Resources