Android, screwy behavior - android

Cant figure this out (am a noob) but I have a default activity that starts with just one image button, clicking that image button opens the second activity that contains a bunch of image buttons but only one has an onclick, this is the exact code:
<ImageButton android:src="#drawable/level1"
android:layout_width="wrap_content" android:id="#+id/imageButton1"
android:layout_height="wrap_content"
android:onClick="button_clicked1">
</ImageButton>
in the java file this is my code:
public void button_clicked1(View v) {
//text1.setText("clicked");
//ib2.setImageResource(R.drawable.level5);
}
The screwy part, if that above function is empty nothing happens but no matter what I put in the above function... for example even a simple settext like the above commented one, and it force closes on me :((
the full java file if you are interested: http://pastebin.com/W4sJUKXH
and the manifest file (as the problem might be there, like I said, am a noobie) http://pastebin.com/yEuG1su7
** Where i think the error is:
In the manifest file I only have declared the second activity, nothing else...

Set id for the ImageViews, this is how you handle many Buttons with one method
public void button_clicked1(View v) {
switch(v.getId())
{
case R.id.myfirstimage:
//do something
break;
case R.id.mysecondimage:
// do something
break;
// add more cases
default:
// do something if none of the cases is your image view or do nothing
}
}
Set setContentView( ) before you do anything with the UI components. (Thats why you get some Exception)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_levels_screen);
text1 = (TextView) findViewById(R.id.textView1);
ib2 = (ImageButton) findViewById(R.id.imageButton2);
}

Related

Android: onClick causing me to go back an Activity

I am trying to build a dynamic UI, but when I add the onClick method to the button whenever I push the button I go back to my previous activity. Any ideas on how to fix it?
my button's code: (the addMenu method is never run in the activities class)
<Button
android:text="New Menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/newButton"
android:layout_weight="1"
android:onClick="addMenu"/>
here is my addmenu code although no matter what goes in here(even if nothing at all) it still won't work
public void addMenu()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.backLayer);
Button newButton = new Button(this);
newButton.setText("menu "+menu);
layout.addView(newButton);
menu++;
}
whenever I push the button I go back to my previous activity.
Sounds like your app is crashing and restarting... read the logcat, and you'd see something along the lines that your method signature is wrong.
android:onClick="addMenu" needs a method of public void addMenu(View v).
Or just use Java to set the button listener and remove android:onClick.
findViewById(R.id.newButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addMenu();
}
}
try this
/**
* #param v android:id="#+id/newButton"
*/
public void addMenu(View v)
{
LinearLayout layout = (LinearLayout) findViewById(R.id.backLayer);
Button newButton = new Button(this);
newButton.setText("menu "+menu);
layout.addView(newButton);
menu++;
}

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.

Android - In a button On click event, button goes missing

I am trying to change the text color of button on click event. But while the button click event is fired, button goes missing. Code mentioned below.
Button design in Layout XML file
<Button
android:id="#+id/btnCategory1"
android:background="#000000"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:layout_margin="10dp"
>
</Button>
Activity.java file
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_expense);
btnType1 = (Button)findViewById(R.id.btnCategory1);
btnType1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == (View)btnType1)
{
btnType1.setTextColor(R.color.darkorange);
}
}
Tried the below Option also. Still Button Goes missing. Log statement is fired successfully.
btnType1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.v("AAAAAAAAAAA","BBBBBBBBBBB");
// TODO Auto-generated method stub
btnType1.setTextColor(R.color.orange);
}
});
If someone can find the reason, kindly share it.
You cannot use just the R.color integer when calling setTextColor. You need to call getResources().getColor(R.color.YOURCOLOR) to set a color properly.
Make your button as below
Button bOne = (Button) findViewById(R.id.btnCategory1);
bOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
bOne.setTextColor(getResources().getColor(R.color.YOURCOLOR));
}
});
Hmmmmm. I'm not seeing a good reason as to why that would happen.
I do think that there is maybe a better/cleaner way to do something so simple, and so I'll tell you it - go ahead and try it. This was should work.
Get rid of the btnType1.setOnClickListener(this); line from your java.
Then, go into your xml and add this to your button:
android:onClick="methodName"
Now, if you go into your java and create a method called methodName that takes a view as an argument:
public void methodName(View v) {
btnType1.setTextColor(R.color.darkorgange);
}
The color should be updated!
EDIT: Just looked again and the reason the previous code wasn't working was because you were trying to update btnType2, not btnType1. Still, try the method I just gave you. It is good practie, and a cleaner and easier way to do things for the future.
EDIT2:
All right, the mystery is solved. Here is your issue. When you do set color, you need to pass in the actual color, not just the id. Here is what you need to change that line to:
btnType1.setTextColor(getResources().getColor(R.color.darkorange));

