I'm trying to use a button to start a new activity like this:
public class MainActivity extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.button1)).setOnClickListener(this);
((Button) findViewById(R.id.button2)).setOnClickListener(this);
}
public void onClick(View view) {
Button clickedBtn = (Button) view;
switch (clickedBtn.getId()) {
case R.id.button1: startActivity(new Intent(this, NewActivity.class));
break;
case R.id.button2: startActivity(new Intent(this, AnotherActivity.class));
break;
}
}
}
But nothing seems to happen when I click on either button. I guess it's because the onClick method does not know which button is actually clicked...but I'm not sure how I can fix this...please advice, thanks!
Edit: added XML file
<?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="fill_parent"
android:orientation="vertical"
android:background="#000000">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="first button" />
<Button android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="second button" />
</LinearLayout>
</ScrollView>
</LinearLayout>
I put the code in the wrong file; everything is working now. Thanks!
Related
I have three XML files are
1) Header
2) FirstActivity
3) SecondActivity.
header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Button 1"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="first1"
>
</Button>
<Button
android:text="Button 2"
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="second"
>
</Button>
</LinearLayout>
firstactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<include layout="#layout/headerfile"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="100dip"
>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Detail"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/simid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/imeino"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/phoneno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</RelativeLayout>
HeaderActivity.java
public class HeaderActivity extends Activity {
Button b1,b2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.headerfile);
b1 = (Button)findViewById(R.id.button1) ;
b2 = (Button)findViewById(R.id.button2) ;
} //onCreate over
public void first1(View v)
{
Intent i =new Intent(this,firstactivity.class);
startActivity(i);
finish();
}
public void second(View v)
{
Intent i1 =new Intent(this, secondactivity.class);
startActivity(i1);
finish();
}
}
But I got ERROR every time …
Log-cat :
ERROR/AndroidRuntime(328): java.lang.IllegalStateException: Could not find a method first1
(View) in the activity class com.contactDetails.ContactDetailsActivity for onClick handler on view class
android.widget.Button with id 'button1'
In ContactDetailsActivity.java class you should add below method
public void first1(View v)
{
// onlick code
}
<include> tags include all the elements in your root layout. So you can always access them using findViewById on your Activity.
You could use another way to handle onClickListener of your Button..
You don't have to write separate Activity for handling include layout views....
you can directly access that buttons from activity where you have used view (i.e. firstactivity.xml).
Try This..
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i =new Intent(this,firstactivity.class);
startActivity(i);
finish();
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i1 =new Intent(this,secondactivity.class);
startActivity(i1);
finish();
}
});
and Remove onClick property from both Button
Getting Warning : unexpected text found in layout file : ""
Also on clicking the button, app is crashing.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/container">
<Button android:id="#+id/bntStartService"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/broadcast_intent"
android:onClick="broadcast_intent">
</Button>
</LinearLayout>
Please help.
// try this way hope this will help you...
1. activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="#+id/container">
<Button android:id="#+id/bntStartService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/broadcast_intent"
android:onClick="broadcast_intent">
</Button>
</LinearLayout>
2.MainActivity .java
public class MainActivity extends Activity{
private Button bntStartService;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bntStartService = (Button) findViewById(R.id.bntStartService);
bntStartService.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
broadcast_intent(view);
}
});
}
private void broadcast_intent(View view){
Toast.makeText(this,"Button Clicked",Toast.LENGTH_SHORT).show();
}
}
I have an app, which shows toast below the actionbar. I'm using Crouton library for displaying toast. Here I'm placing custom layout for toast, that layout contains one textview and one button (for close option). So Toast appears with button. If I click that it should close the toast but nothing happens.. Below Code for this example. Any Help Appreciated
MainActivity.java
public class MainActivity extends SherlockActivity implements OnClickListener {
private View customToastView;
private TextView customToastMessageView;
private Button customToastCloseButton;
private Button doneButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
doneButton = (Button) findViewById(R.id.done_button);
customToastView = getLayoutInflater().inflate(R.layout.toast_custom_layout, null);
customToastMessageView = (TextView) customToastView.findViewById(R.id.messages);
customToastCloseButton = (Button) customToastView.findViewById(R.id.close_button);
customToastCloseButton.setOnClickListener(this);
doneButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.done_button:
Crouton.cancelAllCroutons();
customToastMessageView.setText("Message");
Crouton.show(this, customToastView);
break;
case R.id.close_button:
Crouton.cancelAllCroutons();
break;
}
}
}
toast_custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/messages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="23dp"
android:text="Button" />
</RelativeLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/done_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="146dp"
android:text="Button" />
</RelativeLayout>
when i use linearlayout everything works like its supposed except my button is at the top and not the bottom of the screen.
when i use relativelayout my button is on the bottom like its supposed to but only works for the first listview entry after that it stops working...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button android:id="#+id/plus"
android:background="#drawable/selector"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
<TextView android:id="#android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="#string/helptxt1"
/>
</RelativeLayout>
..
public void addListenerOnButton() {
final Context context = this;
button1 = (Button) findViewById(R.id.plus);
button1.setOnClickListener(new OnClickListener() {
// #Override
public void onClick(View arg0) {
createNote();
// Intent intent = new Intent(context, QuoteEdit.class);
// startActivity(intent);
}
});
}
private void createNote() {
Intent i = new Intent(this, QuoteEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
Your buttons wont work because when you use a Relative layout all the views/buttons are placed in the same location unless you place them above or below each other. Try doing this with your layout:
<Button
android:id="#+id/plus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/plus" >
</ListView>
or you can add an onClick to your button:
<Button
android:id="#+id/plus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="myClickMethod" />
and in your code
public void myClickMethod(View view) {
createNote();
}
I have the following layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:text="Height"
android:layout_height="wrap_content"
android:id="#+id/buttonHeight"
android:layout_width="wrap_content"
android:layout_weight="15"
android:onClick="OnClickHeight">
</Button>
<Button
android:text="Width"
android:layout_height="wrap_content"
android:id="#+id/buttonWidth"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/buttonHeight"
android:layout_weight="1"
android:onClick="onClickWidth">
</Button>
</LinearLayout>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1px"
android:drawSelectorOnTop="false"/>
</RelativeLayout>
I then have a class that extends ListActivity in which I have my onClickWidth and onClickHeight methods, for example:
public void onClickWidth(View v)
{
// does stuff
}
However, these onClick events are not being handled. If I change the class to extend Activity, instead of ListActivity, and also remove the ListView from my layout, then it is detected!
I have tried to set android:clickable="true", played around with the android:focusable attribute, and various other things, but I just cannot get this to work. How can I resolve this, or is this simply not allowed?
Can you tell us what are you try to do? And why do you need ListActivity?
Sorry for edditing:P
You could take a look here: How to handle ListView click in Android
put OnClicklistener within adapter itself .
Rather then using the onClick stuff in the layout, have you tried something like this:
Button buttonWidth = (Button)findViewById(R.id.buttonWidth);
buttonWidth.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do stuff
}
});
Then do the same for buttonHeight.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_land);
Button btn = (Button) findViewbyid(R.id.buttonWidth);
btn.setOnClickListener(this);
}
public void onClickWidth(View v)
{
if(v == btn){
//your code...
}
}
set the android:descendantFocusability attribute of the parent layout which contains the ListView to value blocksDescendants like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"> ...</LinearLayout>