assign variable value with button android - android

i have a little program. U have to insert 3 values on EditText and have a button with metod OnClick. But when i push the button (have to add some variables) my app is closing.
I dont know if the problem is some variable, but i was reading and cant do fix it.
Thats my full class:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.TextView;
public class Preguntas extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preguntas);
}
#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_preguntas, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void calculaP(){
//Declaramos las 2 variables de cada uno de los valores
String edadActual;
int EdadActualFinal;
String edadInicial;
int edadInicialFinal;
String mediaSemana;
int mediaSemanaFinal;
String Resultado;
int ResultadoFinal;
//Objetos de cada widget
EditText miEdad = (EditText)findViewById(R.id.EdadActual);
EditText miEdadInicial = (EditText)findViewById(R.id.EdadInicial);
EditText miMedia = (EditText)findViewById(R.id.MediaSemana);
TextView MiResultado = (TextView)findViewById(R.id.ResultadoValor);
//Convertimos los String por Integers
edadActual = miEdad.getText().toString();
EdadActualFinal = Integer.parseInt(edadActual);
edadInicial = miEdadInicial.getText().toString();
edadInicialFinal = Integer.parseInt(edadInicial);
mediaSemana = miMedia.getText().toString();
mediaSemanaFinal = Integer.parseInt(mediaSemana);
Resultado = MiResultado.getText().toString();
ResultadoFinal = Integer.parseInt(Resultado);
ResultadoFinal = ((EdadActualFinal - edadInicialFinal)* (mediaSemanaFinal * 48));
MiResultado.setText(ResultadoFinal);

But here is an error
Could not find a method calculaP(View) in the activity class
com.example.dam.semenapp.Preguntas for onClick handler on view class
android.support.v7.widget.AppCompatButton with id 'cum' 10-30
Android can not find your function.

Just try moving below lines from CalculaP() to inside your onCreate() method:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.TextView;
public class Preguntas extends AppCompatActivity {
EditText miEdad,miEdadInicial,miMedia;
TextView MiResultado;
Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preguntas);
miEdad = (EditText)findViewById(R.id.EdadActual);
miEdadInicial = (EditText)findViewById(R.id.EdadInicial);
miMedia = (EditText)findViewById(R.id.MediaSemana);
MiResultado = (TextView)findViewById(R.id.ResultadoValor);
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener(){
public void OnClick(View v){
calculaP(); //Call your method
}
});
}
#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_preguntas, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void calculaP(){
//Declaramos las 2 variables de cada uno de los valores
String edadActual;
int EdadActualFinal;
String edadInicial;
int edadInicialFinal;
String mediaSemana;
int mediaSemanaFinal;
String Resultado;
int ResultadoFinal;
//Objetos de cada widget
//Comment below four lines and move them to Oncreate() instead.
/*
EditText miEdad = (EditText)findViewById(R.id.EdadActual);
EditText miEdadInicial = (EditText)findViewById(R.id.EdadInicial);
EditText miMedia = (EditText)findViewById(R.id.MediaSemana);
TextView MiResultado = (TextView)findViewById(R.id.ResultadoValor);
*/
//Convertimos los String por Integers
edadActual = miEdad.getText().toString();
EdadActualFinal = Integer.parseInt(edadActual);
edadInicial = miEdadInicial.getText().toString();
edadInicialFinal = Integer.parseInt(edadInicial);
mediaSemana = miMedia.getText().toString();
mediaSemanaFinal = Integer.parseInt(mediaSemana);
Resultado = MiResultado.getText().toString();
ResultadoFinal = Integer.parseInt(Resultado);
ResultadoFinal = ((EdadActualFinal - edadInicialFinal)* (mediaSemanaFinal * 48));
MiResultado.setText(ResultadoFinal);
}
Hope it helps!!

Related

How to Go to New Screen/Page in Android Studio?

I have a quick question regarding Android Studio.
So First Of all I am COMPLETELY new to this thing so don't get me wrong if i make a mistake.
So what I am doing is that I have created a title screen for my app called "Canada Quiz". And below the title I have a "Start" Button. And what I done so far is I have attached a code that would do something like this: So once a Start Button is clicked it would hide it. Now my Question.
How would I make it so That once Clicked it should hide the Start Button and show the Question and a TRUE and FALSE button. All I have is this. I just want the Questions the appear as soon as the person taps the Start Button. And in My Content_Main.xml i have a phone as a model witch displays the start button and the Canada quiz. So Please help me out as soon as POSSIBLE
Thank you!
package com.example.nilesh.myapplication;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private Button startButton;
private Button trueButton;
private Button falseButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) findViewById(R.id.startButton);
ArrayList <String> questions = new ArrayList<String>();
String firstQue = " Is Canada Located in North America? ";
String secondQue = " Is Ontario the Largest provice in Canada? ";
String thridQue = " Is Toronto the Largest City in Canada? ";
String fourQue = " Canada has 3 professional Basketball Teams. ";
String fiveQue = " Canada has 11 Provinces and 3 Territories. ";
String sixQue = " Justin Trudeau is not Canada's Prime Minister. ";
final String [] allQuestions = {firstQue,secondQue,thridQue,fourQue,fiveQue,sixQue};
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
startButton.setVisibility(View.INVISIBLE);
}
});
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public Button getTrueButton() {
return trueButton;
}
public void setTrueButton(Button trueButton) {
this.trueButton = trueButton;
}
public Button getFalseButton() {
return falseButton;
}
public void setFalseButton(Button falseButton) {
this.falseButton = falseButton;
}
}
Note I cannot see the TRUE BUTTON and FALSE BUTTON the content_main.xml file....
To go to next screen, use this code:
Intent intent = new Intent(MainActivity.this, NextActivity.class);
startActivity(intent);

