i have the edit view and button in main activity. Here is the button code:
<Button
android:id="#+id/button"
style="#style/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:onClick="#{handler::onButtonClick}"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editText" />
how can i change activity in onButtonClick method?
<Button
android:id="#+id/button"
style="#style/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:onClick="onButtonClick"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editText" />
And add this function to your java code:
public void onButtonClick(View view) {
Intent intent = new Intent(context, YourActivityClass.class);
context.startActivity(intent);
}
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Here you can edit the view and change activity
// Change Activity using Intent
startActivity(new Intent(CurrentActivity.this, SecondActivity.class));
}
});
}
Hope this answer helps you.
In your main Activity
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getContext(),YOURACTIVITYNAME.class);
startActivity(intent);
}
});
Related
Only on 2nd click the button loads the new activity.
Usually this problem happens often when I define an onCLick() method in the .xml and also have it in the activity. This is here not the case.
<Button
android:id="#+id/settingsBtn"
android:layout_width="192dp"
android:layout_height="60dp"
android:layout_above="#+id/exitBtn"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_gravity="end|bottom"
android:layout_marginStart="-2dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="24dp"
android:background="#android:color/black"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="#string/settings"
android:textColor="#android:color/white"
android:textSize="24sp" />
private Button mSettingsButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.
.
.
connectUiElements();
setUpUiListeners();
}
.
.
.
private void connectUiElements() {
mSettingsButton = (Button) findViewById(R.id.settingsBtn);
}
private void setUpUiListeners() {
mSettingsButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
SettingsActivity.class);
startActivity(intent);
}
}
);
}
Usually I would expect, that the buttons works when I click once.
I created a simple activity that is intended to send two pieces of information to another activity. However, my button isn't registering any click events. Does anyone have any idea why this would be? I expected that by adding the onClick = "onClickWrite" to the XML file that they would be linked but there is no response when clicked. If anyone could help me out, I would greatly appreciate it. I have tried implementing the onClickListener, however, with that implementation, it throws an error on my Toasts.
Activity
public class InitializeClass extends Activity {
private Service service;
private Button button;
EditText infoOne;
EditText infoTwo;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_control);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(clicked);
infoOne= (EditText) findViewById(R.id.editText);
infoTwo= (EditText) findViewById(R.id.editText1);
}
private View.OnClickListener clicked = new View.OnClickListener(){
#Override
public void onClick(View view) {
if (service != null) {
int size = 100;
byte[] byteArray = new byte[size];
byte[] byteArrayTwo = new byte[size];
byteArray = infoOne.getText().toString().getBytes(Charset.defaultCharset());
byteArrayTwo= infoTwo.getText().toString().getBytes(Charset.defaultCharset());
if ((!(infoOne.getText().toString().isEmpty())) && (!(infoTwo.getText().toString().isEmpty()))) {
service.setInfo(byteArray);
service.setInfoTwo(byteArrayTwo);
intentMethod();
}
}
}
};
public void intentMethod() {
Intent intent = new Intent(this, DeviceScanActivity.class);
startActivity(intent);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InitializeClass">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="#string/send_info"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText1" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="150dp"
android:ems="10"
android:inputType="textPersonName"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColorHint="#android:color/white"
android:textCursorDrawable="#drawable/color_cursor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="info"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColorHint="#android:color/white"
android:textCursorDrawable="#drawable/color_cursor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText" />
</android.support.constraint.ConstraintLayout>
You need to initialize your button first.
Button button = (Button) findViewById(R.id.button);
Hope this helps!
In your activity class assign the button to the id of the button in the xml:
private Button btn = findViewById(R.id.button);
You will then create an OnClickListener:
private View.OnClickListener clicked = new View.OnClickListener(){
#Override
public void onClick(View view) {
//Insert Code here
}
};
In your onCreate method in the same activity class you will instantiate the button and assign the button to the onClickListener:
btn = new Button(this);
btn.setOnClickListener(clicked);
There are couple of things you need to do for that.
Create Field.
private Button button;
Initialize button.
Button button = (Button) findViewById(R.id.button);
Set Listener on button.
button.setOnClickListener();
I found the answer. Service in if (service != null) was never being initialized. Stupid mistake on my part. Thanks, everyone for the help and directing me in a way to implement onClickListener!
You need to cast the button (name) with you Java code .
for example on xml you have id/button (name)
declare it on your Java file, and do the casting.
and the onClickListener will look like this :
Cast the button : Button button ;
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
I was working on a different code site before Android studio, and how they made buttons in a second activity open was different than here. So far I have my second activity button and it opens..
My fifthactivity.java is below
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, AmazonActivity.class);
FifthActivity.this.startActivity(intent);
}
});
}
}
I understand I need to make a new .java and a new layout to direct the button, I just need help with the code to put into my fifth activity.java
Below is my layout for the other button that i need to open.
<Button
tools:ignore="HardcodedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PlayStation"
android:drawableLeft="#drawable/playstation"
android:drawableStart="#drawable/playstation"
android:layout_weight="0.07"
android:textSize="35sp"
android:id="#+id/button5" />
Button button;
Button anotherButton; // the second button OP required
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
anotherButton = (Button)findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, AmazonActivity.class);
FifthActivity.this.startActivity(intent);
}
});
/* new button to open a new activity */
anotherButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating the intent
Intent intent = new Intent(FifthActivity.this, AnotherActivity.class);
// starting activity with the created intent
startActivity(intent);
}
});
}
}
Add new Button to your xml file and style it how do you like and add new id like
android:id="#+id/button6" :
<Button
tools:ignore="HardcodedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PlayStation"
android:drawableLeft="#drawable/playstation"
android:drawableStart="#drawable/playstation"
android:layout_weight="0.07"
android:textSize="35sp"
android:id="#+id/button5" />
<Button
android:id="#+id/button6"
tools:ignore="HardcodedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SomeText"
android:textSize="35sp" />
In your fifthactivity.java add new Button:
Button button, button2;
and, like your previous button, add click listener to that button. Create new Java class with its own layout and open it thru Intent with that button.
I want to forward from current activity to other Activity. But when i click the button i am not able to perform it.I want to move from MainActivity to FeedActivity Please tell what is fault in my code.
<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="com.sample.test.MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="53dp"
android:layout_marginTop="92dp"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/button1"
android:text="#string/button2" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"Welcome to android by Vivek", Toast.LENGTH_LONG)
.show();
}
});
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), FeedActivity.class);
startActivity(intent);
}
});
}
}
When you are creating a new instance of Intent, you need to give it two parameters:
current activity class instance
goto activity class instance
Also, you need to either import View library or use your new OnClickListener as new View.OnClickListener
You can do that by adding this to import:
import android.view.View;
import android.view.View.OnClickListener;
Change your btn2.onClickListener to :
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, FeedActivity.class);
startActivity(intent);
}
Im building an application that requires when you press the button I.E. (search) then it opens the search page, click the (home) button then it goes back to the home view. what I'm missing is the knowledge to make that connection between those two views. this is what the home page looks like in XML.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
//Title
//Search Student Button
<Button
android:id="#+id/button1"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="Search Student" />
//New Student Button
<Button
android:id="#+id/button2"
android:layout_width="99dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="New Studetn " />
//Legal Button
<Button
android:id="#+id/button3"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="Legal Info" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp"
android:text="Student Registration "
android:textAppearance="?android:attr/textAppearanceLarge" />
some pictures of the application.
http://imgur.com/aVpqUCZ
Button searchStudentButton;
Button newStudentButton;
Button legalButton;
searchStudentButton = (Button) findViewById(R.id.button1);
searchStudentButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, searchStudentActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
newStudentButton = (Button) findViewById(R.id.button2);
newStudentButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, newStudentActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
legalButton = (Button) findViewById(R.id.button3);
legalButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, legalActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
Don't forget to add your new activity in the AndroidManifest.xml:
<activity android:label="#string/app_name" android:name="searchStudentActivity"/>
<activity android:label="#string/app_name" android:name="newStudentActivity"/>
<activity android:label="#string/app_name" android:name="legalActivity"/>
When dealing with multiple views and/or buttons like you are, I usually prefer using only one instance of onClickListener for all the views, to keep the code cleaner.
public class MainActivity extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Button btnSearchStudent = (Button) findViewById(R.id.button1);
Button btnNewStudent = (Button) findViewById(R.id.button2);
Button btnLegalInfo = (Button) findViewById(R.id.button3);
btnSearchStudent.setOnClickListener(this);
btnNewStudent.setOnClickListener(this);
btnLegalInfo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1: {
Intent intent = new Intent(this, SearchStudentActivity.class);
startActivity(intent);
break;
}
case R.id.button2: {
Intent intent = new Intent(this, NewStudentActivity.class);
startActivity(intent);
break;
}
case R.id.button3: {
Intent intent = new Intent(this, LegalInfoActivity.class);
startActivity(intent);
break;
}
}
}
}
I would recommend that you change the android:id attribute of your buttons to a more meaningful name. This makes it easier to see what you're referencing in the code. I personally prefer prepending my views with an abreviation of the view class such as btn_ for Button and tv_ for TextView. Remember to update your calls to findViewById() and the id's used in the switch statement.
Finally, don't forget to add your activities to the AndroidManifest.xml file as Sagar Pilkhwal posted, if you haven't already.