Webview buttons stop working after website loads - android

I have created a simple webview with two buttons below it. If I press one of the button the view seems to create a new view called "web" then the page loads and I still have my buttons below but they don't function. If I use the back button on the phone it takes me to the opening view blank page and the buttons work again? Sorry I am new..
I just want it to load in the original view and have the buttons continue to function.
Do I have to suppress the creation of a new view somehow?
Kind Regards,
-Mike
** and I am not sure why my code always has extra crap when I post it because it does't when I copy it to the clipboard. **
Webscreen Class
package com.example.cam;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Webscreen <URL> extends Activity {
WebView webview1;
public static final String URL = "";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String turl = getIntent().getStringExtra(URL);
webview1 = (WebView)findViewById(R.id.webview01);
webview1.clearCache(true);
webview1.loadUrl(turl);
}
}
cam Class
package com.example.cam;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class cam extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Add Click listeners for all buttons
View firstButton = findViewById(R.id.button1);
firstButton.setOnClickListener(this);
View secondButton = findViewById(R.id.button2);
secondButton.setOnClickListener(this);
}
// Process the button click events
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.example.cam.Webscreen.URL,
"http://m.yahoo.com");
startActivity(j);
break;
case R.id.button2:
Intent k = new Intent(this, Webscreen.class);
k.putExtra(com.example.cam.Webscreen.URL,
"http://m.google.com");
startActivity(k);
break;
}
}
}

I don't know why are you not using Button class and using View Class.
Use
Button b1=(Button)findViewById(R.id.button1);
instead of
View b1=findViewById(R.id.button1);
Use relative layout for placement of buttons

On button click you are recreating your activity with the intent of displaying the web page/s.
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.example.cam.Webscreen.URL,
"http://m.yahoo.com");
startActivity(j);
it is only creating new activity on top of the new activity
you need to load the web page on button click instead.
//on button1 click
mWebView.loadUrl("http://m.yahoo.com");

Related

How to make a button open a new layout xml

I'm trying to make a button for my app, which will bring the screen to another page. However, I'm not successful in doing so.
I've tried many things, without a reliefing answer.
My project doesn't accept "Intent" in my program.
My button I need to open a new layout is called "OptionButton"
Here's what I've got:
in MainActivity.java
In the beginning I got this
public class MainActivity extends Activity {
private Button startButton;
private Button pauseButton;
private Button resetButton;
public Button OptionButton;
/** further I got this**/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
/** (I'm just mentioning this because I use savedInstanceState here too)**/
/**MainActivity.java And my code for my button is this **/
OptionButton = (Button) findViewById(R.id.Button1);
OptionButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myClick(v); /* my method to call new intent or activity */
}
public void myClick(View v) {
Intent intent = new Intent(this, Background2.class);
startActivity(intent);// for calling the activity
}
});
}
}
}
I added this in AndroidManifest:
<activity android:name=".Background2"></activity>
and this in the 2nd class (java file in src map)
(package & imports, then this:
public class Background2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
_
I got 2 classes in src map:
-Background2.java
-MainActivity.java
Also 2 layout xml's:
activity_main.xml
activity main2.xml
In Activity_main, I got this for the button:
<Button
android:id="#+id/Button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/OptionButtonLabel"/>
Still it's not working. What am I missing?
Ty all so much!
I've tried changing this:
button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(this);
But it didn't work.
Inside of the listener you are calling this (which refers to the listener itself), while what you want to refer to is the MainActivity.
Simply change to
Intent intent = new Intent(MainActivity.this, Background2.class);
You Main Activity has to implement OnClickListner
public class MainActivity extends Activity implements OnClickListener{
From Eclipse IDE press Ctrl+Shift+O it will automatically implement and import necessary functions
Thank you for the answers! :)
I've tried installing it on my real device.
Now, it opens, but it's the 2nd xml that opens, and when I click the button, it re-opens the same xml.
:-/
I don't get any error messages though when I changed your suggested solutions (from both of you)

Want to show webpage on clicking images in viewpager

I want to show webpage when we click an image in viewpager. Can you please help me in this. Also I want to know if multiple images are there in viewpager so how will I get the respective webpages of them.
Thanks in advance.
**MainActivity.java**
package com.example.viewimage;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
// open the desired page
Intent browserIntent = new Intent("android.intent.action.VIEW",
Uri.parse("http://www.craftsvilla.com/anvi-s-classic-nawabi-earrings-studded-with-white-stones-and-emeralds.html"));
startActivity(browserIntent);
}
});
}
}
If you want to show this webpage inside your app, you would have to add a WebView. Set its visibility to View.GONE. Add OnClickListener to your viewPager. On click set the visibility of the webView to be View.VISIBLE. To go back, override the functionality of 'back' key - and on its click hide the webpage.
Or you can open other activity that holds the webView.
You can also open the webpage with android's browser.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri);
startActivity(browserIntent);
Edited :
Add to your xml before closing your RelativeLayout
<LinearLayout
android:id="#+id/cover_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"/>
instead of 'viewPager.setOnClickListener' write
(findViewById(R.id.cover_layout))

android public textbox

