How to get rid of Bar in Android - java

I am new to developing but I have a problem
Here is the code of the stuff I believe you need to know
Main Activity
package com.mayubrand.basiccalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
private Button circumferenceB, areaB, diameterB;
private TextView resultTxt, radiusTxt, headingTxt;
private EditText radiusTF ;
public void onCreate(){
this.requestWindowFeature(Window.FEATURE_NO_TITLE);}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
//Buttons
circumferenceB = (Button) findViewById(R.id.circumferenceB);
areaB = (Button) findViewById(R.id.areaB);
diameterB = (Button) findViewById(R.id.diameterB);
//Text Field (Edit Text)
radiusTF = (EditText) findViewById(R.id.radiusTF);
//Text (Text View)
resultTxt = (TextView) findViewById(R.id.resultTxt);
radiusTxt =(TextView) findViewById(R.id.radiusTxt);
headingTxt = (TextView) findViewById(R.id.radiusTxt);
//==============
//Listeners
circumferenceB.setOnClickListener(this);
areaB.setOnClickListener(this);
diameterB.setOnClickListener(this);
}
public void onClick(View view) {
String radiusEntered = radiusTF.getText().toString();
double pi = Math.PI;
int two = 2;
if(radiusEntered.equals("")) {
radiusTF.setText("");
}else{
switch (view.getId()){
case R.id.circumferenceB:
double circumference = Integer.parseInt(radiusEntered) * pi * two;
resultTxt.setText(String.valueOf(circumference));
break;
case R.id.areaB:
double area = pi * Integer.parseInt(radiusEntered) *Integer.parseInt(radiusEntered);
resultTxt.setText(String.valueOf(area));
break;
case R.id.diameterB:
double diameter = Integer.parseInt(radiusEntered) * 2;
resultTxt.setText(String.valueOf(diameter));
break;
}
}
}
}
Main Activity XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="MayU Circle Calulator"
android:id="#+id/headingTxt"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Radius"
android:id="#+id/radiusTxt"
android:layout_below="#+id/headingTxt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="36dp"
android:textSize="23dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/radiusTF"
android:layout_alignBottom="#+id/radiusTxt"
android:layout_alignRight="#+id/headingTxt"
android:layout_alignEnd="#+id/headingTxt" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/radiusTxt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Find Circumference"
android:id="#+id/circumferenceB"
android:layout_weight="0.08" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Find Area"
android:id="#+id/areaB"
android:layout_weight="0.08" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Find Diameter"
android:id="#+id/diameterB"
android:layout_gravity="center_horizontal"
android:layout_weight="0.08" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Result"
android:id="#+id/resultTxt"
android:layout_gravity="center_horizontal"
android:layout_weight="0.08"
android:textSize="40dp" />
</LinearLayout>
Android Manifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Circle Calculator"
android:theme="#style/AppTheme"
>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
When I'm in the layout area where you arrange all your buttons and text and any other component you have visually it looks fine, however as soon as I launch it in the emulator or on my phone there is a bar black/brown bar that just stays there. How do I remove that?
One other thing on the side, how do I change the default color of buttons

My guess is that you are referring to the action bar.
If that is the case in your styles.xml change the AppTheme theme to NoActionBar.
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
</style>
To change the default color of your buttons, you can use styles as well.

Related

Crash after splashscreen

