Anda di halaman 1dari 12

A

Lab Record
Submitted
In partial fulfilment
For the award of the Degree of
Master of Computer Application
by

NAVIN SHARMA
Enrolment No.: CA14131711943

Under the supervision of

Mr. Budharam Nayak

Department of Computer Application

Suresh Gyan Vihar University


Jaipur
Year,2018

Suresh Gyan Vihar University, Jaipur Page 1


Q1. Activity Lifecycle?

Ans. Activity Lifecycle

Activities in the system are managed as an activity stack. When a new activity
is started, it is placed on the top of the stack and becomes the running activity
-- the previous activity always remains below it in the stack, and will not come
to the foreground again until the new activity exits.

An activity has essentially four states:

 If an activity is in the foreground of the screen (at the top of the stack),
it is active or running.
 If an activity has lost focus but is still visible (that is, a new non-full-
sized or transparent activity has focus on top of your activity), it
is paused. A paused activity is completely alive (it maintains all state
and member information and remains attached to the window
manager), but can be killed by the system in extreme low memory
situations.
 If an activity is completely obscured by another activity, it is stopped. It
still retains all state and member information, however, it is no longer
visible to the user so its window is hidden and it will often be killed by
the system when memory is needed elsewhere.
 If an activity is paused or stopped, the system can drop the activity
from memory by either asking it to finish, or simply killing its process.
When it is displayed again to the user, it must be completely restarted
and restored to its previous state.

public class Activity extends ApplicationContext {


protected void onCreate(Bundle savedInstanceState);

protected void onStart();

protected void onRestart();

protected void onResume();

protected void onPause();

protected void onStop();

protected void onDestroy();


}

Suresh Gyan Vihar University, Jaipur Page 2


Method Description

onCreate() called when activity is first created.

onStart() called when activity is becoming visible to the user.

onResume() called when activity will start interacting with the user.

onPause() called when activity is not visible to the user.

Suresh Gyan Vihar University, Jaipur Page 3


onStop() called when activity is no longer visible to the user.

onRestart() called after your activity is stopped, prior to start.

onDestroy() called before the activity is destroyed.

Q2. Recycler View?


Ans. Android RecyclerView is more advanced version of ListView with improved
performance and other benefits. but the RecyclerView has less responsibility than
what the ListView had. Different from the ListView, the RecyclerView does not have
the responsibility to position the elements of the list, and, besides what its name may
imply, it does not have the function to recycle the views either. All of those
functionalities are provided by the LayoutManagers.
RecyclerView is a somewhat new view that came to substitute
the ListView and GridView. From its documentation, you can see that it is a more
efficient and advanced widget when compared to its predecessors, despite having
many simplifications to support better animations and better arrangements of
elements.
add dependency
compile 'com.android.support:recyclerview-v7:26.+'

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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.navin.rv.MainActivity">

<android.support.v7.widget.RecyclerView

android:id="@+id/my_recycler_view"

android:scrollbars="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

Suresh Gyan Vihar University, Jaipur Page 4


</RelativeLayout>

MainActivity.java
package com.navin.rv;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;
public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

RecyclerView programmingList = (RecyclerView) findViewById(R.id.my_recycler_view);

programmingList.setLayoutManager(new LinearLayoutManager(this));

String[] Languages = {"Java Core ","JavaScript","Android","PHP","Python","Ruby","SQL"};

programmingList.setAdapter(new programingAdapter(Languages));

}
}

Create list_item_layout.xml file


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="horizontal" android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="8dp">

<ImageView

android:id="@+id/imgicon"

android:layout_width="80dp"

android:layout_height="80dp"

android:src="@drawable/ln"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

Suresh Gyan Vihar University, Jaipur Page 5


android:text="@string/app_name"

android:layout_weight="1"

android:textSize="24sp"

android:layout_gravity="center"

android:paddingLeft="16dp"

android:id="@+id/txtTitle"

/>
</LinearLayout>

Create programingAdapter.java class


package com.navin.rv;

import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class programingAdapter extends


