create buttons in java - android

for an exercise I have to create some buttons. When clicked, these buttons should make appear an animated imagine.
Can you helo to build the java code?
I just created the buttons and the ImageView, but I don't understand if I have to build one or two activity.
the buttons and the imageview have to stay in the same page: for example, when I click the "apple" button, the image view must show an apple, when I click "orange" it shows an orange, etc... all in the same screen.
my java code appear like this:
public class HomeWork extends Activity {
public static final int GET_CODE=1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_work);
Button getResultButton = (Button) findViewById(R.id.btn1);
getResultButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent resultIntent =new Intent();
resultIntent.setClass(HomeWork.this,HomeWork2.class);
HomeWork(resultIntent, GET_CODE);
}
but the onClick gives me an error

> Intent resultIntent =new Intent();
> resultIntent.setClass(HomeWork.this,HomeWork2.class);
> HomeWork(resultIntent, GET_CODE);
I don't cathes what are you doing in this. :)
if you need to open HomeWork2 Activity call
Intent resultIntent =new Intent(HomeWork.this,HomeWork2.class);
startAcrivtyForResult(resultIntent, GET_CODE));

ok, I deleted the GET_CODE (I really don't know why I put it), and now my code appears like this:
ACTIVITY ONE:
public class HomeWork extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_work);
Button getResultButton = (Button) findViewById(R.id.btn1);
getResultButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent resultIntent =new Intent(HomeWork.this,HomeWork2.class);
startActivity(resultIntent);
}
});
}
ACTIVITY TWO:
public class HomeWork2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_work2);
ImageView img=(ImageView)findViewById(R.id.imageView);
img.setBackgroundResource(R.drawable.apple);
}
ACTIVITY ONE LAYOUT XML
<Button
android:id="#+id/btn1"
android:layout_width="108dp"
android:layout_height="32dp"
android:onClick="btn1_click"
android:text="Apple" />
ACTIVITY TWO LAYOUT XML
<Button
android:id="#+id/btn1"
android:layout_width="108dp"
android:layout_height="32dp"
android:onClick="btn1_click"
android:text="Apple" />
no errors, but when I start the emulator, it give me the error "Unfortunately, Homework has stopped"
i watch in the logcat but I don't know what I had to change...

Related

