Hardware back button does not bring me back to previous page - java

I have 2 xml pages, from the main page after clicking the next button it goes to the second page, but when I press on the hardware back button. It does not goes back to the main page.
How do I add the code for the hardware back button?
Testing on Android 4.3.
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Page" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:text="Go to Page 1" />
</RelativeLayout>
java class :
package com.example.linktestfyp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.main1);
Toast.makeText(getApplicationContext(), "Button 1 clicked",
Toast.LENGTH_LONG).show();
}
});
}
}

to navigate between activities you should call startActivity(intent); and not changing the contentView of the Same activity.
you should create two activities : Activity1(which will display the main.xml layout) and Activity2(which will display the main1.xml layout) , and while the user is in the Activity2 and press the back button , the Activity1 will be shown again automatically by the system, and the Activity2 will be destroyed.
Example :
Intent intent = new Intent(Activity1.this, Activity2.class);
startActivity(intent);
see this tutorial about how to switch between activities with a demo sample application which you can download at the end of the tutorial .
NB: don't forget to declare your activities in the Manifest file.

This may help you ..
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
//do your stuff
}
return super.onKeyDown(keyCode, event);
}

You probably want two activities (see below). When you click on the button of MainActivity, the MainActivity1 will start and display the R.layou.main1 layout. If you click the back button, the MainActivity1 will destroy, and the MainActivity will resume (by default, so you don't need to program this)
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
startActivity(new Intent(MainActivity.this, MainActivity2.class));
}
}
}
public class MainActivity1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
}
}

You are not changing activities so when you back press the 1st page is not shown to you,
you should use setcontentview in onbackpressed() method,
then you will be able to show the 1st page otherwise it will always go to the previous activity which is not what you are looking for it seems.

Related

Should we must click the button 2 taps after setting the onclick function for button component in android?

When I wanted to add a function to my button component, I found
a bug in my approach, which is that I have to click the button
once more to achieve the desired effect.
In xml I setup the button like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/Button111"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="button123"
android:text="#string/button" />
</LinearLayout>
My activity file setup like this:
package cn.mr8god.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void button123(View view) {
Button button = findViewById(R.id.Button111);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}
}
You're setting a click listener when you click on the button on the first time. Of course you would need to click it once again.
When you click on the button it will call the button123 function, so you don't need a listener.
Just do it like so:
public void button123(View view) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}

Java-app closes, when i push Button to start a Toast. But why?

I made VERY simple java toast that appears when you press a button. But when I run it on my phone, it stops the app and quits without an error. What did I do wrong?
MainActivity:
package com.example.ras.tests;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonWasClicked(View Button) {
Toast.makeText(this , "Button wurde geklickt!" , Toast.LENGTH_SHORT).show();
}
}
Button:
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="#+id/activity_main"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="#+id/activity_main"
android:layout_marginLeft="16dp"
android:onClick="buttonWasClicked (MainActivity)"
android:visibility="visible" />
</android.support.constraint.ConstraintLayout>
Simple: android:onClick="buttonWasClicked" in your xml should do the trick.
Or in your OnCreate method you can assign listener to your button like this:
final Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonWasClicked(button);
}
});
In this case you delete android:OnClick from your xml.
And you can do it event better: remove parameter View button from method buttonWasClicked this allows you not to declare your variable final.
Try removing the "(MainActivity)" in the android:onClick in your xml file.

How to change text from another activity?

I am creating an Android app - using Android Studio.
The app launches in MainActivity.java which fetches activity_main.xml . On activity_main, the user can select one of 3 buttons. No matter which button they select, it will take them to the SAME layout - primary_layout.xml and the Java class associated with that is PrimaryClass.java .
I have a placeholder in primary_layout. I want this placeholder (id: placeholder) to change according to what button was previously selected.
Eg. If Button 1 (id: button1) is clicked, then the placeholder must say “Button 1 was clicked.”
Or let’s say Button 2 (id: button2) was clicked, then the placeholder must say “Button 2 was clicked”. And the same goes for Button 3.
I have created an intent to open PrimaryClass but I'm not too sure how to code the intent for when the button is clicked. I just don’t know how to change the text of the placeholder, depending on which button the user has clicked. I’ve tried using an ‘if statement’ but it doesn’t seem to work.
Or instead of creating another activity, should I rather create a fragment, and if I should, how would I code the fragment in this particular app?
I have attached images and code to better understand.
And here is my code of my classes and layout files:
MainActivity:
package com.msp.exampleapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.msp.exampleapplication.MainActivity">
<Button
android:text="Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/button1"
android:onClick="clickedButton1" />
<Button
android:text="Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_alignParentStart="true"
android:layout_marginTop="27dp"
android:id="#+id/button2"
android:onClick="clickedButton2" />
<Button
android:text="Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button2"
android:layout_alignParentStart="true"
android:layout_marginTop="32dp"
android:id="#+id/button3"
android:onClick="clickedButton3" />
</RelativeLayout>
PrimaryClass:
package com.msp.exampleapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class PrimaryClass extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.primary_layout);
}
}
primary_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="{holder}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
android:id="#+id/placeholder" />
</LinearLayout>
Your first Activity must look like this:
package com.msp.exampleapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
Button button1,button2,button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this, PrimaryClass.class);
intent.putExtra("message","Button 1 selected");
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this, PrimaryClass.class);
intent.putExtra("message","Button 2 selected");
startActivity(intent);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this, PrimaryClass.class);
intent.putExtra("message","Button 3 selected");
startActivity(intent);
}
});
}
}
Code the second activity as below:
package com.msp.exampleapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class PrimaryClass extends AppCompatActivity {
TextView placeholder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.primary_layout);
placeholder=(TextView) findViewById(R.id.placeholder);
placeholder.setText(getIntent().getStringExtra("message"));
}
}
Here is solution of your problem. You have to use Intent to make action in another activity.
Code for MainActivity, create a intent on each button click like below
button_1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,PrimaryClass.class);
intent.putExtra("button_text", "Button 1 Clicked");
startActivity(intent);
}
});
Same for button 2 and 3
button_2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,PrimaryClass.class);
intent.putExtra("button_text", "Button 2 Clicked");
startActivity(intent);
}
});
button_3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,PrimaryClass.class);
intent.putExtra("button_text", "Button 3 Clicked");
startActivity(intent);
}
});
Now in PrimaryClass use this code inside onCreate Method
String btn_text;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
btn_text = bundle.getString("button_text");
}
Now set text on lable
textView.setText(btn_text);
Hope you understand this, if not let me know, I'll help you.

