package com.example.sliderapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// add functionality for android button
MyClickListener listener = new MyClickListener();
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(listener);
// add functionality for apple button
MyClickListener2 listener2 = new MyClickListener2();
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(listener2);
// Add functionality for don't care button
MyClickListener3 listener3 = new MyClickListener3();
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(listener3);
//Add functionality for reset button
MyClickListener4 listener4 = new MyClickListener4();
Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(listener4);
}
#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;
}
/**
* click listener method referring to android button
*
* #author Ross
*
*/
private class MyClickListener implements View.OnClickListener {
public void onClick(View view) {
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(R.drawable.happypng);
}
}
/**
* click listener method referring to apple button
*
* #author Ross
*
*/
private class MyClickListener2 implements View.OnClickListener {
public void onClick(View view) {
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageResource(R.drawable.happypng);
}
}
/**
* Click listener referring to don't care button
* #author Ross
*
*/
private class MyClickListener3 implements View.OnClickListener {
public void onClick(View view) {
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(R.drawable.whynotpng);
ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView2.setImageResource(R.drawable.whynotpng);
}
}
/**
* Click listener referring to Reset button
* #author Ross
*
*/
private class MyClickListener4 implements View.OnClickListener {
public void onClick(View view) {
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(R.drawable.ic_launcher);
ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView2.setImageResource(R.drawable.apple_gray_logo);
}
}
}
In the code above the last method (myClicklistener4) I am trying to set up the imageResources as stated however it is setting them equal to .whynotpng like in the 3rd class. I am unsure why this is happening.
I cannot tell you where exacltly your error occurs as I can't see anything particular wrong in your code. This is your code shortened. Does it work now? And do you have saved all your drawables in the right folder and with the right names?
package com.example.sliderapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements View.OnClickListener {
private ImageView ivFirst, ivSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// register your button listeners
((Button) findViewById(R.id.button1)).setOnClickListener(this);
((Button) findViewById(R.id.button2)).setOnClickListener(this);
((Button) findViewById(R.id.button3)).setOnClickListener(this);
((Button) findViewById(R.id.button4)).setOnClickListener(this);
// you can also define the onClick method via xml by calling android:onClick="onClick"
// save your ImageViews so that you don't have to find them for every buttonclick
ivFirst = (ImageView) findViewById(R.id.imageView1);
ivSecond = (ImageView) findViewById(R.id.imageView2);
}
#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;
}
#Override
public void onClick(View view) {
// depending on which button is clicked do some action
switch(view.getId()) {
case R.id.button1:
ivFirst.setImageResource(R.drawable.happypng);
break;
case R.id.button2:
ivFirst.setImageResource(R.drawable.happypng);
break;
case R.id.button3:
ivFirst.setImageResource(R.drawable.whynotpng);
ivSecond.setImageResource(R.drawable.whynotpng);
break;
case R.id.button4:
ivFirst.setImageResource(R.drawable.ic_launcher);
ivSecond.setImageResource(R.drawable.apple_gray_logo);
break;
}
}
}
One of the approach to short down your code, is already explained by "Endzeit" .
Another approach which you can follow to short down your code is explained below:
First:
Add android:onClick="method name" in all 4 Buttons in your xml file.
Example:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClick"
android:text="button1" />
Similarly do it for button2, button3 and button 4
Now, add this code in your Java class
public class MainActivity extends Activity {
private ImageView imageView1, imageView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
}
#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;
}
#Override
public void myClick(View view) {
// depending on which button is clicked do some action
switch(view.getId()) {
case R.id.button1:
imageView1.setImageResource(R.drawable.happypng);
break;
case R.id.button2:
imageView2.setImageResource(R.drawable.happypng);
break;
case R.id.button3:
imageView1.setImageResource(R.drawable.whynotpng);
imageView2.setImageResource(R.drawable.whynotpng);
break;
case R.id.button4:
imageView1.setImageResource(R.drawable.ic_launcher);
imageView2.setImageResource(R.drawable.apple_gray_logo);
break;
}
}
}
Note: You dont have to find the ids of buttons in this case.
Related
I can't understand why this simple code is not working.
This code suppose to set the color of the button's text to blue, that's all.
I only added to a new created project the lines :
final Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Perform action on click
btn.setTextColor(Color.BLUE);
}
});
The full code :
package com.example.b;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Perform action on click
btn.setTextColor(Color.BLUE);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Most likely the button is in the fragment and not the activity. If so, move your findViewById and your setonclick code into your PlaceHolderFragment.
with the new ADT from android, the new way of designing layout is using fragment layer.
My problem today however isn't really related to fragment layer. It is in regards to
error opening trace file no such file or directory
Here's my MainActivity code:
package com.skws.calcutil;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
final TextView display = (TextView) findViewById(R.id.display);
Button btn0 = (Button) findViewById(R.id.btn0);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
Button btn3 = (Button) findViewById(R.id.btn3);
Button btn4 = (Button) findViewById(R.id.btn4);
Button btn5 = (Button) findViewById(R.id.btn5);
Button btn6 = (Button) findViewById(R.id.btn6);
Button btn7 = (Button) findViewById(R.id.btn7);
Button btn8 = (Button) findViewById(R.id.btn8);
Button btn9 = (Button) findViewById(R.id.btn9);
Button btnDecimal = (Button) findViewById(R.id.btnDecimal);
Button btnAdd = (Button) findViewById(R.id.btnAdd);
Button btnMinus = (Button) findViewById(R.id.btnMinus);
Button btnMultiply = (Button) findViewById(R.id.btnMultiply);
Button btnDivide = (Button) findViewById(R.id.btnDivide);
Button btnEqual = (Button) findViewById(R.id.btnEqual);
btn0.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
display.setText("0");
}
});
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
The problem is if I removed this section of the code:
btn0.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
display.setText("0");
}
});
This will only load my Layout, which works perfectly. If I add the above code into MainActivity, I'll get that Error.
Is there a new way google wanted us to design with Button Clicks?
I've tried re-installing everything and that didn't work.
I've also tried to sync my versions and that didn't work.
I've tried to remove the version and add in the SD card permission trick and that didn't work.
I've also tried to add an virtual sd space and that didn't work.
I've tried my code on my mobile phone and I get the same results.
Any help will be grateful.
Thank you
Ok, I've found the solution.
After comparing with my old codes, it seems that the problem is with the extension of ActionBarActivity.
Once I've moved all the layout into activity_main.xml, and in the Main_Activity.java changing the ActionBarActivity into Activity, it is working again.
Guess that just means that I need to be updated on how to code with Fragments.
I tried to run this simple code on emulator but it gives error right at the beginning. Helps appreciated. It gives a fatal error in logcat. It says "application has stopped". I get these errors:
"03-22 17:43:53.315: E/AndroidRuntime(1283): Process: com.example.deneme, PID: 1283"
"03-22 17:43:53.315: E/AndroidRuntime(1283): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.deneme/com.example.deneme.MainActivity}: java.lang.NullPointerException"
package com.example.deneme;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends Activity {
final Button button = (Button) findViewById(R.id.button1);
EditText txt = (EditText) findViewById (R.id.editText1);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txt.setText("murat");
}
});
// if (savedInstanceState == null) {
// getFragmentManager().beginTransaction()
// .add(R.id.container, new PlaceholderFragment())
// .commit();
// }
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
You are assigning values to button and txt before the view has been created in onCreate(). You shouldn't assign them until after the call to setContentView(R.layout.activity_main).
You need to put button and textview in onCreate.
Try the following
public class MainActivity extends Activity {
final Button button; // Don't call find view by ID here;
EditText txt; // Don't call find view by ID here;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (EditText) findViewById (R.id.editText1); // Instead call it after setContentView
button = (Button) findViewById(R.id.button1); // Instead call it after setContentView
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txt.setText("murat");
}
});
// if (savedInstanceState == null) {
// getFragmentManager().beginTransaction()
// .add(R.id.container, new PlaceholderFragment())
// .commit();
// }
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
These lines will give you errors:
final Button button = (Button) findViewById(R.id.button1);
EditText txt = (EditText) findViewById (R.id.editText1);
You cannot initialize the Button or the EditText outside of any methods because the layout has not been set. You can, however, declare them outside of any methods and then initialize them inside of your onCreate method just after you have set the layout (the view). Like so:
// declare them at a class level
Button button;
EditText txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize them here
button = (Button) findViewById(R.id.button1);
txt = (EditText) findViewById (R.id.editText1);
button.setOnClickListener(new View.OnClickListener() {
...
Then button cannot be final, though.
I can't find a solution for my problem! I keep getting the error:
onClickListener cannot be resolved to a
type
Please Help!! Here is my code
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements onClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = (Button) findViewById(R.id.button1);
send.setOnClickListener(this);
}
#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;
}
Try adding this method according to your aim:
#Override
public void onClick(View view)
{
//....
//....
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
finish();
}
onClickListener should be like this OnClickListener
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = (Button) findViewById(R.id.button1);
send.setOnClickListener(this);
}
#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;
}
#Override
public void onClick(View v) {
}
}
Create a method like addOnClickListener();
and work on it like:
public void addOnActionListener(){
Button send=(Button)findViewById(R.id.button1);
send.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//...
//...
I am new to android and I tried to implement onclicklistener on image view. But it's not working.. Please help. When I click on the image it dose not responds.
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
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);
ImageView ad = (ImageView) findViewById(R.id.imageView1);
ad.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, ads.class));
}
});
return true;
}
}
this is my code...
I think you should declare your ImageView in onCreate() as below:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView ad = (ImageView) findViewById(R.id.imageView1);
ad.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, ads.class));
}
});
}
You have two problems
First:
startActivity(new Intent(MainActivity.this, ads.class));
The second argument should be an activity
Second:
In your case, the ImageView and the listener should be in your onCreate()
When I had a similar issue, I got it fixed it by adding android:layout_width="44dp" to make sure user would have enough area to click on. Then, you can align image content the way you wannt. In my case:
<ImageView
android:id="#+id/locationImageView"
android:layout_width="44dp"
android:layout_height="16dp"
android:layout_centerVertical="true"
android:layout_marginStart="4dp"
android:layout_toEndOf="#id/locationText"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:src="#drawable/icn_edit_small_white"/>