Multiple buttons perform single one task Android - android

I need to perform a task whereby a user need to click on multiple buttons to perform a single task for example dialog box, image or toast.
I mean that clicking on more than one button in order to execute a task. It would be better to let the user know he or she has already clicked on the first button by highlight the button with a color. And when he or she click on the second or third button,highlight the button and it will execute the task like toast or dialog box .
Can someone assist me thanks.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.android.testing1.MainActivity"
tools:showIn="#layout/activity_main">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="104dp"
android:layout_marginTop="20dp"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="34dp"
android:text="Button2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_centerVertical="true"
android:text="Button3" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button3"
android:layout_below="#+id/button3"
android:layout_marginTop="34dp"
android:text="Button4" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button4"
android:layout_alignParentBottom="true"
android:layout_marginBottom="24dp"
android:text="Button5" />
</RelativeLayout>
package com.example.android.testing1;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import static android.R.id.button1;
import static android.R.id.button2;
import static android.R.id.button3;
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3,b4,b5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button dodo = (Button) findViewById(button1);
Button dede = (Button) findViewById(button2);
Button dada = (Button) findViewById(button3);
Button didi = (Button) findViewById(button4);
Button hehe = (Button) findViewById(Button5);
If(dodo == true, dede==true, dada == true , didi=true, hehe=true);
Then perform same task ike toast or dialog box after click on all the five buttons

Related

How to allow new View.OnClickListener() to function in android studio?