i'm new at android studio and i'm developing my first app.
Everything worked fine until i implemented a splashscreen. Indeed, when i run the app, the splashcreen works, but then, it crashes.
The thing is that the splashscreen wasn't the first activity i've created (the main). I've created my app and when i finished it, i added a splashscreen.
Actually when i disable the splashscreen (the activity) and start directly with the previous "activity that appears on launch" (that is the "Menu"), it works fine.
And one weird thing: with the splashscreen enabled, it crashes only with certain devices. It runs perfectly with my Xiaomi Redmi Note 5 and a Galaxy S4 (or the nexus 5X emulator) but not with a Galaxy S3 and S6 .
I tried to replace the timer by a simple setonclicklistener button. But same, when it has to load the Menu, it crashes.
I haven't find similar problems on the forums so here i am !
The manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="false"
android:theme="#style/AppTheme">
<activity android:name=".Menu"/>
<activity android:name=".restaurant" />
<activity android:name=".courses" />
<activity android:name=".toilettes" />
<activity android:name=".distributeur" />
<activity android:name=".essence" />
<activity android:name=".hopital" />
<activity android:name=".Splashscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"/>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
</application>
Here is the Splashscreen -java
package fr.libertyg.messervices;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Handler;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Timer;
public class Splashscreen extends AppCompatActivity {
private ImageView daccord;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_splashscreen);
final TextView random = (TextView) findViewById(R.id.random);
final String[] mesphrases = {"S1", "S2",
"S3", "S4", "S5","S6"};
int rando = (int) (Math.random() * 6);
random.setText(mesphrases[rando]);
this.daccord = findViewById(R.id.daccord);
daccord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent otheractivity = new Intent(Splashscreen.this, Menu.class);
startActivity(otheractivity);
finish();
}
});
}
The Splashscreen xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".Splashscreen"
android:weightSum="10"
android:orientation="vertical"
android:background="#drawable/splashscreenfond">
<RelativeLayout
android:layout_weight="1.5"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/spashscreenbienvenue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/splashscreenbienvenue"
android:textSize="40sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5">
<ImageView
android:id="#+id/logo"
android:layout_width="249dp"
android:layout_height="217dp"
android:layout_centerInParent="true"
android:src="#drawable/icon" />
<TextView
android:id="#+id/nom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/app_name"
android:textSize="30sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5">
<TextView
android:id="#+id/bonasavoir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/bonasavoir"
android:textSize="17sp" />
<TextView
android:id="#+id/donnees"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/random"
android:layout_centerHorizontal="true"
android:text="#string/activezgps"
android:textSize="14sp" />
<TextView
android:id="#+id/random"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="13dp"
android:text="Un être humain a besoin d'environ 2000 kcal/jour"
android:textSize="14sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2">
<TextView
android:id="#+id/version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/version" />
<ImageView
android:id="#+id/daccord"
android:layout_width="124dp"
android:layout_height="105dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/daccord" />
</RelativeLayout>
</LinearLayout>
And the Menu -java
package fr.libertyg.messervices;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
public class Menu extends AppCompatActivity {
private ImageView distributeur;
private ImageView restaurant;
private ImageView courses;
private ImageView toilettes;
private ImageView essence;
private ImageView hopital;
AdView mAdView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_menu);
this.distributeur = findViewById(R.id.distributeur);
distributeur.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent otherActivity = new Intent(getApplicationContext(), distributeur.class);
startActivity(otherActivity);
}
});
this.restaurant = findViewById(R.id.restaurant);
restaurant.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent otherActivity = new Intent(getApplicationContext(), restaurant.class);
startActivity(otherActivity);
}
});
this.courses = findViewById(R.id.courses);
courses.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent otherActivity = new Intent(getApplicationContext(), courses.class);
startActivity(otherActivity);
}
});
this.toilettes = findViewById(R.id.toilettes);
toilettes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent otherActivity = new Intent(getApplicationContext(), toilettes.class);
startActivity(otherActivity);
}
});
this.essence = findViewById(R.id.essence);
essence.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent otherActivity = new Intent(getApplicationContext(), essence.class);
startActivity(otherActivity);
}
});
this.hopital = findViewById(R.id.hopital);
hopital.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent otherActivity = new Intent(getApplicationContext(), hopital.class);
startActivity(otherActivity);
}
});
MobileAds.initialize(this, "ca-app-pub-6520930039267956~4038950000");
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}
And the Menu xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:background="#drawable/fond"
android:orientation="vertical"
android:weightSum="10">
<RelativeLayout
android:layout_weight="1.5"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/bienvenue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/quoi"
android:textColor="#color/blanc"
android:textSize="30sp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7.6">
<ImageView
android:id="#+id/distributeur"
android:layout_width="140dp"
android:layout_height="130dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:layout_marginStart="37dp"
android:layout_marginTop="29dp"
android:src="#drawable/distributeur" />
<ImageView
android:id="#+id/restaurant"
android:layout_width="140dp"
android:layout_height="130dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="37dp"
android:layout_marginTop="29dp"
android:src="#drawable/restaurant" />
<ImageView
android:id="#+id/courses"
top="#+id/distributeur"
android:layout_width="140dp"
android:layout_height="130dp"
android:layout_alignStart="#+id/distributeur"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:src="#drawable/courses" />
<ImageView
android:id="#+id/toilettes"
android:layout_width="140dp"
android:layout_height="130dp"
android:layout_alignStart="#+id/restaurant"
android:layout_alignTop="#+id/courses"
android:layout_gravity="center_horizontal"
android:src="#drawable/toilettes" />
<ImageView
android:id="#+id/essence"
android:layout_width="140dp"
android:layout_height="130dp"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/distributeur"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="28dp"
android:src="#drawable/essence" />
<ImageView
android:id="#+id/hopital"
android:layout_width="140dp"
android:layout_height="130dp"
android:layout_alignStart="#+id/restaurant"
android:layout_alignTop="#+id/essence"
android:layout_gravity="center_horizontal"
android:src="#drawable/hopital" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9">
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-6520930039267956/3068657000" />
</RelativeLayout>
Thanks by advance for helping me.
In your app gradle make sure that libs use the same version, like:
implementation 'com.android.support:design:28.0.0-alpha1'
implementation 'com.android.support:cardview-v7:28.0.0-alpha1'
implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
implementation 'com.android.support:gridlayout-v7:28.0.0-alpha1'
You probably don't need all of them.
Make the changes and then click "Sync Now"
Solved it:
I had to add this to the manifest:
android:largeHeap="true"
And it worked fine.

