Starting activity with button - android

I'm trying to configure the button1 to change activity, but although I do not detect errors with eclipse, the button does not work, someone can tell me what is wrong thanks.
After the lprimo button there is' another button for data backup, I would not think the problem is caused by the presence of another button.
public class Aggiungi extends Activity implements OnClickListener {
private Button btn_save;
private EditText edit_turno,edit_ore;
private TextView edit_mese,edit_anno;
private DbHelper mHelper;
private SQLiteDatabase dataBase;
private String idturno,anno,mese,turno,ore;
private boolean isUpdate;
Spinner selectedMesi,selectedGiorni;
private final static String MY_PREFERENCES = "MyPref";
private final static String ANNO = "anno";
public void cambia (View view){
final Intent cambioTermini;
cambioTermini = new Intent (this, Cambio.class);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivity(cambioTermini);
}
});
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_activity);
SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
Intent intent=getIntent();
String anno = prefs.getString(ANNO, "2014");
btn_save=(Button)findViewById(R.id.save_btn);
edit_mese=(TextView)findViewById(R.id.txtmese);
edit_anno=(TextView)findViewById(R.id.txtanno);
edit_turno=(EditText)findViewById(R.id.turno_editTxt);
edit_ore=(EditText)findViewById(R.id.ore_editTxt);
isUpdate=getIntent().getExtras().getBoolean("update");
if(isUpdate)
{
idturno=getIntent().getExtras().getString("ID");
mese=getIntent().getExtras().getString("Mese");
anno=getIntent().getExtras().getString("Anno");
turno=getIntent().getExtras().getString("Turno");
ore=getIntent().getExtras().getString("Ore");
edit_mese.setText(mese);
edit_anno.setText(anno);
edit_turno.setText(turno);
edit_ore.setText(ore);
}
btn_save.setOnClickListener(this);
mHelper=new DbHelper(this);
}
// saveButton click event
public void onClick(View v) {
mese=edit_mese.getText().toString().trim();
anno=edit_anno.getText().toString().trim();
turno=edit_turno.getText().toString().trim();
ore=edit_ore.getText().toString().trim();
if(anno.length()>0&& mese.length()>0 && turno.length()>0 &&ore.length()>0)
{
saveData();
}
else

As per my understanding, you want to launch 'Cambio' activity on button1 click. Try this.
public class Aggiungi extends Activity implements OnClickListener {
private Button btn_save;
private EditText edit_turno,edit_ore;
private TextView edit_mese,edit_anno;
private DbHelper mHelper;
private SQLiteDatabase dataBase;
private String idturno,anno,mese,turno,ore;
private boolean isUpdate;
Spinner selectedMesi,selectedGiorni;
private final static String MY_PREFERENCES = "MyPref";
private final static String ANNO = "anno";
public void cambia(){
final Intent cambioTermini;
cambioTermini = new Intent (this, Cambio.class);
// Start cambioTermini activity
startActivity(cambioTermini);
// close this activity
finish();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_activity);
SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
Intent intent=getIntent();
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
cambia();
break;
}
}
}

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class Contact extends Activity
{
public ImageView btnlog1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnlog1=(ImageView)findViewById(R.id.imageView2);
btnlog1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent l = new Intent(Contact.this,
AndroidDashboardDesignActivity.class);
startActivity(l);
Contact.this.finish();
}
});
}
}
Use this code and try like this.. This is simple code for intent changing for one activity to another.. let me know your ans..

I was expecting something like:
public void cambia (View view){
final Intent cambioTermini;
cambioTermini = new Intent (this, Cambio.class);
startActivity(cambioTermini);
}
since you have
<Button
android:id="#+id/button1"
//.. other paramters
android:onClick="cambia"
android:text="cambia" />

Related

How do I change the image in other activities when I click the button?