Displaying a button under a string message

Hello I am very new to android programming. I would like to get some help regarding displaying a button under a string message under an activity which has been newly activated. Hope someone can help me. Here is the code which I have for the class MyActivity which activates when the button app tutorial is pressed. The xml for my main activity is also there. Also the displaymessage activity class and xml is also there.
package com.example.sadi_ace.myfirstapp;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.sadi_ace.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
/** Called when the user clicks the Send button */
public void TutorialText(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
String message = "This is the android tutorial, if you want to pass on the to the " +
"functions please press SKIP!";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
// #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_my, menu);
// return true;
// }
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML for main activity
<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:orientation="vertical"
android:background="#800080"
android:weightSum="1">
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="#string/button_tutorial"
android:background="#FFFFFF"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="TutorialText"
/>
</RelativeLayout>
DisplayMessageActivity class code
package com.example.sadi_ace.myfirstapp;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
// #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_display_message, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML for DisplayMessageActivity class
<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:orientation="vertical"
android:background="#800080"
android:weightSum="1">
</RelativeLayout>
Add button in your xml file activity_my like
<Button
android:id="#+id/btnskip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
After that use this button id in "onCreate " method and assign it to some button
Button skip;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
skip=(Button)findViewById(R.id.btnskip);
//add on click listener
skip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, DisplayMessageActivity.class);
String message = "This is the android tutorial, if you want to pass on the to the " +
"functions please press SKIP!";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
});
}

Why my aplication call the OnCreate's event every i call an activity?

i having a problem.
I try add a value in a listview since other activity, so i have 2 activities
the number 1 have a button (to call the activity 2) and have a listview
the other activity hace a 2 edittext and a button where call to activity 1 (here pass the 2 values ina putextras)
SO when i call the activity 2 and i back the event Oncreate is called againg, someone know why?
my code activity 1
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
private ArrayList<User> arrayOfUsers;
private UsersAdapter adapter;
private ListView listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.lvItems);
arrayOfUsers = new ArrayList<User>();
adapter = new UsersAdapter(this, arrayOfUsers);
listView.setAdapter(adapter);
Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
}
#Override
protected void onResume() {
super.onResume();
Bundle extras = getIntent().getExtras();
if(extras != null)
{
if( extras.getString("ename") != null && extras.getString("ehome") != null) {
User newUser = new User(extras.getString("ename"), extras.getString("ehome"));
adapter.add(newUser);
}
}
listView.setAdapter(adapter);
Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void clicbtn(View v){
Intent i = new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
}
my code activity 2
public class MainActivity2 extends ActionBarActivity {
private EditText ename;
private EditText ehome;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
ename = (EditText) findViewById(R.id.editText);
ehome = (EditText) findViewById(R.id.editText2);
}
#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_activity2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void accion(View v){
Intent i = new Intent(MainActivity2.this, MainActivity.class);
//i.putExtra("ename",ename.getText().toString());
//i.putExtra("ehome",ehome.getText().toString());
startActivity(i);
}
}
onCreate() is getting called because you are again starting it as a new activity from this line
Intent i = new Intent(MainActivity2.this, MainActivity.class);
//i.putExtra("ename",ename.getText().toString());
//i.putExtra("ehome",ehome.getText().toString());
startActivity(i);
If you want to send values to previous activity use startActivityForResult
This SO question can help you implementing this How to manage `startActivityForResult` on Android?

Android edittext with a static string

I am working on an app where the user inserts his/her name and i will be displayed in a textview.
package com.opgaveet.buttonlistener;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
EditText edit;
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.button1);
edit = (EditText) findViewById(R.id.editText1);
text = (TextView) findViewById(R.id.textView2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Your name has been submitted", Toast.LENGTH_LONG).show();
String name = edit.getText().toString();
text.append(name);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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);
}
}
This code work fine, no issues there, but it only displays the name that has been inserted. Is there a method to also display "Welcome name inserted" instead of just the name, when the name has been submitted?
You could simply concatenate the "Welcome" part, something like this:
text.append("Welcome " + name);
What you'll want to do is use a string in your strings.xml file with a marker in that string which will be replaced.
Create a string in your strings.xml file:
<string name="welcome_text">Welcome %s inserted</string>
Now in your code do this:
text.append(String.format(getString(R.string.welcome_text), name);
You can also just do
getString(getString(R.string.welcome_text), name);
More info here:
http://developer.android.com/reference/java/util/Formatter.html
Use:
String name = "Welcome " + edit.getText().toString();`
or:
text.append("Welcome" + name);`
TO append data of same textview. Create a textview and string like this.
String: <string name="welcome">Welcome</string>
Textview
android:id="#+id/text"
android:text="#string/welcome"
In Activity set like this.
txt_welcome = (TextView)findViewById(R.id.text);
txt_welcome.setText(txt_welcome.getText().toString()+" "+USERNAME);

Eclipse 4 errors

These are the error messages I get:
Cannot cast from an int toEditText
EXTRA_MESSAGE cannot be resolved to variable
PlaceholderFragment cannot be resolved to type
The nested type DisplayMessageActivity cannot hide an enclosing type
This is my code
package com.example.thirdapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new
PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) (R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
}
You need to find the edit text view by id.
EditText editText = (EditText) view.findViewById(R.id.edit_message);
You need put the function "sendMessage" out of "PlaceholderFragment".
Here is mine:
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}

Categories

Resources