Android simple program interest calculator

I am in the very first lessons of programming for Android and for my first attempt I tried to develop an interest calculator. The app starts but then crashes. The code follows:
package com.example.vitor.precojusto;
import com.example.vitor.precojusto.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
public class SICalculatorActivity extends Activity {
private TextView PA;
private TextView Interest_Rate;
private TextView Years;
private EditText PA_bar;
private EditText IR_bar;
private SeekBar year_bar;
private Button calculate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sicalculator);
PA = (TextView) findViewById(R.id.PA);
Interest_Rate = (TextView) findViewById(R.id.Interest_Rate);
Years= (TextView) findViewById(R.id.Years);
PA_bar= (EditText) findViewById(R.id.PA_bar);
IR_bar= (EditText) findViewById(R.id.IR_bar);
year_bar=(SeekBar) findViewById(R.id.year_bar);
calculate=(Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
}
});
}
}
sicalculator.xml:
<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"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp"
tools:context=".SICalculatorActivity" >
<TextView
android:id="#+id/Years"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/IR_bar"
android:layout_centerVertical="true"
android:text="2 Year(s)"
android:textSize="20sp" />
<SeekBar
android:id="#+id/year_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Years"
android:layout_below="#+id/Years"
android:layout_marginTop="21dp" />
<Button
android:id="#+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/year_bar"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/year_bar"
android:text="Calculate" />
<EditText
android:id="#+id/IR_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Years"
android:layout_marginBottom="14dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/Interest_Rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/IR_bar"
android:layout_alignLeft="#+id/IR_bar"
android:layout_marginBottom="15dp"
android:text="Interest Rate"
android:textSize="20sp" />
<TextView
android:id="#+id/PA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/PA_bar"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="Principal Amount"
android:textSize="20sp" />
<EditText
android:id="#+id/PA_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Interest_rate"
android:layout_below="#+id/PA"
android:layout_marginTop="17dp"
android:ems="10"
android:inputType="number" />
</RelativeLayout>
Do you guys have any guess why the app crashes? I tried to run it on an emulation of nexus on Android Studio (Android 5.0 Lollipop).
You have to change your manifest.xml to declare this activity as the main one.
<activity android:name=".SICalculatorActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Dont forget to post the stacktrace, you can find it in the Android Monitor, and it shows the error that happened.

