Question

Have 4 buttons in android studio and a picture.

Have 4 buttons in android studio and a picture.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here i have create different three activities

MainActivity for main output, OtherNews for other news and third one is MessagesPage for display entertainment channel

Code MainActivity.java

package com.example.buttondemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.net.Uri;


public class MainActivity extends Activity {
   Button btncnn,btnnytimes,btnothernews,btnmessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//total foutr button as per your requirment
btncnn=(Button)findViewById(R.id.button1);//for cnn channel
btnnytimes=(Button)findViewById(R.id.button2);//for nytimes channel
        btnothernews=(Button)findViewById(R.id.button3);//for othernews channel
       btnmessage=(Button)findViewById(R.id.button4);//for message button
      
btncnn.setOnClickListener(new OnClickListener() {      
           @Override
           public void onClick(View arg0) {//when user click on button intent fire
               Intent CNNChannel=new Intent(Intent.ACTION_VIEW,Uri.parse("https://edition.cnn.com/"));
           startActivity(CNNChannel);      
           }
       });  
btnnytimes.setOnClickListener(new OnClickListener() {//when user click on button intent fire      
           @Override
           public void onClick(View arg0) {//when user click on button intent fire
               Intent CNNChannel=new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.nytimes.com/"));
           startActivity(CNNChannel);      
           }
       });  
btnothernews.setOnClickListener(new OnClickListener() {   //when user click on button intent fire  
           @Override
           public void onClick(View arg0) {//when user click on button intent fire and go to othernews activityes
               Intent myIntent = new Intent(MainActivity.this,OtherNews.class);
startActivityForResult(myIntent, 0);      
           }
       });  
btnmessage.setOnClickListener(new OnClickListener() {   //when user click on button intent fire  
           @Override
           public void onClick(View arg0) {//when user click on button intent fire and go to messagepage activity
               Intent myIntent = new Intent(MainActivity.this,MessagesPage.class);
startActivityForResult(myIntent, 0);      
           }
       });  
}
@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;
}
  
}


Code activity_main.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_below="@+id/imageView1"
android:layout_marginTop="27dp"
android:text="Click For CNN News!" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="24dp"
android:layout_marginTop="29dp"
android:src="@drawable/images" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_alignRight="@+id/button2"
android:layout_below="@+id/button3"
android:text="Click For Messages!" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_alignRight="@+id/button2"
android:layout_below="@+id/button2"
android:text="Click For Other News!" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="Click For NYTIMES NEWS!" />

</RelativeLayout>

Code OtherNews.java

package com.example.buttondemo;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class OtherNews extends Activity {
   Button btnsport,btnmessage;//button for sport and message
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_other_news);
       btnsport=(Button)findViewById(R.id.button1);
       btnmessage=(Button)findViewById(R.id.button2);
       //on click event
       btnsport.setOnClickListener(new OnClickListener() {      
           @Override
           public void onClick(View arg0) {//when user click on button intent fire
               Intent SPORTChannel=new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.skysports.com/watch/sky-sports-cricket"));
           startActivity(SPORTChannel);      
           }
       });  
       btnmessage.setOnClickListener(new OnClickListener() {      
           @Override
           public void onClick(View arg0) {// when user click on button message display with toast
               Toast toast = Toast.makeText(getApplicationContext(),
               "This is a Others news Activity and click above button so you can watch sports channel",
               Toast.LENGTH_SHORT);
              
               toast.show();
           }
       });  
   }

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

}

Code activity_other_news.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".OtherNews" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="41dp"
android:layout_marginTop="63dp"
android:text="SportChannel" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="21dp"
android:text="SportMessage" />

</RelativeLayout>

code MessagesPage.java

package com.example.buttondemo;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MessagesPage extends Activity {
   Button btnentertainment,btnmessage;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_messages_page);
       btnentertainment=(Button)findViewById(R.id.button1);
       btnmessage=(Button)findViewById(R.id.button2);
       btnentertainment.setOnClickListener(new OnClickListener() {      
           @Override
           public void onClick(View arg0) {//when user click on button intent fire
               Intent SPORTChannel=new Intent(Intent.ACTION_VIEW,Uri.parse("https://indianexpress.com/section/entertainment/"));
           startActivity(SPORTChannel);      
           }
       });  
       btnmessage.setOnClickListener(new OnClickListener() {      
           @Override
           public void onClick(View arg0) {// when user click on button message display with toast
               Toast toast = Toast.makeText(getApplicationContext(),
               "This is a entertainment news Activity and click above button so you can wathes entertainment channel",
               Toast.LENGTH_SHORT);
              
               toast.show();
           }
       });  
   }

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

}

Code activity_messages_page.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MessagesPage" >

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="45dp"
android:layout_marginTop="41dp"
android:text="Entertainment News" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="36dp"
android:text="Message entertainment" />

</RelativeLayout>

ShreenShot Of Code:

Package Explorer3 日与▽ーロ D MainActivityjava X DOtherNews.jav D Messages Page.Java MainActivity java MessagesPage.java OtherNewPackage Explorer 日与▽-B D MainActivityjava D otherNews.java D MessagesPage.java 团MainActivityjava package com.example.buttondePackage Explorer X 曰女▽- | D MainActvrtyjava 10 OtherNews Java D Messages PageJava 23 MainActivity java 曰唱祝) DMessagesPage,jav

Output

5554 AndroidPhone 11-1:02 Basic Controls ButtonDEMO Hardware Buttons MENU DPAD not enabled in AVD Hardware Keyboard Use your

5554 AndroidPhone :02Basic Controls IWI OtherNews Hardware Buttons MENU DPAD not enabled in AVD SportChannel SportMessage Har5554:AndroidPhone 1:02Basic Controls MessagesPage Hardware Buttons MENU DPAD not enabled in AVD Entertainment News Message en

Thank you if you have any query regarding above answer please ask me in comment box.

if you like my work appreciate with thumbs up.

Thank You.

Add a comment
Know the answer?
Add Answer to:
Have 4 buttons in android studio and a picture.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT