When i run the app. The buttons refuse to respond. I set a Toast and a logcat to check for response. but non. please help resolve
this is my source code
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/textView" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:id="#+id/button"
tools:onClick="topClick" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:id="#+id/button2"
tools:onClick="bottomClick" />
and for my java methods;
public class MyActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
//Testing the Toast and Log in action
Toast.makeText(this, "Can you see me?", Toast.LENGTH_SHORT).show();
Log.i("info", "Done creating the app");
}
//creating method topclick
public void topClick(View v) {
Toast.makeText(this, "Top button clicked", Toast.LENGTH_SHORT).show();
Log.i("info", "The user clicked the top button");
}
//creating method bottomclick
public void bottomClick(View v) {
Toast.makeText(this, "Bottom button clicked", Toast.LENGTH_SHORT).show();
Log.i("info", "the user clicked the bottom button");
}
}
Replace
tools:onClick
with
android:onClick
for both buttons.
For more context that will help you understand why this change is required, read up on the tools namespace:
Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
I would highly not recommended using that implementation of your as you can hardly know which method for what button. Try this instead.
XML
<Button
android:text="Button"
android:id="#+id/button1" />
<Button
android:text="Button"
android:id="#+id/button2" />
ACTIVITY.JAVA
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Button button01 = (Button)findViewById(R.id.button01);
Button button02 = (Button)findViewById(R.id.button02);
button01 .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, "This is button01", Toast.LENGTH_SHORT).show();
}
});
button02 .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, "This is button02", Toast.LENGTH_SHORT).show();
}
});
}
Related
Can someone tell me how to use a Text Field with an onclick listener on a button in android studio and give me the xaml code too.
First you need write in xml file what you need, we need button and textview:
<Button
android:id="#+id/button1"
android:layout_width="70dp"
android:layout_height="30dp"
android:text="textEdit"
android:layout_marginLeft="5dp"
android:textColor="#fff"
android:layout_marginRight="2dp"
android:background="#333"
android:onClick="ButtonOneClick"
android:layout_marginBottom="4dp"
/>
<TextView
android:id="#+id/txtView"
android:layout_width="50dp"
android:layout_height="30dp"
android:hint="text view"
/>
After xml you need define this in class. Because we define android:onClick="ButtonOneClick" in xml,
we can just call it , and set what you want to do.
TextView tv;
tv =(TextView)findViewById(R.id.txtView);
public void ButtonOneClick(View view) {
tv.setText("Test");
}
Other method is to declare button to, and set onClickListener.
Button butt;
butt= (Button) findViewById(R.id.button1);
butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tv.setText("Test")
}
});
in tampil.xml
<Button
android:id="#+id/button1"
android:text="PENCET"
android:onClick="onPencet" />
and in u're class, use public void nameonClick(view v){ statement }
public class Tampil {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onPencet(View v){
**U're statement }
}
I'm trying to implement Google+ authentication for my Android app, but the sign in button just doesn't work.
My onClick method in SignInActivity. I also tested the same with a normal button.
public void onClick(View view) {
if (view.getId() == R.id.sign_in_button){
Toast.makeText(this, "Log in pressed", Toast.LENGTH_SHORT).show();
}
if (view.getId() == R.id.button1) {
Toast.makeText(this, "Log in pressed", Toast.LENGTH_SHORT).show();
mGoogleApiClient.connect();
}
}
XML showing button properties.
<com.google.android.gms.common.SignInButton
android:id="#+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/sign_in_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="49dp"
android:onClick="onClick"
android:text="Button" />
When I press the normal button the toast appears just fine and authentication works, but when I press the g+ button nothing happens..
Seems you need give the onClickListener to your SignIn Button. Something like that
public class YourActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
//You probably miss this line:
this.findViewById(R.id.sign_in_button).setOnClickListener(this);
this.findViewById(R.id.button1).setOnClickListener(this);
}
public void onClick(View view) {
// Your stuff...
}
}
This should work:
Go to the Google Developer Console and enable Google+ API.
So I have a 2 buttons in a layout
like so:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="onClick">
<Button
android:id="#+id/btnEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edit Spot" />
<Button android:id="#+id/btnGo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Google Spot"
android:layout_weight="1"/>
</LinearLayout>
Then in the onCreate for the activity for this I have:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_spot);
// save button
ButtonEdit = (Button)findViewById(R.id.btnEdit);
ButtonGo = (Button) findViewById(R.id.btnGo);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
pid = i.getStringExtra(TAG_PID);
// Getting complete product details in background thread
new GetProductDetails().execute();
// save button click event
ButtonEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Edit button pressed", Toast.LENGTH_LONG).show();
}
});
// Go button click event
ButtonGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Go button pressed!", Toast.LENGTH_LONG).show();
}
});
}
But for some reason when I tap the buttons nothing happens. I've looked around at others asking the question and tried those but still nothing. Can someone please help me figure out why it isn't working?
Thank you,
Tyler
You have to add the onClick properties in the Button's definition, not in the LinearLayout.
Even if you want to reuse the same method called onClick you can set a tag for each button, and do a switch for each tag. For instance:
In your layout:
android:tag="1"
In your code:
public void onClick(View v) {
String tag = (String) v.getTag();
switch (Integer.parseInt(tag)) {
case 1: // First button
...
break;
case 2: // Second button
...
break;
}
}
Remove onClick from your LinearLayout. Right now you have nested onClickListeners. The LinearLayout is intercepting the event and likely not passing it down.
edit: somebody is blindly downvoting all these answers for some reason. Post a comment explaining why this is wrong.
Please make your Button(s) clickable:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="onClick">
<Button
android:id="#+id/btnEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edit Spot"
android:clickable:true />
<Button android:id="#+id/btnGo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Google Spot"
android:layout_weight="1"
android:clickable:true/>
</LinearLayout>
Also implement the onClick() method in your Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_spot);
// save button
ButtonEdit = (Button)findViewById(R.id.btnEdit);
ButtonGo = (Button) findViewById(R.id.btnGo);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
pid = i.getStringExtra(TAG_PID);
// Getting complete product details in background thread
new GetProductDetails().execute();
// save button click event
ButtonEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Edit button pressed", Toast.LENGTH_LONG).show();
}
});
// Go button click event
ButtonGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Go button pressed!", Toast.LENGTH_LONG).show();
}
});
}
public void onCLick(View v)
{
//Your Implementation
}
I hope this helps.
I need to disable the click-event for a button in Android. Just as a sample, I have tried doing the following. I have taken a TextView named it (entered a text) as Name. The condition checks if, TextView is empty button and clickable should be set to false. However this does not happen when the Toast is printed. Can somemone tell me the reason. Also if the text field is not empty I want to reset the clickable event for button as true.
Java File:
public class ButtonclickabkeActivity extends Activity {
TextView tv;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("Name");
btn = (Button) findViewById(R.id.button1);
if (tv.getText().toString().length() != 0) {
btn.setClickable(false);
Toast.makeText(getApplicationContext(), "" + tv.getText().toString().length(), Toast.LENGTH_LONG).show();
} else {
btn.setClickable(true);
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Button clicked", Toast.LENGTH_LONG).show();
}
});
}
}
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"/>
<TextView
android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Use
btn.setEnable(false);
instead of
btn.setClickable(false);
User 370305 is correct. .setEnable is what your looking for. Or you could use android:clickable in the layout XML file.
In my case
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
view.setEnabled(false);
}
});
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
:)