Why Save Button doesn't work in my Sample Android App

I am beginner in Android system and was trying to create a simple registration from. In this app I created all the needed code but Save button doesn't give any response when clicking.
Here is the code of registration form :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtViewName"
android:text="Student's Name :"
android:layout_marginTop="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtViewFather"
android:text="Father's Name :"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/txtViewName"
android:layout_marginTop="29dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtViewMother"
android:text="Mother's Name :"
android:layout_below="#+id/txtViewFather"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtViewAge"
android:text="Age :"
android:layout_below="#+id/txtViewMother"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtViewClass"
android:text="Class :"
android:layout_below="#id/txtViewAge"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:id="#+id/txtEditName"
android:ems="10"
android:layout_toRightOf="#+id/txtViewName"
android:layout_above="#+id/txtViewFather"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:id="#+id/txtEditFather"
android:ems="10"
android:layout_toRightOf="#+id/txtViewFather"
android:layout_above="#+id/txtViewMother"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/txtEditName"
android:layout_alignStart="#+id/txtEditName"
android:layout_alignRight="#+id/txtEditName"
android:layout_alignEnd="#+id/txtEditName" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:id="#+id/txtEditMother"
android:ems="10"
android:layout_above="#+id/txtViewAge"
android:layout_toRightOf="#+id/txtViewMother"
android:layout_alignLeft="#+id/txtEditName"
android:layout_alignStart="#+id/txtEditName"
android:layout_alignRight="#+id/txtEditName"
android:layout_alignEnd="#+id/txtEditName" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:id="#+id/txtEditAge"
android:ems="10"
android:layout_above="#+id/txtViewClass"
android:layout_toRightOf="#+id/txtViewAge"
android:layout_alignLeft="#+id/txtEditName"
android:layout_alignStart="#+id/txtEditName"
android:layout_alignRight="#+id/txtEditName"
android:layout_alignEnd="#+id/txtEditName" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/txtEditClass"
android:layout_alignBottom="#id/txtViewClass"
android:layout_alignLeft="#+id/txtEditName"
android:layout_alignStart="#+id/txtEditName"
android:layout_alignRight="#+id/txtEditName"
android:layout_alignEnd="#+id/txtEditName" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnBack"
android:text="Back"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnSave"
android:text="Save"
android:layout_toLeftOf="#+id/btnBack"
android:layout_alignParentBottom="true" />
</RelativeLayout>
and here is the StudentDetail.java class
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View.OnClickListener;
import java.util.ArrayList;
public class StudentDetail extends AppCompatActivity implements android.view.View.OnClickListener {
Button btnSave;
Button btnBack;
EditText txtEditName;
EditText txtEditFather;
EditText txtEditMother;
EditText txtEditAge;
EditText txtEditClass;
private int _student_id = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entry_form);
btnSave = (Button) findViewById(R.id.btnSave);
btnBack = (Button) findViewById(R.id.btnBack);
txtEditName = (EditText) findViewById(R.id.txtEditName);
txtEditFather = (EditText) findViewById(R.id.txtEditFather);
txtEditMother = (EditText) findViewById(R.id.txtEditMother);
txtEditAge = (EditText) findViewById(R.id.txtEditAge);
txtEditClass= (EditText) findViewById(R.id.txtEditClass);
btnSave.setOnClickListener(this);
btnBack.setOnClickListener(this);
_student_id = 0;
Intent intent = getIntent();
_student_id = intent.getIntExtra("student_Id", 0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View view) {
if (view == findViewById(R.id.btnSave)){
StudentRepo repo = new StudentRepo(this);
Student student = new Student();
student.name = txtEditName.getText().toString();
student.father_name = txtEditFather.getText().toString();
student.mother_name = txtEditMother.getText().toString();
student.age = Integer.parseInt(txtEditAge.getText().toString());
student.student_class = txtEditClass.getText().toString();
student.student_id =_student_id;
if (_student_id==0){
_student_id = repo.insert(student);
Toast.makeText(this,"New Student Insert",Toast.LENGTH_SHORT).show();
}
} else if(view== findViewById(R.id.btnBack)){
finish();
}
}
}
Here is the AndroidManifest.xml code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.studentregistration" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.studentregistration.StudentDetail"
android:label="#string/title_activity_entry_form" >
</activity>
</application>
</manifest>
Now when I click on Save button nothing happens..
So can somebody help me out from this situation.
Thanks in advance.
Inside onClick function, use switch-case on the view Id as below:
#override
public void onClick(View view) {
switch(view.getId()) {
case R.id.btnSave:
// Your custom save code goes here ...
break;
case R.id.btnBack:
// ...
break;
}
}
Try this code:
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
StudentRepo repo = new StudentRepo(StudentDetail .this);
Student student = new Student();
student.name = txtEditName.getText().toString();
student.father_name = txtEditFather.getText().toString();
student.mother_name = txtEditMother.getText().toString();
student.age = Integer.parseInt(txtEditAge.getText().toString());
student.student_class = txtEditClass.getText().toString();
student.student_id =_student_id;
if (_student_id==0){
_student_id = repo.insert(student);
Toast.makeText(this,"New Student Insert",Toast.LENGTH_SHORT).show();
}
}
});
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
instead of
btnSave.setOnClickListener(this);
btnBack.setOnClickListener(this);
Oh thanks to all of you to give time to my question and suggesting me to do to fix the bug.
But after reviewing all the code line to line I found that bug myself.
The bug was launching wrong activity in AndroidManifest.xml file.
To fix this I removed the tag from MainActivity tag and placed this to StudentDetail activity tag and everything started working fine.
The updated code was :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.studentregistration" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.studentregistration.StudentDetail"
android:label="#string/title_activity_entry_form" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thanks once again to all.

