Android Studio, Call to different class in different file - java

I have written the piece of code below to reference to the class called Forecast Fragment, and the app runs. but on line 33, i am getting a error for "cannot resolve symbol 'android' "
package com.example.sunshinepre_beta;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
int commit = getSupportFragmentManager().beginTransaction()
.add(R.id.container, new com.example.android.sunshinepre_beta.app.ForecastFragment())
.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);
}
}

Replace new com.example.android.sunshinepre_beta.app.ForecastFragment() with new ForecastFragment()
Also move ForecastFragment.java to the same folder where MainActivity is.

Related

WebView videos on fullscreen

Please see below my code: I have tried several codes that fulllscreen for videos and I did not succeed, or I did not know how to install.
I need enable fullscreen for videos for any player video
package com.androidapp.www.WEBSITE;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView)findViewById(R.id.webView);
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(true);
wv.loadUrl("https://www.mfshd.net");
wv.setWebViewClient(new MobWebViewClient());
}
#Override
public void onBackPressed() {
if(wv.canGoBack()){
wv.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_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);
}
private class MobWebViewClient extends WebViewClient {
}
}
Firstly you have to add a FrameLayout in your activity_main and make its visibility "gone"
Secondly add this code to your MainActivity:
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
wv.setWebChromeClient(new WebChromeClient() {
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomView = view;
wv.setVisibility(View.GONE);
frameLayout.setVisibility(View.VISIBLE);
frameLayout.addView(view);
mCustomViewCallback = callback;
}
#Override
public void onHideCustomView() {
super.onHideCustomView();
if (mCustomView == null)
return;
wv.setVisibility(View.VISIBLE);
frameLayout.setVisibility(View.GONE);
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
frameLayout.removeView(mCustomView);
mCustomViewCallback.onCustomViewHidden();
mCustomView = null;
}
});

How do i implement the text to speech code in my new project?

The text to speech code i was doing it in Eclipse and it's working there great.
But now i created a new bigger project in Android Studio and i want to add the Text To Speech code to this project.
Both project and the code are in java.
This is the Text To Speech code:
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v7.app.ActionBarActivity;
import java.util.Locale;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import fi.iki.elonen.NanoHTTPD;
public class MainActivity extends ActionBarActivity implements OnInitListener {
private static final int MY_DATA_CHECK_CODE = 0;
TextToSpeech mTts;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
//tts = new TextToSpeech(this,(OnInitListener) this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTTS();
}
#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);
}
private void initTTS() {
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == MY_DATA_CHECK_CODE) {
if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
mTts = new TextToSpeech(this, this);
} else {
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
#SuppressWarnings("deprecation")
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.US);
if(result == TextToSpeech.LANG_AVAILABLE
|| result == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
mTts.setPitch(1);
mTts.speak("this is a voice test", TextToSpeech.QUEUE_FLUSH, null);
}
}
}
}
Should i add this code somehow to my MainActivity.java in my project in Android Studio ?
Maybe i should create a new class and somehow to implement the Text To Speech code there ?
This is my MainActivity.java code now:
package com.adi.webservertest;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity
{
#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);
}
/**
* Dispatch onStart() to all fragments. Ensure any created loaders are
* now started.
*/
#Override
protected void onStart()
{
super.onStart();
TextToSpeechServer.main(null);
}
#Override
protected void onStop() {
super.onStop();
}
}
And this is the code of TextToSpeechServer and from there i want to be able to call and use the Text To Speech code:
package com.adi.webservertest;
import java.util.Map;
/**
* An example of subclassing NanoHTTPD to make a custom HTTP server.
*/
public class TextToSpeechServer extends NanoHTTPD {
public TextToSpeechServer() {
super(8080);
}
#Override public Response serve(IHTTPSession session) {
Method method = session.getMethod();
String uri = session.getUri();
System.out.println(method + " '" + uri + "' ");
String msg = "<html><body><h1>Hello server</h1>\n";
Map<String, String> parms = session.getParms();
if (parms.get("username") == null)
msg +=
"<form action='?' method='get'>\n" +
" <p>Your name: <input type='text' name='username'></p>\n" +
"</form>\n";
else
msg += "<p>Hello, " + parms.get("username") + "!</p>";
msg += "</body></html>\n";
return new Response(msg);
}
public static void main(String[] args) {
ServerRunner.run(TextToSpeechServer.class);
}
}
Migrating From Eclipse Projects to Android Studio:- http://tools.android.com/tech-docs/new-build-system/migrating-from-eclipse-projects
Detailed Document : https://developer.android.com/sdk/installing/migrate.html

Android WebView app resetting on rotation

