Android edittext with a static string - android

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);

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);

assign variable value with button 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!!

How to create button dynamically from one activity to another activity

I am creating an android application that consists of adding button dynamically.Here when I click the button in one activity then a button will dynamically created another activity. Is it possible to create a button when clicking a button in one activity then create a button in another activity.Please help me with this.
package com.example.dynamicbutton;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button Add_Button;
Activity_2 act_child;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Add_Button = (Button)findViewById(R.id.button_add);
Add_Button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent invisible = new Intent(MainActivity.this,Activity_2.class);
startActivity(invisible);
act_child.visible.setVisibility(View.GONE);
}
});
}
#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);
}
}
The solution is simple.Do Following in First Activity.
If the button clicked use following.
Intent invisible = new Intent(MainActivity.this,Activity_2.class);
//here add this line
.putExtra("visibility", "1");
startActivity(invisible);
else
Intent invisible = new Intent(MainActivity.this,Activity_2.class);
//here add this line
.putExtra("visibility", "0");
startActivity(invisible);
In your Second Activity:
String visibility = getIntent().getStringExtra("visibility");
if (visibility.equals("1")){
act_child.visible.setVisibility(View.VISIBLE);
}else{
act_child.visible.setVisibility(View.GONE);
}
Don't make variables public.Change your design to
1 ] Pass some variable to Second Activity . (In Button click of First Activity).
2 ] Get the variable in Second Activty & change the visibilty of Button (in Second Activity according to that variable)
Intent i = new Intent(CurrentActivity.this,SecondActivity.class);
i.putExtra("flag","show");
startActivity(i);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("flag");
if(value.equals("show")){
button1.setVisible(View.Visible);
}
else{
button1.setVisible(View.Gone);
}

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);
});
}

SharedPreference values not available between activities

I have 2 activities. One is the main activity. One is the Preference activity. I'm trying to change the background color of the activity based on the color selected in a RadioGroup in the Preference activity.
This is the class file for the main activity.
package com.example.mycsimodules;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ArrayAdapter;
public class ModList extends ActionBarActivity {
SharedPreferences savedData;
private String[] moduleArray = { "COMP 41600", "COMP 41620", "COMP 47330","COMP 30160", "COMP 30500", "COMP 40725", "COMP 41100", "COMP 41110" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mod_list);
savedData=getSharedPreferences("MyPrefs",0);
String colorMine=savedData.getString("color", "deflt");
TextView head=(TextView) findViewById(R.id.mymodlist);
head.setText(colorMine);
ArrayAdapter<String> moduleAdapter = new ArrayAdapter<String>(this, R.layout.activity_list_view, R.id.list1, moduleArray);
final ListView list = (ListView) findViewById(R.id.mod_list);
list.setAdapter(moduleAdapter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mod_list, 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) {
Intent i=new Intent(this, Preferences.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
This is the class file for the Preference class.
package com.example.mycsimodules;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class Preferences extends ActionBarActivity implements OnClickListener{
RadioGroup colorList;
Button saveButton;
SharedPreferences savedData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
colorList=(RadioGroup) findViewById(R.id.colorRadioGroup);
saveButton=(Button) findViewById(R.id.bSave);
saveButton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.preferences, 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);
}
#SuppressLint("NewApi") #Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==saveButton){
RadioButton myColor=(RadioButton) findViewById(colorList.getCheckedRadioButtonId());
String colorSelected=myColor.getText().toString();
TextView label=(TextView) findViewById(R.id.the_color_is);
label.setText(colorSelected);
savedData=getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor=savedData.edit();
editor.putString("color", colorSelected);
editor.apply();
editor.commit();
}
}
}
I haven't actually done the background color part. I'm just trying to display the value in a TextView currently. But it shows only the default value all the time. What I'm trying to do is.
Click on Settings.
Click on RadioButton for color desired.
Click on Save button.
Click on back key to return to main activity.
I'm pretty new to this and might be doing something wrong. But I just can't figure out what it is.
savedData=getSharedPreferences("MyPrefs",0);
it's diferent from
savedData=getSharedPreferences("MyPref", 0);
Use the same file when you open your SharedPreferences

Categories

Resources