by Android studio Task 1: Build a movie fragment . You need to implement the Mov
ID: 3588137 • Letter: B
Question
by Android studio
Task 1: Build a movie fragment. You need to implement the Movie_Detail fragment to display the information for a movie. Your movie data can contain many fields, and you should display at least 6 information including the followings:
Title
Description
Image
You are responsible for designing the layout of this task. Make it looks nice. This layout will be reused again.
There is no need to demonstrate this task, because the fragment will be used in the next two tasks, and we will grade your fragment based on what we see in the next two tasks.
Task 2: ViewPager Activity .. Implement a ViewPager activity. Each page of the ViewPager is a fragment that displays the details of a movie. Users can swipe left or right to go the previous or next movie. You should satisfy the following requirements:
You should use the Movie_Detail fragment developed in Task 1.
You need to have a tab (one tab for each movie). When user clicks on a tab, the corresponding movie should be displayed.
Hint: use TabLayout with your ViewPager
Task 3: Master/Detail Flow . This task consists of two fragments: Master and Detail. Implement an Activity that initially load a Master fragment with 1 TextView and 2 Buttons (could be image buttons) (this fragment is called Movie_Choice fragment); each of the buttons corresponds to the index of a movie. When the increase button is clicked, increase the index by the maximum (# of movies) and when the decrease button is clicked, decrease the index by 0 (cannot be negative). Then your appshould behave like the following:
On Handset with a small screen, the Detail fragment showing the details of the movie will be loaded into the same activity, replacing the Movie_Choice fragment with the Movie_Detail fragment from Task 1. When the “back” button of the phone is clicked, the Movie_Choice fragment with same index value will come back.
On Tablet with a large screen, the fragment showing the details of the movie will be loaded into the same activity, but both the Movie_Choice fragment and the Movie_Detail fragment will be shown side by side.
Explanation / Answer
MainActivity.java
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TableLayout;
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
ViewPagerAdapter viewPagerAdapter;
//This is our viewPager
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
}
activity_main.xml
<LinearLayout
android:id="@+id/main_layout"
android:orientation="vertical"
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=".MainActivity">
<!-- our toolbar -->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<!-- our tablayout to display tabs -->
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
ViewPagerAdapter.java
package com.example.rns_2.movie;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/**
* Created by RNS-2 on 10/8/2017.
*/
public class ViewPagerAdapter extends
FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
if (position == 0)
{
fragment = new MovieFragment();
}
else if (position == 1)
{
fragment = new Games();
}
else if (position == 2)
{
fragment = new Songs();
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
String title = null;
if (position == 0)
{
title = "Movie";
}
else if (position == 1)
{
title = "Tab-2";
}
else if (position == 2)
{
title = "Tab-3";
}
return title;
}
}
MovieFragment.java
package com.example.rns_2.movie;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class MovieFragment extends Fragment {
public MovieFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_movie, container, false);
}
}
fragment_movie.xml
<FrameLayout 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:background="#ff0013"
android:orientation="vertical"
tools:context="com.example.rns_2.movie.MovieFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:text = "Sulthan"
android:gravity="center"
android:textSize="30sp"
android:textColor="#fff"
/>
<ImageView
android:layout_width="match_parent"
android:layout_marginTop="50dp"
android:layout_height="150dp"
android:background="@drawable/sul"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:text = "Description: "
android:gravity="center"
android:textSize="30sp"
android:layout_marginTop="200dp"
android:textColor="#fff"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text = "Sulthan is the 10th highest grossing indian film in overseas marketsby overseas gross"
android:gravity="center"
android:textSize="20sp"
android:layout_marginTop="220dp"
android:textColor="#fff"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:text = "Director:"
android:gravity="center"
android:textSize="30sp"
android:textColor="#fff"
android:layout_marginTop="300dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:text = "Ali Abbas Zafer"
android:textColor="#fff"
android:textSize="20sp"
android:layout_marginTop="330dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:text = "Reviews:"
android:textColor="#fff"
android:gravity="center"
android:textSize="30sp"
android:layout_marginTop="350dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text = "4.2/5"
android:textColor="#fff"
android:textSize="20sp"
android:layout_marginTop="380dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text = "Productions:"
android:textColor="#fff"
android:gravity="center"
android:textSize="30sp"
android:layout_marginTop="410dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text = "Yash Raj films"
android:textColor="#fff"
android:textSize="20sp"
android:layout_marginTop="440dp"
/>
</FrameLayout>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.