I have 4 xml named a1,a2 and a3 then I also have a main xml. In a1 there's an editText and an ok button, in a2 there's an ok button which is "disabled". Now, what I want to happen is when the data entered in a1 is correct, it will proceed to a2 then the "disabled" button there should now be "enabled". Now what happened on my program is, it's already running except for the button in a2 is getting enabled but it will return immediately into being disabled. How can I prevent it from getting disabled after it's been enabled? I'm new in android so please explain it as simple as it can be. Thanks in advance.
here's my code
Main Activity.java
package com.example.myact1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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);
Button btnClose = (Button) findViewById(R.id.btnExit);
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
finish();
System.exit(0);
}
});
Button page1 = (Button) findViewById(R.id.btn1);
page1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), a1.class);
startActivityForResult(myIntent, 0);
}
});
}
}
MainActivity.xml
<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/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="45dp"
android:text="Exit" />
<Button
android:id="#+id/btn1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btnExit"
android:layout_alignLeft="#+id/btnExit"
android:layout_marginBottom="30dp"
android:text="enter" />
a1 xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btna1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="155dp"
android:text="Enter" />
<EditText
android:id="#+id/eta1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btna1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="52dp"
android:ems="10" >
<requestFocus />
</EditText>
a1.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a1);
Button page1 = (Button) findViewById(R.id.btna1);
page1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText a1et = (EditText) findViewById(R.id.eta1);
String a1 = a1et.getText().toString();
if (a1.equalsIgnoreCase("abcde")) {
Toast.makeText(getApplicationContext(), "correct", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(view.getContext(), a2.class);
startActivityForResult(myIntent, 0);
//enables the button in a2
setContentView(R.layout.a2);
Button stage2 = (Button) findViewById(R.id.btna2);
stage2.setClickable(true);
stage2.setEnabled(true);
} else {
Toast.makeText(getApplicationContext(), "wrong", Toast.LENGTH_SHORT).show();
}
}
});
}
here's the code of my a2.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a2);
}
}
a2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btna2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp"
android:clickable="false"
android:enabled="false"
android:text="enter" />
Also add
stage2.setEnabled(true);
Right below:
Button stage2 = (Button) findViewById(R.id.btna2);
stage2.setClickable(true);
EDIT After seeing all of your code, here are two corrections I did to your code so that it will work:
Remove this part of the code in a1.java:
//enables the button in a2
setContentView(R.layout.a2);
Button stage2 = (Button) findViewById(R.id.btna2);
stage2.setClickable(true);
stage2.setEnabled(true);
Remember that we decided to use new activity and not to change the contentView of the current one? Because you had this code the button appeared enabled for a short while before it became disabled once more.
Change the code of a2.java#onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a2);
Button stage2 = (Button) findViewById(R.id.btna2);
stage2.setEnabled(true);
}
Here you were not enabling the button and that was your problem. Rendering a2 Activity you overwrite the view you already rendered in a1 Activity with a new one. One that has the button disabled once more. I correct that with this code.
Related
Hello this is my xml file
<RelativeLayout
android:id="#+id/tutorialBox"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="15dip">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
i have made an on click listener for it
final Button closeBt = (Button) findViewById(R.id.closeBen);
closeBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
closeBt.setText("Im a button");
}
});
for some reason when i click this button nothing happens it doesnt look like it has been clicked.
when i took the button out of the realtive layout everything worked fine
any suggestions?
Instead of this add onClick attribute to the button tag in xml.
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:onClick = "close_clicked"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
Then in Main Activity just make a new method like this.
public void close_clicked (View v){
// Your code
}
No need to add on click listner.
Your RelativeLayout does not look good, is it your main container? Maybe try it this way
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
And a good practice is to declare your widgets globally then instantiate them in theOnCreate
public class Foo extends AppCompatActivity {
private Button closeBenny;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
closeBenny = (Button)findViewById(R.id.closeBen);
closeBenny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
closeBenny.setText("Im a button");
}
});
}
}
I placed a Button in a layout
here is my xml 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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="#ff0000"/>
and wrote the following code in activity:
public class Basic extends Activity {
Button btn;
public void oncreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.first);
btn = (Button) findViewById(R.id.button1);
}
public void clkBtn(View v) {
Toast.makeText(this, "hai.........", Toast.LENGTH_SHORT).show();
}
}
When I run this code, I am getting white blank screen (without any button). Can any one tell me what is wrong with my code?
Update your first.xml under res->layout folder 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:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clkBtn"
android:text="Click Me" />
</LinearLayout>
To get the Button event you need to first register your Button for that. The code which you have written is right but to invoke that method you have to add the property android:onClick="clkBtn" in your layout file.
OR
If you don't want to use this way then you can also explicitly invoke the event by registering your Button in your class as below:
public class Basic extends Activity {
Button btn;
public void oncreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.first);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(this, "hai.........", Toast.LENGTH_SHORT).show();
}
}
And also to launch your activity make sure have added your activity as launcher in your manifest file as below .
<activity android:label="#string/app_name"
android:name="Basic" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This is My Answer. In XML file Button should be like this:
<Button
android:id="#+id/ID of you button "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name To Display Button name" />
In the MainActivity OR In any activity (in which you have called XML):
public class MainActivity extends Activity {
public void oncreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout."Your XML file in which Button is.");
Button btn = (Button)findViewById(R.id."Your button Id");
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Do your onclick program here
}
});
}
}
I have a test code using the View Property Animation to switch locations of two buttons. The code is as below:
public class TestAnimationActivity extends Activity {
private View mainView;
private View TempView;
private Button B1, B2, B3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate your view
setContentView(R.layout.main_test);
mainView = findViewById(R.id.mainTest);
B1 = (Button) mainView.findViewById(R.id.B1);
B2 = (Button) mainView.findViewById(R.id.B2);
B3 = (Button) mainView.findViewById(R.id.B3);
OnClickListener OCL = new OnClickListener(){
#Override
public void onClick(View v) {
if(TempView == null){
TempView = v;
}else{
int originalPos1[] = new int[2];
TempView.getLocationOnScreen( originalPos1 );
int originalPos2[] = new int[2];
v.getLocationOnScreen( originalPos2 );
v.animate().translationX((float)originalPos1[0] - (float)originalPos2[0]);
v.animate().translationY((float)originalPos1[1] - (float)originalPos2[1]);
TempView.animate().translationX((float)originalPos2[0] - (float)originalPos1[0]);
TempView.animate().translationY((float)originalPos2[1] - (float)originalPos1[1]);
TempView = null;
}
}};
B1.setOnClickListener(OCL);
B2.setOnClickListener(OCL);
B3.setOnClickListener(OCL);
}
}
The XML file is as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainTest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/B1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#B4B4B4"
android:gravity="center" />
<Button
android:id="#+id/B2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#470047"
android:gravity="center" />
<Button
android:id="#+id/B3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center" />
</LinearLayout>
When I was testing this code on my phone. The animation did not work properly.
Two buttons will switch position the first time, but the button will go to wrong position if I continue clicking them. It seems that the button still calculate the position it should move to based on its original location when the app launched.
Anyone knows what is going wrong?
Hi am new to android and have spent over 10 hours looking for this answer but i cant seem to find and understand it. I am at the main xml page. I am trying to create a button that goes to another page. I need the easiest and simplest way to do this. Can anyody please help me? Heres my code for my main xml.
<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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="#dimen/padding_medium"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="Button" />
</RelativeLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="Button" />
Go to your activity Initialize your button
Button btn=(Button)findViewById(R.id.button1);
// Register listener to button btn
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your action
Intent newActivityIntent= new Intent(currentActivity.this, NewClass.class);
startActivity(newActivityIntent);
}
});
You can also do it different...
You can instantiate a Button in your Activity:
private Button button;
Inside the onCreate(Bundle bundle) method you find your button, as defined at this Activity XML, which is the one you used setContentView(R.layout.yourxml);:
button = (Button) findViewById(R.id.button);
Then you use an OnClickListener:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Inside the onClick method, you instantiate an Intent
Intent intent = new Intent(CurrentActivity.this, ActivityToGo.class);
CurrentActivity = the one you are, and ActivityToGo the one you want to load.
startActivity();
Read the basics on Android here.
public class MyActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
public void onClick(View v) {
// Use a switch on v.getId() if you have multiple clickable views.
// Since there's just one button, ...
Intent intent = new Intent(this, TargetActivity.class;
startActivity(intent);
}
}
You have to learn How to switch between Activities/Screens in Android, you can find here a well explained tutorial for beginners
This process is documented on the website concerning buttons. Google search Android Button
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="Button"
android:clickable="true"
android:onClick"insertMethodNameHere"
/>
This will call the method you define in the onClick tag, then start a new activity or update the view. Whatever you need to do
I am trying to increment and decrement the middle textview via buttons on the sides. The application starts up finely but by the time I click on any of the buttons it gets closed with following error.
Error: process <package> has stopped unexpectedly.
My main.xml:
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="250dp"
android:text="+"
android:textSize="40dp" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="80dp"
android:layout_toRightOf="#+id/button1"
android:layout_marginTop="75dp"
android:layout_marginLeft="80dp"
/>
<Button
android:id="#+id/button2"
android:layout_width="50dp"
android:layout_height="250dp"
android:layout_alignParentRight="true"
android:text="-"
android:textSize="40dp" />
My java file:
public class IncrementDecrementActivity extends Activity {
int counter;
Button add, sub;
TextView tv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add = (Button) findViewById(R.id.button1);
sub = (Button) findViewById(R.id.button2);
tv = (TextView) findViewById(R.id.tv1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
tv.setText(counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
tv.setText(counter);
}
});
}
}
Well. This should be.
In this statement
counter--;
tv.setText(counter);
It should be
counter--;
tv.setText(String.valueOf(counter));
I guess the error is
ResourceNotFoundException
How you encountered this error?
on the code above.
you declared int counter;
let's consider the value of counter is 0
and then
you called
counter--; // take note this is an integer
tv.setText(counter);
counter is now -1
by calling setText(counter);
will search first a string value from strings.xml with an integer -1 and set the text to textview
If android fails to find that string it will throw ResourceNotFoundException
:)