I am having some issues with webview where on rotation the app resets to the default URL. I have hunted round for a solution and tried a number on here and run into different issues with each.
This is what is in my .java code
package uk.co.grcade.grcade;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebViewClient;
public class GRcade extends Activity {
WebView web;
String webURL = "http://grcade.co.uk";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grcade);
if (savedInstanceState != null)
{
web.loadUrl(webURL);
}
web = (WebView) findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl(webURL);
web.setPadding(0, 0, 0, 0);
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setUseWideViewPort(true);
web.getSettings().setSupportZoom(true);
web.getSettings().setBuiltInZoomControls(true);
web.getSettings().setDisplayZoomControls(false);
}
#Override
protected void onSaveInstanceState (Bundle outState )
{
super.onSaveInstanceState(outState);
web.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
web.restoreState(savedInstanceState);
}
#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_grcade, menu);
return true;
}
private class HelloWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView web, String url) {
web.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int ketCode, KeyEvent event) {
if ((ketCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
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);
}
}
I got the code from a post on here but can't comment to ask why the line:
web = (WebView) findViewById(R.id.web);
is showing an unable to resolve symbol error, I am new to this whole thing so would appreciate some help on how I can get that error to go away and allow me to have a webview app that can be rotated without resetting back to the default page.
Thanks.
Your problem is in OnCreate method.
Your code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grcade);
if (savedInstanceState != null)
{
web.loadUrl(webURL);
}
.........
.........
You check if your Bundle object is not null and you load the URL. Instead, you should be restoring the state when the bundle object is not null. Something like this
if (savedInstanceState != null) {
webView.restoreState(savedInstanceState);
} else {
webView.loadUrl("http://grcade.co.uk");
}

java.lang.StackOverFlowError in android studio

i am working as a beginner on new android application, i have done everything according to the tutorials, but im still getting this error. here is the code for MyAcitivty.java:
this is MYActivity.java
package com.example.ambuj.myfirstapp;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.ambuj.myfirstapp";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return onCreateOptionsMenu(menu);
}
#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.
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void openSearch(){
Toast.makeText(this, "Search Button Pressed", Toast.LENGTH_LONG).show();
}
public void openSettings(){
Toast.makeText(this, "Settings Button Pressed",Toast.LENGTH_LONG).show();
}
}
this is my logcat:
01-18 14:22:33.180 26194-26194/com.example.ambuj.myfirstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ambuj.myfirstapp, PID: 26194
java.lang.StackOverflowError
at android.util.SparseArray.get(SparseArray.java:111)
at android.util.SparseArray.get(SparseArray.java:102)
at android.content.res.StringBlock.get(StringBlock.java:70)
at android.content.res.AssetManager.getPooledString(AssetManager.java:275)
at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:730)
at android.content.res.TypedArray.getText(TypedArray.java:97)
at android.support.v7.internal.view.SupportMenuInflater$MenuState.readItem(SupportMenuInflater.java:374)
at android.support.v7.internal.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:168)
at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
at com.example.ambuj.myfirstapp.MyActivity.onCreateOptionsMenu(MyActivity.java:32)
at com.example.ambuj.myfirstapp.MyActivity.onCreateOptionsMenu(MyActivity.java:33)
Yout problem is here:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return onCreateOptionsMenu(menu); //<--- conflicting line
}
Change
return onCreateOptionsMenu(menu);
to
return true;
Hope it helps

java - need help for keycode_home [duplicate]

This question already has answers here:
Keycode_home doesn't get called ANDROID
(4 answers)
Closed 8 years ago.
i need help in my app which shows a toast when home button is pressed, back button is pressed and it check if the phone have got the nav bar
MainActivity.java
package com.example.myapp;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Toast.makeText(getApplicationContext(), "goodbye! (home button pressed)", Toast.LENGTH_LONG).show(); //doesn't work here
finish();
return true;
}
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Toast.makeText(getApplicationContext(), "goodbye! (back button pressed)", Toast.LENGTH_LONG).show();
finish();
return true;
}
return false;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
if (hasBackKey && hasHomeKey) {
// no navigation bar
Toast.makeText(getApplicationContext(), "no nav bar", Toast.LENGTH_LONG).show();
} else {
// navigation bar
Toast.makeText(getApplicationContext(), "nav bar", Toast.LENGTH_LONG).show();
}
if (savedInstanceState == null) {
getFragmentManager().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;
}
}
}
I have got a nexus 5 so it has the nav bar, and I think this is the problem
how can I fix the prolem?
nexus 5, android 4.4.3
thanks
The framework never sends KEYCODE_HOME to apps.
Right now your best indicator of when the user leaves your app (as opposed to a single activity), is listening for TRIM_MEMORY_UI_HIDDEN in onTrimMemory(int).
public class MainActivity extends Activity {
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if (level == TRIM_MEMORY_UI_HIDDEN) {
Context ctx = getApplicationContext();
Toast.makeText(ctx, "gone", Toast.LENGTH_SHORT).show();
}
}
}
Also you should not finish() just because the user leaves, then when the user gets back the activity will always have to be recreated from scratch, without state.

Categories