Creating a different activity within main activity triggered by a button - java

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.

Related

How to create a button on a different activity programmatically and save it on this activity?

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

Saving Activity State when switching activities

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!

Buttons outside the onCreate

Always in my apps I added buttons in void onCreate, but now I'm trying to do app with more buttons (about 10). I would like to all buttons active on start app.
In my opinion it is too much buttons to add in this onCreate and app will be starting to long.
I tried to put this:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
})
out of onCreate
but AndroidStudio underlines setOnClickListener and view
I don't have ideas, how and where can i add button out of onCreate.
If you don't want to overcrowd your oncreate method, then create a clicklistener outside onCreate anywhere in activity and in onCreate just set it.
onCreate :
edit_a_member = (Button) findViewById(R.id.edit_member);
delete_a_member = (Button) findViewById(R.id.delete_member);
edit_a_member.setOnClickListener(handleClick);
delete_a_member.setOnClickListener(handleClick);
clickListener:
private View.OnClickListener handleClick = new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.edit_member:
member_selected = EDIT_MEMBER_SELECTED;
callDialog();
break;
case R.id.delete_member:
callDeleteAlert();
break;
}
}
};
You can simply add a separate method for your buttons in the same class, e.g.:
public void onCreate(...){
//Standard setup of views or whatever you want to do here
this.addButtons();
}
private void addButtons(){
Button b1 = new Button("Hi");
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
});
Button b2 = new Button("Hi to you too");
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
});
}
This is an example. You can do this in soooo many ways. I feel like you should thoroughly learn Java's fundamental Object Oriented programming, because that's really what your question suggests you don't understand. Go follow a youtube tutorial. I always like "The New Boston"'s Java tutorial series on youtube.
PS: You can make code like this beautiful under the 'Words of wisdom': Don't repeat yourself
If you have to do a lot of work in your onCreate but you are worried that the UI will take too long to load you can always post a delayed runnable to a handler so in the onCreate method put :
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//add your code here
}
},10);
what this will do is your UI will load then the code in your Runnable will be executed 10 milliseconds after your UI loads thus your app will not take too long to load the UI, even though in your case I doubt it would be necessary.
If you are declaring the buttons in xml file :
Add these properties in each button Declaration in your Xml :
android:clickable="true"
android:onClick="onClick"
And now in Activity Class create a method like this :
public void onClick(View v){
switch(v.getId){
case R.id.{buttons_id_in_xml}
(Your Code)
break;
(Like for others)
}
}
If you want to add buttons dynamically :
Create a method to add the button like this:
void addButton(String buttonName, int button id){
Button button = new Button(this);
button.setText("Push Me");
(add it to parent Layout of xml)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(id){
case id1:
(handle )
break;
(like for others)
}
}
});
}
The best way to do this is:
add implements View.OnClickListener to
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// declare variables
private Button mBtn1;
private Button mBtn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
// make an instance to the btns
mBtn1 = findViewById(R.id.btn1);
mBtn2 = findViewById(R.id.btn2);
// set onClickListener
mBtn1.setOnClickListener(this); // with "this" you are passing the view
mBtn2.setOnClickListener(this);
}
// implement onClick
#Override
public void onClick(View view) {
// check which btn was clicked by id
switch (view.getId()) {
case R.id.btn1:
btn1Clicked();
break;
case R.id.btn2:
btn2Clicked();
break;
}
}
private void btn1Clicked() {
// your code btn1 clicked
}
private void btn2Clicked() {
// your code btn2 clicked
}
Hope this helped. Cheers!

How to create 2 or more than 2 button in Android

I want to create 2 or more than 2 two button in android but I got the problem in this line
View btnClick = findViewById(R.id.buttonClick);
View btnscan = findViewById(R.id.btscanClick);
//set event listener
btnClick.setOnClickListener(this);
}
//override the OnClickListener interface method
#Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.buttonClick){
//define a new Intent for the second Activity
Intent intent = new Intent(this,SecondActivity.class);
//start the second Activity
this.startActivity(intent);
public void onClick1(View arg1) {
if(arg1.getId() == R.id.btscanClick){
//define a new Intent for the second Activity
Intent intent = new Intent(this,ScanActivity.class);
//start the second Activity
this.startActivity(intent);
may be the problem would be with your design, I thing you are implementing it in surface view. if you do so make your layout with LinearLayout and RelativeLayout, it would be better if you also add your .manifestfile
Hey cast your views to Button i.e. instead of
View btnscan = findViewById(R.id.btscanClick);
use
Button btnscan = (Button)findViewById(R.id.btscanClick);
also post the errors you are getting so that I can help you out.
Add listener to other button as well
Button btnscan = (Button)findViewById(R.id.btscanClick);
btnscan.setOnClickListener(this);
Button btnclick = (Button)findViewById(R.id.buttonClick);
btnclick.setOnClickListener(this);
And let handle the click with only one OnClick method.
public void onClick(View arg0) {
if(arg0.getId() == R.id.buttonClick){
//define a new Intent for the second Activity
Intent intent = new Intent(this,SecondActivity.class);
//start the second Activity
this.startActivity(intent);
}
else if(arg1.getId() == R.id.btscanClick){
//define a new Intent for the second Activity
Intent intent = new Intent(this,ScanActivity.class);
//start the second Activity
this.startActivity(intent);
}
}
The way you usually want to go to define the OnClickListener for buttons is as follow :
btnClick.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View arg0) {
//define a new Intent for the second Activity
Intent intent = new Intent(this,SecondActivity.class);
//start the second Activity
this.startActivity(intent);
}
});
btnScan.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View arg0) {
//define a new Intent for the scan Activity
Intent intent = new Intent(this,ScanActivity.class);
//start the second Activity
this.startActivity(intent);
}
});
This means that you use a different onClickListener for each button.

How to make a button permanently unclickable

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.

Categories

Resources