Can somebody help me in Android studio - java

I created a wallpapers and ringtones app.
Wallpapers and Ringtones are 2 buttons.
These 2 buttons open each other a new page.
In the wallpapers page I have a button which I want to open a new page, but it doesn't work.
Below I pasted java and the manifest from android studio.
Main activity java:
public class MainActivity extends AppCompatActivity {
#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;
}
public void onButtonClicks(View c) {
if (c.getId() == R.id.Bdisplay) {
Intent i = new Intent(MainActivity.this, display.class);
startActivity(i);
overridePendingTransition(R.animator.animation1, R.animator.animation2);
}
}
public void onClick (View v) {
if (v.getId() == R.id.Bdisplay) {
Intent intent = new Intent(MainActivity.this, display.class);
startActivity(intent);
overridePendingTransition(R.animator.animation1, R.animator.animation2);
}
}
public void onButtonClick(View x) {
if (x.getId() == R.id.Bdisplay) {
Intent i = new Intent(MainActivity.this, ringtone.class);
startActivity(i);
overridePendingTransition(R.animator.animation1, R.animator.animation2);
}
}
ringtone java code:
import android.app.Activity;
import android.os.Bundle;
public class ringtone extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ringtone);
}
display(the name for wallpapers) java code:
import android.app.Activity;
import android.os.Bundle;
public class display extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
}
}
And the java code for the new page:
import android.app.Activity;
import android.os.Bundle;
public class newpage extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
>
<application
android:allowBackup="true"
android:icon="#mipmap/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:name=".display"></activity>
<activity android:name=".ringtone"></activity>
<activity android:name=".newpage"></activity>
</application>
</manifest>
What is am I doing wrong?