Android: Unable to call back buttonOnCklick [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Actually I don't understand why it doesn't see the buttonOnClick listener?
public class GeneralActivity extends AppCompatActivity {
// with or without these lines below
private View.OnClickListener buttonOnClick = this.buttonOnClick;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GlobalData.getInstance().gContext = this;
setContentView(R.layout.activity_general);
((TextView) findViewById(R.id.licence)).setText(MySettings.Values().LicenceNo);
Button connect = (Button)findViewById(R.id.connect);
connect.setOnClickListener(buttonOnClick);
Button send = (Button)findViewById(R.id.sendall);
send.setOnClickListener(buttonOnClick);
}
boolean clicked = false;
public static String LICENCE = "";
public void buttonOnClick(View v) {
MessageBox.Show("Connect me");
Button b = (Button) v;
if (b.getId() == R.id.connect) {
MessageBox.Show("Connect me");
return;
Also,
In GeneralActivitry designer everything is written out - onClick for both buttons and this method referred to the GeneralActivity.
Pls, see the movie and tell me what's wrong now?
youtube.com/watch?v=heD9QGKtusY&feature=youtu.be
FULL CODE OF GENERALACTIVITY LAYOUT:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="#+id/licence"
android:layout_width="match_parent"
android:layout_height="30pt"
android:ems="10"
android:hint="1SAT"
android:inputType="textPersonName"
android:layout_weight="0"
android:singleLine="true"
android:text="SATEST LICENCE NUMBER" />
<Button
android:id="#+id/connect"
android:layout_width="match_parent"
android:layout_height="30pt"
android:layout_weight="0"
android:onClick="buttonOnClick"
android:text="CONNECT" />
<LinearLayout
android:id="#+id/messages"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" />
<Button
android:id="#+id/sendall"
android:layout_width="match_parent"
android:layout_height="30pt"
android:layout_weight="0"
android:enabled="false"
android:onClick="buttonOnClick"
android:text="SEND2ALL" />
Well, I don't like this site exactly because of that kind of people who set -1 without any reason for that.. they just like shitters in that pages.
I explained my problem with MAXIMUM example and even VIDEO.. WHAT ELSE do you need to see to get the question clear? Idiots.
Ok, let they live, it is the shit policy of this site holders..
Now you read carefully what was my error:
When I implemented OnViewListener interface I forgot to mark the method onClick with #Override and another "BIG" mistake was that the handler name onClick I wrote from UPPER LETTER OnClick..
Actually that was my main mistake. Before publishing my Q here, I try to understand everything by myself, and I tried all of the suggestions that guys gave me.. but the
letter O and o
hmm you understood )))
I am 30 years in software developing, and these kind of mistakes are the hardest mistakes I have ever met. Same like brackets [ { jr (..
Thank you all for you help.. I'll set the Answer to the first one.. Thank you again.
you can use built it OnClick method to do this for you: here is an example of how to do it
just don't forget to implement View.OnClickListener in your Activity
`
public class GeneralActivity extends AppCompatActivity implements View.OnClickListener {
// with or without these lines below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GlobalData.getInstance().gContext = this;
setContentView(R.layout.activity_general);
((TextView) findViewById(R.id.licence)).setText(MySettings.Values().LicenceNo);
Button connect = findViewById(R.id.connect);
connect.setOnClickListener(this);
Button send = findViewById(R.id.sendall);
send.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.connect:
//TODO
break;
case R.id.sendall:
//DO something
break;
}
}
`
Option - 1: You have already set the onClick listener to button from xml, so no need to set it again from java code. Remove those line.
public class GeneralActivity extends AppCompatActivity {
// with or without these lines below
//private View.OnClickListener buttonOnClick = this.buttonOnClick;
#Override
protected void onCreate(Bundle savedInstanceState) {
....
//Button connect = (Button) findViewById(R.id.connect);
//connect.setOnClickListener(buttonOnClick);
//Button send = (Button) findViewById(R.id.sendall);
//send.setOnClickListener(buttonOnClick);
}
public void buttonOnClick(View v) {
MessageBox.Show("Connect me");
if (v.getId() == R.id.connect) {
MessageBox.Show("Connect me");
return;
} else if(v.getId() == R.id.sendall) {
//Add your logic here
}
}
}
Option - 2: If you want to set the listener from java code, then remove onClick attribute from xml and set it like below from code:
public class GeneralActivity extends AppCompatActivity {
boolean clicked = false;
public static String LICENCE = "";
private View.OnClickListener buttonOnClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.connect:
// Your connect logic here
break;
case R.id.sendall:
// Your send all logic here
break;
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GlobalData.getInstance().gContext = this;
setContentView(R.layout.activity_general);
((TextView) findViewById(R.id.licence)).setText(MySettings.Values().LicenceNo);
Button connect = (Button) findViewById(R.id.connect);
Button send = (Button) findViewById(R.id.sendall);
connect.setOnClickListener(buttonOnClick);
send.setOnClickListener(buttonOnClick);
}
}
Solution:
public class MainActivity extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
Button one = (Button) findViewById(R.id.oneButton);
one.setOnClickListener(this); // calling onClick() method
Button two = (Button) findViewById(R.id.twoButton);
two.setOnClickListener(this);
}
//outside of your oncreate()
public void onClick(View v) {
switch (v.getId()) {
case R.id.oneButton:
// do your code
break;
case R.id.twoButton:
// do your code
break;
default:
break;
}
}
TIP: Enter new View. And then press Ctrl+Spacebar you will get the function auto generated.

How do i create a button click event and make sure that only when i click on the button something it will do something?

In the MainActivity.java i changed it to:
public abstract class MainActivity extends ActionBarActivity implements OnClickListener
changed to abstract and added the implements OnClickListener.
Now i want to add event click for the button.
This is the button settings in the layout.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Ip"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
I want to do that when i click the button only if i clicked on the button area not the screen then do something.
And when i finished done everything set the button to be not visible and also not clickable instead if i click now on the place where the button is it will act now like i touch the screen.
This is what i tried so far.
In the designer added a button changed the id of the button to checkforip.
Then in the MainActivity.java i did:
public abstract class MainActivity extends ActionBarActivity implements OnClickListener
{
private static final int MY_DATA_CHECK_CODE = 0;
public static MainActivity currentActivity;
TextToSpeech mTts;
private String targetURL;
private String urlParameters;
private Button btnClick;
private String clicking = "clicked";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(new SingleTouchEventView(this, null));
btnClick = (Button) findViewById(R.id.checkipbutton);
btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
clicking = "clicked";
}
});
First the abstract is wrong i guess also when running the program with debug mode it dosen't even pass the abstract and shut down the program.
Second i want the button click event handler to be on it's own out of the onCreate i guess.
First get button in your Activity:
Button btn = (Button) findViewById(R.id.button);
Second, listen when the button is clicked:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// add here what you want to do when button is clicked.
}
});
Third, if you want to make button disappear then add this line in onClick
button.setVisibility(View.GONE);
First there is no need to define the class as an abstract you should remove it and remove the implementation of the listener .
Instead there is an attribute for all views in android which is onClick and you can define as follow :
<Button
......
android:onClick="onBtnClick"
\>
After that you define a method in the java file that display this layout as follow :
#override
Public void onBtnClick (View v){
//to make it unclickable
v.setClickable (false);
//to hide it
v.setVisibility (View.GONE);
}

Linking buttons to pages using android(Eclipse) - can't get it to work