Android app crashes upon startup

I am new to programming and after a while I finally fixed all my errors but now the app crashes on startup! I have not written all of this code by myself but I have edited it. Also when I still received errors the app could start (but not work obviously).
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tk.iWeld.iweld"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="tk.iWeld.iweld.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Activity_Main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
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=".MainActivity" >
<EditText
android:id="#+id/editText2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignBottom="#+id/editText2"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:ems="10"
android:hint="Text2"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText2"
android:layout_centerHorizontal="true"
android:text="X"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/holo_orange_dark" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:text="_________________________"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/holo_orange_dark" />
<EditText
android:id="#+id/Text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Text3"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/Text3"
android:layout_marginTop="22dp"
android:text="="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/holo_orange_dark"
android:textColorHint="#android:color/holo_orange_dark"
android:textSize="30sp" />
<TextView
android:id="#+id/textRes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignParentRight="true"
android:clickable="false"
android:longClickable="false"
android:text="Result"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/resultbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignTop="#+id/textView3"
android:text="Result" />
<EditText
android:id="#+id/editText1"
android:layout_width="89dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_alignLeft="#+id/textView2"
android:ems="10"
android:hint="Text1"
android:inputType="number" />
And the MainActivity
package tk.iWeld.iweld;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.menu.main);
}
public void calculateClickHandler(View view) {
// make sure we handle the click of the calculator button
if (view.getId() == R.id.resultbutton) {
// get the references to the widgets
EditText text1Text = (EditText)findViewById(R.id.editText1);
EditText text2Text = (EditText)findViewById(R.id.editText2);
EditText text3Text = (EditText)findViewById(R.id.Text3);
TextView resultText = (TextView)findViewById(R.id.textRes);
// get the users values from the widget references
float text1 = Float.parseFloat(text1Text.getText().toString());
float text2 = Float.parseFloat(text2Text.getText().toString());
float text3 = Float.parseFloat(text3Text.getText().toString());
// calculate the result value
float totalresult = calculateRESULT(text1, text2, text3);
// now set the value in the result text
resultText.setText("" + totalresult);
}
}
// the formula to calculate the result index
private float calculateRESULT (float text1, float text2, float text3) {
return (float) (text1 * text2 / text3);
}
}
There is a mistake in the second line in onCreate():
setContentView(R.menu.main);
You cannot set a menu as view. If you want to use the main layout, use this:
setContentView(R.layout.main);
This...
android:targetSdkVersion="22" />
is SCIENCE FICTION!
The maximum API level today is 19