I have come across another prickly little problem.
I am writing an app with tabs, but I have a textbox (EditText) at the top of the screen that I want to be able to receive text data from any of the tabs. As each of the tabs has it's own activity and layout, this is proving difficult to achieve.
I want to be able to use:
editText1.setText("Hello World");//sample text
from any Tab/Activity.
Does anyone know how to make a textbox from one layout public and able to recieve text?
I am using TabActivity, yes I know it's deprecated but as this is my first app with tabs, it's easier to learn than Fragments. I will try them next time, unless they are the answer to my problem, in which case I have a lot of re-coding to do!!
ok, new part.
package com.epsilonitsystems.epecsandroid;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
import android.view.Menu;
import android.speech.tts.TextToSpeech;
public class MainActivity extends TabActivity {
public EditText editText1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
TabHost tabHost = getTabHost();
// Tab for Core
TabSpec corespec = tabHost.newTabSpec("Core");
corespec.setIndicator("", getResources().getDrawable(R.drawable.ic_i));
Intent coreIntent = new Intent(this, CoreActivity.class);
corespec.setContent(coreIntent);
// Tab for Drink
TabSpec drinkspec = tabHost.newTabSpec("Drink");
drinkspec.setIndicator("", getResources().getDrawable(R.drawable.ic_drink));
Intent drinkIntent = new Intent(this, DrinkActivity.class);
drinkspec.setContent(drinkIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(corespec); // Adding Core tab
tabHost.addTab(drinkspec); // Adding Drink tab
}
#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;
}
}
That's the Main Activity, I'll just show the Core Activity as they will all be the same when I get it working.
package com.epsilonitsystems.epecsandroid;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.speech.tts.TextToSpeech;
public class CoreActivity extends Activity {
private TextToSpeech mTts;
// This code can be any value you want, its just a checksum.
private static final int MY_DATA_CHECK_CODE = 1234;
EditText editText1;
String Spch,Str;
ImageButton imageButton1,imageButton2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.core_layout);
//button1 = (Button) findViewById(R.id.button1);
editText1 = (EditText) findViewById(R.id.editText1);
mTts = new TextToSpeech(this, null);
}
public void onimageButton1Click(View view) {
//mTts.speak("I",TextToSpeech.QUEUE_FLUSH,null);
//Spch=editText1.toString();
//Spch=Spch+"I ";
editText1.setText("Hello World");
}
}//Activity End
I can't post a screenshot as I'm still a new user, sorry.
Any ideas please?
Gary,
What you are looking for probably is how to share data between different Activities.
You can do this in a couple of ways, but the cleanest is using the Intent you start the new Activity with.
With this you can set extra data via the Intent.putXXXXX methods.
You could use something as Intent.putExtra("my_private_key", mTextVar); and fetch that out in your other Activity with this.getIntent().getStringExtra("my_private_key");
ps. As tempting as it might seem to start with the TabActivity, you're actually making it a lot more difficult for yourself by making a bad start and learning classes which you should not use anymore. I'd advise you to pick up some good fragments tutorials which will be just as easy once correctly explained. You should take a look at per example http://www.vogella.com/android.html

Enable home button ActionbarSherlock, Sherlockfragment

I want to enable the home button in my fragment. This question is asked earlier but for an activity.
I tried ...
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
... but this doesn't work.
Here is my code:
import com.actionbarsherlock.R;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.MenuItem;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
public class SafanTab extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.safantab, container, false);
}
public OnClickListener onOverClick = new OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Over_Safan.class);
startActivityForResult(myIntent, 0);
}
};
public OnClickListener onProductenClick = new OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Over_Safan.class);
startActivityForResult(myIntent, 0);
}
};
public OnClickListener onTwitterClick = new OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Over_Safan.class);
startActivityForResult(myIntent, 0);
}
};
}
How can you enable a home button on SherlockFragment?
You also need to override the options menu selection:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Keep in mind, the code above would run in an activity (hence finish()). If you don't use an Activity (which would be odd to me...), then you'll need to replace that.
Have you tried adding the below code the main Activity - the one that extends SherlockActivity or extends SherlockFragmentActivity?
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
I don't think you can getSupportActionBar in something that only extends SherlockFragment.
The simplest solution is to follow these three simple steps:
1 - Declare in the AndroidManifest which is the parent of your activity:
<activity android:theme="#style/Theme.MyApp"
android:name="com.example.CurrentActivity"
android:parentActivityName="com.example.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.ParentActivity" />
2 - Add the Up icon in the action bar, simply calling from your onCreate method:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
3 - Specify the activity to open when the user presses the Up button:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
NOTE:
If you call (as suggested by #Eric):
finish();
instead of:
NavUtils.navigateUpFromSameTask(this);
your are not really performing "up navigation", since as the documentation says:
Up navigation is distinct from the back navigation provided by the system Back button. The Back button is used to navigate in reverse chronological order through the history of screens the user has recently worked with. It is generally based on the temporal relationships between screens, rather than the app's hierarchy structure (which is the basis for up navigation).

other way to make start up menu instead using button

i am thinking of doing a start up menu before going into the real page like the picture below.
i think i can do it with button menu. so, the pikpok, all-star, etc i made it using button widget. Is there any other way to make this?
anyway, when user click on all-star button, it appear to the next xml file. but then the activity seems not working out.
this is my code. this code shows the start up activity.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button button;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
setContentView(R.layout.about);
}
});
button = (Button) findViewById(R.id.signin);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
setContentView(R.layout.report);
}
});
}
}
report.xml and about.xml will have their own activity named ReportActivity and AboutActivity but it does not work.
is there a better way to the start up menu instead of using button. also it would be nice if the menu could have animation to it.
For rather complex "Buttons" you can define your own View by using layouts/images/other view
check
http://developer.android.com/guide/topics/ui/custom-components.html
for more information.
To call another activity, you have to define an activity class for that activity first, which calls the layout etc. On how to start another activity read
http://developer.android.com/guide/components/activities.html#StartingAnActivity

Categories

Resources