When I click the image button (can_button) in "Giftshop Activity", how to change the "main_cat" image in "Main Activity" to the "can_cat" image for 9 seconds and return to the "main_cat" image?
I tried this code, but don't change the image.
public class GiftshopActivity extends AppCompatActivity {
private static final String TAG = "Giftshop_Activity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_giftshop);
// change MainActivity
ImageButton home = (ImageButton) findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(GiftshopActivity.this, MainActivity.class);
startActivity(intent);
}
});
// change can_cat image
ImageButton can_button = (ImageButton) findViewById(R.id.can_button);
can_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(GiftshopActivity.this, MainActivity.class);
intent.putExtra((String) ImageNavInfo.BUNDLE_KEY, ImageNavInfo.longDelay(R.raw.can)); //second param is Serializable
startActivity(intent);
}
});
public class MainActivity extends AppCompatActivity {
// main_cat
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// main_cat image
ImageView main_cat = (ImageView) findViewById(R.id.maincat);
Glide.with(this).load(R.raw.main_cat).into(main_cat);
public class CanActivity extends AppCompatActivity {
// can_cat
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_can);
ImageNavInfo imageNavInfo = (ImageNavInfo) getIntent().getSerializableExtra("ImageNavInfo");
if (imageNavInfo == null)
imageNavInfo = ImageNavInfo.noDelay(R.raw.main_cat);
ImageView can = (ImageView) findViewById(R.id.can_cat);
Glide.with(this).load(ImageNavInfo.drawableId).into(can);
Handler hand = new Handler();
hand.postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(CanActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}, imageNavInfo.delayMillis);
public class ImageNavInfo implements Serializable {
public static final Object BUNDLE_KEY = "ImageNavInfo";
#DrawableRes
static int drawableId;
final long delayMillis;
private ImageNavInfo(int drawableId, long delayMillis) {
this.drawableId = drawableId;
this.delayMillis = delayMillis;
}
// Getters skip
public static ImageNavInfo noDelay(int drawableId) {
return new ImageNavInfo(drawableId, 0L);
}
public static ImageNavInfo longDelay(int drawableId) {
return new ImageNavInfo(drawableId, 9000L);
}
}
English is not natural because I am Korean. Thank you for your understanding.

Put EditText in First Activity to TextView on Second Activity

I'm french so sorry for my bad english.
I need to update on my second activity my TextView with a EditText of my first Activity. But I don't how to do.
That is my code on First Activity:
public class MainActivity extends AppCompatActivity {
private TextView mGreetingTextView;
private EditText mLoginEditText,mEmailEditText,mPasswordEditText;
private Button mLoginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGreetingTextView = findViewById(R.id.main_textview_info);
mLoginEditText = findViewById(R.id.main_edittext_login);
mEmailEditText = findViewById(R.id.main_edittext_email);
mPasswordEditText = findViewById(R.id.main_edittext_password);
mLoginButton = findViewById(R.id.main_button_log);
mLoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
validateLogin();
}
String login = mLoginEditText.getText().toString();
});
}
private void validateLogin(){
if (mEmailEditText.getText().toString().equals("admin#admin.com") &&
mPasswordEditText.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(), "Login successful",Toast.LENGTH_SHORT).show();
callHome();
} else {
Toast.makeText(getApplicationContext(), "Wrong login",Toast.LENGTH_SHORT).show();
}
}
public void callHome(){
Intent i = new Intent(getApplicationContext(),HomeActivity.class);
i.putExtra("mLoginText", mLoginEditText.getText());
startActivity(i);
}
But I search a same topic who has the same problem and I don't find.
Apologize for my bad level on Android but I'm student on Android. This is my first topic on StackOverFlow ^^
You are calling mLoginEditText.getText() and that returns Editable. You will need to call mLoginEditText.getText().toString() and that will be a String.
You can update your second activity (HomeActivity) like this
public class HomeActivity extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
textView = findViewById(R.id.textView);
// get the text from main activity
Intent intent = getIntent();
String text = intent.getStringExtra("mLoginText");
textView.setText(text);
}}
I found a result :)
MainActivity:
public class MainActivity extends Activity {
public final static String LOGIN_DATA = "mLoginText";
private EditText mLoginEditText,mEmailEditText,mPasswordEditText;
private Button mLoginButton;
private boolean isValid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLoginEditText = findViewById(R.id.main_edittext_login);
mEmailEditText = findViewById(R.id.main_edittext_email);
mPasswordEditText = findViewById(R.id.main_edittext_password);
mLoginButton = findViewById(R.id.main_button_log);
mLoginButton.setOnClickListener(v -> {
isValid = validateLogin();
if (isValid){
callHome();
}
});
}
private boolean validateLogin(){
if (mEmailEditText.getText().toString().equals("admin#admin.com") &&
mPasswordEditText.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(), "Login successful",Toast.LENGTH_SHORT).show();
return true;
} else {
Toast.makeText(getApplicationContext(), "Wrong login",Toast.LENGTH_SHORT).show();
return false;
}
}
public void callHome(){
Intent i = new Intent(MainActivity.this,HomeActivity.class);
i.putExtra(LOGIN_DATA, mLoginEditText.getText().toString());
startActivity(i);
}
}
HomeActvity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mHelloText = findViewById(R.id.home_textview_hello);
Intent intent = getIntent();
String login = intent.getStringExtra(MainActivity.LOGIN_DATA);
mHelloText.setText(login);
With that, the login name of the first activity is in the second activity :)

