I am trying to convert a series of buttons that will open webpages in the browser from an xml layout file into my Activity class (programmatically). How can I do this?
I have not been able to find questions that answer this.
/**
* Open the specified URL on the device's browser
*
* #param context Current Context.
* #param url The url to display.
*/
public static void openWebPage(#NonNull Context context, String url) {
Uri webPage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webPage);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}
Then in the onCreate of your Activity you get the button:
View button = findViewById(R.id.my_button);
button.setOnClickListener(new onClickListener(){
... // call the above method with the corresponding url
});
More information here
The code....
public class MainActivity extends Activity {
TextView textView;
Spanned spannedText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView1);
spannedText = Html.fromHtml("text with a link in it <br />" +
"<a href='https://www.aol.com//'>aol.com</a>");
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(spannedText);
}
}
XML TextView
<TextView
android:id="#+id/textView1"
android:layout_width="match_content"
android:layout_height="match_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="LINK WILL SHOW UP HERE"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center" />
You need to use webview in your android application.
More about web view
Related
(1) I am using recyleview to fetch image, text & audio file from API. For text I used volley & for image use picasso, what I will use for audio file.
(2) after fetch the image & text when apply onclicklistener pass the info to another activity. Can pass only text not images.
What I need to do to solve this problem?
this is adapter part
viewHolder.textViewStory.setText(banglaItem.getStoryName());
viewHolder.textViewWritter.setText(banglaItem.getWritterName());
Picasso.get().load(banglaItem.getStoryImage()).fit().into(viewHolder.imageView);
viewHolder.linear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,AudioActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("storyName",banglaItem.getStoryName());
intent.putExtra("storyImage",banglaItem.getStoryImage());
context.startActivity(intent);
}
});
this is the new activity part
public class AudioActivity extends AppCompatActivity {
TextView name;
ImageView image;
String nameStory;
String imageStory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
name = findViewById(R.id.audioName);
image = findViewById(R.id.audioImage);
nameStory = getIntent().getStringExtra("storyName");
imageStory = getIntent().getStringExtra("storyImage");
name.setText(nameStory);
int resourceId = getResources().getIdentifier(imageStory,"drawable", getPackageName());
image.setImageResource(resourceId);
}
}
(1.) if you have complete url of your audio file then you can directly stream audio file using media player classes provided by android.
(2.) you are passing storyImage in next activity ,which is a url that you get from api. so you have to find same on next activity i.e
TextView name;
ImageView image;
String nameStory;
String imageStory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
name = findViewById(R.id.audioName);
image = findViewById(R.id.audioImage);
nameStory = getIntent().getStringExtra("storyName");
imageStory = getIntent().getStringExtra("storyImage");
name.setText(nameStory);
Picasso.get().load(imageStory).fit().into(image);
//here image story contain url that you sent through first activity. As picasso uses cache, it will load (already loaded image in first activity) very smoothly from it cache.
}
I am learning Android development and have completed the first app (https://developer.android.com/training/basics/firstapp/index.html). Now, I added a second text field and a second button. The idea is that the text that the user will enter in the text field will be used as an input in a Google Search Engine.
So I did the following:
Modified the AndroidManifest.xml to add the permission <uses-permission android:name="android.permission.INTERNET" />.
Modified my MainActivity.java to add a call to my new activity:
/** Called when the user taps the Search button */
public void searchMessage(View view) {
Intent intent = new Intent(this, DisplaySearchActivity.class);
EditText editText = (EditText) findViewById(R.id.editText3);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
Then, created a new activity that has a WebView and, for now just displays the main page of Google.com
public class DisplaySearchActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_search);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("https://www.google.com");
}
}
Finally, my activity_display_search.xml looks like this
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.DisplaySearchActivity">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</android.support.constraint.ConstraintLayout>
When I click the Search button the app takes me to the Search WebView, but nothing happens. What am I missing here?
You need to provide a WebViewClient for your WebView.
public class DisplaySearchActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_search);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("https://www.google.com/search?q=" + message);
}
}
Originally in the project I have two image buttons, which I coded like the below:
XML:
<ImageView
android:layout_width="125dp"
android:layout_height="125dp"
android:id="#+id/sidebanner1"
android:padding="15dp"
/>
<ImageView
android:layout_width="125dp"
android:layout_height="125dp"
android:id="#+id/sidebanner2"
android:padding="15dp"
/>
Main_Activity.java
public class MainActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
ImageView SideBanner1=(ImageView)findViewById(R.id.sidebanner1);
SideBanner1.setImageResource(R.drawable.sidebanner1);
SideBanner1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String url1 = "http://www.chris.com";
Intent a = new Intent(Intent.ACTION_VIEW);
a.setData(Uri.parse(url1));
startActivity(a);
}
});
ImageView SideBanner2=(ImageView)findViewById(R.id.sidebanner2);
SideBanner2.setImageResource(R.drawable.sidebanner2);
SideBanner2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String url2 = "http://www.peter.com";
Intent b = new Intent(Intent.ACTION_VIEW);
b.setData(Uri.parse(url2));
startActivity(b);
}
});
Now I have stored the images and url links in server, and created sql table in order to retrieve these information.
In the splash screen, I started to retrieve the information as follow:
JSONArray sidebannerAry = result.getJSONArray("sidebannerData");
for (int i = 0; i < sidebannerAry.length(); i++){
JSONObject temp = sidebannerAry.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
String image = DefensiveClass.optString(temp, "bannerImage");
String url = DefensiveClass.optString(temp, "bannerURL");
map.put("image", image);
map.put("url", url);
MainActivity.sidebannerAry.add(map);
}
But now I got confused and not sure how to apply the data to the two buttons in MainActivity.
Can anyone guide me thru how to pass the image and link information retrieved to the two banner buttons so that they can use it. Thank you.
In MainActivity public class, I added:
public static ArrayList<HashMap<String,String>> sidebannerAry = new ArrayList<HashMap<String,String>>();
Problem solved, I was thinking assigning sidebannerAry to adapter to do it.
But I was stupid, all I need is just to load the image and have the onClickListener open up the website and load the url directly from the sidebannerAry.
As per your question you need to set your buttons with the information retrieved from api call, so you can simply store the values retrieved from api call(server) on local variables and apply those to the buttons as:
btnExample.setText(the_text_or_url_retrieved_from_server);
and if you want to set image to the button with the image url retrieved from server then you can use glide as:
Glide.with(context)
.load(url_retrieved_from_server)
.into(imageButton);
but make sure you use image button for this.
when i click a button it have to go one particular web site. i'm getting confusion how can write code on android please help me i did like this
xml code :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click me"
android:id="#+id/button1"
android:onClick="search"/>
java code :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void search(View view)
{
Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intent);
}
}
You should write below codes to your onCreate method, after setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Change google url with your url
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
});
Also you should delete android:onClick="search" from your xml
You need to be very clear in your question -based on your comment to Batuhan's answer it sounds like you want to use a WebView - either way, you need to clarify what you want;
Assuming you want a Webview - here is what you need to do:
Create a layout file that will load the Web page, call it my web_page_viewer:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myWebPageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Then you create a "Web Page Viewing Activity" (I assume you wanted to use Main2Activity) where you will initialize and load the URL to be displayed:
public class Main2Activity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//here you load your "Web Page Viewing" layout file
setContentView(R.layout.web_page_viewer);
//here you get handle to your WebView
webView = (WebView) findViewById(R.id.myWebPageView);
webView.getSettings().setJavaScriptEnabled(true);
//then specify the URL you want to load
webView.loadUrl("http://ishlema.co.za");
}
}
Then finally, your button click handler will now load the "Web Page Viewing" activity:
public void search(View view)
{
Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intent);
}
I really hope this helps you. Please take some time and look at this complete and easy Android WebView Example
I am making an android application that needs to open the webbrowser when the go button on quick search is pressed. How can i make this happen? that it starts the webbrowser? This is the code that i got so far:
public class SearchFunction extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction)) {
String searchKeywords = queryIntent.getStringExtra(SearchManager.QUERY);
//Is it here that i can start intents/webbrowser???
}
}
}
For how to do the search activity you have all the info here: http://developer.android.com/guide/topics/search/search-dialog.html
Starting the browser:
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.ro/search?q=" + searchKeywords.replace(' ', '+')));
startActivity(i);
Hope it helps.