0% found this document useful (0 votes)
21 views10 pages

Task 8

The document describes code for a home screen activity in an Android app. It includes code for themes, layouts, fragments, and navigation. The home activity manages tabs, fragments, navigation options and handles back presses.

Uploaded by

Aniket Shekokar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views10 pages

Task 8

The document describes code for a home screen activity in an Android app. It includes code for themes, layouts, fragments, and navigation. The home activity manages tabs, fragments, navigation options and handles back presses.

Uploaded by

Aniket Shekokar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Task 8

Name: Aniket Sarang Shekokar

Code:--

1. Theme:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.aniketsapp" parent="Theme.Material3.DayNight">

<!-- Customize your light theme here. -->


<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
</style>

<style name="NoActionBar"
parent="Theme.Material3.DayNight.NoActionBar">

</style>

<style name="Theme.aniketsapp" parent="Base.Theme.aniketsapp">

</style>

<style name="myTabStyle" parent="TextAppearance.AppCompat">

<item name="android:textSize">16sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/yellow</item>
</style>

</resources>

2. Home activity xml:


3. <?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=".HomeActivity">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:theme="@style/ThemeOverlay.AppCompat.DayNight.ActionBar">

<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tablayout"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabTextAppearance="@style/myTabStyle"/>

</com.google.android.material.appbar.AppBarLayout>

<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewpager"
android:layout_marginTop="50dp"/>

</RelativeLayout>

4. ViewPageAdapter:--

package com.example.aniketsapp;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.ArrayList;

public class ViewPageAdapter extends FragmentPagerAdapter {

ArrayList<Fragment> fragmentArrayList=new ArrayList<>();


ArrayList<String> tabtitle=new ArrayList<>();

public ViewPageAdapter(@NonNull FragmentManager fm)


{
super(fm);
}
public void addFragment(Fragment fragment,String title)
{
this.fragmentArrayList.add(fragment);
this.tabtitle.add(title);
}

@NonNull
@Override
public Fragment getItem(int position)
{
return fragmentArrayList.get(position);
}

@Override
public int getCount()
{
return fragmentArrayList.size();
}

@Nullable
@Override
public CharSequence getPageTitle(int position)
{
return tabtitle.get(position);
}
}

5. HomeActivity Java:--

package com.example.aniketsapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TableLayout;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;

public class HomeActivity extends AppCompatActivity {

boolean doubletap=false;
SharedPreferences preferences;
SharedPreferences.Editor editor;

ViewPageAdapter viewPageAdapter;
TabLayout tabLayout;
ViewPager viewPager;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

preferences=
PreferenceManager.getDefaultSharedPreferences(HomeActivity.this);
editor=preferences.edit();

tabLayout=findViewById(R.id.tablayout);
viewPager=findViewById(R.id.viewpager);

viewPageAdapter=new ViewPageAdapter(getSupportFragmentManager());
viewPageAdapter.addFragment(new ChatsFragment(),"Chats");
viewPageAdapter.addFragment(new StatusFragment(),"Status");
viewPageAdapter.addFragment(new CallsFragment(),"Calls");
viewPager.setAdapter(viewPageAdapter);
tabLayout.setupWithViewPager(viewPager);

preferences=getSharedPreferences("database",MODE_PRIVATE);
boolean isFirsttime=preferences.getBoolean("firsttime",true);

if (isFirsttime)
{
welcome();
}

setTitle("Home Activity");
}

private void welcome()


{
AlertDialog.Builder ad=new AlertDialog.Builder(HomeActivity.this);
ad.setTitle("My Application");
ad.setMessage("Welcome to our app");
ad.setPositiveButton("Thank You", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

dialog.cancel();
}
}).create().show();
editor=preferences.edit();
editor.putBoolean("firsttime",false).commit();

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.home_menu,menu);
return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId()==R.id.home_menu_my_profile)
{
Toast.makeText(HomeActivity.this,"Open My
Profile",Toast.LENGTH_SHORT).show();
Intent i=new Intent(HomeActivity.this,MyprofileActivity.class);
startActivity(i);
}
else if (item.getItemId()==R.id.home_menu_setting)
{
Toast.makeText(HomeActivity.this, "Opening Setting",
Toast.LENGTH_SHORT).show();
Intent i=new Intent(HomeActivity.this,SettingActivity.class);
startActivity(i);
}
else if (item.getItemId()==R.id.home_menu_contact_us)
{
Toast.makeText(HomeActivity.this, "Opening contact us",
Toast.LENGTH_SHORT).show();
Intent i=new Intent(HomeActivity.this,ContactusActivity.class);
startActivity(i);
}
else if (item.getItemId()==R.id.home_menu_about_us)
{
Toast.makeText(HomeActivity.this, "Opening about us",
Toast.LENGTH_SHORT).show();
Intent i=new Intent(HomeActivity.this,AboutusActivity.class);
startActivity(i);
}
else if (item.getItemId()==R.id.home_menu_logout)
{
AlertDialog.Builder ad=new
AlertDialog.Builder(HomeActivity.this);
ad.setTitle("Logout");
ad.setMessage("Are you sure want to logout ?");
ad.setPositiveButton("Cancel", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
ad.setNegativeButton("Logout", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i=new
Intent(HomeActivity.this,LoginActivity.class);
editor.putBoolean("login",false).commit();
startActivity(i);
finish();
}
}).create().show();
}
return true;
}

@Override
public void onBackPressed() {
if (doubletap)
{
super.onBackPressed();
}
else
{
Toast.makeText(HomeActivity.this,"Press again to exit
app",Toast.LENGTH_SHORT).show();
doubletap=true;
Handler h=new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
doubletap=false;
}
},2000);
}
}
}

Output:-

You might also like