So I have a screen where a user enters their name, on button click they are redirected to a menu where it displays their name.
In the menu screen I have a button that takes me to another screen (About Me), on that screen I have a button with an Intent to go back to the Menu Activity.
The issue is that when I click the button with the Intent to go back, the name displays as null instead of what the User entered.
This does not happen when I use the actual android navigation buttons to go to the previous screen, only the Button i created to go back to the Menu Activity.
Launcher Activity where user enters their name
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher_screen);
Button launcherNextBtn = (Button) findViewById(R.id.launcherNextBtn);
EditText launcherVisitorNameEditText = (EditText) findViewById(R.id.launcherVisitorNameEditText);
TextView launcherVisitorNameErrorTextView = (TextView) findViewById(R.id.launcherVisitorNameErrorTextView);
launcherVisitorNameErrorTextView.setText("");
launcherNextBtn.setOnClickListener((View v) -> {
String visitorName = launcherVisitorNameEditText.getText().toString();
if (visitorName.equals("")) {
launcherVisitorNameErrorTextView.setText("Please enter a value");
} else {
launcherVisitorNameErrorTextView.setText("");
Intent goToMenuActivity = new Intent(LauncherScreen.this, MenuScreen.class);
goToMenuActivity.putExtra("visitorName", visitorName);
startActivity(goToMenuActivity);
}
});
Menu activity where it displays the users name
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_screen);
Button menuHomeBtn = (Button) findViewById(R.id.menuHomeBtn);
Button menuAboutMeBtn = (Button) findViewById(R.id.menuAboutMeBtn);
TextView menuNameTextView = (TextView) findViewById(R.id.menuNameTextView);
String visitorName = getIntent().getStringExtra("visitorName");
menuNameTextView.setText("Dear " + visitorName);
menuHomeBtn.setOnClickListener((View v) -> {
Intent goToLauncherActivity = new Intent(MenuScreen.this, LauncherScreen.class);
goToLauncherActivity.putExtra("visitorName", visitorName);
startActivity(goToLauncherActivity);
});
menuAboutMeBtn.setOnClickListener((View v) -> {
Intent goToAboutMeActivity = new Intent(MenuScreen.this, AboutMeScreen.class);
startActivity(goToAboutMeActivity);
});
}
About me activity, when I click the button to back to Menu, i get "Dear null"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_me_screen);
Button aboutMeBackBtn = (Button) findViewById(R.id.aboutMeBackBtn);
String visitorName = getIntent().getStringExtra("visitorName");
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
startActivity(goToMenuActivity);
});
}
Your problem is in About me activity screen
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
startActivity(goToMenuActivity);
});
This time you are not passing an extra field, so you are getting null
Answer 1
aboutMeBackBtn.setOnClickListener((View v) -> {
onBackPressed();
});
Answer 2
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
goToMenuActivity.putExtra("visitorName", visitorName)
startActivity(goToMenuActivity);
});
Related
I wanted to create a button on Homepage activity by clicking the button on GroupSettings activity. However, when I do so, and go back to Homepage, the button is not saved on the page.
Here is the java code that i have:
public void btnpressedme(View caller){
ConstraintLayout constraintLayout = findViewById(R.id.homepageLayout);
Button btnShow = new Button(this);
btnShow.setText("SIUUUUUUU");
btnShow.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
btnShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View caller) {
Toast.makeText(GroupSettings.this, "HELLOOOO", Toast.LENGTH_LONG).show();
//Intent intent = new Intent(this, Group.class);
//startActivity(intent);
}
});
if (constraintLayout != null) {
constraintLayout.addView(btnShow);
}
}
I'm currently trying to create an activity, which should be creating a new TextView on my main activity, everytime a Button is clicked on the first activity. However, instead of creating a new TextView everytime the button is clicked, it just changes the values of the first created TextView, so that there is always only one TextView. Is there a way to make it so that my first activity will not only create one single textview?
Here's the code from my "NewSubjectActivity":
**public class NewSubjectActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_subject);
Button SaveBtn = findViewById(R.id.SaveBtn);
nsaSaveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Click();
}
});
}
protected void Click(){
Intent intent = new Intent(NewSubjectActivity.this, MainActivity.class);
Boolean createnewTextView = true;
intent.putExtra("createnewTextView",createnewTextView);
startActivity(intent);
}
}
And here's the (relevant) code from my MainActivity:
protected void ReceiveValue (){
//getting Extras
Intent nsareceivedvalues = getIntent();
boolean createTextView = false;
createTextView = nsareceivedvalues.getExtras().getBoolean("createnewTextView");
//declaring fixed Views
final LinearLayout mainLinearLayout = (LinearLayout)findViewById(R.id.mainLinearLayout);
//Params for TextView
RelativeLayout.LayoutParams Params = new RelativeLayout.LayoutParams(1000, 200);
Params.setMargins(0, 10, 0, 10);
while(createTextView) {
//creating a TextView
TextView newsubject = new TextView(MainActivity.this);
//applying values to the TextView
newsubject.setLayoutParams(Params);
newsubject.setGravity(CENTER);
newsubject.setBackgroundColor(Color.GRAY);
mainLinearLayout.addView(newsubject);
createTextView = false;
}
}
As I said, this only create one text view, everytime I press the button on my "NewSubjectActivity" I think this might be, because the previous text view is not saved and the MainActivity is reset everytime I switch between the activities.
Every help and advise is much appreciated <3
Maybe when you are going back to your main activity you should call to onBackPressed() function, so it will reset nothing at all. Here's the example in kotlin
val back_button:Button = findViewById(R.id.button2)
back_button.setOnClickListener { v -> run {
//Change activity
onBackPressed()
} }
in Java should be:
Button back_button = findViewById(R.id.button2);
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Change activity
onBackPressed()
}
});
Let's hope it works!
This is my login form is valid means :
if(isUserValidated && isPasswordValidated)
{
if(DetailProductDescription.product_id==null){
Intent intent = new Intent(LoginForm.this,HomePage.class);
startActivity(intent);
}
else
{
Intent intent = new Intent(LoginForm.this,WatchList.class);
startActivity(intent);
}
}
EDIT:
Here i have to check another condition in else part:
in else part having to check another condition:
if am getting the product_id on DetailProductDescription page without login :
DetailProductDescription class only having these 2 buttons.They are watchlist and wishlist.
if i have to click watchlist button on DetailProductDescription.class means its go to WatchList class.
if i have to click wishlist button on DetailProductDescription.class means its go to
new AddToWishListAsync().execute(); class.
How can i identify the these DetailProductDescription.watchlist and DetailProductDescription.wishlist button on LoginForm and how can i write the condition for these ??
please provide me solution for tehse.
So your Problem is,
Button watchlist -> Login class -> WatchList class
Button wishlist -> Login class-> AddToWishListAsync class
'Login' have to identify which button.
isn't it?
I'll suggest put an extra with intent call. ie,
in DetailProductDescription
watchListBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DetailProductDescription.this, LoginForm.class);
intent.putExtra("from","watch");
startActivity(intent);
}
});
wishListBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DetailProductDescription.this, LoginForm.class);
intent.putExtra("from","wish");
startActivity(intent);
}
});
Now, in LoginForm
String from=getIntent.getStringExtra("from");// you got it as 'watch' or 'wish', or null.
Now check the string and proceed.
It will be better to have WatchList button and WishList button inside your login xml layout itself. The visibility of these buttons should be View.INVISIBLE or View.GONE by default. Either you can hide these buttons from layout or you can do so in onCreate method of your Login Activity.
Button watchListBtn = null;
Button wishListBtn = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_login_screen);
watchListBtn = (Button) findViewById(R.id.loginBtn);
watchListBtn.setVisibility(View.GONE);
watchListBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(LoginForm.this, WatchList.class);
startActivity(intent);
}
});
wishListBtn = (Button) findViewById(R.id.wishlistBtn);
wishListBtn.setVisibility(View.GONE);
wishListBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your code to launch wishlist activity
}
});
// your other onCreate stuff........
}
Then your login validation code
if(isUserValidated && isPasswordValidated) {
if(DetailProductDescription.product_id==null) {
Intent intent = new Intent(LoginForm.this,HomePage.class);
startActivity(intent);
} else {
Intent intent = new Intent(LoginForm.this,WatchList.class);
startActivity(intent);
}
} else {
watchListBtn.setVisibility(View.VISIBLE);
wishListBtn.setVisibility(View.VISIBLE);
}
I want to show a custom dialog and force the user to click on whether button one or two.
The problem is that users can use the back button AND if they click on the view that is shown in the background my dialog also disappears.
Why? And how could I prevent this?
final Main t = this;
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.prompt_input_access);
dialog.setTitle("Title");
Button cmdLoginAccount = (Button) dialog.findViewById(R.id.cmdLoginAccount);
Button cmdLoginBank = (Button) dialog.findViewById(R.id.cmdLoginBank);
cmdLoginAccount.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
loginToBank = true;
dialog.dismiss();
Intent intent = new Intent(t, UserMenu.class);
startActivity(intent);
}
});
cmdLoginBank.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
loginToBank = false;
dialog.dismiss();
Intent intent = new Intent(t, UserMenu.class);
startActivity(intent);
}
});
dialog.show();
You just need to use the setCanceledOnTouchOutside method :
dialog.setCanceledOnTouchOutside(false);
I've fingers on my teeths with Java and Android. Normaly i code with Python, it easier and easier !
Java never let me do anything, or only in a unique method that i never know :(
So the only code working is copy-paste and it's hard to customize...
So, i've a simple editable text and a button, when i click on it, i go to an url. The button works : i can open my webview.
But i want to make the url with the editText and i can't :(
See that :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Add Click listeners for all buttons
final EditText Login = (EditText) findViewById(R.id.entry);
View firstButton = findViewById(R.id.ok);
firstButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.ok:
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.tatai.froyo.Webscreen.URL,
"http://192.168.1.12/index2.php");
startActivity(j);
break;
}
So, how can i have the login edittext to make the url like http://192.168.1.12/index2.php?log=toto
... where toto may is get with the login edittext.
Impossible to read a global var on the onclick, i'm lost ! :(
i trid that on the onclick listner :
String logDetail = Login.getText().toString();
but it can't see Login, it's out ! : undeclared
on your code, TRy this,
public void onClick(View v)
{
switch(v.getId())
{
case R.id.ok:
String logDetail = Login.getText().toString();
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.tatai.froyo.Webscreen.URL,"http://192.168.1.12/index2.php?log="+logDetail);
startActivity(j);
break;
}
EDIT:
EditText Login;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Add Click listeners for all buttons
Login = (EditText) findViewById(R.id.entry);
View firstButton = findViewById(R.id.ok);
firstButton.setOnClickListener(this);
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.ok:
String logDetail = Login.getText().toString();
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.tatai.froyo.Webscreen.URL,"http://192.168.1.12/index2.php?log="+logDetail);
startActivity(j);
break;
}