I'm a beginner in Android. Just started working on a Calculator app and don't know why the app keeps crashing all the time.
I have Two Activities... MainActivity and CalculatorActivity
MainActivity:
public class MainActivity extends AppCompatActivity
{
Button calcubtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calcubtn = (Button) findViewById(R.id.calculatorbtn);
calcubtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent calculator = new Intent(MainActivity.this, CalculatorActivity.class);
startActivity(calculator);
}
});
}
}
MainActivity Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rfkha.myfirstapp.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:textSize="27sp"
android:textColor="#android:color/holo_red_light"
android:text="#string/functions_text" />
<Button
android:id="#+id/calculatorbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView"
android:layout_marginStart="43dp"
android:layout_marginTop="70dp"
android:text="#string/calcu_string"
/>
</RelativeLayout>
CalculatorActivity: (i.e child activity)
Here in the activity class, I've added implements View.OnclickListener . I found it in stackoverflow somewhere. I think the problem is from this thing. Also it required to used the keyword abstractto use it with the activity class, so i used that too.
public abstract class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, zerobtn, dotbtn, equalbtn, minusbtn, sumbtn, divbtn, mulbtn;
double num1, num2;
boolean sum, minus, div, mul;
EditText textarea= (EditText) findViewById(R.id.textarea);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
(((Same Code for buttons upto 9 and SUM, MINUS, DIVISION and MULtiplication Buttons)))
}
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btn1: {
textarea.setText(textarea.getText()+"1");
break;
}
case R.id.btn2: {
textarea.setText(textarea.getText()+"2");
break;
}
case R.id.btn3:
((((( And So on... Upto Button 9 ))))))
case R.id.zerobtn: {
textarea.setText(textarea.getText()+"0");
break;
}
case R.id.dotbtn: {
textarea.setText(textarea.getText()+".");
break;
}
case R.id.sumbtn: {
num1=Double.parseDouble(textarea.getText().toString());
sum=true;
textarea.setText(null);
break;
}
(((((Likewise for Minus, Division and Multiplication buttons))))))))))
case R.id.equalbtn:
{
if (num1!=0)
{
num2=Double.parseDouble(textarea.getText().toString());
if (sum) {
textarea.setText(num1+num2+"");
sum=false;
}
else if(mul) {
textarea.setText(num1*num2+"");
mul=false;
}
else if(div) {
textarea.setText(num1/num2+"");
div=false;
}
else if(minus) {
textarea.setText(num1-num2+"");
minus=false;
}
}
else{
Toast.makeText(getApplicationContext(), "First select a number!!", Toast.LENGTH_SHORT);
}
break;
}
}
}
}
Calculator Activity Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
tools:context="com.example.rfkha.myfirstapp.CalculatorActivity">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="13dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:text="#string/calculatortxt"
android:textSize="25sp" />
<EditText
android:id="#+id/textarea"
android:layout_width="351dp"
android:layout_height="154dp"
android:layout_marginStart="16dp"
android:ems="10"
android:hint="#string/resultsString"
android:textSize="33sp"
android:textAlignment="textEnd"
android:padding="11dp"
android:inputType="textPersonName" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/clearbt"
android:layout_width="293dp"
android:layout_height="51dp"
android:text="#string/cetxt"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/btn7"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="7"
android:textSize="24sp" />
<Button
android:id="#+id/btn8"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="8"
android:textSize="24sp" />
<Button
android:id="#+id/btn9"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="9"
android:textSize="24sp" />
<Button
android:id="#+id/mulbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:layout_marginStart="27dp"
android:text="x"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/btn4"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="4"
android:textSize="24sp" />
<Button
android:id="#+id/btn5"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="5"
android:textSize="24sp" />
<Button
android:id="#+id/btn6"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="6"
android:textSize="24sp" />
<Button
android:id="#+id/minusbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:layout_marginStart="27dp"
android:text="-"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/btn1"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="1"
android:textSize="24sp" />
<Button
android:id="#+id/btn2"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="2"
android:textSize="24sp" />
<Button
android:id="#+id/btn3"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="3"
android:textSize="24sp" />
<Button
android:id="#+id/sumbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:layout_marginStart="27dp"
android:text="+"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/dotbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="."
android:textSize="24sp" />
<Button
android:id="#+id/zerobtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="0"
android:textSize="24sp" />
<Button
android:id="#+id/equalbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="="
android:textSize="24sp" />
<Button
android:id="#+id/divbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:layout_marginStart="27dp"
android:text="/"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
Logcat: (Errors)
Link to Logcat: https://pastebin.com/hbwZVUZg
Delete the abstract keyword. then the line textarea= (EditText) findViewById(R.id.textarea); change for EditText textarea; and last put this on the onCreate() textarea = (EditText)findViewById(R.id.textarea);
Remember that you cannot instantiate an abstract class. here
And check how to debug here
public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, zerobtn, dotbtn, equalbtn, minusbtn, sumbtn, divbtn, mulbtn;
double num1, num2;
boolean sum, minus, div, mul;
EditText textarea;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
textarea = (EditText) findViewById(R.id.textarea);
Put this code in onCreate:
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
textarea = (EditText)findViewById(R.id.textarea);
...
}
Check whether you are using dependencies of same version or not. There should not be any version conflict among dependencies.
Related
Dialog was designed by the XML layer and named layout inside it contains a collection of spinner and TextView and some buttons are called from main activity class now I want to know which one of the spinner inside the
D1 () function
I want to define the elements within the layer layout , How do I do that?
//layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="260dp"
android:layout_height="200dp"
android:orientation="vertical"
android:background="#f71717">
<LinearLayout
android:id="#+id/l_layout"
android:paddingTop="10dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
tools:ignore="ObsoleteLayoutParam">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#ffffff"
android:text="وقت التسليم"
android:textSize="20dp"
tools:ignore="HardcodedText,SpUsage" />
<Spinner
android:id="#+id/tex"
android:layout_width="172dp"
android:paddingRight="40dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:drawSelectorOnTop="true"
android:popupBackground="#c853d7"
style="#style/spinner_style"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry,SpUsage"
android:entries="#array/day_"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/rl"
android:layout_width="250dp"
android:layout_height="129dp"
android:layout_below="#+id/l_layout"
android:background="#f71717"
android:layout_marginTop="0dp"
tools:ignore="ObsoleteLayoutParam">
<TextView
android:id="#+id/text_h"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/spinner_minutes2"
android:paddingLeft="10dp"
android:text="ساعة"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry" />
<TextView
android:id="#+id/text_m"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/spinner_minutes2"
android:layout_alignBottom="#+id/spinner_minutes2"
android:layout_alignStart="#+id/button_holder"
android:paddingLeft="10dp"
android:text="دق"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry" />
<TextView
android:id="#+id/text_pam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:text="ص/م"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry"
android:layout_marginEnd="12dp"
android:layout_alignBaseline="#+id/spinner_minutes"
android:layout_alignBottom="#+id/spinner_minutes"
android:layout_alignEnd="#+id/spinner_minutes3" />
<Spinner
android:id="#+id/spinner_minutes"
android:layout_width="85dip"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
style="#style/spinner_style"
android:layout_alignStart="#+id/spinner_minutes2"
android:entries="#array/fruits" />
<Spinner
android:id="#+id/spinner_minutes2"
android:layout_width="85dip"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner_minutes"
android:layout_marginStart="16dp"
style="#style/spinner_style"
android:layout_toEndOf="#+id/text_m"
android:entries="#array/fruits" />
<Spinner
android:id="#+id/spinner_minutes3"
android:layout_width="85dip"
android:layout_height="wrap_content"
style="#style/spinner_style"
android:paddingRight="20dp"
android:entries="#array/apm"
tools:ignore="RtlHardcoded,RtlSymmetry"
android:layout_alignBaseline="#+id/spinner_minutes2"
android:layout_alignBottom="#+id/spinner_minutes2"
android:layout_toEndOf="#+id/spinner_minutes" />
<TextView
android:id="#+id/text_timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="gone" />
<LinearLayout
android:id="#+id/button_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner_minutes"
android:layout_centerHorizontal="true"
android:paddingTop="10dp"
android:layout_marginTop="20dip">
<Button
android:id="#+id/button_set"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginLeft="10dip"
android:text="Set"
tools:ignore="ButtonStyle,HardcodedText,RtlHardcoded" />
<Button
android:id="#+id/button_cancel"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginRight="10dip"
android:text="Cancel"
tools:ignore="ButtonOrder,ButtonStyle,HardcodedText,RtlHardcoded" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
// class: mainactivity
public class MainActivity extends AppCompatActivity {
Button buttonstartSetDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
buttonstartSetDialog.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
d1();
}});
}
public void d1(){
Dialog about_dlg = new Dialog(MainActivity.this);
about_dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
about_dlg.setContentView(R.layout.layout);
about_dlg.show();
//Spinner spinner = (Spinner) findViewById(R.id.spinner);
}
}
to access to Spinner in your layout :
public void d1(){
Dialog about_dlg = new Dialog(MainActivity.this);
about_dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
about_dlg.setContentView(R.layout.layout);
Spinner sp1 = about_dlg.findViewById(R.id.spinner_minutes);
Spinner sp1 = about_dlg.findViewById(R.id.spinner_minutes);
Spinner sp1 = about_dlg.findViewById(R.id.spinner_minutes);
about_dlg.show();
}
Hello StackOverFlowGeeks!
Currently, I'm learning how to write a basic Android app, that has few buttons.
I have four activities(Java Classes) and four xmls.
I have the RegisterScreen.java that has two buttons(Register and Back button). Currently, I am trying to get back from this RegisterScreen.java to SignInScreen.java. The problem is that when I click on the back button it causes a failure and the app is forced to shut down...
So I've uploaded this way:
1) RegisterScreen.java
2) SignInScreen.java
3) RegisterScreen XML file
4) SignInScreen XML file
Thanks in advance.
RegisterScreen:
public class RegisterScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_screen);
}
public void onClickRegisterButton(View view){
}
public void onClickBackButton(View view) {
Button backBtn = (Button) findViewById(R.id.backBtn);
backBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(RegisterScreen.this, SignInScreen.class);
RegisterScreen.this.startActivity(intent);
}
});
}
}
SignInScreen:
public class SignInScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_screen);
}
#SuppressLint("SetTextI18n")
public void onClickSignInButton(View view){
TextView username = (TextView)findViewById(R.id.username_text);
TextView password = (TextView)findViewById(R.id.password_text);
Button signIn = (Button)findViewById(R.id.sign_in_button);
if (username.getText().toString().equals("Ivan Simeonov") && password.getText().toString().equals("Ivan9603116245")){
signIn.setText("Welcome " + username.getText().toString());
signIn.setBackgroundColor(Color.YELLOW);
setContentView(R.layout.activity_logged_screen);
}else{
signIn.setText("The input data is incorrect! Try again!");
signIn.setBackgroundColor(Color.GREEN);
}
}
public void onClickRegisterButton(View view){
Button register = (Button)findViewById(R.id.register_button);
setContentView(R.layout.activity_register_screen);
}
public void onClickResetPasswordButton(View view){
Button resetPassword = (Button)findViewById(R.id.reset_password_button);
setContentView(R.layout.activity_reset_password_screen);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.megat0n.startproject.RegisterScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView_Register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/register_your_account_by_filling_up_the_following_data"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_username"
android:inputType="textPersonName" />
<EditText
android:id="#+id/first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_firstname"
android:inputType="textPersonName" />
<EditText
android:id="#+id/last_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_lastname"
android:inputType="textPersonName" />
<EditText
android:id="#+id/birth_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_birthdate"
android:inputType="date" />
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_email"
android:inputType="textPersonName" />
<EditText
android:id="#+id/email_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/confirm_email"
android:inputType="textPersonName" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_password"
android:inputType="textPassword" />
<EditText
android:id="#+id/password_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/confirm_password"
android:inputType="textPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/reg_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/register"
android:onClick="onClickRegisterButton"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/backBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/back"
android:onClick="onClickBackButton"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.megat0n.startproject.SignInScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="#string/welcome_to_the_home_screen"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="#+id/username_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/username"
android:inputType="textPersonName"
android:textAlignment="center"
android:textStyle="bold" />
<EditText
android:id="#+id/password_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/password"
android:inputType="textPassword"
android:textAlignment="center"
android:textStyle="bold" />
<Button
android:id="#+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickSignInButton"
android:text="#string/sign_in"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickRegisterButton"
android:text="#string/register"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/reset_password_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickResetPasswordButton"
android:text="#string/reset_password"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Use this onClickBackButton() instead of yours. If you use onClick attribute in your layout, you don't need creating button and click listener.
public void onClickBackButton(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
Try in RegisterScreen use onBackPressed() method:
backBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
I'm trying to make a basic android calculator. when i press button numbers show at edittext but nothing's happining. Thanks for any help.
here is my MainActivity.java`
public class MainActivity extends AppCompatActivity {
private EditText Scr;
private float NumerBf;
private String Operation;
private ButtonClickListener btnClick;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Scr=(EditText)findViewById(R.id.editText3);
Scr.setEnabled(false);
int idList[]={R.id.button0,R.id.button1,R.id.button1,R.id.button2,R.id.button3,R.id.button4,R.id.button5,
R.id.button6,R.id.button7,R.id.button8,R.id.button9,R.id.buttonAdd,R.id.buttonC,R.id.buttonDivide,
R.id.buttonEqu,R.id.buttonMinus,R.id.buttonMultiply};
for(int id:idList){
View v=(View)findViewById(id);
v.setOnClickListener(btnClick);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void mMath(String str){
NumerBf=Float.parseFloat(Scr.getText().toString());
Operation =str;
Scr.setText("0");
}
public void getKeyboard(String str){
String SrcCurrent = Scr.getText().toString();
if(SrcCurrent.equals("0"))
SrcCurrent="";
SrcCurrent +=str;
Scr.setText(SrcCurrent);
}
public void mResult(){
float NumAf=Float.parseFloat(Scr.getText().toString());
float result=0;
if(Operation.equals("+")){
result=NumAf+NumerBf;
}
else if(Operation.equals("-")){
result=NumerBf-NumAf;
}
else if(Operation.equals("*")){
result=NumAf*NumerBf;
}
else if(Operation.equals("/")){
result=NumerBf/NumAf;
}
Scr.setText(String.valueOf(result));
}
private class ButtonClickListener implements View.OnClickListener {
public void onClick(View v){
switch(v.getId()){
case R.id.buttonC: //ekranı temizle
Scr.setText("0");
NumerBf=0;
Operation="";
break;
case R.id.buttonAdd:
mMath("+");
break;
case R.id.buttonMinus:
mMath("-");
break;
case R.id.buttonMultiply:
mMath("*");
break;
case R.id.buttonDivide:
mMath("/");
break;
case R.id.buttonEqu:
mResult();
break;
default:
String numb= ((Button) v).getText().toString();
getKeyboard(numb);
break;
}
}
}
`
and here is my content_main.xml`
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="#+id/button3"
android:textStyle="bold"
android:layout_above="#+id/button5"
android:layout_toRightOf="#+id/button5"
android:layout_toEndOf="#+id/button5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="#+id/buttonC"
android:textStyle="bold"
android:longClickable="true"
android:layout_alignTop="#+id/button3"
android:layout_alignRight="#+id/buttonDivide"
android:layout_alignEnd="#+id/buttonDivide"
android:layout_alignLeft="#+id/buttonDivide"
android:layout_alignStart="#+id/buttonDivide" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/button1"
android:textStyle="bold"
android:layout_above="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/button4"
android:textStyle="bold"
android:layout_above="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="#+id/button6"
android:textStyle="bold"
android:layout_below="#+id/button3"
android:layout_toRightOf="#+id/button5"
android:layout_toEndOf="#+id/button5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="#+id/button5"
android:textStyle="bold"
android:layout_above="#+id/button8"
android:layout_toRightOf="#+id/button4"
android:layout_toEndOf="#+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="#+id/button8"
android:textStyle="bold"
android:layout_above="#+id/button0"
android:layout_toRightOf="#+id/button7"
android:layout_toEndOf="#+id/button7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="#+id/button7"
android:textStyle="bold"
android:layout_below="#+id/button5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="#+id/button9"
android:textStyle="bold"
android:layout_alignTop="#+id/button8"
android:layout_toRightOf="#+id/button8"
android:layout_toEndOf="#+id/button8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="#+id/button0"
android:textStyle="bold"
android:layout_above="#+id/buttonMinus"
android:layout_alignLeft="#+id/button8"
android:layout_alignStart="#+id/button8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/buttonAdd"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/buttonMinus"
android:textStyle="bold"
android:layout_alignTop="#+id/buttonAdd"
android:layout_alignLeft="#+id/button0"
android:layout_alignStart="#+id/button0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:id="#+id/buttonMultiply"
android:textStyle="bold"
android:layout_alignBottom="#+id/buttonMinus"
android:layout_toRightOf="#+id/buttonMinus"
android:layout_toEndOf="#+id/buttonMinus" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="#+id/buttonDivide"
android:textStyle="bold"
android:layout_alignTop="#+id/buttonMultiply"
android:layout_toRightOf="#+id/buttonMultiply"
android:layout_toEndOf="#+id/buttonMultiply" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="#+id/buttonEqu"
android:textStyle="bold"
android:layout_below="#+id/buttonMinus"
android:layout_alignLeft="#+id/buttonMinus"
android:layout_alignStart="#+id/buttonMinus" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText3"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toLeftOf="#+id/button3"
android:gravity="right"
android:text="0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/button2"
android:textStyle="bold"
android:layout_alignBottom="#+id/button3"
android:layout_toLeftOf="#+id/button3"
android:layout_toStartOf="#+id/button3" />
`
ButtonClickListener btnClick is not initialized. You are setting a null onClickListener. So it makes sence that pressing the buttons does nothing
I was searching solution for my problem, but I didn't find any.
I'm creating android application and one activity layout has several activities:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".EkranGlowny"
android:id="#+id/aaa"
android:background="#ffff8000"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="130dp"
android:layout_height="540dp"
android:background="#ffff8000"
android:layout_gravity="right"
android:baselineAligned="false"
android:clickable="false"
android:focusable="false"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Krystian Pruchnik"
android:id="#+id/waiter_name"
android:textSize="20sp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID: 180881"
android:id="#+id/waiter_id"
android:textSize="20sp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zamówienie"
android:textSize="20sp"
android:id="#+id/order"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="130dp"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/viewOrder"
android:layout_marginRight="5dp"
android:background="#ffffffff" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="58dp"
android:layout_height="540dp"
android:background="#69ff8300"
android:weightSum="1"
android:baselineAligned="false">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="1"
android:id="#+id/button1" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="2"
android:id="#+id/button2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="3"
android:id="#+id/button3" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="4"
android:id="#+id/button4" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="5"
android:id="#+id/button5" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="6"
android:id="#+id/button6" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="7"
android:id="#+id/button7" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="8"
android:id="#+id/button8" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="9"
android:id="#+id/button9" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="0"
android:id="#+id/button0" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="#string/clear_button"
android:id="#+id/clear_button"
android:layout_gravity="top"
android:background="#ffff0000"
android:textColor="#ffffffff"
android:textSize="11sp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="540dp"
android:background="#69ff8300"
android:weightSum="1"
android:baselineAligned="false">
<ScrollView
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="480dp"
android:background="#ffff8000">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Zestawy"
android:id="#+id/zestawy"
android:textAlignment="center"
android:textIsSelectable="false"
android:layout_span="3"/>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa1"
android:id="#+id/meal1"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa2"
android:id="#+id/meal2"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa3"
android:id="#+id/meal3"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa4"
android:id="#+id/meal4"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa5"
android:id="#+id/meal5"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa6"
android:id="#+id/meal6"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa7"
android:id="#+id/meal7"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa8"
android:id="#+id/meal8"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa9"
android:id="#+id/meal9"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa10"
android:id="#+id/meal10"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa11"
android:id="#+id/meal11"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa12"
android:id="#+id/meal12"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa13"
android:id="#+id/meal13"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa14"
android:id="#+id/meal14"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#69ff8300"
android:weightSum="1"
android:baselineAligned="false">
<Button
android:layout_width="48dp"
android:layout_height="34dp"
android:text="VOID"
android:id="#+id/anuluj"
android:background="#ffff2800" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rozlicz"
android:id="#+id/rozlicz"
android:layout_weight="1.18" />
</LinearLayout>
</LinearLayout>
I want that if I click some button (from second and third linear layout) it'll put text in TextView (id: viewOrder in first linear layout). Whenever I try this, it's showing message: "Unfortunately WaiterApp is stopped" and application crashes.
My Main Activity in java:
public class EkranGlowny extends ActionBarActivity implements OnClickListener{
Button summary;
Context context;
Button but0;
Button but1;
Button but2;
Button but3;
Button but4;
Button but5;
Button but6;
Button but7;
Button but8;
Button but9;
Button butClr;
Button voiD;
Button meal1;
Button meal2;
Button meal3;
Button meal4;
Button meal5;
Button meal6;
Button meal7;
Button meal8;
Button meal9;
Button meal10;
Button meal11;
Button meal12;
Button meal13;
Button meal14;
TextView viewOrder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ekran_glowny);
setupVariables();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rozlicz:
context = getApplicationContext();
Intent intent = new Intent(context, ResumeScreen.class);
startActivity(intent);
break;
/*case R.id.button0:
viewOrder.append(but0.getText().toString());
break;
case R.id.button1:
viewOrder.append("1");
break;
case R.id.button2:
viewOrder.append("2");
break;*/
default:
viewOrder.append(((Button) v).getText().toString());
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//implementacja zmiennych
private void setupVariables() {
summary = (Button) findViewById(R.id.rozlicz);
summary.setOnClickListener(this);
but0 = (Button) findViewById(R.id.button0);
but0.setOnClickListener(this);
but1 = (Button) findViewById(R.id.button1);
but1.setOnClickListener(this);
but2 = (Button) findViewById(R.id.button2);
but2.setOnClickListener(this);
but3 = (Button) findViewById(R.id.button3);
but3.setOnClickListener(this);
but4 = (Button) findViewById(R.id.button4);
but4.setOnClickListener(this);
but5 = (Button) findViewById(R.id.button5);
but5.setOnClickListener(this);
but6 = (Button) findViewById(R.id.button6);
but6.setOnClickListener(this);
but7 = (Button) findViewById(R.id.button7);
but7.setOnClickListener(this);
but8 = (Button) findViewById(R.id.button8);
but8.setOnClickListener(this);
but9 = (Button) findViewById(R.id.button9);
but9.setOnClickListener(this);
butClr = (Button) findViewById(R.id.clear_button);
butClr.setOnClickListener(this);
meal1 = (Button) findViewById(R.id.meal1);
meal1.setOnClickListener(this);
meal2 = (Button) findViewById(R.id.meal2);
meal2.setOnClickListener(this);
meal3 = (Button) findViewById(R.id.meal3);
meal3.setOnClickListener(this);
meal4 = (Button) findViewById(R.id.meal4);
meal4.setOnClickListener(this);
meal5 = (Button) findViewById(R.id.meal5);
meal5.setOnClickListener(this);
meal6 = (Button) findViewById(R.id.meal6);
meal6.setOnClickListener(this);
meal7 = (Button) findViewById(R.id.meal7);
meal7.setOnClickListener(this);
meal8 = (Button) findViewById(R.id.meal8);
meal8.setOnClickListener(this);
meal9 = (Button) findViewById(R.id.meal9);
meal9.setOnClickListener(this);
meal10 = (Button) findViewById(R.id.meal10);
meal10.setOnClickListener(this);
meal11 = (Button) findViewById(R.id.meal11);
meal11.setOnClickListener(this);
meal12 = (Button) findViewById(R.id.meal12);
meal12.setOnClickListener(this);
meal13 = (Button) findViewById(R.id.meal13);
meal13.setOnClickListener(this);
meal14 = (Button) findViewById(R.id.meal14);
meal14.setOnClickListener(this);
voiD = (Button) findViewById(R.id.anuluj);
voiD.setOnClickListener(this);
viewOrder = (TextView) findViewById(R.id.viewOrder);
}
}
I tried viewOrder.setText and viewOrder.append and nothing is working.
Is the problem is that TextView and buttons are in different layouts?
Could you tell me what I'm doing wrong?
edit:
In logcat I get:
06-07 14:17:40.290 7755-7755/com.example.krystian.waiterapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40da51f8)
06-07 14:17:40.290 7755-7755/com.example.krystian.waiterapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.krystian.waiterapp.EkranGlowny.onClick(EkranGlowny.java:78)
at android.view.View.performClick(View.java:3524)
at android.view.View$PerformClick.run(View.java:14194)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
at dalvik.system.NativeStart.main(Native Method)
Check your xml file name. It should be ekran_glowny.
My guess is you showed xml code from different xml file.
I have several Views, text views, and a button that have the android:visibility="invisible" attribute. My goal is to click a button that resides above these 'invisible' widgets, so that these widgets will become visible. I created another java class called 'VisibilityActivity.java" and tried the following method. But for some reason when I run the app, the button doesn't do anything. I don't know what I'm missing.
Here's the code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class VisibilityActivity extends Activity {
private View mVictim;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mVictim = findViewById(R.id.horizontalRule1);
mVictim = findViewById(R.id.TextView03);
mVictim = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.INVISIBLE);
}
};
}
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/custom_background"
android:isScrollContainer="true"
android:orientation="vertical"
android:paddingTop="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/ratingsInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/yourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp" />
<Button
android:id="#+id/submitRatingButton"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:background="#drawable/custom_button"
android:text="#string/submitRating"
android:textColor="#color/black1" />
<View
android:id="#+id/horizontalRule1"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/summaryInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1"
android:visibility="invisible" />
<View
android:id="#+id/horizontalRule2"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/ourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="10dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/overallRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar3"
android:color="#color/black1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="40dp"
android:visibility="invisible" />
<Button
android:id="#+id/saveContinueButton3"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="15dp"
android:background="#drawable/custom_button"
android:text="#string/saveContinue"
android:textColor="#color/black1"
android:onClick="onSaveAndContinue3Clicked"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
Thanks. Help would be greatly appreciated!
I am updating user936414's answer.
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
if( mText.getVisibility() == View.INVISIBLE )
mText.setVisibility(View.VISIBLE);
else
mText.setVisibility(View.INVISIBLE);
if( mRule1.getVisibility() == View.INVISIBLE )
mRule1.setVisibility(View.VISIBLE);
else
mRule1.setVisibility(View.INVISIBLE);
if( mRule2.getVisibility() == View.INVISIBLE )
mRule2.setVisibility(View.VISIBLE);
else
mRule2.setVisibility(View.INVISIBLE);
}
};
Also you might want to experiment with View.GONE.
findViewById(R.id.ratingBar3).setVisibility(View.VISIBLE);
findViewById(R.id.saveContinueButton3).setVisibility(View.VISIBLE);
you made it invisible view invisible again.. try the above code
Try
public class VisibilityActivity extends Activity {
private TextView mText;
private View mRule1, mRule2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mText= (TextView)findViewById(R.id.horizontalRule1);
mRule1 = findViewById(R.id.TextView03);
mRule2 = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mText.setVisibility(View.VISIBLE);
mRule1.setVisibility(View.VISIBLE);
mRule2.setVisibility(View.VISIBLE);
}
};
}
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(new View.onClickListener)
{
#Override
public void onClick(View v)
{
//Insert your code here
}
}