RecyclerView.Adapter<programingAdapter.programingViewHolder> {
private String[] data;

public programingAdapter(String[] data){

this.data = data;

@Override

public programingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

LayoutInflater inflater = LayoutInflater.from(parent.getContext());

View view = inflater.inflate(R.layout.list_item_layout, parent, false);

return new programingViewHolder(view);

@Override

public void onBindViewHolder(programingViewHolder holder, int position) {

String title = data[position];

holder.txtTitle.setText(title);
}
@Override

Suresh Gyan Vihar University, Jaipur Page 6


public int getItemCount() {

return data.length;

public class programingViewHolder extends RecyclerView.ViewHolder{

ImageView imgicon;

TextView txtTitle;

public programingViewHolder(View itemView) {

super(itemView);

imgicon = itemView.findViewById(R.id.imgicon);

txtTitle = itemView.findViewById(R.id.txtTitle);

} }}

Q.3 Fragment
Ans .
Fragments
A Fragment represents a behavior or a portion of user interface in a FragmentActivity.
You can combine multiple fragments in a single activity to build a multi-pane UI and
reuse a fragment in multiple activities. You can think of a fragment as a modular
section of an activity, which has its own lifecycle, receives its own input events, and
which you can add or remove while the activity is running (sort of like a "sub activity"
that you can reuse in different activities).

Create activity_main.xml:-

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 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"
android:orientation="vertical">

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment1"
android:onClick="change"/>

<Button
android:id="@+id/button3"

Suresh Gyan Vihar University, Jaipur Page 7


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment2"
android:onClick="change"/>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_place"></FrameLayout>
</LinearLayout>

MainActivity:-
package com.navin.fragment2;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void change(View view){


Fragment frag;

if(view.getId()==R.id.button2) {
frag = new Fragment1();

getFragmentManager().beginTransaction().replace(R.id.fragment_place,frag).commit();
}
if(view.getId()==R.id.button3) {
frag = new Fragment2();

getFragmentManager().beginTransaction().replace(R.id.fragment_place,frag).commit();
}
}
}

Q.4 Tab layout


Ans. In Android TabLayout is a new element introduced in Design Support library. It
provides horizontal layout to display tabs on the screen. We can display more
screens in a single screen using tabs. We can quickly swipe between the
tabs. TabLayout is basically view class required to be added into our layout(xml) for
creating Sliding Tabs. We use different methods of TabLayout to create, add and
manage the tabs.

Suresh Gyan Vihar University, Jaipur Page 8


Add dependency
compile 'com.android.support:design:26.+'

MainActivity:-

package com.navin.fragment_demo;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toolbar;

public class MainActivity extends AppCompatActivity {


private ViewPager viewPager;
private TabLayout tabLayout;
private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager viewPager = (ViewPager)findViewById(R.id.viewpager);


viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(),this));
tabLayout = (TabLayout)findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);

}
}

activity_main.xml:-

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout 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">

<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="280dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@drawable/image" />

<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignBottom="@+id/image"
android:layout_alignParentStart="true"

app:tabGravity="fill"
app:tabIndicatorColor="#ffffff"
app:tabMode="fixed"

app:tabSelectedTextColor="@color/white"></android.support.design.widget.TabLayout
>
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"

Suresh Gyan Vihar University, Jaipur Page 9


android:id="@+id/viewpager"
android:layout_below="@+id/tablayout"
></android.support.v4.view.ViewPager>
</RelativeLayout>

Create PageAdapter:-

package com.navin.fragment_demo;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;

public class PagerAdapter extends FragmentPagerAdapter {


private String[] tabtitle = new String[]{"Call","Chat", "Status"};
Context context;
private int pagecounnt = 3;

public PagerAdapter(FragmentManager fragmentManager, MainActivity mainActivity){


super(fragmentManager);
this.context = context;
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
Fragment1 fragment1 = new Fragment1();
return fragment1;
case 1:
Fragment2 fragment2 = new Fragment2();
return fragment2;
case 2:
Fragment3 fragment3 = new Fragment3();
return fragment3;
default:return null;
}
}

@Override
public int getCount() {
return pagecounnt;
}
public CharSequence getPageTitle(int position){
return tabtitle[position];
}
}

Q.5 Custom Navigation with action bar


Ans. MainActivity:-

package com.navin.custom_navigationbar_with_actionbar;

import android.support.design.widget.AppBarLayout;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;

Suresh Gyan Vihar University, Jaipur Page 10


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {


Toolbar toolbar;
DrawerLayout drawerLayout;
NavigationView navigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

drawerLayout = findViewById(R.id.drawer);
navigationView = findViewById(R.id.nav_view);

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,


drawerLayout,toolbar, R.string.open, R.string.close);;
drawerLayout.addDrawerListener(toggle);
toggle.syncState();

}
}
activity_main.xml:-

<?xml version="1.0" encoding="utf-8"?>


<android.support.v4.widget.DrawerLayout
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"
android:id="@+id/drawer"
tools:openDrawer="start"
android:fitsSystemWindows="true">

<include layout="@layout/toolbar"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nagivation_header"
app:menu="@menu/mymenu"/>
</android.support.v4.widget.DrawerLayout>

Create toolbar.xml:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"

Suresh Gyan Vihar University, Jaipur Page 11


app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
</RelativeLayout>

Create navigation_header:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="160dp"
android:padding="16dp"
android:gravity="bottom"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:background="@color/colorAccent">

</LinearLayout>

Create menubar.xml:-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="@+id/home" android:title="Home"></item>
<item android:id="@+id/setting" android:title="Setting"></item>
<item android:id="@+id/about" android:title="About"></item>
<item android:id="@+id/logout" android:title="Logout"></item>
</group>
<item android:title="Advance Option">
<menu>
<item android:title="Index"
android:id="@+id/index"></item>
<item android:id="@+id/email"
android:title="Email"></item>
</menu>
</item>
</menu>

Suresh Gyan Vihar University, Jaipur Page 12

Anda mungkin juga menyukai