I am writing this cause I was searching for days to get an answer on my question but I didn't find any answers . . .
I am like for a month using android studio and everything is going perfect but . . . I cant find an solution to something, I added some buttons into a layout and I wat that when I click on the button it redirects me to my Webview. I want to use only one Webview and every time when I click one of the buttons it had to go to another website in the Webview. I tried many of things I found on SO but couldn't find the right answer.
Could you please help me?
Call WebView's loadUrl() method inside the listeners of your buttons:
my_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
my_webView.loadUrl("www.some_url.com");
}
});
You can always change your webview's url to what you need depending on the button you clicked.
I fixed it.. I had to do this in the webview activity:
public class Webview extends Activity {
Sorry had the wrong activity at the end!
Related
What I want to do:
I want to have multiple activities each prefaced with a page explaining to the user what the activity is about.
What I'm currently doing:
So my main class BaseModuleActivity extends Activity and I am trying to write a function called showTutorial() which will explain the next steps to the users.
Here is my attempt in doing so:
public void showTutorial(String title, String explanation){
setContentView(R.layout.tutorial_screen);
TextView tv1 = (TextView)findViewById(R.id.tutoTextTitle);
tv1.setText(title);
TextView tv2 = (TextView)findViewById(R.id.tutoTextExplanation);
tv2.setText(explanation);
findViewById(R.id.tutoButton).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//remove the tutorial's view
findViewById(R.id.tutoLayout).setVisibility(View.GONE);
}
});
}
And this method is called in the following:
public class myFirstActivity extends BaseModuleActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
//First show tuto
super.showTutorial(getString(R.string.upTitle),getString(R.string.upExplanation));
//TODO then actually do the activity stuff
/*
(findViewById(R.id.next_button)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
*/
}
}
Problem:
I think the problem is mainly conceptual. I don't know the best approach to do this and the approach I'm taking is not working.
What I'm doing is not working because the view just become empty. I thought setting the visibility of the linearLayout to gone would make it disappear and the actual activity will able to take place.
What I need:
I need to understand if I can do what I want with my approach or not? Or what approach should I take.
I found some similar questions. However, the answer to these questions didn't seem to fit my problem.
I also looked into layout inflater and fragment, but layout inflater seem to be more for listView and fragment uses layout inflater.
Well, there are some approaches to show a guide for your activity (or application).
First one, and probably the easiest, is to show a dialog/TextView when user enters an activity and explain the activity guide in that dialog/TextView using plain text. From your explanation, I think this one is what your are trying to do.
Second one is to use something like slides with pictures to explain about your activity (like Google Sheets application).
Third one is to explain each control in your activity separatly by highlighting them (similar to how Go Launcher explains its feature on first launch)
You can find more info in below links:
How to implement first launch tutorial like Android Lollipop apps: Like Sheets, Slides app?
Android - first launch interactive tutorial
Seems that what you want is actually an introduction. Take a look at this project:
https://github.com/rubengees/introduction
From each introduction page you can launch the correspondent activity.
So I have a WebView, and there is an EditText which a user can type in a URL then press the Go button to navigate to that URL. There are also a back button and forward button which seem to work fine (just calling goBack() and goForward() ).
Now, the problem is refreshing the EditText to display the correct URL anytime the back or forward buttons are pressed. Sometimes it seems to work fine, but more often it will lag behind the click by 1 webpage. What I mean by this is that if I am on a webpage (call this WP2) and click back, the webpage shown will go back to the previous one (WP1) correctly, but the URL will initially stay the same in the editText (WP2). If I click back again, the webpage will go back once again fine (WP0), but now the editText shows the URL for the previously displayed webpage (WP1).
I hope my problem is clear enough, and if anyone has any suggestions I would greatly appreciate it. Thank you!
EDIT: I should probably post some code, so here it is:
My goBack button OnClick:
leftWebViewBackBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
WebView webView = webViewFrag.web; //this is getting the webview from its fragment.
webView.goBack();
urlEditText.setText(webView.getUrl());
}
});
Reading the documentation for getUrl() it says
Gets the URL for the current page. This is not always the same as the URL passed to WebViewClient.onPageStarted because although the load for that URL has begun, the current page may not have changed.
Your best bet is to override WebViewClient#onPageStarted() and use setWebViewClient(...). It's also possible you'll get into some redirect loop like mentioned here (WebView back history without redirects) so you might want to consider this (unless you know you won't encounter them).
The code might look like this:
webView.setWebViewClient(new WebViewClient() {
....
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
urlEditText.setText(url);
}
....
}
I am using startApp-SDK . on pressing back button from mainActivity. it shows an add by default. I want to stop showing that add on Backpressed. MainActivity extends from TabActivity and contains three tabs on pressing back from any of them show ads. I have override onBackpressed. in all of them. but still showing those ads.
#Override
public void onBackPressed() {
super.onBackPressed();
}
and also tried some other way around but problem don't solve ..
Thanks in advance..
I have find the solution to my question . May be it help someone else..
whlile declaring StartappAd
StartAppSDK.init(this, "105605040","205467351", false); // true for showing adds.
//NOTE: If you don't wish your application to display ads when pressing the Home button,Back Button simply pass false in the last parameter
I have a text with some words which if we click on and move to other activities.
I tried to search a lot from internet or stack overflow. Is there any technique to solve this issue?
Thanks and best regards
If it's about specific words inside a bigger piece of text, mark the clickable words as URLSpan and override the URLSpan's onClick(View widget) method to start the new Activity - by default it will fire off on Intent.ACTION_VIEW. I'm sure you'll be able to find multiple examples if you do a search here on SO.
If you also want to remove the underline that is inherent to the URLSpan, have a look at this answer.
simply setOnClickListener on TextView
textView.setOnClickListener(new OnClikListener(){
public void onClick(View v){
//here call your activity
}
});
I have a problem that I can't seem to find the solution to.
I have an app that loads the main.xml file on startup, of course. In it are several buttons, and I want the buttons to take me to a different XML file. I just used setContentView(R.layout.newlayout.xml) method for that, and it works great.
The problem comes in after that. If I reference any of the buttons or other objects in the new layout, the app won't even finish loading before it errors out and closes on the emulator. However, if I take all references to objects out, the app runs fine.
I can navigate TO the new layouts, but their buttons can't do anything. Do I need to create a separate Java file for each layout? Or am I doing it all wrong? I'm trying to be as specific as I can. I suppose you could say I need to have different "pages" in my app as a website would.
I think what you are trying to do is best solved using multiple java files, each one defining it's own android Activity.
While it is possible to have multiple layouts/views in a single activity, this will generally make the code more complex and harder to read/debug in the future. By having each 'screen' in its own file, it will be a bit easier to manage all the different views you need to juggle.
The buttons and views only can refer to those mentioned in the current SetContentView() file..
u can test this by creating a button and initialising to an R.id... without setting the content view.. U will get a force close..
so if u change the XML file u shud initialise stuff again....
Ok, for anyone out there with the same problem and haven't figured out how to do it, as I said in my comment on ylebre, my Coworker and I have finally discovered how to do it. First off, we added
implements OnClickListener
to the class, after
extends Activity
then, we created a new java file, and at the beginning of the file it called
setContentView(R.layout.newlayout);
instead of main. Then, we made a button as follows:
Button button1 = (Button) findViewById(R.id.button01;
button1.setOnClickListener(this);
then later in the code:
public void onClick(View v) {
switch(v.getId()) {
case R.id.button01:
startActivity(new Intent(this, NEWJAVAFILE.class));
break;
}
}
And that's it! We just copied and pasted that code into NEWJAVAFILE, changed the names and such, and we were able to navigate freely back and forth. As ylebre said, all of the code for the new activity is in the NEWJAVAFILE.java. OH and don't forget to add the name of the java file to the manifest inside the tags:
<activity android:name=".NEWJAVAFILE">
</activity>
it all seems so simple now!