Button not working (android studio) [duplicate] - java

This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 5 years ago.
My app runs well and shows no errors, but my button is not working ( is unClickable in device)
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button11=(Button)findViewById(R.id.button11);
button11.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
new Intent(MainActivity.this, Main2Activity.class);
}
});
}
}
and XML file :
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50px"
android:layout_marginTop="200dp"
android:background="#color/colorPrimary"
android:text="صفحه اصلی"
android:onClick="onClick"/>/>
On the real device when I click on this button nothing happens !

try this way
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
EDIT: I wanted to add a bit of an explanation to help
Hi MRAK,
First things first, you do NOT need to create an onClick listener. Remove all of that completely. Android studio is a beast and automatically does that for you using the XML file. When you set "onClick" in the XML file, it automatically calls the name of whatever method you put in there. You should change it so it is not also called "onClick." I would prefer to call it "startAcitivty2" or so on so you are not confused later. I stuck with your method name for now.
See below for corrected code:
public void onClick(View v){
// note in the below line i'm just using "this"
Intent myIntent = new Intent(this, Main2Activity.class)
// Secondly, you need to end the current activity
finish();
// Third, you need to start your new activity...
// Creating an Intent does not the activity alone
startActivity(myIntent);
}
Also, this has so many downvotes because this has been asked 1000+ times. Please use google or the search bar above before asking. Google will reroute you to stackoverflow anyway :)

Related

How to use the same UI with multiple java files? ANDROID JAVA

I'm a newbie and I'm working on a Unit Converter.
I would like to open the same UI regardless of which button I click(Weight or Length)
main_activity.xml UI
Below is the UI I want to open: activity_conversion.xml UI
And I would like each button in the main_activity to run on a different java class.
So, (minus the main_activity.java and it's .xml file)
I have 1 xml file(activity_conversion.xml) and 2 java files one for each button of the main_activity.xml
activity_main.xml Weight button
android:onClick="weightPage"
activity_main.xml Length button
android:onClick="lengthPage"
MainActivity.java
public void weightPage(View view){
Intent intent = new Intent(this, WeightActivity.class);
startActivity(intent);
}
public void lengthPage(View view) {
Intent intent2 = new Intent(this, LengthActivity.class);
startActivity(intent2);
}
Length_Activity.java code for Length button
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversion);
}
setContentView() method doesn't work for me:(
Thanks in advance!
I would like to open the same UI regardless of which button I click(Weight or Length)
You can do that by creating an activity and its layout XML file. And then start that activity via explicit intent like this:
//Place this code inside the onClick method
Intent intent = new Intent(SoucreActivity.this, DestinationActivity.class);
startActivity(intent);
And I would like each button in the main_activity to run on a different java class.
No, you cannot. All UI elements on a screen are always in the same activity; they cannot run on different java classes. (Unless you are using fragments of which you need not worry about as you are a newbie)
Apparently, you want the two buttons in your main activity to open the same activity. Which you can achieve using intents using the code snippet mentioned above.

Transfer data from one screen to another [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
How to pass an object from one activity to another on Android
(35 answers)
Closed 4 years ago.
I am currently working on a project for my programming class, it is a d&d character creator I've decided to do for my final project. I am nearly finished with it but have run into one problem. I want to take the user's data from a screen where they create a character and store it into a screen where they can view their created characters, but I am not sure how to do this. This is the code from the create a character screen:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_char_screen);
Button btnSave = (Button)findViewById(R.id.btnSaveChar);
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (checkData())
startActivity(new Intent(NewCharScreen.this, HomeScreen.class));
}
});
Button btnEx = (Button)findViewById(R.id.btnExplanation);
btnEx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(NewCharScreen.this, ExplanationScreen.class));
}
});
}
private boolean checkData(){
final EditText charName = (EditText) findViewById(R.id.txtNameInput);
if (charName.getText().toString().isEmpty()){
charName.setError("Enter character name");
charName.requestFocus();
return false;
}
return true;
}
}
If there is a need for the rest of my code or the project itself here is a link to a repository on GitHub:
https://github.com/cbetlinski98/MAD105-final_project
If you want to pass data from one activity to another one, you should pass it inside the Intent object, so intead of creating it inside StartActivity you can create it outside as a normal object, and use putExtra() to put data inside it.
Intent intent = new Intent(getBaseContext(), DestinationActivity.class);
intent.putExtra("OBJECT_PARAM_NAME", your_object);
startActivity(intent);
To recover data on the other activity you can use
String sessionId= getIntent().getStringExtra("OBJECT_PARAM_NAME");
Pass basic types
If you need to pass basic objects like int, float, strings, you can always pass them with putExtra() but to take them in the destination activity there are relative methods like getStringExtra() or getIntExtra() and many others.
Pass your class
If your user is a class defined by you, you can implement Serializable in that class, put it inside the intent with putExtra() and recover it from the other activity with getIntent().getSerializableExtra("OBJECT_PARAM_NAME") and then just cast it to your class before putting it inside an object
In order to pass information across intents:
Intent i = new Intent(this, Character.class);
myIntent.putExtra("paramKey","paramValue1");
startActivity(i);
Then in your activity to obtain the parameters:
Intent i2 = getIntent();
i2.getStringExtra("paramKey");
startActivity(new Intent(CurrentActivity.this, AnotherActivity.class).putExtra(KEY, DATA));