I have some suggestions for you:
Always name Activities with capital letter and camelcase: NewPage instead of newpage and Ringtone instead of ringtone. this helps you with variables/activities & methods identifications.
Calling an OnClick method use an explicit way.
Do like this:
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//do stuffs here
}
});`
than yes, you always call the same class, probably this is why it doesn't work ;)
EDIT: to fix the problem try this:
in your Activities u have two buttons which send users to ringtones or wallpapers. Imagine they are called WallpaperButton and RingtoneButton; both in the activity called MainActivity, this activity should look like:
public class MainActivity extends AppCompatActivity {
Button btnRingtone, btnWallpaper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRingtone = (Button) findViewById(R.id.RingtoneButton);
btnWallpaper = (Button) findViewById(R.Id.WallpaperButton);
btnRingtone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//Call the ringtone activity as "RingtoneActivity"
Intent i = new Intent(MainActivity.this, RingtoneActivity.class);
StartActivity(i);
}
});`
btnWallpaper.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//Call the wallpaper activity as "WallpaperActivity"
Intent i = new Intent(MainActivity.this, WallpaperActivity.class);
StartActivity(i);
}
});`
}
}
With this code in your mainactivity if an user clicks on the button wallpaper, he goes to wallpaper activity, if he clicks on button ringtone he goes to ringtone activity.
Do the same with the NewPageActivity. If you have trouble on how to create "SetOnClickListener" look here for an example.
If you have troubles or questions feel free to ask :)

Related

How to maintain Android Activity

I created a simple example application that creates a button in MainActivity to call the SecondActivity.
The application works well.
But I found something strange.
If I press the home key to go outside and run the launcher of the application, it returns to Main Activity, not SecondActivity.
Through a search, I got the following guide:
Intent intent = new Intent(MainActivity.this, SecondMainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); //<- add option
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //<- add option
startActivity(intent);
This confirmed that the Activity was maintained as I wanted.
However, when using the back key, the application program ended without returning to MainActivity!
The questions I want to ask are as follows.
How to maintain SecondActivity when I press the home key to go outside and run the program again without terminating the program in SecondActivity.
How to return to Main Activity when I press the back key in Second Activity.
I hope 1) and 2) will be satisfied at once.
Source code is as follows
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication2">
<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=".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=".SecondMainActivity"
android:label="SecondMainActivity"></activity>
</application>
</manifest>
MainActivity
package com.example.myapplication2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button GoToNewActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoToNewActivity = (Button)findViewById(R.id.button1);
GoToNewActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Intent code for open new activity through intent.
Intent intent = new Intent(MainActivity.this, SecondMainActivity.class);
// //[1]
// //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
// //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//
// //[2]
// intent.addFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
}
}
SecondMainActivity
package com.example.myapplication2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class SecondMainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_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.second_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);
}
}
You can download the application source.
1.If you going to home screen that because of your app is restarting
android system clear memory automatically after some time.
2.You don't need "intent.flag" tags , just start the activity and as Gunesh Shanbhag said in comment activity are arranged in stack. You
don't need to write code for that is automatic process but you can
ovveride the onbackpressed method to do something else like press
back button twice to exit the app.

Android - by clicking instead of going forward is back to the previous page

I use Android Studio, created four new activity each button. I try every button leads to the next page.
After I attach all the buttons I run the app and it really works.
When I press the button on the first page it takes me to the second page, but when I press the button on the second page to go to the third, it takes me back to the first page.
first activity:
package liranbenzino.kids;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View viewBtnClick) {
// link to class
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}});
}}
second activity:
package liranbenzino.kids;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View viewBtnClick1) {
// link to class
Intent intent = new Intent(Main2Activity.this,Main3Activity.class);
startActivity(intent);
}});
}
}
thied activity:
package liranbenzino.kids;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Main3Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View viewBtnClick) {
// link to class
Intent intent = new Intent(Main3Activity.this,Main4Activity.class);
startActivity(intent);
}});
}
}
four activity:
package liranbenzino.kids;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import android.view.View;
public class Main4Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View viewBtnClick) {
Toast.makeText(Main4Activity.this,"NAME", Toast.LENGTH_LONG).show();
}});
}}
AndroidMenifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="liranbenzino.kids">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity" />
<activity android:name=".Main3Activity" />
<activity android:name=".Main4Activity"></activity>
</application>
</manifest>
thank'S
Your code is ok
How your decide that you back to firstPage when your click 2nd activity button. your code is ok.. use label in each page. then try ...
May be your some button label is same and you become confuse in which page you are in..
You can change your button label as First_Page_Button,Second_Page_Button,Third_Page_Button etc to decide in which page you are...
in each activity change the INTENT name for example in activity2 change
Intent intent = new Intent(Main2Activity.this,Main3Activity.class);
startActivity(intent);
to
Intent intent2 = new Intent(Main2Activity.this,Main3Activity.class);
startActivity(intent2);

Preventing WebView reload on Rotate - Android Studio

When I rotate my screen, the WebView reloads the whole page. So I disabled rotation in Android_Manifest.xml file, but it would be really cool if I could make rotation possible.
SkateTube.java -
package com.example.skatetube.skatetube;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class SkateTube extends AppCompatActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_skatetube);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
if (savedInstanceState == null)
{
mWebView.loadUrl("http://skatetube.sytes.net/");
mWebView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
#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_skate_tube, 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);
}
}
Android_manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.skatetube.skatetube">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SkateTube"
android:theme="#style/AppTheme.NoActionBar"
android:configChanges="keyboard|keyboardHidden|orientation"
android:label="State preserving implementation"/>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
There are 3 points must be done. This is a full solution for me.
AndroidManifest.xml need config both orientation and screenSize in android:configChanges for target activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.skatetube.skatetube">
<application
<activity android:name=".WebViewActivity"
android:configChanges="orientation|screenSize">
</activity>
</application>
</manifest>
Need to override onSaveInstanceState and onRestoreInstanceState of Activity to make state can be restore when rotated
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
mWebView.restoreState(savedInstanceState);
}
Not loading URL again when screen rotate, use savedInstanceState to know current status in onCreate() of target activity
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
mWebView = (WebView) findViewById(R.id.webView_h5);
if (savedInstanceState == null) {
mWebView.loadUrl(url);
}
}
Miss any one of those three, my activity still reload.
All you need to do is Override onSaveInstanceState and onRestoreInstanceState.
#Override
protected void onSaveInstanceState(Bundle outState ){
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
mWebView.restoreState(savedInstanceState);
}
and one other solution could be to update your Activity tag in manifest to
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
well no need of java code just copy this code in place of your activity tag in Manifest.xml
<activity
android:name=".SkateTube"
android:theme="#style/AppTheme.NoActionBar"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:label="State preserving implementation"/>

how to open another activity in android using a button

so the code below is not doing the proper function it is called to do
it is supposed to open up a new activity upon a click of a button, but instead
nothing happens buttons display and no errors
and this stupid website is requiring me to explain a little more so im just going type random nonsense until it lets me post my question
package com.Tripp.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setting up the button references
Button jokeD = (Button) findViewById(R.id.jokeoftheday);
Button jokeC = (Button) findViewById(R.id.jokecatagories);
jokeD.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Menu.this, JokeOfTheDay.class));
}
});
jokeC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent s = new Intent("com.Tripp.thebasics.JOKECATAGORIES");
startActivity(s);
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
create seperate xml and java files for new activity.
Create on click listener like below for the button.
Button.setOnClickListener() {
public void onClick() {
Intent myintent = new Intent(this, newactivity.class);
startActivity(myintent);
}
}
3.add new activity on android manifest.xml file.
<application>
`<activity android:name=".classname" ></activity>`
</application
If you want more clarifications ,inform me
Help with creating an intent such that you can call an activity when a button is clicked. Also need help declaring an activity in a manifest file:
myBtn.setOnClickListener() {
public void onClick() {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
jokeD.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(this, JokeOfTheDay.class));
}
});
And make sure you have your new class in the AndroidManifest.xml:
<activity
android:name=".youractivitypackagename.JokeOfTheDay"
android:label="JokeOfTheDay" >
</activity>
EDIT:
Take a look here for a better explanation of how to declare an activity in the Manifest, as it depends on how you declare the package: http://developer.android.com/guide/topics/manifest/manifest-intro.html.
If your package for your class is, for example, com.example.project.Test, then you should have the following, within the tags:
<activity
android:name="com.example.project.Test.JokeOfTheDay"
android:label="JokeOfTheDay" >
</activity>

Android app crashes on phone.

I've tried to develop my own app but now that i wanna see how it looks on my phone it says "Appname has stopped". Im installing the app via dropbox.
Here's a copy of the MainActivity.java
package com.example.aikk;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mBtn1 = (Button) findViewById(R.id.northpowerramsor);
mBtn1.setOnClickListener((OnClickListener) this);
}
#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;
}
/** Called when the user clicks the Send button */
public void onClick(View view) {
Log.i("clicks","You Clicked B1");
Intent i=new Intent(MainActivity.this, Ramsor.class);
startActivity(i);
// Do something in response to button
}
}
And here's a copy of Ramsor.java
public class Ramsor extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_Ramsor);
//Vettefan var det här är till med när jag skulle göra quick fix på den kom jag till nån knas layout sida
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
And the Android.Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aikk"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="1"
android:targetSdkVersion="19"
android:maxSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.aikk.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.aikk.Ramsor"
android:label="#string/title_activity_ramsor"
android:parentActivityName="com.example.AIKK.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.AIKK.MainActivity" /> >
</activity>
</application>
</manifest>
Any thoughts would be GREATLY appreciated!
The problem is here , you are trying to cast the activity(this) to OnClickListener
mBtn1.setOnClickListener((OnClickListener) this);
The solution is to make your activity implements OnClickListener and create the unimplemented method
public class MainActivity extends Activity {
should be
public class MainActivity extends Activity implements OnClickListener {
There can be other reasons for the crash as well in which case it is better you post the stacktrace.
You can do as below using annonymous inner class
mBtn1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// do something
}
});
Change your class definition to implement OnClickListener:
public class MainActivity extends Activity implements OnClickListener {
...and set the click listener on mBtn1 as follows:
mBtn1.setOnClickListener(this);
try below this instead of mBtn1.setOnClickListener((OnClickListener) this);
mBtn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Activity to do onclick .
}
});
return true;

Categories