Task 8
Task 8
Code:--
1. Theme:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.aniketsapp" parent="Theme.Material3.DayNight">
<style name="NoActionBar"
parent="Theme.Material3.DayNight.NoActionBar">
</style>
</style>
<item name="android:textSize">16sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/yellow</item>
</style>
</resources>
<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;
@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;
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");
}
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:-