How to read a value from a String from a java file to another file?

I have a String value which is the text from an EditText and I want to use that String value in another activity.
In a TextView the user puts his/her name and in the next activity I want to have a Welcome screen that says, Hello, name
package com.example.aprendelastablasdemultiplicar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class pantalla2 extends AppCompatActivity {
private EditText ingresarnombre;
private TextView cifracero;
private TextView cifrauno;
ingresarnombre = (EditText)findViewById(R.id.ingresarnombre);
String nombre = ingresarnombre.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pantalla2);
cifracero.setText(0);
cifrauno.setText(1);
}
}
In your first activity where you will put the name on the edittext, just get the string from the Edittext and pass that with intent.
FirstActivity :
public class FirstActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edittext = (EditText)findViewById(R.id.edittext);
String name = edittext.getText().toString();
Intent intent = new Intent(ActivityOne.this,ActivityTwo.class);
intent.putExtra("name_extra",name);
startActivity(intent);
}
}
On your second activity just receive the string extra value from intent and work with it.
Second Activity :
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String name = getIntent().getStringExtra("name_extra");
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Welcome "+name);
}
}
Create a button in your pantalla2 so that on button click you can go to another activity.And make sure you have given the id of your textview and button in activity_pantalla2 See this it will work:
public class pantalla2 extends AppCompatActivity {
private EditText ingresarnombre;
private Button button;
private TextView cifracero;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pantalla2);
cifracero = findViewById(R.id.txtCifracero);
button = findViewById(R.id.button);
String cifracero = cifracero.getText().toString().trim();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("key", cifracero);
startActivity(intent);
}
});
}
}
In your SecondActivity you just have to add textview in your layout and get the value send from first activity.
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String name = getIntent().getStringExtra("name_extra");
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Welcome "+name);
}
}

Android:Error:(20, 51) error: cannot find symbol variable Message_Text

I am trying to send data through an activity to another but there is a variable error that I didn't understand, please Help. Thank you very much for your time and assistance in this matter.
public class MainActivity extends AppCompatActivity {
EditText Message_Text;
public final static String MESSAGE_KEY="com.example.zeeshan.userinterface.message_key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View views) {
Message_Text= (EditText) findViewById(R.id.Message_Text);
String message=Message_Text.getText().toString();
Intent intent= new Intent(this, SecondActivity.class);
intent.putExtra(MESSAGE_KEY,message);
startActivity(intent);
}
}
the second activity code is:
public class SecondActivity extends AppCompatActivity {
public final static String MESSAGE_KEY="com.example.zeeshan.userinterface.message_key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
String message = intent.getStringExtra(MESSAGE_KEY);
TextView textView = new TextView(this);
textView.setTextSize(35);
// setContentView(R.layout.second_layout);
}
}
you have to write like below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendMessage();
}
public void sendMessage() {
EditText Message_Text= (EditText) findViewById(R.id.Message_Text);
String message=Message_Text.getText().toString();
Intent intent= new Intent(this, SecondActivity.class);
intent.putExtra("MESSAGE_KEY",message);
startActivity(intent);
}

Pass value from activity to CustomDialog class

