Android - Go to new screen on button click - java

Followed a tutorial but doesnt seem to work on emulation.
MainActivity.java:
package b.myfirstapp;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity2.class);
startActivity(intent);
}
});
}
}
Activity2.java:
package b.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Activity2 extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="b.myfirstapp" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".Activity2" >
</activity>
</application>
</manifest>
There are no errors in the code, its just that when i emulate it it doesnt work, maybe ive coded it incorrectly, not too sure, it looks fine to me.
I'm new to android dev so im confused. Help!

UPDATED: (For clearance purposes with comments properly inserted)
I think you are missing the call to addListenerOnButton in your MainActivity class, for example inside onCreate right after setContentView....
Also, you should make it private since it's only going to be called from within this class.

You have not called your addListenerOnButton method so you have not set onClick handler to your button.
Your onCreate should look like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton()
}

Related

Android BroadcastReceiver in ScrollActivity

I had working BroadcastReceiver which notifys user about network status changes, but since i added ScrollActivity to my app, the receiver is not working anymore. So i tried to make a blank ScrollActivity template project to test the reciever and i can see the same - the receiver wont work in apps with ScrollActivity. im a little bit desperate with this situation, would be thankful for help.
I have permissions and receiver registred in manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_app">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".ScrollingActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ConnectivityStatusReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
Here comes the receiver code, which hasnt been called even once in scrollactivity apps yet..
package com.example.test_app;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
public class ConnectivityStatusReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//THIS HASNT BEEN CALLED EVEN ONCE YET SINCE SCROLLACTIVITY
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
Log.d("RECIEVER_LOG", "intent:"+intent.getAction());
if(activeNetworkInfo.isConnected()){
Log.d("NETWORK_LOG", "connected.");
}else{
Log.d("NETWORK_LOG", "not connected.");
}
if (activeNetworkInfo == null){
//no connection activity opens on connection loss
context.startActivity(new Intent(context , NoInternetConnection.class));
}
}
}
And here is the Scrolling activity itself, untouched as Android Studio generated it
package com.example.test_app;
import android.os.Bundle;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class ScrollingActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CollapsingToolbarLayout toolBarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
toolBarLayout.setTitle(getTitle());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_scrolling, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Would be thankful for any help.

Bringing first activity on top of back stack

I have two applications with one activity in each of them. Application A and application B. Application A has its activity's launch type as SingleTask. Both Activities have buttons calling each other. What I want is that when I launch activity A from activity B's button which is in turn launched from activity A's button, and then when I press back button activity B should come to foreground. But instead it is going back to launcher. I have attached code for reference.
Application A MainActivity:-
package com.example.appa;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1= (Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i =new Intent();
i.setClassName("com.example.appb", "com.example.appb.MainActivity");
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Application A Manifest:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Application B MainActivity:
package com.example.appb;
import com.example.appb.R;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1= (Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i =new Intent();
i.setClassName("com.example.appa", "com.example.appa.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
});
}
Application B Manifest is unmodified default mainfest
As it can be seen I have already tried FLAG_ACTIVITY_REORDER_TO_FRONT but it did not help.
P.S :- I cannot change launch type of activity in application A
Thanks in advance
The singleTask launch mode is not compatible with that behavior. You can set a different launch mode each time you start an activity by setting flags, but a better approach might be to reevaluate the choice made for default launch mode.
Tasks and back stack article.

Shifting between activities (Android)

