Reputation: 39
I am beginner in Android Studio. I want to use 2 button but it always ı a getting an error. I am working another Computer,so ı can not send you a code view. Can you help me,if you have a example code about this subject?
Upvotes: 2
Views: 8303
Reputation: 39
Thank you all your answers. I was trying to solve the problem that using two button to write text. But i solved like this;
final TextView text= (TextView)findViewById(R.id.text);
final TextView text2=(TextView)findViewById(R.id.text2);
Button buton2= (Button)findViewById(R.id.buton2);
Button buton=(Button)findViewById(R.id.buton);
buton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text.setText("Hi!");
}
});
buton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text2.setText("Whats's up?");
}
});
Upvotes: 0
Reputation: 1435
Try with this:
STEP ONE - Create a XML layout with the two buttons
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="net.napples.myapplication.MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BUTTON 1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BUTTON 2"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
STEP TWO - Create a java class for the activity
public class MainActivity extends AppCompatActivity {
/* UI Views */
Button mButton1;
Button mButton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get UI references
findViewsById();
// Set listener for the Views (like ImageView, TextView, Button, etc)
setListenerForViews();
}
private void findViewsById() {
try {
/* Get a reference for each button */
mButton1 = (Button) findViewById(R.id.button1);
mButton2 = (Button) findViewById(R.id.button2);
} catch (NullPointerException exc) {
exc.printStackTrace();
}
}
private void setListenerForViews() {
mButton1.setOnClickListener(myListener);
mButton2.setOnClickListener(myListener);
}
View.OnClickListener myListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
// See Step 3
}
};
}
STEP THREE - Create a listener to manage the click on each button
View.OnClickListener myListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
if (view == mButton1)
/* Your code, for example start a new Activity */
startActivity(this, SecondActivity.class);
if (view == mButton2)
/* Your code, for example set text for a TextView */
mTextView1.setText("New text");
}
};
Upvotes: 0
Reputation: 171
this is straight from android documentation. the link is here: http://developer.android.com/reference/android/widget/Button.html
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}
so you retrieve a button from xml with "findViewById" method. or you can programmatically add the view by initializing the button like this
Button bt = new Button(this); //"this" if inside a view or activity class or you have to get context somehow
ViewGroup.LayoutParams vl = new ViewGroup.LayoutParams(height, width);
bt.setLayoutParams(vl);
ViewGroup vg = new ViewGroup(this);
vg.addView(bt);
Upvotes: 0
Reputation: 6140
Here is simple way of use button.
In layout file
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"/>
Define buttons
Button btn1 = (Button)findViewById(R.id.button1);
Button btn2 = (Button)findViewById(R.id.button2);
Click event for button
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Upvotes: 3