Unable to instantiate activity ComponentInfo, Runtime and NullPointer exceptions

I get that error whenever I attempt to run this app. It's simple right now, I'm just trying to make sure the first button does what I want it to. It also mentions NullPointerException.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
calendarButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ComponentName cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");
Intent i = new Intent();
i.setComponent(cn);
startActivity(i);
}
});
}
This (and declaring the buttons) are all I have done in the activity's Java file. I have set placeholders for listeners on other buttons but commented them out until I figure this one.
I have tried starting a new project running the same code (even pieced the layout back together) to make sure the Manifest wasn't accidentally messed with. Same issue.
I derped. Hard. I assigned the button variables a value during instantiation (which I have above onCreate) rather than INSIDE onCreate. I fixed it myself. Sorry about that.

How do I bind a button to another xml file?

I'm creating my first mobile app and I'm trying to bind a button to go straight to another xml file. It seems like such an easy answer but I can't find the solution anywhere. I'm using Eclipse as my IDE and using the Android ADT bundle, if that's at all relevant.
Put this in onCreate:
findViewById(R.id.my_button).setOnClickListener(new View.OnClickListener {
#Override
public void onClick() {
startActivity(new Intent(MainActivity.this, OtherActivity.class))
}
});
Replace my_button with the id of your button and MainActivity.this with the name of your main activity class.this and OtherActivity with the name of your other activity.class.
Are you asking how to create a layout with button that brings different layout? If so, you can drag button on your layout and put method in the "On Click" property of the button, something like myClick and then in your code add method
public void myClick(View v) {
startActivity(new Intent(this, MyNewActivity.class));
}
Make sure to declare new activity in AndroidManifest
Hope it helps
In your xml file, add this line of code to your button:
<Button>
//other button properties here
android:onClick='onNextPage'
</Button>
and in the activity/java file for that page, do this:
private void onNextPage(View view){
Intent intent = new Intent(this, nextActivity.java);
startActivity(intent);
}
when you click the button, you'll go to nextActivity.java or whatever you named your second page to be.

Change class view using buttons

I'm trying to have a program go from one class to another by the press of a button. I know this needs to be done with intent, but I'm not sure exactly what action to use for intent. Also, i would rather not try to use OnClickListener since it seems to me to be a bit excessive for what I need to do. Though I could be wrong and I'm just over thinking it. Here's what I have for the code:
public void loginScreen(View view) {
// Should take user to the next view when button pressed
Intent intent = new Intent(this, LoginActivity.class);
Button login = (Button) findViewById(R.id.button1);
intent.getData(login);
startActivity(intent);
}
I want it to go to the next screen when only button1 is pressed so i don't know if just lines 3 and 6 are the only ones needed, but it won't work if it's just those two. I believe the problem is getData, i don't know what to put there, but i know getData's wrong. I'm probably also missing something else, but I don't know what.
Also, forgive me if this isn't easy to follow, first time trying to ask a question here.
In your XML file you can declare your on click:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="loginScreen" />
Then in your Activity:
public void loginScreen(View button) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
Here is the onClick View API
Name of the method in this View's context to invoke when the view is
clicked. This name must correspond to a public method that takes
exactly one parameter of type View. For instance, if you specify
android:onClick="sayHello", you must declare a public void
sayHello(View v) method of your context (typically, your Activity).
Note that if you add an onClick to a Fragment layout it will still need to be caught in the Activity.
Without explicitly setting an onClickListener:
<button
...
android:onClick="loginClicked" />
public void loginClicked(View v) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
Also setting an onClickListener explicitly is not overkill.
Your question isn't entirely clear, so let me know if you need more and I'll edit my answer.

Categories

Resources