Shifting between activities isn't working.
What the app does is it opens the android.xml file then after 5 secs open the main file
Here's the code:
android manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.android.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.android.android"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.ANDROID" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
android.java file:
package com.example.android;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class android extends Activity{
MediaPlayer sound1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.android);
sound1 = MediaPlayer.create(android.this , R.raw.android);
sound1.start();
Thread timer = new Thread()
{public void run()
{
try{
sleep(5000);
}
catch(InterruptedException ac){
ac.printStackTrace();
}
finally{
Intent openactivity = new Intent ("android.intent.action.MAIN");
startActivity(openactivity);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
sound1.release();
finish();
}
}
Mainactivity.java:
package com.example.android;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
android.xml file:(the first file that launches up)
<?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"
android:background = "#drawable/android"
>
</LinearLayout>
Try this way
Intent openactivity = new Intent(android.this,MainActivity.class);
startActivity(openactivity);
And for more information go to starting-new-activity
First, always Capitalise 1st letter of class names (code convention),
and then write your intents like below:
Intent intent = new Intent(android.this, MainActivity.class);
startActivity(intent );
You can check Starting Another Activity in Android Developers website.

Activity not declared in Manifest (Apparently)

I can't for my life figure out why it keeps saying "unable to find Explicit Activity Class com.example.timer.beep. Have you declared the Activity in your Android Manifest?". As far as I can tell it has been declared and i have followed the convention of the last small app I built. My xml files are lowercase, my .java files are CamelCase. Does anyone have any other ideas as to why it would give me this error? The error is in the readyToBeep function in MainActivity.java.
Main Activity.java
package com.example.timer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void readyToBeep(View v)
{
Intent intent = new Intent(this, Beep.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Beep.java
package com.example.timer;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Beep extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buzz);
}
private Context Context;
Timer timer;
public Beep(){};
public Beep(Context Context) {
this.Context = Context;
timer = new Timer();
timer.schedule(new RemindTask(),
0, //initial delay
1*1500); //subsequent rate
}
class RemindTask extends TimerTask {
public void run() {
MediaPlayer mPlayer = MediaPlayer.create(Context, R.raw.beep);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.start();
}
}
public void main(String args[]) {
new Beep();
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.timer.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.timer.buzz" >
</activity>
</application>
</manifest>
TIA
unable to find Explicit Activity Class com.example.timer.beep. Have you declared the Activity in your Android Manifest
There is no declaration of your Beep activity in your manifest. Add it :
<activity android:name="com.example.timer.Beep" >
</activity>

unable to start another activity at click of button

My project is fairly simple for training purposes only. I am trying to load another interface/view/activity when I click a button on my main layout. Here is the code for the main activity:
package com.example.interfacemanipulation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public static final String EXTRA_MESSAGE = "Something Here....";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void goToWelcomeActivityMethod(View view)
{
Intent intent = new Intent(this, WelcomeActivity.class);
EditText editText = (EditText) findViewById(R.id.welcomeMessage);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
And this is the code in the other activity:
package com.example.interfacemanipulation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class WelcomeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.welcome, menu);
return true;
}
}
the code here is basically modified from Google documentation. I just want to be able to load the view of the second activity with label in it. Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.interfacemanipulation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.interfacemanipulation.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.interfacemanipulation.WelcomeActivity"
android:label="#string/title_activity_welcome" >
</activity>
</application>
</manifest>
I've only been trying android for the last 4 days...so I appreciate your support
You have 2 problems: 1. You don't have the button to take you to the activity (or at least you haven't shown it in the code you posted. If you do, then you don't have the onClickListener set for that button). 2. You don't call the method that takes you to your second activity.
To have a button that takes you to another activity, you need to add the button to the layout, and then add the onClickListener, which executes your goToWelcomeActivityMethod when you click on it, like so:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public static final String EXTRA_MESSAGE = "Something Here....";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//this will insert the button into the Main Activity layout
Button goToWelcome = (Button)findViewById(R.id.goToWelcome);
//this is the onClickListener which will call the method to go to the next activity
goToWelcome.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
goToWelcomeActivityMethod();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//you don't need to pass in the View
public void goToWelcomeActivityMethod()
{
Intent intent = new Intent(this, WelcomeActivity.class);
EditText editText = (EditText) findViewById(R.id.welcomeMessage);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Then in your XML file make sure you add the button too:
<Button
android:id="#+id/goToWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Go To Welcome" />
in the main activity's layout XML, add the attribute android:onClick="goToWelcomeActivityMethod" to the button declaration.
In this way, the method goToWelcomeActivityMethod is called when you press the button.
For the "other" way, check https://developer.android.com/reference/android/widget/Button.html

Categories

Resources