I'm developing an android application and I'm using Eclipse ADT.
I want to get the content of a webpage that say only true or false
Here is my code of my MainActivity.java
package com.appmobirep;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addButtonClickListner();
}
public void addButtonClickListner()
{
Button btnLogin = (Button)findViewById(R.id.btnTest);
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Need to get the webpage content and set it to a string
String Data = "";
}
});
}
#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;
}
}
Can you please help me on this
Thank you...
Related
as posts are limited in size on this platform I will post in parts first my mainactivity.java file package com.cancunsteve.aboutcancunsteve;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.MainActivity;
import android.NewActivity2;
public class MainActivity extends AppCompatActivity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.MyButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(MainActivity.this,
NewActivity2.class);
startActivity(myIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
You have to create a NewActivity2 class/activity
Your import is wrong for the NewActivity2, I doubt it is part of the android package.
I am making an app, and I need to reference a resource file while in the main activity. The path is res/menu/main.xml, how do I do this? Here is the source code.
package com.example.bradenmyers.quikiflipi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.view.Menu;
import android.view.MenuInflater;
public class MainActivity extends AppCompatActivity {
private PopupMenu menuInflator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R(This R is Red).layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflator().inflate(R(This R is also Red).menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
private MenuInflater getMenuInflator() {
return null;
}
}
The method launchAbout(null) is undefined for the type new View.OnClickListener(){}
package org.example.asteroides;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Asteroides extends Activity {
private Button bAbout;
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bAbout =(Button) findViewById(R.id.Button03);
bAbout.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
launchAbout(null);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.asteroides, menu);
return true;
}
}
you can fix it like this:
bAbout.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
launchAbout(null);
}
private void launchAbout(Object object){
....
}
});
or define a method named launchAbout in the class Asteroides
If this is your complete code, it's pretty obvious to get that error because method launchAbout is not declared.
If this is not your complete code, please edit your answer.
I'm making a WebView browser and I get an unreachable code error at one line of the MainActivity, I'm still learning Android coding so i apologize if this is a noob question I'll paste the code below. Thanks!
package com.browser.tssomas;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
public class MainActivity extends Activity{
WebView ourBrow;
Button go;
Button back;
Button refresh;
Button forward;
Button clearHistory;
EditText Url;
ProgressBar Pbar;
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;
ourBrow = (WebView) findViewById(R.id.wvBrowser); <-- UNREACHABLE CODE HERE
WebSettings webSettings = ourBrow.getSettings();
webSettings.setJavaScriptEnabled(true);
ourBrow.getSettings().setLoadWithOverviewMode(true);
ourBrow.getSettings().setUseWideViewPort(true);
ourBrow.getSettings().setBuiltInZoomControls(true);
ourBrow.getSettings().setAllowFileAccess(true);
refresh = (Button) findViewById(R.id.bRefresh);
Url = (EditText) findViewById(R.id.etURL);
Pbar = (ProgressBar) findViewById(R.id.progBar);
ourBrow.setWebViewClient(new InsideWebViewClient());
ourBrow.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
Pbar.setVisibility(ProgressBar.VISIBLE);
}
Pbar.setProgress(progress);
if(progress == 100) {
Pbar.setVisibility(ProgressBar.GONE);
}
}
});
{
ourBrow.loadUrl("http://www.google.com");
}
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.cH:
ourBrow.clearHistory();
return true;
case R.id.HAGAY:
String theWebsite = Url.getText().toString();
if(theWebsite != null)
ourBrow.loadUrl(theWebsite);
return true;
case R.id.Forward:
if (ourBrow.canGoForward())
ourBrow.goForward();
return true;
case R.id.back:
if (ourBrow.canGoBack())
ourBrow.goBack();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
class myWebClient extends WebViewClient
{
public void refreshButtonClicked(View view)
{
ourBrow.reload();
}
}
}
Move the return true; statement to the end of your method. In Java, returning from a method means that the method stops running.
Therefore, it is impossible for the code ourBrow = (WebView) findViewById(R.id.wvBrowser); to ever execute, because the method is instructed to stop right before it. This gives a compile time error.
I'm trying to make a game for adroid and am having some problems with my windows.
I can open the "newgame" window from the "mainactivity", I'm trying to work on the "back" button, but I can't get it to work.
I also can't open new windows from the second "newgame" window (was testing if it would start to "load")
This may be a stupid mistake but I have no idea why its not working. Most app tutorials deal with one window and thus don't help me
My code:
mainactivity.java:
package dream.o.eternaty;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button NewGame = (Button) findViewById(R.id.button1);
NewGame.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.newgame);
}
});
final Button Load = (Button) findViewById(R.id.button2);
Load.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.loadgame);
}});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
newgame.java:
package dream.o.eternaty;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class NewGame extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newgame);
final Button Back = (Button) findViewById(R.id.newgameback);
Back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.loadgame);
}
});
};
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;
}});
}
You shouldn't be transitioning the views, but actually starting a new activity for NewGame, as that's what you've declared it as, you can do it like this
Intent newIntent = new Intent(MainActivity.this,NewGame.class);
startActivityForResult(newIntent, 0);
in your MainActivity's onClick listener