Android calculator buttons

I am trying to make a calculator for Android. Here is the code for my buttons:
int[] button_ids = {
R.id.BtnNum0, R.id.BtnNum1, R.id.BtnNum2, R.id.BtnNum3, R.id.BtnNum4, R.id.BtnNum5, R.id.BtnNum6,
R.id.BtnNum7, R.id.BtnNum8, R.id.BtnNum9, R.id.BtnAdd, R.id.BtnSub, R.id.BtnDiv, R.id.BtnMult,
R.id.BtnClear, R.id.equals
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditTextValue = (EditText) findViewById(R.id.editText1);
TVValue = (TextView) findViewById(R.id.textView1);
buttons = new ArrayList<Button>();
for(int id : button_ids) {
Button button = (Button)findViewById(id);
button.setOnClickListener(this);
buttons.add(button);
}
}
How I can change this part to a block of code where I won't have to declare the IDs of the buttons? (e.g. R.id.BtnNum0)
int[] button_ids = {
R.id.BtnNum0, R.id.BtnNum1, R.id.BtnNum2, R.id.BtnNum3, R.id.BtnNum4, R.id.BtnNum5, R.id.BtnNum6,
R.id.BtnNum7, R.id.BtnNum8, R.id.BtnNum9, R.id.BtnAdd, R.id.BtnSub, R.id.BtnDiv, R.id.BtnMult,
R.id.BtnClear, R.id.equals
};
I have been searching for an answer, but I still can't find a solution.
What you can do, since this code seems to only set a single OnClickListener for all Buttons, is to do it in xml
For each Button set
android:onClick="functionName"
then in your code you can do away with all of the id's and your for loop. In Java just create a function like
public void functionName(View v)
{
switch (v.getId())
{
case R.id.buttonId:
// do work for this Button
break;
...
}
The way you are doing it is fine but this is how I prefer to handle this situation. You just have to give all of the Buttons the same function name in xml then use that name as your function name in Java. You also just have to be sure to declare the function with a void return type and make sure it takes a View as its one and only parameter as in my example.
The Button Docs also have an example of this
in your layout file add this to every button
<Button
...
android:onClick="btnClicked"
.../>
then in your code add this method and check for each button in this method
public void btnClicked(View v)
{
switch(v.getId())
{
case R.id.BtnNum0:
// your code
break;
....
}
}
That is likely the best solution unfortunately, unless you use some sort of annotation framework which still doesn't cut down much on the boilerplate.
edit:
You could try to get a pointer to whatever ViewGroup is holding the Button views and then getting all of its children, and then looping through them while casting them to Buttons as you go.
For example: If your Button objects in XML are housed in a LinearLayout, you could get the pointer to that and do something like this:
for(int i=0; i < ((ViewGroup)v).getChildCount(); ++i) {
Button nextChild = (Button) ((ViewGroup)v).getChildAt(i);
}
Of course, I recommend against this, but it is still a possibility.
As trevor-e suggested, you can give an annotation processor a try. Android Annotations can simplify your code to:
#Click
public void BtnNum0() {
// Button 0 clicked
}
#Click
public void BtnNum1() {
// Button 1 clicked
}
// etc.
If you go this route, please do try to use names following the Java convention as the button names correspond with function names.

Detect which child is clicked on click of the parent

I have a layout which contains lots of images. What I have to do is when an image is clicked, I have to show its details. But I don't want to have onClickListeners for all the images. How can I achieve this?
You don't have to have different handlers for all the images. Instead use one handler for all the images. This would make your code cleaner, manageable and solve your problem too.
public void onCreate(Bundle bundle) {
//...
OnClickListener mHandler = new OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.img1:
//..
break;
case R.id.img2:
//....
break;
}
}
};
ImageButton btn1 = (ImageButton)findViewById(R.id.img1);
ImageButton btn2 = (ImageButton)findViewById(R.id.img2);
//...
btn1.SetOnClickListener(mHandler);
btn2.SetOnClickListener(mHandler);
//...
}
One Listener to rule them all.
Implement onClick() on an object, register it as listener
In onClick(), examine the View object passed as parameter to determine which of the images was clicked. You can do anything from getId() to casting it to (ImageView) and getting the actual image out.
Once you know which image was clicked, do what you will with it.
If you're looking to implement custom behavior for an ImageView (or whatever), and then have multiple instances of that type of view, you should subclass the ImageView and put your listener in there. Then you've got an encapsulated View that implements the custom behavior you want, and if you decide later that you want more or less or them, or to put them in another place, it's easy to move the View and its behavior without ripping apart your Activity.

Categories

Resources