How do i create a button to another page for my app

i was just wondering how i would make a button on my page so that it would go to another page for my app. I am a beginner so if you could explain how it all works and where it goes it would help a lot.
PS i am using android studio if that makes a difference and this is the code i have so far in my fragment_main.xml. I have not entered any code into the .java
<TextView android:text="#string/hello_world"
android:id="#id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/firstbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/homebutton"
android:layout_below="#+id/text"/>
You can declare views and objects dynamically in Java, and then pass the button from fragment to fragment (or Activity to Activity, depending on your app).
To declare a Relative Layout with a Button, for example:
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.RelativeLayout;
public class JavaLayoutActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button myButton = new Button(this);
RelativeLayout myLayout = new RelativeLayout(this);
myLayout.addView(myButton);
setContentView(myLayout);
}
This doesn't set the properties or anything, I am just using this as proof of concept.
XML makes things easy with regards to user interface design because you don't have to manage it in code, but this is one case where it is an exception. If you want dynamic interface objects, you need to use Java.
Instead of creating view dynamically you should obtain your view in activity
ImageButton button = (ImageButton) findViewById(R.id.firstButton)
and assign an onClick listener to id
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// start new Activity here
}
});
You can also do it in xml:
<ImageButton
android:id="#+id/firstbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/homebutton"
android:onClick="sendMessage"
android:layout_below="#+id/text"/>
with such configuration, you should add method in activity:
public void sendMessage(View view) {
// start another activity here
}
There are two methods available in android using which you can go from one Activity to another.
1. Use button.setOnClickListener()
Create a button in xml file.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
Now set event listener for the button in your .class file
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//set the event you want to perform when button is clicked
//you can go to another activity in your app by creating Intent
Intent intent = new Intent(getApplicationContext, Activity2.class);
startActivity(intent);
}
});
2. Use <android:onClick="goNext">
Put the onClick as the attribute of the button you have created in xml file.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="goNext" />
Now in your .class file define an event for that button as,
goNext() {
Intent intent = new Intent(getApplicationContext, Activity2.class);
startActivity(intent);
}

How to do back button that go back to previous layout?

i am new in android .. I want to ask question about Link 2 layouts using buttons. I have 2 xml layout and first layout can link to 2nd layout but 2nd layout can't go back to 1st layout. Please help me.
below are my codes...
activity_main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="120dp"
android:text="Link to page 1" />
</RelativeLayout>
page1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Page 1 test"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to main page" />
</LinearLayout>
MainActivity.java
package com.example.linktest2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 =(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.page1);
}
});
}}
page1.java
package com.example.linktest2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class page1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page1);
Button btn1 =(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.activity_main);
}
});
}}
You're doing it wrong.
To go from MainActivity to page1 (by convention it should be Page1), you should start a new Activity (instead of changing the contentview of the current activity). Then to go back from Page1 to MainActivity, you can programmatically finish() the Activity, or the user can touch Back.
You can either keep your both layout in same layout file and make the show or hide the layout accordingly.
Also another way is you can create two activity which contains two separate layout and load a second layout on click of button using intent and start activity as below
To Go to other activity you can do like this :
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
Normally android deals with that itself.
However you can override OnBackPressed in your second activity and launch an intent that leads to your first activity
Going to other activity you can do like this :
Intent intent = new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
For each XML you need to have another Activity.
In Main Activity :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 =(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),page1.class);
startActivity(intent);
finish();
}
});
}
Replace this
And in your Page1 Activity replace this
public class page1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
Button btn1 =(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
Hope this will help you
If you want to come back to activity A from activity B with some result,you can use like this
activity B
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
activity A
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}

Categories