Hi i want to pass a value "username" which i saved through SharedPreferences in my activity to a EditText in a CustomDialog class.
Meaning i want to use EditText.settext (username) in my CustomDialog class.
Can somebody help me to achieve this?
Thank you.
SharedPreference code in Main activity:
public class AndroidLogin extends Activity implements OnClickListener {
public SharedPreferences mPrefs;
...
#Override
protected void onPause() {
super.onPause();
Editor e = mPrefs.edit();
e.putString(USERNM, username);
e.commit();
}
}
Now i want to set the value "USERNM" to the EditText in my CustomDialog class:
public class MyCustomForm extends Dialog {
public SharedPreferences mPrefs;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.sencide.R.layout.inlogdialog);
EditText userTest = (EditText)findViewById(R.id.txtUserName);
mPrefs = getSharedPreferences("PREFS_NAME", this.MODE_PRIVATE); // does not work
String text = mPrefs.getString("USERNM", "");
userTest.setText(text);
}
}
CustomDialog:
import android.app.Dialog;
...
public class MyCustomForm extends Dialog {
String mTextChange;
public SharedPreferences mPrefs;
public interface ReadyListener {
public void ready(String user, String pass, boolean save);
}
private String usernm;
private String passwd;
private ReadyListener readyListener;
public MyCustomForm(Context context, String user, String pass, ReadyListener readyListener) {
super(context);
this.usernm = user;
this.passwd = pass;
this.readyListener = readyListener;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.sencide.R.layout.inlogdialog);
setTitle("Login:");
EditText userTest = (EditText)findViewById(R.id.txtUserName);
//mPrefs = getSharedPreferences("PREFS_NAME", this.MODE_PRIVATE);
//String text = mPrefs.getString("USERNM", "");
userTest.setText();
Button buttonConfrim = (Button) findViewById(R.id.btnConfirm);
buttonConfrim.setOnClickListener(new OKListener());
Button buttonCancel = (Button) findViewById(R.id.btnCancel);
buttonCancel.setOnClickListener(new CancelListener());
}
private class OKListener implements android.view.View.OnClickListener {
public void onClick(View v) {
EditText user = (EditText) findViewById(R.id.txtUserName);
EditText pass = (EditText) findViewById(R.id.txtpass);
CheckBox saveuser = (CheckBox) findViewById(R.id.saveuser);
readyListener.ready(user.getText().toString(),pass.getText().toString(), saveuser.isChecked());
MyCustomForm.this.dismiss();
}
}
private class CancelListener implements android.view.View.OnClickListener {
public void onClick(View v) {
MyCustomForm.this.dismiss();
}
}
}
Edit, in my main activity:
public void createDialog(Context context) {
SharedPreferences prefs = context.getSharedPreferences("mPrefs", Context.MODE_PRIVATE);
String user = prefs.getString("USERNM", "");
new MyCustomForm(context, user, user, null); // where the class that is calling this is aa OnTextChangeListener
}
This is how i show the Dialog:
public void getLoginData() throws Exception {
if (checkInternetConnection() == true){
MyCustomForm dialog = new MyCustomForm (this, "", "", new OnReadyListener());
dialog.setContentView(R.layout.inlogdialog);
dialog.show();
}
It's not recommended to pass context around (and by extension views). I would suggest creating a listener for your dialog, maybe something like an OnTextChangedListener#onChange(String).
Create the listener in your activity and pass it to your dialog. Then when the dialog is done, call the listeners onChange(String) method which will be set to fire an event to a UI thread handler to update the edit text. Conversely, you could just pass the handler.
public class MyDialog exteds Dialog implements Dialog.OnClickListener {
OnTextChangeListener mListener;
String mTextChange;
public MyDialog(OnTextChangeListener listener) {
mListener = listener;
// set up your stuff
setOnClickListener(this);
}
...
public void onClick(DialogInterface dialog) {
// Do what ever you need to do to get set mTextChange
mListener.onChange(mTextChange);
}
public static interface OnTextChangeListener {
void onChange(String textChange);
}
Here is how you pass it:
public void createDialog(Context context) {
SharedPreferences prefs = context.getSharedPreferences("MyPreferencesName", Context.MODE_PRIVATE);
String user = prefs.getString("what_ever_string_you_want", "fall_back_user_name");
new MyDialog(user, this); // where the class that is calling this is aa OnTextChangeListener
}

Categories

Resources