3

i am new to android ....

i have designed a view where i have created 4 image buttons

in that one has to go to facebook url second has to go twitter url third has to go to youtube url etc like that fourth to linkedin

but i dont know how to configure the button to send a url and the page must inside the same design view shown below

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/apple" >



<Button
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#2C3539"
    android:gravity="center"
    android:text="@string/apple"
    android:textColor="#FFFFFF"
    android:textSize="22sp" />

<Button
    android:id="@+id/video"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/music"
    android:background="#2C3539"
    android:text="@string/video"
    android:textColor="#FFFFFF"
    android:textColorHint="@color/black"
    android:textSize="20sp" />

<Button
    android:id="@+id/music"
    android:layout_width="160dp"
    android:layout_height="55dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="#2C3539"
    android:text="@string/music"
    android:textColor="#FFFFFF"
    android:textColorHint="@color/black"
    android:textSize="20sp" />

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/main"
    android:layout_marginLeft="41dp"
    android:layout_marginTop="72dp" >

</TableLayout>

<ImageButton
    android:id="@+id/twitter1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/facebook1"
    android:src="@drawable/twitter" />

<ImageButton
    android:id="@+id/facebook1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/main"
    android:src="@drawable/facebook" />

<ImageButton
    android:id="@+id/youtube1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/twitter1"
    android:src="@drawable/youtube" />

<ImageButton
    android:id="@+id/instagram1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/youtube1"
    android:src="@drawable/instagram_icon" /></RelativeLayout>

the above code looks like this if i click on facebook image then it has to populate this link https://www.facebook.com/apple.fruit

enter image description here

And link has to populate onto body (shown in image)

can anyone write the backend code how to use the button

i have created one java file

package com.lay.background;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;









public class AppleActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.apple);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }   }

Can anyone please resolve the code if any queries please comment

Sandeep V
  • 463
  • 1
  • 11
  • 20

2 Answers2

7

for each button you can pass the intent with specific URL like this

 Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.facebook.com/apple.fruit"));
    startActivity(browserIntent);

For example if your button is facebookButton then use it in your Activity like this:

ImageButton facebookButton = (ImageButton)findViewById(R.id.facebook1);
  facebookButton.setOnClickListener(new View.onClickListener() 
   {  
        public void onClick(View arg0) 
          { 
               Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.facebook.com/apple.fruit"));
                startActivity(browserIntent);

               }
         });

other way would be loading your URL into a WebView.

myWebView.loadUrl(http://"+tempUrl);

As for the missing "http://" I'd just do something like this:

if (!url.startsWith("http://") && !url.startsWith("https://"))
   url = "http://" + url;

Adding WebView in xml layout file:

<WebView android:id="@+id/webview"
android:layout_width="fill_parent" android:layout_height="50dip"/>

Add the WebView content with your Url in MainActivity :

 // Enable javascript for the view
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(this, description, Toast.LENGTH_SHORT).show();
        }
    });

    mWebview .loadUrl("http://www.facebook.com/apple.fruit");
Pankaj
  • 2,115
  • 2
  • 19
  • 39
  • where should i write this in which java file i have to include it in – Sandeep V Mar 11 '14 at 06:12
  • 1
    it depends upon your requirement how your app should navigate.or else let's go with the simplicity,do this for each button in your MainActivity file. – Pankaj Mar 11 '14 at 06:15
  • 1
    if you want the url to be loaded in same layout go with the WebView.Add the WebView in you xml layout file and open the url in same layout itself – Pankaj Mar 11 '14 at 06:18
  • how to add that in xml can u show me where to add i have added my xml file above where i have created buttons – Sandeep V Mar 11 '14 at 06:31
  • geek it has to display exactly onto body ........can u pls write the code for me i need it very urgently i am very new to android so......... – Sandeep V Mar 11 '14 at 06:33
  • if i click facebook it has to go to the left scroll and print the web page how to do can u edit it or it will automatically navigate left when i click on the button @androidGeek sorry i am disturbing u more today – Sandeep V Mar 11 '14 at 07:20
  • you need to tell what exactly you want and the flow of your app-@Sandeep – Pankaj Mar 11 '14 at 07:28
  • no no @androidGreek now it is perfect ....but i have one more query can u solve it – Sandeep V Mar 11 '14 at 10:08
  • yup.. bro.speak out -@Sandeep – Pankaj Mar 11 '14 at 10:32
  • Actually when i click on facebook it will go and populate the facebook page correct when i click on youtube it has to go to another xml file and populate the videos on right hand side and content on the left how to do dude we have to use database or manually we can take the link and do ....Do we require facebook API , twitter Api etc for buliding this app – Sandeep V Mar 11 '14 at 10:50
  • see API's are used if you want to use web services.to integrate facebook,g+,youtube in your app you will need API's for the same.But if you want to share something from your app to these you can use shareIntent where you can share images,audio,files etc. – Pankaj Mar 11 '14 at 12:42
0

try this.
first, add Webview in you layout file that visible is gone.
if button is clicking, set webview visible is visible, and using webview is connect you want connect url.

Amadas
  • 703
  • 1
  • 5
  • 10