I have made a vertical linear layout made up of 6 horizontal linear layouts. Each horizontal layout contains 10+ buttons on a scrollview. Below is the main linear layout with one of the horizontal layouts included.
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="75dp">
<Button
android:id="#+id/button1"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button1"/>
<Button
android:id="#+id/button2"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button2"/>
<Button
android:id="#+id/button3"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button3"
android:text="" />
<Button
android:id="#+id/button4"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button4"
android:text="" />
<Button
android:id="#+id/button5"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button5"
android:text="" />
<Button
android:id="#+id/button6"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button6"
android:text="" />
<Button
android:id="#+id/button7"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button7"
android:text="" />
<Button
android:id="#+id/button8"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button8"
android:text=""
android:textSize="" />
<Button
android:id="#+id/button9"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button9"
android:text="" />
<Button
android:id="#+id/bCaitlyn"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/caitlynsplash"
android:text="" />
<Button
android:id="#+id/button10"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button10"
android:text=""
android:textSize="10.5dp" />
<Button
android:id="#+id/button11"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button11"
android:text=""
android:textSize="11dp" />
<Button
android:id="#+id/button12"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button12"
android:text="" />
<Button
android:id="#+id/button13"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button13"
android:text=""
android:textSize="11dp" />
</LinearLayout>
</HorizontalScrollView>
There seems to be a problem with the onClick command not executing on press. The button is recognizing the touch(as it changes colors) however nothing seems to happen.
package com.LeagueProject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Project extends Activity implements View.OnClickListener{
Button Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8,
Button9, Button10, Button11, Button12, Button13;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
projectbuttons();
Button1.setOnClickListener(this);
Button2.setOnClickListener(this);
Button3.setOnClickListener(this);
}
// Assigned Buttons
private void projectbuttons() {
Button1 = (Button) findViewById(R.id.button1);
Button2 = (Button) findViewById(R.id.button2);
Button3 = (Button) findViewById(R.id.button3);
Button4 = (Button) findViewById(R.id.button4);
Button5 = (Button) findViewById(R.id.button5);
Button6 = (Button) findViewById(R.id.button6);
Button7 = (Button) findViewById(R.id.button7);
Button8 = (Button) findViewById(R.id.button8);
Button9 = (Button) findViewById(R.id.button9);
Button10 = (Button) findViewById(R.id.button10);
Button11 = (Button) findViewById(R.id.button11);
Button12 = (Button) findViewById(R.id.button12);
Button13 = (Button) findViewById(R.id.button13);
}
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.button1:
setContentView(R.layout.screen1);
break;
case R.id.button2:
setContentView(R.layout.screen2);
break;
case R.id.button3:
setContentView(R.layout.screen3);
break;
}
}
}
Any insight as well as tips/techniques are highly welcomed.
When you implements View.OnClickListener in your Activity then you have to
Override the onClick() method else it never executes..
#Override
public void onClick(View v) {
.
.
.
}
i tried your code using default buttons without giving any images to button and it is working fine... it goes to the corresponding layout....
I'm not sure if this is what you need, but you can define the onClick() method within the layout file itself.
<Button
android:id="#+id/button1"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button1"
android:onClick="onClick"
/>
Can you try this?
Related
I have built a calculator app which uses EditTexts for you to insert the two values, and then I also have 4 Buttons each with an operation (eg.+,-,/,x). It seems though as if the buttons aren't doing its function assigned to it, No value is being sent to the TextView. Could you please help me in making those buttons do its operation?
Here is my code below:
package com.example.user.zidcalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int enter1;
int enter2;
int newHeight;
EditText et1;
EditText et2;
TextView tv;
Button bt1;
Button bt2;
Button bt3;
Button bt4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.edittext1);
et2 = (EditText) findViewById(R.id.edittext2);
tv = (TextView) findViewById(R.id.tvans);
bt1 = (Button) findViewById(R.id.button);
bt2 = (Button) findViewById(R.id.button2);
bt3 = (Button) findViewById(R.id.button3);
bt4 = (Button) findViewById(R.id.button4);
newHeight = et2.getHeight();
tv.setHeight(newHeight);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enter1 = Integer.parseInt(et1.getText().toString());
enter2 = Integer.parseInt(et2.getText().toString());
int answer = enter1 + enter2;
tv.setText(String.valueOf(answer));
}
});
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enter1 = Integer.parseInt(et1.getText().toString());
enter2 = Integer.parseInt(et2.getText().toString());
int answer = enter1 - enter2;
tv.setText(String.valueOf(answer));
}
});
bt3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enter1 = Integer.parseInt(et1.getText().toString());
enter2 = Integer.parseInt(et2.getText().toString());
int answer = enter1 * enter2;
tv.setText(String.valueOf(answer));
}
});
bt4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enter1 = Integer.parseInt(et1.getText().toString());
enter2 = Integer.parseInt(et2.getText().toString());
int answer = enter1 / enter2;
tv.setText(String.valueOf(answer));
}
});
}
}
Layout File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter two numbers below and perform an operation to see the output"
android:gravity="center_horizontal"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter first number here -->"
android:textSize="18sp" />
<EditText
android:id="#+id/edittext1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:inputType="numberSigned"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter second number here -->"
android:textSize="18sp" />
<EditText
android:id="#+id/edittext2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:inputType="numberSigned"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer -->"
android:textSize="18sp"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="#+id/tvans"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<Button
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button"
android:layout_centerHorizontal="true"
android:id="#+id/button2" />
<Button
android:text="x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_alignEnd="#+id/button"
android:layout_marginTop="39dp"
android:id="#+id/button3" />
<Button
android:text="/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button3"
android:layout_alignStart="#+id/button2"
android:id="#+id/button4" />
</RelativeLayout>
When you getHeight of et2, it haven't text, so it height is 0. Then, you set height 0 for tv, you can't see it in screen althought it has text.
Try to run the code giving a constant height and width to answerTextView,tvans in your code(for eg: 30 DP height and 30 DP width).
This issue is not related to button. This is due to the invisibility of textView due to 0 height and width
<TextView
android:id="#+id/tvans"
android:layout_width="30dp"
android:layout_height="30dp"
android:gravity="center_horizontal"/>
I removed the Height variable newHeight and everything associated with it.
Currently trying to get my simple Android Calculator App working. When I try to run on an emulator I get:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
I'm also having an issue returning the result to the textview. I've tried tvResult.setText(result); but it isn't allowing me to pass in a float to the text.
My Java:
package com.example.justin.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import javax.xml.transform.Result;
public class MainActivity extends Activity implements OnClickListener {
EditText digit1;
EditText digit2;
TextView tvResult;
Button nine, eight, seven, six, five, four, three, two, one, zero, decimal, plus, minus, divide, multiply, equals;
String s = "0";
char enterDigit = ' ';
String enterOperation;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//button elements
nine = (Button) findViewById(R.id.button9);
eight = (Button) findViewById(R.id.button8);
seven = (Button) findViewById(R.id.button7);
six = (Button) findViewById(R.id.button6);
five = (Button) findViewById(R.id.button5);
four = (Button) findViewById(R.id.button4);
three = (Button) findViewById(R.id.button3);
two = (Button) findViewById(R.id.button2);
one = (Button) findViewById(R.id.button1);
zero = (Button) findViewById(R.id.button0);
decimal = (Button) findViewById(R.id.buttondecimal);
plus = (Button) findViewById(R.id.buttonadd);
minus = (Button) findViewById(R.id.buttonminus);
divide = (Button) findViewById(R.id.buttondivide);
multiply = (Button) findViewById(R.id.buttonmultiply);
equals = (Button) findViewById(R.id.buttonequals);
tvResult = (TextView) findViewById(R.id.tvResult);
//set listeners
nine.setOnClickListener(this);
eight.setOnClickListener(this);
seven.setOnClickListener(this);
six.setOnClickListener(this);
five.setOnClickListener(this);
four.setOnClickListener(this);
three.setOnClickListener(this);
two.setOnClickListener(this);
one.setOnClickListener(this);
decimal.setOnClickListener(this);
plus.setOnClickListener(this);
minus.setOnClickListener(this);
divide.setOnClickListener(this);
multiply.setOnClickListener(this);
equals.setOnClickListener(this);
tvResult.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
float num1 = 0;
float num2 = 0;
float result = 0;
num1 = Float.parseFloat(digit1.getText().toString());
num2 = Float.parseFloat(digit2.getText().toString());
switch (v.getId()) {
case R.id.buttonadd:
enterOperation = "+";
result = num1 + num2;
break;
case R.id.buttonminus:
enterOperation = "-";
result = num1 - num2;
break;
case R.id.buttonmultiply:
enterOperation = "*";
result = num1 * num2;
break;
case R.id.buttondivide:
enterOperation = "/";
result = num1 / num2;
break;
default:
break;
}
}
}
My XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="0"
android:gravity="right"
android:textSize="50sp"/>"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:onClick="enterDigit">
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="7"
android:onClick="enterDigit"/>
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="8"
android:onClick="enterDigit"/>
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="9"
android:onClick="enterDigit"/>"
<Button
android:id="#+id/buttondivide"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="/"
android:onClick="enterOperation"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="4"
android:onClick="enterDigit"/>
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="5"
android:onClick="enterDigit"/>
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="6"
android:onClick="enterDigit"/>
<Button
android:id="#+id/buttonmultiply"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="*"
android:onClick="enterOperation"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1"
android:onClick="enterDigit"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2"
android:onClick="enterDigit"/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="3"
android:onClick="enterDigit"/>
<Button
android:id="#+id/buttonminus"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="-"
android:onClick="enterOperation"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/button0"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="0"
android:onClick="enterDigit"/>
<Button
android:id="#+id/buttondecimal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="." />
<Button
android:id="#+id/buttonequals"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="="
android:onClick="calculate"/>
<Button
android:id="#+id/buttonadd"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="+"
android:onClick="enterOperation"/>
</LinearLayout>
</LinearLayout>
Your EditText widgets do not exist in your xml. They need to be added in your main LinearLayout somewhere. Then, in your onCreate() method, you need to set them up via the findViewById(R.id.digit1)... Also, get rid of the onClick attribute in the LinearLayout; because your linearLayout is not clickable, it is useless.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal>
<EditText
android:id="#+id/digit1"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1" />
<EditText
android:id="#+id/digit2"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:id="#+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="0"
android:gravity="right"
android:textSize="50sp"/>"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="7"
android:onClick="enterDigit"/>
...
then, in your activity...
#Override
public void onCreate(Bundle sIS){
super.onCreate(sIS);
digit1 = (EditText)findViewById(R.id.digit1);
digit2 = (EditText)findViewById(R.id.digit2);
...
}
you need to init your EditText (digit1 and digit1) firstlg as your buttons do, if not it will lead to NullPointException just like your proplem
I have made a very simple android program with help of 3 imagebuttons(circle) ,Now i want is after some clicks on that partcular buttons its background images should be changed..i have tried the code as below but its not working:
main.java
package com.esp.therisemethod.ui;
import com.esp.therisemethod.R;
import com.esp.therisemethod.uc.Header;
import com.esp.therisemethod.uc.Menu;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class ProgressActivity extends Activity implements OnClickListener {
private ImageView resetall, undo, b1, b2, b3;
int cnt1,cnt2,cnt3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
b1 = (ImageView) findViewById(R.id.b1);
b2 = (ImageView) findViewById(R.id.b2);
b3 = (ImageView) findViewById(R.id.b3);
resetall = (ImageView) findViewById(R.id.reset_all);
undo = (ImageView) findViewById(R.id.undo);
setContentView(R.layout.activity_progress);
Header header =(Header)findViewById(R.id.header);
header = (Header) findViewById(R.id.header);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
header.btn_bak.setVisibility(View.VISIBLE);
header.btn_bak.setVisibility(View.VISIBLE);
Menu menu =(Menu)findViewById(R.id.prog_footer);
menu.setSelectedTab(1);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b1:
cnt1++;
if(cnt1>1 && cnt1<=5)
{
b1.setBackgroundResource(R.drawable.circle_red);
}
else if (cnt1>=6 && cnt1<10) {
b1.setBackgroundResource(R.drawable.circle_orange);
}
else{
b1.setBackgroundResource(R.drawable.circle_green);
}
break;
case R.id.b2:
cnt2++;
if(cnt2<=5)
{
b2.setImageResource(R.drawable.circle_red);
}
else if (cnt2>=6 && cnt2<10) {
b2.setImageResource(R.drawable.circle_orange);
}
else{
b2.setImageResource(R.drawable.circle_green);
}
break;
case R.id.b3:
cnt3++;
if(cnt3<=5)
{
b2.setImageResource(R.drawable.circle_red);
}
else if (cnt3>=6 && cnt3<10) {
b2.setImageResource(R.drawable.circle_orange);
}
else{
b2.setImageResource(R.drawable.circle_green);
}
break;
case R.id.reset_all:
break;
case R.id.undo:
break;
}
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.esp.therisemethod.uc.Header
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_below="#+id/header"
android:layout_above="#+id/prog_footer">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/linearp1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:orientation="vertical" >
<com.esp.therisemethod.uc.EspTextView
android:id="#+id/client_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:tag="800"
android:text="Welcome Back SAM!"
android:textSize="25dp"
android:textStyle="bold" />
<com.esp.therisemethod.uc.EspTextView
android:id="#+id/make_choice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:tag="800"
android:text="Please make your choice:"
android:textSize="20dp" />
</LinearLayout>
<RelativeLayout
android:id="#+id/ralativep1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearp1"
android:layout_centerVertical="true"
android:layout_marginTop="10dp" >
<ImageView
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:src="#drawable/cir_grey1" />
<ImageView
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:src="#drawable/cir_grey1" />
</RelativeLayout>
<ImageView
android:id="#+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ralativep1"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:src="#drawable/cir_grey1" />
<LinearLayout
android:id="#+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/b3"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="30dp"
android:weightSum="2" >
<Button
android:id="#+id/reset_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="#drawable/btn_med"
android:text="Reset all"
android:textColor="#ffffff" />
<Button
android:id="#+id/undo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="#drawable/btn_med"
android:text="Undo"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<com.esp.therisemethod.uc.Menu
android:id="#+id/prog_footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
If you have set src attribute in xml, then to update UI use setImageResource()
If you have set background attribute in xml then to update UI use setImageDrawable()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//First set content view..
setContentView(R.layout.activity_progress);
b1 = (ImageView) findViewById(R.id.b1);
b2 = (ImageView) findViewById(R.id.b2);
b3 = (ImageView) findViewById(R.id.b3);
resetall = (ImageView) findViewById(R.id.reset_all);
undo = (ImageView) findViewById(R.id.undo);
Header header =(Header)findViewById(R.id.header);
If Your onClickListener works correct, in Your case You have to change the source not background, for example:
b2.setImageResource(R.drawable.yourImage);
I've followed a tutorial to realise a calculator. It seems that my buttons objects that i define, and instantiate with the ID are not instantiated because when i add a listener on them, they return a null pointer exception.
here's the java code:
package com.example.calculette;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
//Variables declaration
Button b0;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
Button b9;
Button bC;
Button bCE;
Button bEgal;
Button bPlus;
Button bMoins;
Button bDiviser;
Button bMultiplier;
EditText saisie;
private double number1 = 0;
private boolean clicOperateur = false;
private boolean update = true;
private String operateur = "";
//onCreate is the first procedure called by the activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b0 = (Button) findViewById(R.id.button0);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
b5 = (Button) findViewById(R.id.button5);
b6 = (Button) findViewById(R.id.button6);
b7 = (Button) findViewById(R.id.button7);
b8 = (Button) findViewById(R.id.button8);
b9 = (Button) findViewById(R.id.button9);
bC = (Button) findViewById(R.id.buttonC);
bCE = (Button) findViewById(R.id.buttonCE);
bEgal = (Button) findViewById(R.id.buttonEgal);
bPlus = (Button) findViewById(R.id.buttonPlus);
bMoins = (Button) findViewById(R.id.buttonMoins);
bDiviser = (Button) findViewById(R.id.buttonDiviser);
bMultiplier = (Button) findViewById(R.id.buttonMultiplier);
saisie = (EditText) findViewById(R.id.EditText01);
//Listeners declaration
b0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
numberClick("0");
}
});
(i don't link the whole code because it's still the same for the 9 buttons and after it's simple methods).
here you can see the XML associated:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="3">
<EditText android:id="#+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="80px"
android:textSize="20px"
android:editable="false"
android:cursorVisible="false"
/>
<View
android:layout_height="3dip"
android:background="#FF999999"
/>
<TableRow>
<Button android:id="#+id/buttonCE"
android:text="CE"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="2"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/buttonC"
android:text="C"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="2"
style="#style/button_text"
android:background="#drawable/button"
/>
</TableRow>
<TableRow>
<Button android:id="#+id/button7"
android:text="7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/button8"
android:text="8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/button9"
android:text="9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/buttonPlus"
android:text="+"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
</TableRow>
<TableRow>
<Button android:id="#+id/button4"
android:text="4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/button5"
android:text="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/button6"
android:text="6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/buttonMoins"
android:text="-"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
</TableRow>
<TableRow>
<Button android:id="#+id/button1"
android:text="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/button2"
android:text="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/button3"
android:text="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/buttonMultiplier"
android:text="*"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
</TableRow>
<TableRow>
<Button android:id="#+id/button0"
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/buttonPoint"
android:text="."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/buttonEgal"
android:text="="
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/buttonDiviser"
android:text="/"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
</TableRow>
</TableLayout>
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="#drawable/logosafran"
android:id="#+id/imageView1"
></ImageView>
</LinearLayout>
The logcat says that i have a nullPointerException on the first listener declaration with b0.
Thank you guys!
The problem is that in your activity you're trying to inflate the activity_main.xml file which obviously doesnt'hold your button declaration. You told that your buttons are defined into a button_classical.xml file.
You would have to change
setContentView(R.layout.activity_main);
in
setContentView(R.layout.button_classical);
Notice I edited the XML File!!!!
I got an Activity that create a button and when you click on the button it calls to another Activity but when i run the app it's shows just a blank page.
Activity:
package com.elichai.tfillin;
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 {
public void OnCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
private void displayCamera() {
Intent i = new Intent(this, CameraActivity.class);
startActivityForResult(i, 0);
}
public void addListenerOnButton() {
final Button button1 = (Button) findViewById(R.id.button1);
//Button button2 = (Button) findViewById(R.id.button2);
//Button button3 = (Button) findViewById(R.id.button3);
//Button button4 = (Button) findViewById(R.id.button4);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
displayCamera();
//finish();
}
});
}
}
And here is the activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_marginLeft="7dp"
android:text="#string/button" />
<Button
android:id="#+id/button2"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_marginLeft="182dp"
android:text="#string/button" />
<Button
android:id="#+id/button3"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="260dp"
android:text="#string/button" />
<Button
android:id="#+id/button4"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_marginLeft="182dp"
android:layout_marginTop="260dp"
android:text="#string/button" />
<TextView
android:id="#+id/textView1"
android:layout_width="223dp"
android:layout_height="52dp"
android:layout_marginLeft="85dp"
android:layout_marginTop="215dp"
android:text="#string/switch_cam"
android:textSize="30sp" />
</LinearLayout>
Thanks!
There is nothing wrong with the layout. In your MainActivity class, the onCreate() method signature is written incorrectly. Use onCreate() instead of OnCreate(). (small 'o')
The system calls onCreate() when creating your activity.
Replace the FrameLayout with a vertical LinearLayout
Try using A RelativeLayout in place of FrameLayout unless there was relay a need of using it.
or if you want to use FrameLayout try this
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<Button
android:id="#+id/button1"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_gravity="top"
android:layout_marginLeft="7dp"
android:text="#string/button" />
<Button
android:id="#+id/button2"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_gravity="top"
android:layout_marginLeft="182dp"
android:text="#string/button" />
<Button
android:id="#+id/button3"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_gravity="top"
android:layout_marginLeft="7dp"
android:layout_marginTop="260dp"
android:text="#string/button" />
<Button
android:id="#+id/button4"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_gravity="top"
android:layout_marginLeft="182dp"
android:layout_marginTop="260dp"
android:text="#string/button" />
<TextView
android:id="#+id/textView1"
android:layout_width="223dp"
android:layout_height="52dp"
android:layout_gravity="top"
android:layout_marginLeft="85dp"
android:layout_marginTop="215dp"
android:text="switchcam"
android:textSize="30sp" />
</FrameLayout>
layout_ margins does not work in FrameLayout until you also set your subviews layout gravity e.g.
android:layout_gravity="top"
( for framelayout)