So, content of second activity does not appear when app is running, although content is showing in xml design. Programming in Java on Android Studio. In similar article answer didn't help. I also tried just to put one element in second activity, same result. Thanks in advance!
This is code from MainActivity.java:
package todo.beginner.com.carchooser2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import static todo.beginner.com.carchooser2.R.id.checkBoxPrice;
import static todo.beginner.com.carchooser2.R.id.checkBoxGas;
import static todo.beginner.com.carchooser2.R.id.checkBoxYear;
import static todo.beginner.com.carchooser2.R.id.checkBoxMileage;
import static todo.beginner.com.carchooser2.R.id.checkBoxCapacity;
public class MainActivity extends AppCompatActivity {
private CheckBox check1, check2, check3, check4, check5;
private static Button button_next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerToCeckBox();
OnClickButtonListener();
}
public void OnClickButtonListener() {
button_next = (Button)findViewById(R.id.button);
button_next.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("todo.beginner.com.SecondActivity");
startActivity(intent);
}
}
);
}
public void addListenerToCeckBox() {
check1 = (CheckBox)findViewById(checkBoxCena);
check1.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Price is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check2 = (CheckBox)findViewById(checkBoxGads);
check2.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Year is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check3 = (CheckBox)findViewById(checkBoxTilpums);
check3.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Engine capacity is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check4 = (CheckBox)findViewById(checkBoxDegviela);
check4.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Gas consumption is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
check5 = (CheckBox)findViewById(checkBoxNobraukums);
check5.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()){
Toast.makeText(MainActivity.this,
"Mileage is chosen", Toast.LENGTH_LONG).show();
}
}
}
);
}
}
This is from activity_main.xml:
<?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"
tools:context="todo.raitis.com.carchooser.MainActivity">
<CheckBox
android:text="Price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBoxPrice"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="67dp" />
<CheckBox
android:text="Year"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxPrice"
android:layout_alignRight="#+id/checkBoxPrice"
android:layout_alignEnd="#+id/checkBoxPrice"
android:layout_marginTop="33dp"
android:id="#+id/checkBoxYear" />
<CheckBox
android:text="Engine Capacity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="37dp"
android:id="#+id/checkBoxCapacity"
android:layout_below="#+id/checkBoxYear"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox
android:text="Gas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxCapacity"
android:layout_alignRight="#+id/checkBoxCapacity"
android:layout_alignEnd="#+id/checkBoxCapacity"
android:layout_marginTop="30dp"
android:id="#+id/checkBoxGas" />
<CheckBox
android:text="Mileage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxGas"
android:layout_alignRight="#+id/checkBoxGas"
android:layout_alignEnd="#+id/checkBoxGas"
android:layout_marginTop="33dp"
android:id="#+id/checkBoxMileage" />
<Button
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:id="#+id/button" />
<TextView
android:text="Choose criteria!"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
This is from SecondActivity.java:
package todo.beginner.com.carchooser2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
And this is from activity_second.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<TableRow
android:background="#607D8B"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Car Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Price" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Year" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gas" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mileage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Capacity" />
</TableRow>
<TableRow
android:background="#ECEFF1"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Audi" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2001" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="280000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2.5" />
</TableRow>
</TableLayout>
You are using the intent incorrectly. It should be:
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
Don't think you're starting the activity correctly. Try changing Intent intent = new Intent("todo.beginner.com.SecondActivity"); to Intent intent = new Intent(MainActivity.this, SecondActivity.class); in your MainActivity.
Related
The main Activity has a menu of 7 buttons. Clicking on each button opens a separate Activity.
The problem is that the Activity you click on does not display its content when launched on a real device. Instead, a white screen.
Google USB driver installed.
I tried to click Build - Clean Project and Invalidate Caches.
It didn't help. What is the problem?
Main Activity (menu):
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity"
android:orientation="vertical"
android:background="#color/tealbg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:orientation="horizontal">
<ImageView
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginRight="7dp"
android:src="#drawable/ic_greetings">
</ImageView>
<Button
android:id="#+id/button1"
android:background="#drawable/buttonshape"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="ПРИВЕТСТВИЯ"
android:textColor="#000000"
android:textSize="14sp"
android:stateListAnimator="#null"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_marginBottom="15dp"
android:orientation="horizontal">
<ImageView
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginRight="7dp"
android:src="#drawable/ic_game_info">
</ImageView>
<Button
android:id="#+id/button2"
android:background="#drawable/buttonshape"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="ИГРОВАЯ ИНФОРМАЦИЯ"
android:textColor="#000000"
android:textSize="14sp"
android:stateListAnimator="#null"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_marginBottom="15dp"
android:orientation="horizontal">
<ImageView
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginRight="7dp"
android:src="#drawable/ic_thoughts">
</ImageView>
<Button
android:id="#+id/button3"
android:background="#drawable/buttonshape"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="ВЫРАЖЕНИЕ МЫСЛЕЙ"
android:textColor="#000000"
android:textSize="14sp"
android:stateListAnimator="#null"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_marginBottom="15dp"
android:orientation="horizontal">
<ImageView
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginRight="7dp"
android:src="#drawable/ic_farewells">
</ImageView>
<Button
android:id="#+id/button4"
android:background="#drawable/buttonshape"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="ПРОЩАНИЯ"
android:textColor="#000000"
android:textSize="14sp"
android:stateListAnimator="#null"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_marginBottom="15dp"
android:orientation="horizontal">
<ImageView
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginRight="7dp"
android:src="#drawable/ic_numbers">
</ImageView>
<Button
android:id="#+id/button5"
android:background="#drawable/buttonshape"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="ЦИФРЫ/ЧИСЛА"
android:textColor="#000000"
android:textSize="14sp"
android:stateListAnimator="#null"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_marginBottom="15dp"
android:orientation="horizontal">
<ImageView
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginRight="7dp"
android:src="#drawable/ic_abusive_language">
</ImageView>
<Button
android:id="#+id/button6"
android:background="#drawable/buttonshape"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="РУГАТЕЛЬСТВА"
android:textColor="#000000"
android:textSize="14sp"
android:stateListAnimator="#null"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_marginBottom="15dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:layout_weight="1"
android:typeface="sans"
android:textSize="14sp"
android:textColor="#ffff"
android:text="Если Вам с трудом даётся общение с англоязычными игроками, то это приложение будет просто находкой для Вас! Коммуникация в CS:GO такая же важная составляющая как точная стрельба или отточенные движения.">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:layout_weight="1"
android:typeface="sans"
android:textSize="14sp"
android:textColor="#ffff"
android:text="Слаженная игра в команде - залог практически каждого взятого раунда. Крайне важно уметь давать информацию не только на Великом и Могучем, но и на английском языке.">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:layout_weight="1"
android:typeface="sans"
android:textSize="14sp"
android:textColor="#ffff"
android:text="Приложение содержит в себе часто используемые слова и фразы для общения с союзниками.">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="15dp"
android:orientation="horizontal">
<ImageView
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginRight="7dp"
android:src="#drawable/ic_about">
</ImageView>
<Button
android:id="#+id/button7"
android:background="#drawable/buttonshape"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="О ПРИЛОЖЕНИИ"
android:textColor="#000000"
android:textSize="14sp"
android:stateListAnimator="#null"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Menu item 1 (the contents of which are not displayed):
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#color/tealbg"
tools:context=".p1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
tools:ignore="UselessParent">
<Button
android:id="#+id/button1"
android:background="#drawable/buttonshape"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="ПРИВЕТСТВИЯ"
android:textColor="#000000"
android:textSize="14sp"
android:stateListAnimator="#null"
tools:ignore="HardcodedText"
tools:targetApi="lollipop" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
JAVA:
package en.my.voicechat;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Animation animScale = AnimationUtils.loadAnimation(this, R.anim.scale);
Button btnScale1 = findViewById(R.id.button1);
btnScale1.setOnClickListener(view -> view.startAnimation(animScale));
animScale.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animScale) {
Intent intent = new Intent(MainActivity.this, p1.class);
startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Button btnScale2 = findViewById(R.id.button2);
btnScale2.setOnClickListener(view -> view.startAnimation(animScale));
animScale.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animScale) {
Intent intent = new Intent(MainActivity.this, p2.class);
startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Button btnScale3 = findViewById(R.id.button3);
btnScale3.setOnClickListener(view -> view.startAnimation(animScale));
animScale.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animScale) {
Intent intent = new Intent(MainActivity.this, p3.class);
startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Button btnScale4 = findViewById(R.id.button4);
btnScale4.setOnClickListener(view -> view.startAnimation(animScale));
animScale.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animScale) {
Intent intent = new Intent(MainActivity.this, p4.class);
startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Button btnScale5 = findViewById(R.id.button5);
btnScale5.setOnClickListener(view -> view.startAnimation(animScale));
animScale.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animScale) {
Intent intent = new Intent(MainActivity.this, p5.class);
startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Button btnScale6 = findViewById(R.id.button6);
btnScale6.setOnClickListener(view -> view.startAnimation(animScale));
animScale.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animScale) {
Intent intent = new Intent(MainActivity.this, p6.class);
startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Button btnScale7 = findViewById(R.id.button7);
btnScale7.setOnClickListener(view -> view.startAnimation(animScale));
animScale.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animScale) {
Intent intent = new Intent(MainActivity.this, p7.class);
startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
In the below code, I've set the new Intent in the autocomplete_next TextView, but in my Mobile, when I run the app, and When I try to move to the next activity, It does not go the next Activity, Instead of that, it comes back again to the same previous Activity. In the Android Studio LOGCAT, I've seen the RenderScript: SETAFFINITY ret = -1 . I got no trace about this error on Google? Please Help me to fix my issue. Thank You in Advance...
activity_calculator.xml
<?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=".CalculatorActivity"
android:layout_centerHorizontal="true">
<TableLayout
android:id="#+id/tableinput"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Operand 1 : "
android:textSize="20dp"
android:textAlignment="textStart"
/>
<EditText
android:id="#+id/oper1_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:id="#+id/autocomplete_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Operand 2 : "
android:textSize="20dp"
android:textAlignment="textStart"
android:layout_weight="0"/>
<EditText
android:id="#+id/oper2_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
<GridLayout
android:id="#+id/calculator"
android:layout_below="#id/tableinput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/add_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="0"
android:layout_column="0"
android:text="Add"/>
<Button
android:id="#+id/sub_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="0"
android:layout_column="1"
android:text="Sub"/>
<Button
android:id="#+id/min_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="0"
android:layout_column="2"
android:text="Min"/>
<Button
android:id="#+id/mul_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="1"
android:layout_column="0"
android:text="Multiply"/>
<Button
android:id="#+id/div_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="1"
android:layout_column="1"
android:text="Divide"/>
<Button
android:id="#+id/pow_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="2"
android:layout_column="1"
android:text="Power"/>
<Button
android:id="#+id/max_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="1"
android:layout_column="2"
android:text="Max"/>
<Button
android:id="#+id/rem_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="2"
android:layout_column="0"
android:text="Remainder"/>
<Button
android:id="#+id/clear_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="2dp"
android:textSize="20dp"
android:layout_row="2"
android:layout_column="2"
android:text="clear"/>
</GridLayout>
<TableRow
android:layout_below="#id/calculator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:id="#+id/video_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ans: "
android:textSize="20dp"
android:textAlignment="textStart"
android:layout_weight="0"/>
<EditText
android:id="#+id/ans_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"/>
</TableRow>
</RelativeLayout>
CalculatorActivity.java (Activity which calls another Activity)
package com.example.lab_excercise_2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.lang.Math;
public class CalculatorActivity extends AppCompatActivity {
private EditText oper1, oper2, ans;
private Button add_btn, sub_btn, div_btn, mul_btn, rem_btn, max_btn, min_btn, pow_btn, clear_btn;
private Integer oper1_text, oper2_text;
private TextView video_next,autocomplete_next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
add_btn = findViewById(R.id.add_btn);
sub_btn = findViewById(R.id.sub_btn);
mul_btn = findViewById(R.id.mul_btn);
div_btn = findViewById(R.id.div_btn);
rem_btn = findViewById(R.id.rem_btn);
min_btn = findViewById(R.id.min_btn);
max_btn = findViewById(R.id.max_btn);
pow_btn = findViewById(R.id.pow_btn);
clear_btn = findViewById(R.id.clear_btn);
oper1 = findViewById(R.id.oper1_edit_text);
oper2 = findViewById(R.id.oper2_edit_text);
ans = findViewById(R.id.ans_edit_text);
video_next = findViewById(R.id.video_textView);
autocomplete_next = findViewById(R.id.autocomplete_textView);
add_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text+oper2_text));
}
});
sub_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text-oper2_text));
}
});
mul_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text*oper2_text));
}
});
div_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text/oper2_text));
}
});
rem_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText(String.valueOf(oper1_text%oper2_text));
}
});
max_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText((oper1_text>oper2_text ? oper1_text: oper2_text).toString());
}
});
min_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
ans.setText((oper1_text<oper2_text ? oper1_text: oper2_text).toString());
}
});
pow_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1_text=Integer.parseInt(oper1.getText().toString());
oper2_text=Integer.parseInt(oper2.getText().toString());
Double pow = Math.pow(oper1_text,oper2_text);
ans.setText(pow.toString());
}
});
clear_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oper1.setText("");
oper2.setText("");
ans.setText("");
}
});
video_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent videoIntent = new Intent(CalculatorActivity.this,VideoActivity.class);
startActivity(videoIntent);
}
});
autocomplete_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent autocompleteIntent = new Intent(CalculatorActivity.this,AutocompleteActivity.class);
startActivity(autocompleteIntent);
}
});
}
}
activity_autocomplete.xml
<?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=".AutocompleteActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="30sp"
android:textAlignment="center"
android:layout_width="wrap_content"
android:text="Date-Time Picker Autocomplete"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:layout_margin="20dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/setTime_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:text="Set Time"
android:layout_weight="1"
android:layout_margin="20dp"/>
<TextView
android:id="#+id/time_text"
android:textSize="20sp"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_weight="3"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/setDate_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:text="Set Date"
android:layout_margin="20dp"
android:layout_weight="1" />
<TextView
android:id="#+id/date_text"
android:textSize="20sp"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_weight="3"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/label_text"
android:textSize="20sp"
android:textAlignment="center"
android:layout_width="wrap_content"
android:text="Label "
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:layout_margin="20dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<MultiAutoCompleteTextView
android:id="#+id/label_edit"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_weight="3" />
</TableRow>
<Button
android:id="#+id/setalarm_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_margin="20dp"
android:text="Set Time" />
</TableLayout>
</RelativeLayout>
AutocompleteActivity.java (Activity which was called by the above Activity)
package com.example.lab_excercise_2;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.MultiAutoCompleteTextView;
import android.widget.TextView;
import android.widget.TimePicker;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class AutocompleteActivity extends AppCompatActivity {
private Button setTime, setDate, setalarm;
private TextView time_text, date_text, label_text;
private Calendar calendar = Calendar.getInstance();
private String date_string, time_string, label_string;
private String[] labels = {"Wake Up","Time to School","Time to College","Time to Work","Complete Activity","Finish Project"};
private MultiAutoCompleteTextView label_autocomplete;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autocomplete);
setTime = findViewById(R.id.setTime_btn);
setDate = findViewById(R.id.setDate_btn);
setalarm = findViewById(R.id.setalarm_btn);
time_text = findViewById(R.id.time_text);
date_text = findViewById(R.id.date_text);
label_autocomplete = findViewById(R.id.label_edit);
label_text = findViewById(R.id.label_textView);
setDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDateDialog(setDate);
}
});
setTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showTimeDialog(setTime);
}
});
setalarm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextpage(v);
}
});
label_autocomplete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent alarmIntent = new Intent(AutocompleteActivity.this,AlarmActivity.class);
startActivity(alarmIntent);
}
});
label_text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent alarmIntent = new Intent(AutocompleteActivity.this,AlarmActivity.class);
startActivity(alarmIntent);
}
});
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,labels);
label_autocomplete.setAdapter(adapter);
label_autocomplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
public void showTimeDialog(final Button time){
TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
time_text.setText(simpleDateFormat.format(calendar.getTime()));
}
};
new TimePickerDialog(AutocompleteActivity.this,timeSetListener,calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),false).show();
}
public void showDateDialog(final Button date){
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd");
date_text.setText(simpleDateFormat.format(calendar.getTime()));
}
};
new DatePickerDialog(AutocompleteActivity.this, dateSetListener,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
public void nextpage(View view){
date_string = date_text.getText().toString();
time_string = time_text.getText().toString();
label_string = label_autocomplete.getText().toString();
Intent nextIntent = new Intent(this,Autocomplete2Activity.class);
nextIntent.putExtra("date",date_string);
nextIntent.putExtra("time",time_string);
nextIntent.putExtra("label",label_string);
startActivity(nextIntent);
}
}
LOGCAT
2020-03-02 19:55:10.865 4078-4136/com.example.lab_excercise_2E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4137/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4139/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4138/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4140/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4142/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1
2020-03-02 19:55:10.865 4078-4141/com.example.lab_excercise_2 E/RenderScript: SETAFFINITY ret = -1`
I'm creating a simple calculator in Android Studio but the equal button didn't work. When I clicked the equal button, the app crash but it didn't show any error message. Other buttons work fine, I couldn't figure out why my equal button didn't work.
This is my MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private EditText showNum;
private Button one;
private Button two;
private Button three;
private Button four;
private Button five;
private Button six;
private Button seven;
private Button eight;
private Button nine;
private Button zero;
private Button divide;
private Button multiple;
private Button minus;
private Button equal;
private Button dot;
private Button plus;
private Button clear;
private Boolean addButton, multipleButton, minusButton, divideButton;
Float value, currentValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showNum=(EditText) findViewById(R.id.editView);
one = (Button)findViewById(R.id.button1);
two = (Button)findViewById(R.id.button2);
three = (Button)findViewById(R.id.button3);
four = (Button)findViewById(R.id.button4);
five = (Button)findViewById(R.id.button5);
six = (Button)findViewById(R.id.button6);
seven = (Button)findViewById(R.id.button7);
eight = (Button)findViewById(R.id.button8);
nine = (Button)findViewById(R.id.button9);
zero = (Button)findViewById(R.id.button0);
divide = (Button)findViewById(R.id.buttonDivide);
multiple = (Button)findViewById(R.id.buttonMultiple);
minus = (Button)findViewById(R.id.buttonMinus);
equal = (Button)findViewById(R.id.buttonEqual);
dot = (Button)findViewById(R.id.buttonDot);
plus = (Button)findViewById(R.id.buttonPlus);
clear = (Button)findViewById(R.id.buttonC);
clear.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText("");
}
});
zero.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"0");
}
});
one.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"1");
}
});
two.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"2");
}
});
three.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"3");
}
});
four.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"4");
}
});
five.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"5");
}
});
six.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"6");
}
});
seven.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"7");
}
});
eight.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"8");
}
});
nine.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+"9");
}
});
dot.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
showNum.setText(showNum.getText()+".");
}
});
divide.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
value = Float.parseFloat(showNum.getText()+"");
divideButton=true;
showNum.setText(null);
}
});
multiple.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if(showNum==null){
showNum.setText("");
}
else{
value = Float.parseFloat(showNum.getText()+"");
multipleButton=true;
showNum.setText(null);
}
}
});
plus.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
value = Float.parseFloat(showNum.getText()+"");
addButton=true;
showNum.setText(null);
}
});
minus.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
value = Float.parseFloat(showNum.getText()+"");
minusButton=true;
showNum.setText(null);
}
});
equal.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
currentValue = Float.parseFloat(showNum.getText()+"");
if (divideButton) {
showNum.setText(value / currentValue + "");
divideButton = false;
Log.i("gina", "b"+divideButton);
}
if (multipleButton){
showNum.setText(value * currentValue + "");
multipleButton = false;
}
if (addButton){
showNum.setText(value+currentValue + "");
addButton = false;
}
if (minusButton){
showNum.setText(value-currentValue + "");
minusButton = false;
}
}
});
}
}
Below is my activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.iu.hsiaoyuanwang.proficiency2.MainActivity">
<EditText
android:id="#+id/editView"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:padding="35dp"
android:textSize="30sp"
android:gravity="top|center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/editView"
android:orientation="vertical"
android:id="#+id/linear1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/button7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"
android:textSize="20dp" />
<Button
android:id="#+id/button8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8"
android:textSize="20dp"
/>
<Button
android:id="#+id/button9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="20dp"
android:text="9" />
<Button
android:id="#+id/buttonDivide"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="÷"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
android:textSize="20dp" />
<Button
android:id="#+id/button5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"
android:textSize="20dp"
/>
<Button
android:id="#+id/button6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="20dp"
android:text="6" />
<Button
android:id="#+id/buttonMultiple"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="x"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
android:textSize="20dp" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
android:textSize="20dp"
/>
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="20dp"
android:text="3" />
<Button
android:id="#+id/buttonMinus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="-"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/button0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0"
android:textSize="20dp" />
<Button
android:id="#+id/buttonDot"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="."
android:textSize="20dp"
/>
<Button
android:id="#+id/buttonEqual"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="20dp"
android:text="=" />
<Button
android:id="#+id/buttonPlus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="+"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/buttonC"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
As Kuti suggested, its better to keep only one OnClickListener, so keep his advice in mind.
A Boolean is an object, is not true or false, so the sentence if(addButton) when addButton is a Boolean is always false. Try using addButton.booleanValue() or use a boolean instead of a Boolean
I am trying to generate Multiplication Table of any number that is input by user. I have developed the interface for the application but cannot understand where to start with the logical part(coding). What i want is when a user inputs a number into the EditText then in the TextView (id: printArea) should show the table of the input number in the format as given in image 2. [Just to show you example i used TextView in the printArea part and i do not know what to use instead]
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:weightSum="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Please Enter a Number: "
android:textSize="20sp" />
<EditText
android:id="#+id/num"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginLeft="30dp"
android:textSize="20sp" />
</LinearLayout>
<Button
android:id="#+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/log"
android:onClick="submitNumber"
android:text="Get Table" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:id="#+id/printArea" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/clear"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="1dp"
android:text="Clear" />
<Button
android:id="#+id/credits"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:text="Credits" />
<Button
android:id="#+id/exit"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginRight="2dp"
android:text="Exit" />
</LinearLayout>
</LinearLayout>
and the MainActivity.java is:
package com.example.tara.multiplicationtable;
import android.content.Intent;
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 {
EditText num;
Button credits;
Button calculate;
Button clear;
Button exit;
TextView printArea;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num = new EditText(this);
clear = new Button(this);
calculate = new Button(this);
credits = new Button(this);
calculate = new Button(this);
printArea = new TextView(this);
num = (EditText) findViewById(R.id.num);
credits = (Button) findViewById(R.id.credits);
clear = (Button) findViewById(R.id.clear);
exit = (Button) findViewById(R.id.exit);
printArea = (TextView) findViewById(R.id.printArea);
calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//change the integer value into string
int num1 = Integer.parseInt(num.getText().toString());
// Perform action on click
for (int i = 1; i <= 10; i++) {
printArea.setText(num1 + "X" + i + "=" + i * num1);
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num.setText("");
}
});
credits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Credits.class);
startActivity(i);
}
});
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
}
}
when application is launched it should show like thisThe initial state of application
and i want to make the application show the table like this if user input is 2 The Final Result
Make a Listview and then in its adapter add your table data.
Make adapter's layout like this:
<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="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="=" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
Here are the codes that i used to build the Multiplication Table that i imagined to build. The code for activity_main.xml file will be:
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginStart="2dp"
android:text="#string/Enter_number"
android:textSize="16sp" />
<EditText
android:id="#+id/num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="2dp"
android:layout_marginStart="1dp"
android:digits="0123456789"
android:hint="#string/hidden_text"
android:inputType="number"
android:maxLength="3"
android:textSize="16sp" />
</LinearLayout>
<Button
android:id="#+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:imeOptions="actionDone"
android:textSize="16sp"/>
<!--android:onClick="submitNumber"-->
<TextView
android:id="#+id/printArea"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20sp"
android:background="#drawable/backimage" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginStart="2dp"
android:textSize="16sp"
android:text="#string/clear"
/>
<Button
android:id="#+id/credits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginStart="1dp"
android:textSize="16sp"
android:text="#string/credits" />
<Button
android:id="#+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:layout_marginStart="1dp"
android:layout_weight="1"
android:textSize="16sp"
android:text="#string/exit" />
</LinearLayout>
</LinearLayout>
The Code for the MainActivity.java file will be:
package com.example.tara.multiplicationtable;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText num;
Button credits;
Button calculate;
Button clear;
Button exit;
TextView printArea;
//private static final String result = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num = new EditText(this);
clear = new Button(this);
calculate = new Button(this);
credits = new Button(this);
calculate = new Button(this);
printArea = new TextView(this);
num = (EditText) findViewById(R.id.num);
credits = (Button) findViewById(R.id.credits);
clear = (Button) findViewById(R.id.clear);
exit = (Button) findViewById(R.id.exit);
calculate = (Button) findViewById(R.id.calculate);
printArea = (TextView) findViewById(R.id.printArea);
calculate.setOnClickListener(new View.OnClickListener() {
// Perform action on Get Table Button click
public void onClick(View v) {
if (num.getText().length() == 0 ){
printArea.setText(R.string.err_msg);
}
else {
//change the integer value into string
printArea.setText("");
int num1 = Integer.parseInt(num.getText().toString());
String result;
for (int i = 1; i <= 10; i++) {
result = (num1 + " X " + i + " = " + i * num1);
printArea.append("\n" + result);
}
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num.setText("");
printArea.setText("");
}
});
credits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Credits.class));
}
});
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Good Bye, User!", Toast.LENGTH_SHORT).show();
finish();
}
});
}
}
Thank you everyone for the guidance.
I am new to android and Java and I am having trouble using the Thread.sleep function.My program is a simple game that's almost like table tennis with two players. The code is not complete but the Thread is not working properly.I am changing the margins to move the ball.Is the catch block being run because of some exception > My code is as follows :
package com.example.game;
import android.app.Activity;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private int x1=0,x2=0,m=150;
public int height;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display display = getWindowManager().getDefaultDisplay();
Point size= new Point();
display.getSize(size);
// int width = size.x;
height = size.y;
showview2();
showview1();
game();
}
public void game()
{
buttonSetup1_rig();
buttonSetup1_lef();
buttonSetup2_rig();
buttonSetup2_lef();
//while(m>=0)
for(int i=0;i<150;i++)
{
showball();
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(500);
m=m-10;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
protected void buttonSetup1_rig()
{
ImageButton ticon=(ImageButton)findViewById(R.id.imageButton2);
ticon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(x1<(height-320))
x1=x1+10;
showview1();
}
});
}
protected void buttonSetup1_lef()
{
ImageButton ticon=(ImageButton)findViewById(R.id.imageButton1);
ticon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(x1>(0))
x1=x1-10;
showview1();
}
});
}
protected void buttonSetup2_rig()
{
ImageButton ticon=(ImageButton)findViewById(R.id.imageButton6);
ticon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(x2<(height-320))
x2=x2+10;
showview2();
}
});
}
protected void buttonSetup2_lef()
{
ImageButton ticon=(ImageButton)findViewById(R.id.imageButton4);
ticon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(x2>(0))
x2=x2-10;
showview2();
}
});
}
public void showview1()
{
ImageView image=(ImageView)findViewById(R.id.imageView1);
LinearLayout.LayoutParams lp ;
lp=(LinearLayout.LayoutParams)(image.getLayoutParams());
lp.setMargins(x1,0,0,0);
image.setLayoutParams(lp);
}
public void showview2()
{
ImageView image=(ImageView)findViewById(R.id.imageView2);
LinearLayout.LayoutParams lp ;
lp=(LinearLayout.LayoutParams)(image.getLayoutParams());
lp.setMargins(x2,0,0,0);
image.setLayoutParams(lp);
}
public void showball()
{
ImageView img=(ImageView)findViewById(R.id.imageView3);
LinearLayout.LayoutParams l ;
l=(LinearLayout.LayoutParams)(img.getLayoutParams());
int leftMargin = l.leftMargin;
int rightMargin = l.rightMargin;
int topMargin = l.topMargin;
l.setMargins(m,0,m,0);
img.setLayoutParams(l);
}
}
My Xml is :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="400dp" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_above="#+id/relativeLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/left" />
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/right" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="350dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout1" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/lc" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="280dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/linearLayout1"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_marginTop="140dp"
android:src="#drawable/untitled" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/linearLayout2"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="64dp"
android:src="#drawable/lc" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_weight="2.25" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/left" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageButton1"
android:layout_alignParentRight="true"
android:src="#drawable/right" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>