How can I use the square root of a number that the user enters in an input box, instead of the number itself?

I'm trying to make an Android app, and the feature that I'm working on right now is calculating the square root of a number entered by the user.
How can I take a number that the user enters in a text box, and use the square root of that number in the doCalc part of my program? I'm limiting the number to be an integer between 1 and 20. For example, if the user enters 2 in the input box, I want to use 1.41 in the doCalc method.
Here is my .java code:
package learn.text;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LearntextActivity extends Activity {
TextView text;
EditText input;
TextView text2;
EditText input2;
TextView text3;
EditText input3;
Button calc;
TextView output;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text);
text.setText("Enter the design GPM for Chiller");
input = (EditText) findViewById(R.id.input);
text2 = (TextView) findViewById(R.id.text2);
text2.setText("Enter the Square root of the actual pressure drop across the coil");
input2 = (EditText) findViewById(R.id.input2);
text3 = (TextView) findViewById(R.id.text3);
text3.setText("Enter the design pressure drop of coil");
input3 = (EditText) findViewById(R.id.input3);
calc = (Button) findViewById(R.id.calc);
output = (TextView) findViewById(R.id.output);
}
public void doCalc (View view) {
double mInput = Double.parseDouble(input.getText().toString());
double mInput2 = Double.parseDouble(input2.getText().toString());
double mInput3 = Double.parseDouble(input3.getText().toString());
double mOutput = (mInput*mInput2)/(mInput3);
output.setText("GPM is" + mOutput);
}
}
Here is the .xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<TextView android:layout_height="wrap_content" android:text="" android:layout_width="wrap_content" android:id="#+id/text"></TextView>
<EditText android:layout_height="wrap_content" android:text="" android:layout_width="match_parent" android:id="#+id/input"></EditText>
<TextView android:layout_height="wrap_content" android:text="" android:layout_width="wrap_content" android:id="#+id/text2"></TextView>
<EditText android:layout_height="wrap_content" android:text="" android:layout_width="match_parent" android:id="#+id/input2"></EditText>
<TextView android:layout_height="wrap_content" android:text="" android:layout_width="wrap_content" android:id="#+id/text3"></TextView>
<EditText android:layout_height="wrap_content" android:text="" android:layout_width="match_parent" android:id="#+id/input3"></EditText>
<Button android:layout_height="wrap_content" android:text="Get GPM" android:layout_width="wrap_content" android:id="#+id/calc" android:password="false" android:onClick="doCalc"></Button>
<TextView android:layout_height="wrap_content" android:text="" android:layout_width="wrap_content" android:id="#+id/output"></TextView>
</LinearLayout>
Here is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="learn.text"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher" android:label="string/app_name">
android:label="#string/app_name" >
<activity
android:name=".LearntextActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Probably I'm misunderstanding something, but — can't you just change
Double.parseDouble(input2.getText().toString())
to
Math.sqrt(Double.parseDouble(input2.getText().toString()))
? (See http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#sqrt(double) for documentation of Math.sqrt.)

Categories