I am a starter in Android Studio and I am working on the program that reads user input and process the information. I ran into this problem that user input is not getting processed at all even though I did follow the tutorials and videos. Here's the code in MainActivity.java.
package com.example.postcodechecker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int postalcodeinputvalue;
EditText postalcodeinput;
Button submit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
postalcodeinput = (EditText) findViewById(R.id.postalcodeinput);
Button submit = findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
postalcodeinputvalue = Integer.valueOf(postalcodeinput.getText().toString());
showToast(String.valueOf(postalcodeinputvalue));
}
});
}
private void showToast(String text) {
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
}
note that new View.OnClickListener() isn't doing anything as the font colour is gray in the editor.
This is the layout file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:scaleX="1"
android:scaleY="1"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout2"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:fontFamily="sans-serif-condensed"
android:text="Welcome to the Postal Code checker"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textSize="20sp"
android:textStyle="bold"></TextView>
<EditText
android:id="#+id/postalcodeinput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter your postal code here"
android:inputType="number" />
<Button
android:id="#+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="check" />
</LinearLayout>
</RelativeLayout>
'''
Thanks a lot if you would like to give some help.
You are passing an Integer to the function ShowText but the constructor expects a String. Do this.
Change
showToast(String.valueOf(postalcodeinputvalue));
To
showToast(postalcodeinput.getText().toString());

First activity won't run on AVD

I created an application, my first, and am having problems running it on an AVD. Won't load the first activity, just says 'Unfortunately, FussyFestivalrV7 has stopped. This is the main starting activity:
package com.example.fussyfestivalr;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, News.class);
startActivity(intent);
}
});
}
}
And my layout
<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:background="#string/title"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/newsbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="#string/news" />
<Button
android:id="#+id/exit"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="17dp"
android:layout_toRightOf="#+id/title"
android:text="#string/exit" />
<Button
android:id="#+id/bands"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/exit"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp"
android:text="#string/bands" />
<Button
android:id="#+id/fafq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/bands"
android:layout_centerHorizontal="true"
android:layout_marginBottom="53dp"
android:text="#string/fafq" />
<Button
android:id="#+id/profiles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/fafq"
android:layout_centerHorizontal="true"
android:layout_marginBottom="41dp"
android:text="#string/festivals" />
Check whether you have </RelativeLayout>.
I hope to help.
Paul.

onClick error - Android

I'm very new to this. My buttons weren't responding to clicks. I added android:onClick="onClick" to the xml file. Now i get the error below.
java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class testapp.two.MainActivity for onClick handler on view class android.widget.Button with id 'buttonMinus'
Please help, java code along with the manifest file and main xml are included below.
Thanks.
package testapp.two;
import testapp.two.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
Button okButton, minusButton, plusButton;
TextView textScore, scoreCard;
int score = 95;
private static final String TAG = "GolfScore";
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate started");
okButton = (Button)findViewById(R.id.buttonOK);
minusButton = (Button)findViewById(R.id.buttonMinus);
plusButton = (Button)findViewById(R.id.buttonPlus);
textScore = (TextView)findViewById(R.id.textScore);
scoreCard = (TextView)findViewById(R.id.scoreCard);
//set button listeners
okButton.setOnClickListener(this);
minusButton.setOnClickListener(this);
plusButton.setOnClickListener(this);
textScore.setText(String.valueOf(score));
Log.d(TAG, "onCreate finished");
}//onCreate
#Override
public void onClick(View v) {
Log.d(TAG, "in onCreate");
switch (v.getId()){
case R.id.buttonMinus:
score--;
textScore.setText(String.valueOf(score));
break;
case R.id.buttonPlus:
score++;
textScore.setText(String.valueOf(score));
break;
case R.id.buttonOK:
break;
}//end of switch
}//end of my onclick
}//end of MainActivity
<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:onClick="#+id/buttonMinus, #+id/buttonPlus, #+id/textScore"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="false"
android:layout_centerVertical="false"
android:text="Golf Score App"
android:textAlignment="center" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/title"
android:layout_marginTop="14dp"
android:baselineAligned="false"
android:gravity="fill"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonMinus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="5dp"
android:clickable="true"
android:onClick="onClick"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="-" />
<Button
android:id="#+id/buttonPlus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="0dp"
android:clickable="true"
android:onClick="onClick"
android:layout_weight="1"
android:text="+" />
<TextView
android:id="#+id/textScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="92"
android:textSize="20sp" />
<Button
android:id="#+id/buttonOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="0dp"
android:layout_marginRight="33dp"
android:clickable="true"
android:onClick="onClick"
android:layout_weight="1"
android:text="Ok"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:id="#+id/scoreCard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/LinearLayout1"
android:layout_marginTop="22dp"
android:text="Score card info goes here" />
</RelativeLayout>
As 0xDEADC0DE says, you don't need the OnClickListener if you use the onClick attribute in your XML and vice versa.
The reason why the onClick(View v) method can't be found is the Overrideannotation. It depends to the OnClickListener this way. There is a "free" method without annotation needed that the method can be found if the onClick tag is used.
Now you have two options: get rid of the OnClicklistener and the association to the buttons or the onClick in your XML.
Edit:
Remove the onClick attributes from the RelativeLayout and the Buttons in your XML. In addition, remove clickable from your Buttons. If you set clickable = true, the View will get an empty OnClickListener automatically. This means your OnClickListener in your Activity will not be called.
Just remove android:onClick="onClick" from your XML.
Note that it is preferrable to make a single OnClickListener for each button rather than one big OnClickListener that has to check the id to decide what to do.

Button writes to EditText

I want to make some buttons that write a letter to an EditText. My java code:
package com.koostamas.keyboard;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
public class MainActivity extends Activity implements OnClickListener {
Button buttona;
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttona = (Button) findViewById(R.id.buttona);
buttona.setOnClickListener(this);
buttona.getText();
}
public void onClick(View v) {
Button buttona = (Button)v;
editText.setText(editText.getText().toString()+buttona.getText().toString());
}
}
My layout file:
<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"
tools:context=".MainActivity" >
<Button
android:id="#+id/buttonb"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/buttona"
android:text="#string/buttonb"
android:textSize="50sp"
android:textStyle="bold" />
<Button
android:id="#+id/buttona"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/buttona"
android:textSize="50sp"
android:textStyle="bold" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="58"
android:textSize="20sp"
android:textStyle="bold"
android:inputType="text" >
<requestFocus />
</EditText>
</RelativeLayout>
It doesn't work and I don't know why. I found this solution on the Internet and I'm not an expert. Could you write me clearly how to do it?
Thanks in advance.
Take reference of EditText id in the onCreate():
editText = (EditText) findViewById(R.id.editText);

Android development set background

I want to change the background photo by changing the main activity java code here is what I write. When I run the application on my galaxy s3 and when I click the button, my phone get stuck and exit the app
This is the xml code:
<RelativeLayout android:id="#+id/helpLayout"
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"
android:background="#drawable/bgr"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView2"
android:textStyle="bold"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/How" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_centerVertical="true"
android:onClick="onRadioButtonClicked"
android:text="Click Me :)" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="26dp" >
<RadioButton
android:id="#+id/radio0"
android:textColor="#FFFF00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Happy" />
<RadioButton
android:id="#+id/radio1"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sad" />
<RadioButton
android:id="#+id/radio2"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="In Love" />
<RadioButton
android:id="#+id/radio3"
android:textColor="#808080"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Missing" />
</RadioGroup>
</RelativeLayout>
and this is the java code:
package com.example.anny;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onRadioButtonClicked(View view) {
LinearLayout ll = (LinearLayout) findViewById(R.layout.activity_main);
ll.setBackgroundResource(R.drawable.p1);
}
}
In your onRadioButtonclicked your code is:
LinearLayout ll = (LinearLayout) findViewById(R.layout.activity_main);//parameter is layout instead of Id.
ll.setBackgroundResource(R.drawable.p1);
First of all you don't have a LinearLayout in your XML file. The container is a RelativeLayout. Secondly, you have provided a layout reference instead of an ID. In Android, we provide ID as R.id.IdName. The ID of your RelativeLayout in your XML code is helpLayout. So, the code should be :
RelativeLayout ll = (RelativeLayout) findViewById(R.id.helpLayout);
change your code of onRadioButtonClicked with this one,then your problem will definetly solved:
RelativeLayout ll = (RelativeLayout) findViewById(R.id.helpLayout);
ll.setBackgroundResource(R.drawable.p1);

Categories

Resources