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!
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);
}
}
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);
});
I have rather simple task to complete, but I can't get my head around it, I have my main class here, I choosing between screens using buttons, My task is to create a About page just explaing the rules of the game (my application).
public class Hashi_Main extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up click listeners for all the buttons
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
// click handling
public void onClick(View view) {
switch (view.getId()) {
case R.id.exit_button:
finish();
break;
case R.id.new_button:
NewGame();
break;
case R.id.about_button:
NewGame();
break;
}
}
And here i create a my NewGame activity this all works.
public void NewGame() {
// We first ask for the difficulty level.
new AlertDialog.Builder(this)
.setTitle(R.string.new_game_title)
// we provide a char array with the on click listener.
.setItems(R.array.difficulty,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int hardness) {
Intent intent = new Intent(Hashi_Main.this, HashiGame.class);
intent.putExtra(HashiGame.KEY_DIFFICULTY, hardness);
startActivity(intent);
}
})
.show();
}
What I want to do is the same thing but to use it for about page, I want to use TextView for the rules, this activity will have nothing else except text and a back to main menu button. I tried something like this.
public void About() {
LinearLayout lheader = new LinearLayout(this);
lheader.setOrientation(LinearLayout.HORIZONTAL);
TextView about_rules = new TextView(this);
about_rules.setId(about_id);
lheader.addView(about_rules);
}
But I am stuck for a while now, how can i trigger this activity?
Create an about activity and use an intent to launch your newly created activity. like so:
Intent intent = new Intent(Hashi_Main.this, AboutActivity.class);
startActivity(intent);
I cannot see any Activity in the About() method. It is just a local LinearLayout with a TextView.
You need to learn more about Android development before making any app.
This is my project:
Activity 1
int ThePlayedSound= R.raw.Sound_ID;
Intent MyIntent= new Intent(this, Main2Activity.class);
MyIntent.putExtra("key",ThePlayedSound);
startActivity(MyIntent);
Activity 2
I want to play the raw source with a Sound Pool With a clicking button so:
Intent MyIntent=getIntent();
int ThePlayedSound= MyIntent.getIntExtra("key", 0);
Sound_Pool_thing= new SoundPool(1, AudioManager.STREAM_MUSIC,0);
Click = Sound_Pool_thing.load(this,ThePlayedSound , 1);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sound_Pool_thing.play(Click,1,1,1,0,1);
}
});
Edited :
and when i try to open the app I got Unfortunately , My_App has stopped
I can't figure it out ..Help =)
Editing adding the complete code for the second Activity
public class Main2Activity extends AppCompatActivity {
Button button;
SoundPool Sound_Pool_thing;
int Click;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2_main);
Intent MyIntent=getIntent();
int ThePlayedSound= MyIntent.getIntExtra("key", 0);
Sound_Pool_thing= new SoundPool(1, AudioManager.STREAM_MUSIC,0);
Click = Sound_Pool_thing.load(this,ThePlayedSound , 1);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sound_Pool_thing.play(Click,1,1,1,0,1);
}
});
}
}
#Claudenix, I guess the problem is with your file name used for sound file i.e Sound_ID.
int ThePlayedSound= R.raw.Sound_ID;
try renaming the file in your raw folder in lowercase for eg:- "sound_id"
Hope this helps.
I am making a log-in system on Android. And I want the register Button to be unclickable when it has been clicked. I am using this code:
final Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
register.setEnabled(false);
Intent register = new Intent(getApplicationContext(), register.class);
startActivity(register);
}
});
This is working great, but I want the Button to remain unclickable even when the application or phone has been restarted. Does anyone know a way to make the Button unclickable permanently even when the application has been shut down?
As I already said in the comments section something like this may work:
public class MyActivity extends Activity {
private static final String KEY_IS_BUTTON_CLICKABLE = "key_clickable";
#Override
public void onCreate(Bundle savedInstanceState) {
...
final Button register = (Button) findViewById(R.id.register);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean isClickable = sharedPreferences.getBoolean(KEY_IS_BUTTON_CLICKABLE, true);
register.setEnabled(isClickable);
if(isClickable) {
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
register.setEnabled(false);
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit()
.putBoolean(KEY_IS_BUTTON_CLICKABLE, false);
Intent register = new Intent(getApplicationContext(), register.class);
startActivity(register);
}
});
}
}
...
}
In this case you could take a pessimistic approach and disable the button in the layout (by default) with android:clickable="false" and enable it in the condition where registration is required.