I have tow on_click event in one activity linked to tow different buttons but what happened is when I press the first or second button the first on_click event is the one who is working!! here is the java code:
TextView user;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_activity);
user=findViewById(R.id.textView);
Intent i = getIntent();
String msg = i.getStringExtra(("msg"));
user.setText("welcome"+" "+msg+" choose a category");
}
public void science(View v){
Intent i = new Intent(this,science_first_activity.class);
startActivity(i);
}
public void art(View v)
{
Intent i = new Intent(this,art_first_activity.class);
startActivity(i);
}
and here is the xml:
<Button
android:id="#+id/button_s"
android:layout_width="258dp"
android:layout_height="65dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="77dp"
android:layout_marginLeft="77dp"
android:layout_marginEnd="76dp"
android:layout_marginRight="76dp"
android:layout_marginBottom="437dp"
android:onClick="science"
android:text="science" />
<Button
android:id="#+id/button_a"
android:layout_width="258dp"
android:layout_height="65dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="77dp"
android:layout_marginLeft="77dp"
android:layout_marginEnd="76dp"
android:layout_marginRight="76dp"
android:layout_marginBottom="328dp"
android:onClick="art"
android:text="art" />
no matter what button I click he will keep go to the science activity!
Start activity if view id are equal to avoid that.
public void science(View v){
if(view.getId==R.id.button_s){
Intent intentS = new Intent(this,science_first_activity.class);
startActivity(intentS );
}}
public void art(View v)
{
if(view.getId==R.id.button_a){
Intent intentA = new Intent(this,art_first_activity.class);
startActivity(intentA );
}}
Related
I was working yesterday and not today. Have two buttons one to go back, one that is an emergency button that calls a method to open the dialer for a call to the ambulance.
I think it may be an XML problem not sure was working fine now unresponsive maybe I changed something I shouldn't have. When it was clicking earlier it was saying the method could not be found which I just don't get it as it is in the correct file
ReportActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//allows for a smoother transition
overridePendingTransition(0, 0);
setContentView(R.layout.activity_report);
EditText incidentReport = this.findViewById(R.id.Incident_Report);
incidentReport.setSelection(0);
try {
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ReportActivity.this, MainActivity.class);
startActivity(intent);
}
});
} catch (NullPointerException n) {
Toast.makeText(this, "Error,", Toast.LENGTH_LONG);
}
try {
emergency.setOnClickListener(new View.OnClickListener() {
public void onClick(#NonNull View v) {
Intent call = new Intent(ReportActivity.this, CallActivity.class);
startActivity(call);
}
});
} catch (NullPointerException n) {
Toast.makeText(this, "Error,", Toast.LENGTH_LONG);
}
}
#Override
public void onClick(View v) {
}
private boolean checkPermission(String permission) {
int permissionCheck = ContextCompat.checkSelfPermission(this, permission);
return (permissionCheck == PackageManager.PERMISSION_GRANTED);
}
public void call(View view) {
if (checkPermission("android.permission.CALL_PHONE")) {
Intent call = new Intent(Intent.ACTION_DIAL);
String number = "tel:" + getString(R.string.phone_number);
call.setData(Uri.parse(number));
startActivity(call);
}
}
}
activity_report.xml
android:gravity="start"
android:hint="#string/Hint"
android:inputType="text"
android:textAlignment="textStart"
android:textCursorDrawable="#drawable/color_cursor"
app:layout_constraintBottom_toTopOf="#+id/submit_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/submit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="32dp"
android:background="#drawable/yes_button"
android:text="Submit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Incident_Report" />
<Button
android:id="#+id/back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:background="#drawable/diagnose_button"
android:onClick="onClick"
android:text="Back"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Incident_Report" />
<Button
android:id="#+id/emergency_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="131dp"
android:layout_marginBottom="106dp"
android:background="#drawable/no_button"
android:onClick="call"
android:text="EMERGENCY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/submit_btn" />
</androidx.constraintlayout.widget.ConstraintLayout>
If you plan on using android:onClick="call"in your xml then you should also have a corresponding function called callin your activity and the logic to perform the click goes inside it.
If you plan on extending the View.onClickListener then inside your override void onClick() you can have a switch statement of your button ids and perform your logic
alternatively you can ignore the first two and get the id of the button and implement the click logic inside the onCreate()
here is the code of my xml.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn2"
android:layout_gravity="center_horizontal"
android:text="Make New Account"
android:onClick="new"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
and here is my code of java.
public Button btn2;
public void onClick(){
btn2=(Button)findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
});
}
my button is not working yet, even an message will appear after clicking on button that unfortunately application has stopped... what is the reason??
Try this code:-
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn2"
android:layout_gravity="center_horizontal"
android:text="Make New Account"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
Put this in your activity:-
btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
});
Your code is perfect Nida..
Just Add Your activity name in menifest file like below code,
<activity android:name=".submit"/>
You have two ways:
Either
1) Set an onClick listener on the button
Or
2) Set an onClick attribute on the button and create a method
Method 1
Xml file
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn2"
android:layout_gravity="center_horizontal"
android:text="Make New Account"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
Java File
public Button btn2;
btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
});
Method 2
Xml file
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn2"
android:layout_gravity="center_horizontal"
android:onclick="newAccount"
android:text="Make New Account"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
Java file
public Button btn2;
btn2=(Button)findViewById(R.id.btn2);
public void newAccount(View v) {
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
The thing is you are calling the onClick function when you have declared the onClick of button as new
Try this :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn2"
android:layout_gravity="center_horizontal"
android:text="Make New Account"
android:onClick="submit"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
And then in your activity class declare this function:
public void submit(View view){
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
That's it. Hope this helps.
Activity is the most basic Android components is also the most common use of the four components (Activity, Service, Content Provider, BroadcastReceiver).
The steps to create an Activity:
Create a new Java class And extends the Activity
Add in the AndroidManifest
<activity android:name=".ActivityClassName"/>
If is to start the interface
<activity android:name=".ActivityClassName">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Override the onCreate() function and load layout
Note: The Activity of the Java classes generally ends in Activity
I have 2 activities that involved in this question:
LoginActivity: A Login in Screen (called by a subclass of Activity Class)
Updater Activity: A Update Screen that show updating progress when new app version is availble (called by a subclass of Application Class)
This is what I want:
When the app FIRST LAUNCH, and if new version of the app is available, the UPDATER ACTIVITY should be on top of every activity include the Login Activity,
so it will be the first screen user see during first launch of the app.
only after the updating progress is done, the UPDATER ACTIVITY will be dismissed then Login Screen be on top.
However this is what actually happened:
The LOGIN ACTIVITY always on top of UPDATER ACTIVITY...
My Guess is that since the UPDATER ACTIVITY is called By APPLICATION CLASS's subClass, and the LOGINACTIVITY is called by an ACTIVITY CLASS sub class,
the Application class's method such as OnResume always get called before Activity onCreate Method, thus the LOGINACTIVITY always appear on top of UPDATER ACTIVITY,
I just wondering if there is anyway I could make my UPDATER ACTIVITY be on top of any activities including the LOGIN ACTIVITY?
//LoginActivity involved
public abstract ClassA extends Activity {
.....
.....
protected void onResume() {
super.onResume();
.....
if(isAuthenticated){
Intent intent = new Intent(this, LoginActivity.class)
.putExtra(...)
startActivityForResult(intent, XXX);
}
}
}
====
//Updater Activity involved
public abstract class ClassB extends Application {
....
....
....
public void onCreate() {
super.onCreate();
Intent updaterIntent = new Intent(this, UpdaterActivity.class);
updaterIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(updaterIntent);
}
}
i have diffrent solution instent of activity create dialog box activity.
public class ViewDialog {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.updatedialog);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogUpdate = (Button) dialog.findViewById(R.id.update);
Button dialogcancel = (Button) dialog.findViewById(R.id.cancel);
Button dialogremindme = (Button) dialog.findViewById(R.id.remindeme);
TextView textView= (TextView) dialog.findViewById(R.id.machhint1);
TextView textView2= (TextView) dialog.findViewById(R.id.machhint2);
textView2.setText("Your old version is : "+versionName);
textView.setText("Current version in playstore : "+version);
dialogcancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialogUpdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
SharedPreferences.Editor editoradddetail1 = getSharedPreferences("later", MODE_PRIVATE).edit();
editoradddetail1.putString("days", "");
editoradddetail1.commit();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
later="";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
dialog.dismiss();
}
});
dialogremindme.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editoradddetail1 = getSharedPreferences("later", MODE_PRIVATE).edit();
Date someDate = new Date(); // Or whatever
Date dayAfter = new Date(someDate.getTime() + TimeUnit.DAYS.toMillis( 2 ));
DateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy");
String formattedDate=dateFormat.format(dayAfter);
editoradddetail1.putString("days", formattedDate);
editoradddetail1.commit();
dialog.dismiss();
}
});
dialog.show();
}
}
//and xml is.
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="250dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="250dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/main"
android:background="#FFed6904"
android:layout_height="120dp">
<TextView
android:id="#+id/machhint"
android:text="Note:- Your current Version Did Not Match"
android:layout_width="match_parent"
android:textColor="#color/white"
android:textSize="13dp"
android:layout_height="60dp" />
<TextView
android:id="#+id/machhint1"
android:text="preschool version is"
android:layout_width="wrap_content"
android:textSize="13dp"
android:textColor="#color/white"
android:layout_below="#+id/machhint"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/machhint2"
android:text="Your Vertion is :"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:textSize="13dp"
android:layout_below="#+id/machhint1"
android:layout_height="30dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/text1"
android:layout_below="#+id/main"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXTO"
android:id="#+id/text_dialog"
android:layout_marginTop="3dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="3dp"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_below="#+id/text1"
android:gravity="center_vertical|center_horizontal"
android:layout_height="55dp">
<Button
android:layout_width="wrap_content"
android:layout_height="45dp"
android:text="Update"
android:id="#+id/update"
android:gravity="center"
android:layout_toLeftOf="#+id/remindeme"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="45dp"
android:text="Later"
android:id="#+id/remindeme"
android:gravity="center"
android:layout_centerHorizontal="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="45dp"
android:text="Cancel"
android:id="#+id/cancel"
android:gravity="center"
android:layout_toRightOf="#+id/remindeme"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
I try to make a image button for login. But the result is weird. Please see the attachment.
The weird thing is the image button is inside the button...
Hope for helps.
this is xml code...
<ImageButton
android:id="#+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/chkRememberMe"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="#drawable/login_off" />
this is java code for the login button...
imageButtonLogin = (ImageButton) findViewById(R.id.loginButton);
imageButtonLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String inputPassword = passwordEditText.getText().toString();
if (password.isEmpty()) {
showDialog(DIALOG_ALERT);
} else {
String inputUserName = userNameEditText.getText()
.toString();
Contact contact = new Contact();
contact.setUsername(inputUserName);
contact.setPassword(inputPassword);
if (contactDb.searchContact(contact)) {
// logged in
/*Toast.makeText(getApplicationContext(),
getResources().getString(R.string.loggedIn),
Toast.LENGTH_LONG).show();*/
Intent newActivity = new Intent();
//go to AudioRecoder page
newActivity
.setClass(MainActivity.this, AudioActivity.class);
startActivity(newActivity);
} else {
// login failed
showDialog(DIALOG_ALERT);
}
}
}
You need to use
android:background="#drawable/login_off"
instead of src like you are.
<ImageButton
android:id="#+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/chkRememberMe"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#null"
android:src="#drawable/login_off" />
you can set the background or trasparent android:background="#00FFFFFF" or to null android:background="#null"
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