requestFeature() must be called before adding content exception - java

I've seen lots of people asking about this and usually the answer has been something along the lines of: "Do it before
setContentView(R.layout.test_layout);
or
Do it before super.onCreate(savedInstanceState);
I've tried them both, but putting it before
setContentView(R.layout.test_layout);
gives the exception, where as putting it before:
super.onCreate(savedInstanceState);
doesn't give an exception, but it doesn't work either...
I got the fullscreen working by putting:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);`
before
super.onCreate(savedInstanceState);
I've seen plenty of other ways to force noTitleScreen and fullscreen, but none of them seemed to work. (No errors on most of the other ways, they just didn't seem to do anything) Like changing the layout theme... Did nothing for some reason.
I'm using Android Studio.
Any help would be appreciated.
This is the whole activity. I feel the need to defend myself by saying this is my first time developing with java, so excuse me if I have any obvious mistakes here :3 ->
package the1n07.bt_rc;
import android.content.Intent;
import android.gesture.Gesture;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
public class ControlActivity extends ActionBarActivity {
Button speedPlusButton, speedMinusButton, forwardButton, backwardButton, leftButton, rightButton;
EditText speedText;
public String speed;
public Integer speedInt;
public boolean forward = false, backward = false, left = false, right = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
//have tried putting the line here
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//and here
super.onCreate(savedInstanceState);
//and here
setContentView(R.layout.test_layout);
//and even here just in case, though I'm pretty sure it's not supposed to go here
Main();
}
public void Main(){
speedPlusButton = (Button) findViewById(R.id.speedPlusButton);
speedMinusButton = (Button) findViewById(R.id.speedMinusButton);
speedText = (EditText) findViewById(R.id.speedText);
forwardButton = (Button) findViewById(R.id.forwardButton);
backwardButton = (Button) findViewById(R.id.backwardButton);
leftButton = (Button) findViewById(R.id.leftButton);
rightButton = (Button) findViewById(R.id.rightButton);
speedPlusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speed = speedText.getText().toString();
speedInt = Integer.parseInt(speed);
if(speedInt != 5) {
speedInt = speedInt + 1;
}
speed = Integer.toString(speedInt);
speedText.setText(speed);
}
});
speedMinusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speed = speedText.getText().toString();
speedInt = Integer.parseInt(speed);
if(speedInt != 1) {
speedInt = speedInt - 1;
}
speed = Integer.toString(speedInt);
speedText.setText(speed);
}
});
forwardButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
forward = true;
forwardButton.setBackgroundColor(0xFF7D7D7D);
break;
case MotionEvent.ACTION_UP:
forward = false;
forwardButton.setBackgroundColor(0xff222222);
break;
case MotionEvent.ACTION_CANCEL:
forwardButton.setBackgroundColor(0xff222222);
forward = false;
}
return false;
}
});
backwardButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
backward = true;
backwardButton.setBackgroundColor(0xFFB9B9B9);
break;
case MotionEvent.ACTION_UP:
backward = false;
backwardButton.setBackgroundColor(0xffffffff);
break;
case MotionEvent.ACTION_CANCEL:
backwardButton.setBackgroundColor(0xffffffff);
backward = false;
}
return false;
}
});
leftButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
left = true;
leftButton.setBackgroundColor(0xFF7D7D7D);
break;
case MotionEvent.ACTION_UP:
left = false;
leftButton.setBackgroundColor(0xff222222);
break;
case MotionEvent.ACTION_CANCEL:
leftButton.setBackgroundColor(0xff222222);
left = false;
}
return false;
}
});
rightButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
right = true;
rightButton.setBackgroundColor(0xFFB9B9B9);
break;
case MotionEvent.ACTION_UP:
right = false;
rightButton.setBackgroundColor(0xffffffff);
break;
case MotionEvent.ACTION_CANCEL:
rightButton.setBackgroundColor(0xffffffff);
right = false;
}
return false;
}
});
}
}

Do like this:
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
}

Related

Cleaning up a one file project

So I'm working on a project and am using a lot of code or atleast a decent amount all in one file and am wondering if there is a way to clean it up into multiple files. I've only every worked with single file. So when It come to adding more files I'm not really sure how to go about this so I'm gonna ask for help on cleaning up the project. the ideal outcome is to have it more understandable via small chunks in other files to clean up the CoreActivity. or if you see a way to clean up some code into a easier and less exhaustive way of writing it then do fix
CoreActivity.java
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;
import com.github.pwittchen.gesture.library.Gesture;
import com.github.pwittchen.gesture.library.GestureListener;
import com.github.pwittchen.swipe.library.rx2.Swipe;
import com.github.pwittchen.swipe.library.rx2.SwipeListener;
public class CoreActivity extends AppCompatActivity {
//Widgets
ConstraintLayout m_Preference_Toolbar;
ConstraintLayout m_Toolbar;
ConstraintLayout m_Preferences;
EditText m_WebView_Search;
WebView m_WebView;
private Swipe WebSwipe;
private Gesture WebGesture;
//Variables
String Url = "https://www.Google.ca";
int WebS =0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_core);
//Find Views
m_Toolbar = findViewById(R.id.m_Toolbar);
m_Preferences = findViewById(R.id.m_Preferences);
m_WebView_Search = findViewById(R.id.m_WebView_Search);
m_WebView = findViewById(R.id.m_WebView);
m_Preference_Toolbar = findViewById(R.id.m_Preferences_Toolbar);
m_Preference_Toolbar.setVisibility(View.GONE);
m_Preferences.setOnTouchListener(new View.OnTouchListener() {
Animation ToolbarGone,ToolbarPrefVisible;
int x = 0;
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
m_Preferences.setVisibility(View.GONE);
x =1;
ToolbarGone = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_toolbar_gone);
m_Toolbar.startAnimation(ToolbarGone);
if(m_Preferences.getVisibility() == View.GONE || x == 1) {
ToolbarPrefVisible = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_preference_toolbar_appear);
m_Preference_Toolbar.startAnimation(ToolbarPrefVisible);
}
return false;
}
});
//m_WebView
final WebSettings m_WebViewSetting = m_WebView.getSettings();
m_WebView.setWebViewClient(new WebViewClient());
m_WebViewSetting.setJavaScriptEnabled(true);
m_WebViewSetting.setSupportZoom(true);
m_WebViewSetting.setLoadWithOverviewMode(true);
m_WebViewSetting.setUseWideViewPort(true);
m_WebView.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
m_WebView.loadUrl(Url); //Default HomePage
//m_WebView_Search TextView and Keyboard
m_WebView_Search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
if (m_WebView_Search.getText().toString().contains(".com")
|| m_WebView_Search.getText().toString().contains(".ca")
|| m_WebView_Search.getText().toString().contains(".net")
|| m_WebView_Search.getText().toString().contains(".org")) {
Url = m_WebView_Search.getText().toString();
m_WebView.loadUrl("https://www." + Url);
m_WebView_Search.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(m_WebView_Search.getWindowToken(), 0);
} else {
if (m_WebView_Search.getText().toString().contains("")) {
Url = m_WebView_Search.getText().toString();
m_WebView.loadUrl("https://www.google.ca/search?q=" + Url);
m_WebView_Search.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(m_WebView_Search.getWindowToken(), 0);
}
}
}
return false;
}
});
//Swipe Events WebSwipe
WebSwipe = new Swipe(350, 700);
WebSwipe.setListener(new SwipeListener() {
int Web = 0;
Animation WebShrink, WebEnlarge;
#Override
public void onSwipingLeft(final MotionEvent event) {
if(m_WebView.canGoForward()){
m_WebView.goForward();
}
else{
m_WebView.reload();
}
}
#Override
public void onSwipedLeft(final MotionEvent event) {
}
#Override
public void onSwipingRight(final MotionEvent event) {
if(m_WebView.canGoBack()){
m_WebView.goBack();
}
else{
m_WebView.reload();
}
}
#Override
public void onSwipedRight(final MotionEvent event) {
}
#Override
public void onSwipingUp(final MotionEvent event) {
}
#Override
public void onSwipedUp(final MotionEvent event) {
//WebView Animation (Enlarge)
if(Web == 0){
WebEnlarge = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_webview_enlarge);
m_WebView.startAnimation(WebEnlarge);
Web = 1;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
m_Toolbar.setVisibility(View.GONE);
}
}, 350);
}
}
#Override
public void onSwipingDown(final MotionEvent event) {
}
#Override
public void onSwipedDown(final MotionEvent event) {
//WebView Animation (Shrink)
if(Web == 1){
WebShrink = AnimationUtils.loadAnimation(CoreActivity.this,R.anim.m_webview_shrink);
m_WebView.startAnimation(WebShrink);
m_Preference_Toolbar.setVisibility(View.GONE);
Web = 0;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
m_Toolbar.setVisibility(View.VISIBLE);
}
}, 350);
}
}
});
WebGesture = new Gesture();
WebGesture.addListener(new GestureListener() {
#Override public void onPress(MotionEvent motionEvent) {
}
#Override public void onTap(MotionEvent motionEvent) {
}
#Override public void onDrag(MotionEvent motionEvent) {
}
#Override public void onMove(MotionEvent motionEvent) {
}
#Override public void onRelease(MotionEvent motionEvent) {
}
#Override public void onLongPress(MotionEvent motionEvent) {
}
#Override public void onMultiTap(MotionEvent motionEvent, int clicks) {
}
});
}
#Override
public boolean dispatchTouchEvent (MotionEvent event){
WebSwipe.dispatchTouchEvent(event);
WebGesture.dispatchTouchEvent(event);
return super.dispatchTouchEvent(event);
}
}
The simplest way to split code is to think about Single Responsibility (as per the SOLID principles).
As an example in your code, your touch listener can be separated. This would entail making a class that extended the touch listener's class (View.OnTouchListener) instead of extending an activity.
This block in your code:
m_Preferences.setOnTouchListener(new View.OnTouchListener() {
...
});
Would become MyTouchListener.java:
public class MyTouchListener implements View.OnTouchListener {
Animation ToolbarGone,ToolbarPrefVisible;
int x = 0;
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
... // keep exactly your same code here
}
}
And then your activity gets changed to:
m_Preferences.setOnTouchListener(new MyTouchListener());

How do I go about doing an onTouchListener that allows pressing two buttons at the same time on android

What I mean is, that I want to be able to press two separate buttons at the same time and have them do things on ACTION_DOWN and UP.
I have multiple onTouchListeners, but the app only lets me press one at a time. I need to be able to press at least two at the same time. I assume it has to do with the program getting stuck in a loop when the first button is being held down?
I read something about ACTION_POINTER1_DOWN and ACTION_POINTER2_DOWN, but I have to revise something to make them work right? Or am I even on the right track here?
package the1n07.bt_rc;
import android.content.Intent;
import android.gesture.Gesture;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
public class ControlActivity extends ActionBarActivity {
Button speedPlusButton, speedMinusButton, forwardButton, backwardButton, leftButton, rightButton;
EditText speedText;
public String speed;
public Integer speedInt;
public boolean forward = false, backward = false, left = false, right = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
Main();
}
public void Main(){
speedPlusButton = (Button) findViewById(R.id.speedPlusButton);
speedMinusButton = (Button) findViewById(R.id.speedMinusButton);
speedText = (EditText) findViewById(R.id.speedText);
forwardButton = (Button) findViewById(R.id.forwardButton);
backwardButton = (Button) findViewById(R.id.backwardButton);
leftButton = (Button) findViewById(R.id.leftButton);
rightButton = (Button) findViewById(R.id.rightButton);
speedPlusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speed = speedText.getText().toString();
speedInt = Integer.parseInt(speed);
if(speedInt != 5) {
speedInt = speedInt + 1;
}
speed = Integer.toString(speedInt);
speedText.setText(speed);
}
});
speedMinusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speed = speedText.getText().toString();
speedInt = Integer.parseInt(speed);
if(speedInt != 1) {
speedInt = speedInt - 1;
}
speed = Integer.toString(speedInt);
speedText.setText(speed);
}
});
forwardButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("forward", "is held down");
forward = true;
forwardButton.setBackgroundColor(0xFF7D7D7D);
break;
case MotionEvent.ACTION_UP:
Log.d("forward", "is lifted");
forward = false;
forwardButton.setBackgroundColor(0xff222222);
break;
case MotionEvent.ACTION_CANCEL:
forwardButton.setBackgroundColor(0xff222222);
forward = false;
break;
}
return true;
}
});
backwardButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
backward = true;
backwardButton.setBackgroundColor(0xFFB9B9B9);
break;
case MotionEvent.ACTION_UP:
backward = false;
backwardButton.setBackgroundColor(0xffffffff);
break;
case MotionEvent.ACTION_CANCEL:
backwardButton.setBackgroundColor(0xffffffff);
backward = false;
break;
}
return true;
}
});
leftButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("left", "is held down");
left = true;
leftButton.setBackgroundColor(0xFF7D7D7D);
break;
case MotionEvent.ACTION_UP:
Log.d("left", "is lifted");
left = false;
leftButton.setBackgroundColor(0xff222222);
break;
case MotionEvent.ACTION_CANCEL:
leftButton.setBackgroundColor(0xff222222);
left = false;
break;
}
return true;
}
});
rightButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
right = true;
rightButton.setBackgroundColor(0xFFB9B9B9);
break;
case MotionEvent.ACTION_UP:
right = false;
rightButton.setBackgroundColor(0xffffffff);
break;
case MotionEvent.ACTION_CANCEL:
rightButton.setBackgroundColor(0xffffffff);
right = false;
break; //Does last case need break?
}
return true;
}
});
}
}
This question may have already been answered by these two links : 1).Android - How to handle two finger touch.
2).Detect multitouch with Ontouch listener Android
The following links was also posted there :
1).http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html
2).http://developer.android.com/training/gestures/multi.html

How to play music that is added on both the main screen and the second screen?

I am new to the android world. I am developing an app that has 6 buttons on main screen and 5 buttons on the second screen. The 5 buttons on the main screen should be able to play the music when the user click on those buttons. And each buttons should be able to play different music that corresponds to the music that is located in the raw folder. The 6th button on the main screen should allow the user to go to the next screen (Second Screen). Likewise, the 5 buttons on the second screen should play the music that corresponds to the music in the raw folder. Please look at my code.. I don't know what is wrong with my code ?
My main Java code looks like the following...
public class MainActivity extends Activity implements OnClickListener{
public class onClickListener implements OnClickListener {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent("com.example.playaudio.SecondActivity");
startActivity(i);
}
}
public class onClick {
}
private MediaPlayer mp;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonSwitch);
button.setOnClickListener(new onClickListener());
setVolumeControlStream(AudioManager.STREAM_MUSIC);
Button button1=(Button)findViewById(R.id.button_1);
Button button2=(Button)findViewById(R.id.button_2);
Button button3=(Button)findViewById(R.id.button_3);
Button button4=(Button)findViewById(R.id.button_4);
Button button5=(Button)findViewById(R.id.button_5);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
}
private void setOnClickListener(
com.example.playaudio.MainActivity.onClick onClick) {
// TODO Auto-generated method stub
}
public static void onClick(View v) {
int resId;
switch (v.getId()) {
case R.id.button_1:
resId = R.raw.a;
break;
case R.id.button_2:
resId = R.raw.b;
break;
case R.id.button_3:
resId = R.raw.c;
break;
case R.id.button_4:
resId = R.raw.d;
break;
case R.id.button_5:
resId = R.raw.e;
break;
default:
resId = R.raw.a;
break;
}
// Release any resources from previous MediaPlayer
if (mp != null) {
mp.release();
}
// Create a new MediaPlayer to play this sound
mp = MediaPlayer.create(this, resId);
mp.start();
}
{
startActivity(new Intent(MainActivity.this,SecondActivity.class));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
protected void onDestroy() {
if(null!=mp){
mp.release();
}
super.onDestroy();
}
}
Second Screen Java class
import com.example.playaudio.MainActivity.onClick;
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
b= (Button) findViewById (R.id.buttonSwitch);
b.setOnClickListener(new onClick());
setVolumeControlStream(AudioManager.STREAM_MUSIC);
Button button7=(Button)findViewById(R.id.button7);
Button button8=(Button)findViewById(R.id.button8);
Button button9=(Button)findViewById(R.id.button9);
Button button10=(Button)findViewById(R.id.button10);
Button button11=(Button)findViewById(R.id.button11);
button7.setOnClickListener((OnClickListener) this);
button8.setOnClickListener((OnClickListener) this);
button9.setOnClickListener((OnClickListener) this);
button10.setOnClickListener((OnClickListener) this);
button11.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
int resId;
switch (v.getId()) {
case R.id.button7:
resId = R.raw.f;
break;
case R.id.button8:
resId = R.raw.g;
break;
case R.id.button9:
resId = R.raw.h;
break;
case R.id.button10:
resId = R.raw.i;
break;
case R.id.button11:
resId = R.raw.j;
break;
default:
resId = R.raw.a;
break;
}
{
startActivity(new Intent(MainActivity.this,SecondActivity.class));
}
I think that you don't prepare your mediaplayer. I leave you a code that works. I always prepare the same song. Remember change it by your music and create an "assets" folder where you can put your ".mp3" files.
public class MediaPlayerActivity extends ActionBarActivity implements View.OnClickListener {
private Button mButton1;
private Button mButton2;
private Button mButton3;
private Button mButton4;
private Button mButton5;
private Button mButton6;
private MediaPlayer mPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mButton1 = (Button) findViewById(R.id.button1);
mButton2 = (Button) findViewById(R.id.button2);
mButton3 = (Button) findViewById(R.id.button3);
mButton4 = (Button) findViewById(R.id.button4);
mButton5 = (Button) findViewById(R.id.button5);
mButton6 = (Button) findViewById(R.id.button6);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
mButton4.setOnClickListener(this);
mButton5.setOnClickListener(this);
mButton6.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
playMusic("lori.mp3");
break;
case R.id.button2:
playMusic("lori.mp3");
break;
case R.id.button3:
playMusic("lori.mp3");
break;
case R.id.button4:
playMusic("lori.mp3");
break;
case R.id.button5:
playMusic("lori.mp3");
break;
case R.id.button6:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
break;
}
}
private void playMusic(String songName) {
if (mPlayer != null && mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
try {
mPlayer = new MediaPlayer();
mPlayer.reset();
AssetFileDescriptor descriptor = getAssets().openFd(songName);
mPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I hope it works for you. Remember mark the answer as correct if this code works for you. Thanks!

How to save URLs to view later on

I have an application with a WebView.
But I want to make it save URLs, for example: when the application starts, it opens google.com. So if I want to type something in and save that search or website, I want it to have a button: "Save site" and "Load site". How do I do this as simple as possible?
I am using this code so far:
package com.mysoftwaremobileapps.Finnnofortablets;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class finnnoActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button TilbakeButton;
WebView FinnWeb;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FinnWeb = (WebView)findViewById(R.id.FinnWeb);
FinnWeb.setWebViewClient(new MyWebViewClient());
FinnWeb.getSettings().setBuiltInZoomControls(true);
FinnWeb.getSettings().setJavaScriptEnabled(true);
FinnWeb.loadUrl("http://m.finn.no");
FinnWeb.requestFocus(View.FOCUS_DOWN);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
setContentView(R.layout.main);
FinnWeb = (WebView) findViewById(R.id.FinnWeb);
FinnWeb.setWebViewClient(new MyWebViewClient());
FinnWeb.getSettings().setJavaScriptEnabled(true);
FinnWeb.getSettings().setBuiltInZoomControls(true);
FinnWeb.requestFocus(View.FOCUS_DOWN);
FinnWeb.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onCreateOptionsMenu(Menu meny) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menubuttons, meny);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.LoggInn:
setContentView(R.layout.main);
FinnWeb = (WebView) findViewById(R.id.FinnWeb);
FinnWeb.setWebViewClient(new MyWebViewClient());
FinnWeb.getSettings().setJavaScriptEnabled(true);
FinnWeb.loadUrl("http://m.finn.no/#loginForm.html");
FinnWeb.requestFocus(View.FOCUS_DOWN);
break;
case R.id.OmUtvikler:
setContentView(R.layout.omutvikler);
TilbakeButton = (Button)findViewById(R.id.TilbakeButton);
TilbakeButton.setOnClickListener(this);
break;
case R.id.RegistrerDeg:
setContentView(R.layout.main);
FinnWeb = (WebView) findViewById(R.id.FinnWeb);
FinnWeb.setWebViewClient(new MyWebViewClient());
FinnWeb.getSettings().setJavaScriptEnabled(true);
FinnWeb.loadUrl("http://m.finn.no/#auth/registrer.html");
FinnWeb.requestFocus(View.FOCUS_DOWN);
break;
case R.id.Tilbake:
if (FinnWeb.canGoBack()) {
FinnWeb.goBack();
}
else{
}
break;
case R.id.Frem:
if (FinnWeb.canGoForward()) {
FinnWeb.goForward();
}
else{
}
break;
case R.id.ExitWithSaving:
System.exit(0);
break;
case R.id.Exit:
System.exit(0);
break;
case R.id.LastAnnonse:
break;
}
return true;
}
public void onClick(View src) {
switch(src.getId()) {
case R.id.TilbakeButton:
setContentView(R.layout.main);
FinnWeb = (WebView) findViewById(R.id.FinnWeb);
FinnWeb.setWebViewClient(new MyWebViewClient());
FinnWeb.getSettings().setJavaScriptEnabled(true);
FinnWeb.getSettings().setBuiltInZoomControls(true);
FinnWeb.requestFocus(View.FOCUS_DOWN);
break;
}
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
There are a couple of different ways to do this. The first is to remember the URL via a hook in your already overridden "shouldOverrideUrlLoading".
Like:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{ view.loadUrl(url);
//save off url in instance variable
this.currentURL = url.toString();
return true;
}
Secondly is to call out to your webView to get the current URL when the user clicks on your mentioned "Save Site" button via the method:
http://developer.android.com/reference/android/webkit/WebView.html#getUrl()
Either of these methods will allow you to access what URL is currently being displayed. I hope this helps.

play sound while button is pressed -android

i have this code
package com.tct.soundTouch;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class main extends Activity implements OnTouchListener {
private MediaPlayer mp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button zero = (Button) this.findViewById(R.id.button);
zero.setOnTouchListener(this);
mp = MediaPlayer.create(this, R.raw.sound);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
case MotionEvent.ACTION_UP:
mp.pause();
}
return true;
}
}
and it works but not as i expected. The sound plays but only for each time that i press the button. My idea is. While i press the button the sound plays, when i stop the action (finger out of the button) music pause.
Any idea please?
thanks
This should work (there was something wrong with your switch-cases I think):
#Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
break;
case MotionEvent.ACTION_UP:
{
mediaPlayer.pause();
}
break;
}
return true;
}
public class MainActivity extends AppCompatActivity {
Button button;
MediaPlayer player;
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
button = findViewById( R.id.Click );
button.setOnTouchListener( new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()== MotionEvent.ACTION_DOWN) {
player= MediaPlayer.create( MainActivity.this,R.raw.horn );
player.start();
}
else if(event.getAction()==MotionEvent.ACTION_UP){
player.stop();
player.release();
}
return true;
}
} );
}
}

Categories