I'm new to Android development, and to this site!
I have done a few tutorials etc and am working on a project at the moment, and had a good look through other answers to similar questions, but haven't been able to find quite what i'm looking for (but loads of good suggestions!)
I am trying to get buttons on my main screen linking to individual pages. I am using my phone instead of an emulator, but every time i click on a button, the app dies... can you help me?
This is my main Screen code for button1:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declaring and defining the buttons used
Button student1 = (Button) findViewById(R.id.button1);
// Setting the onClickListener for button1
student1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//calling the page1 function
page1(view);
}
});
This is the page1 function:
public void page1(View view) {
Intent intent = new Intent(this, Page1.class);
startActivity(intent);
}
Here is the code for the Page1 class file:
public class Page1 extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_1);
}
}
This is the code for the layout file: (page_1.xml)
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="shannon.white.finalyear.DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
If you need anything else, let me know
Any ideas?
Thanks :)
Your coding looks to be correct.
The next thing to check would be to look inside your AndroidManifest.xml file to ensure you have added the activity to it so the android OS knows it exists. You add it like so:
<activity android:name="Page1" />
If your activity resides in a different package then the one declared inside the manifest file, then you need to specify the full package inside the "name" like so:
<activity android:name="some.other.package.name.Page1" />
Thats about all i can say from the provided code. If you are simply starting another activity which is Page1.class then your code looks correct and you might be missing the manifest declaration as i stated above.
Try moving this following code
// Declaring and defining the buttons used
Button student1 = (Button) findViewById(R.id.button1);
// Setting the onClickListener for button1
student1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
underneath your page1 function so your code looks like this:
public void page1(View view) {
Button student1 = (Button) findViewById(R.id.button1); // Declaring and defining the buttons used
student1.setOnClickListener(new View.OnClickListener() { // Setting the onClickListener for button1
#Override
public void onClick(View v) {
startActivity(intent);
Intent intent = new Intent(this, Page1.class);
}
}
}
and your onCreate look like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
page1(view); // NOTE I'm now declaring it on the onCreate instead of onClick
}
If that doesn't help, well your code still looks cleaner. Could just be my OCD though...
This is a very good tutorial by Mkyong on how to achieve what you are trying to do. If no other answers help, restarting using this tutorial will likely help you succeed. On multiple occasions I've tested his code and its worked.

Opening a new activity on button click

I have looked at every example of switching between activities and I always get the same result. The app bombs.
As far as I can tell if you have a java class that populates the content of a layout then in order to switch to the other layout, you must 'link' to the java file which in turn will open the setContentView(R.layout.whatever);
When I try to do this, like I say my app bombs out. My code is as follows:-
FROM Java class:-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.goesnews);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), ac2.class);
startActivityForResult(myIntent, 0);
}
});
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
}
TO java file (ac2)
public class ac2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}}
Can anyone help out here?
Try to remove the zero from this line:
startActivityForResult(myIntent, 0);
like this and change it to just:
startActivity(myIntent);
and change this line:
Intent myIntent = new Intent(view.getContext(), ac2.class);
To this:
Intent myIntent = new Intent(firstActivityName.this, ac2.class);
because you are getting here the context of the button and not of the activity.
The solution was simple and was based on the response from VSK (Thanks) with a little tweak.
The ImageButton required :-
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:onClick="move" />
Java required:-
public void move(View v)
{
Intent myIntent = new Intent(yourclass.this, ac2.class);
startActivityForResult(myIntent, 0);
}
VSK - please note '0' attribute of startActivityForResult
Thanks all
try this way
add onclick function to your button in xml file
like this
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:onClick="move" />
and in ur java file
make a function move like this
public void move(View v)
{
Intent myIntent = new Intent(yourclass.this, ac2.class);
startActivityForResult(myIntent,0);
}

click on the android phone screen

I have an app in android in which I wanna achieve the following thing:
I want to click on an image on android mobile through python script.
Does anyone know how could I achieve that?
You can create a layout with a single button. Set the background of the button as the image you want. Make the button width and height to match parent. Then register the button in a normal way to start the activity. So something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:id="#+id/button1"
android:layout_height="match_parent"
android:background="#drawable/background">
</Button>
</LinearLayout>
With your activity like this:
public class ActivityA extends Activity implements OnClickListener
{
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
Button buttonA = (Button) findViewById(R.id.button1);
buttonA.setOnClickListener(this);
}
#Override
public void onClick(View v) {
final Intent intent = new Intent(getApplicationContext(), ActivityB.class);
startActivity(intent);
}
}
When the user presses "back", he or she will go back to the giant button activity.
Please try this whenever you want to move from one activity to another activity..
Intent myIntent = new Intent(CurrentActivity.this,NextActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
Above code will take you from CurrentActivity to NextActivity....
Whenever you want that to happen just execute those three statements,
that will do your work....

Categories

Resources