Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: #42 Add Java Example #54

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- Kotlin Example -->
<activity
android:name="com.sscustombottomnavigation.MainActivity"
android:name="com.sscustombottomnavigation.KotlinActivity"
android:screenOrientation="fullSensor"
android:exported="true">
<intent-filter>
Expand All @@ -19,6 +20,18 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!-- Uncomment it to review Java implementation -->
<!-- <activity-->
<!-- android:name="com.sscustombottomnavigation.JavaActivity"-->
<!-- android:screenOrientation="fullSensor"-->
<!-- android:exported="true">-->
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->

<!-- <category android:name="android.intent.category.LAUNCHER" />-->
<!-- </intent-filter>-->
<!-- </activity>-->
</application>

</manifest>
179 changes: 179 additions & 0 deletions app/src/main/java/com/sscustombottomnavigation/JavaActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package com.sscustombottomnavigation;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.simform.custombottomnavigation.Model;
import com.sscustombottomnavigation.databinding.ActivityHomeBinding;

/**
* Java Example for Custom Bottom Navigation
*/
public class JavaActivity extends AppCompatActivity {

private ActivityHomeBinding binding;

private static final int ID_HOME = 0;
private static final int ID_EXPLORE = 1;
private static final int ID_MESSAGE = 2;
private static final int ID_NOTIFICATION = 3;
private static final int ID_ACCOUNT = 4;
private static final String KEY_ACTIVE_INDEX = "activeIndex";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_home);

// Uncomment to use the bottom navigation in traditional way, such as without using Navigation Component
// setBottomNavigationInNormalWay(savedInstanceState);
setBottomNavigationWithNavController(savedInstanceState);
}

private void setBottomNavigationInNormalWay(Bundle savedInstanceState) {
TextView tvSelected = binding.tvSelected;
// Uncomment to set the typeface of text
// tvSelected.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/SourceSansPro-Regular.ttf"));

int activeIndex = savedInstanceState == null ? ID_MESSAGE : savedInstanceState.getInt(KEY_ACTIVE_INDEX);

binding.bottomNavigation.setSelectedIndex(activeIndex);

binding.bottomNavigation.add(new Model(R.drawable.ic_home, ID_HOME, ID_HOME, R.string.title_home, R.string.empty_value));
binding.bottomNavigation.add(new Model(R.drawable.ic_favorite_border_black, ID_EXPLORE, ID_EXPLORE, R.string.title_favorite, R.string.empty_value));
binding.bottomNavigation.add(new Model(R.drawable.ic_message, ID_MESSAGE, ID_MESSAGE, R.string.title_chat, R.string.empty_value));
binding.bottomNavigation.add(new Model(R.drawable.ic_notification, ID_NOTIFICATION, ID_NOTIFICATION, R.string.title_notifications, R.string.count));
binding.bottomNavigation.add(new Model(R.drawable.ic_account, ID_ACCOUNT, ID_ACCOUNT, R.string.title_profile, R.string.empty_value));

// If you want to change count
binding.bottomNavigation.setCount(ID_NOTIFICATION, R.string.count_update);

binding.bottomNavigation.setOnShowListener(item -> {
String name;
switch (item.getId()) {
case ID_HOME:
name = "Home";
break;
case ID_EXPLORE:
name = "Explore";
break;
case ID_MESSAGE:
name = "Message";
break;
case ID_NOTIFICATION:
name = "Notification";
break;
case ID_ACCOUNT:
name = "Account";
break;
default:
name = "";
}

int bgColor;
switch (item.getId()) {
case ID_HOME:
bgColor = ContextCompat.getColor(JavaActivity.this, R.color.color_home_bg);
break;
case ID_EXPLORE:
bgColor = ContextCompat.getColor(JavaActivity.this, R.color.color_favorite_bg);
break;
case ID_MESSAGE:
bgColor = ContextCompat.getColor(JavaActivity.this, R.color.color_chat_bg);
break;
case ID_NOTIFICATION:
bgColor = ContextCompat.getColor(JavaActivity.this, R.color.color_notification_bg);
break;
case ID_ACCOUNT:
bgColor = ContextCompat.getColor(JavaActivity.this, R.color.color_profile_bg);
break;
default:
bgColor = ContextCompat.getColor(JavaActivity.this, R.color.colorPrimary);
}

tvSelected.setText(getString(R.string.main_page_selected, name));
binding.lnrLayout.setBackgroundColor(bgColor);
return null;
});

binding.bottomNavigation.setOnClickMenuListener(item -> {
String name;
switch (item.getId()) {
case ID_HOME:
name = "HOME";
break;
case ID_EXPLORE:
name = "EXPLORE";
break;
case ID_MESSAGE:
name = "MESSAGE";
break;
case ID_NOTIFICATION:
name = "NOTIFICATION";
break;
case ID_ACCOUNT:
name = "ACCOUNT";
break;
default:
name = "";
}
// Use the 'name' variable here if needed
return null;
});

binding.bottomNavigation.setOnReselectListener(item -> {
Toast.makeText(JavaActivity.this, "item " + item.getId() + " is reselected.", Toast.LENGTH_LONG).show();
return null;
});
}

private void setBottomNavigationWithNavController(Bundle savedInstanceState) {
// If you don't pass activeIndex then by default it will take 0 position
int activeIndex = savedInstanceState == null ? 2 : savedInstanceState.getInt("activeIndex");

NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home,
R.id.navigation_favorite,
R.id.navigation_chat,
R.id.navigation_notifications,
R.id.navigation_profile
).build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

Model[] menuItems = new Model[]{
new Model(R.drawable.ic_home, R.id.navigation_home, ID_HOME, R.string.title_home, R.string.empty_value),
new Model(R.drawable.ic_favorite_border_black, R.id.navigation_favorite, ID_EXPLORE, R.string.title_favorite, R.string.empty_value),
new Model(R.drawable.ic_message, R.id.navigation_chat, ID_MESSAGE, R.string.title_chat, R.string.empty_value),
new Model(R.drawable.ic_notification, R.id.navigation_notifications, ID_NOTIFICATION, R.string.title_notifications, R.string.count),
new Model(R.drawable.ic_account, R.id.navigation_profile, ID_ACCOUNT, R.string.title_profile, R.string.empty_value)
};

binding.bottomNavigation.setMenuItems(menuItems, activeIndex);
binding.bottomNavigation.setupWithNavController(navController, false);

// manually set the active item, so from which you can control which position item should be active when it is initialized.
// binding.bottomNavigation.onMenuItemClick(4);

// If you want to change notification count
binding.bottomNavigation.setCount(ID_NOTIFICATION, R.string.count_update);
}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
outState.putInt(KEY_ACTIVE_INDEX, binding.bottomNavigation.getSelectedIndex());
super.onSaveInstanceState(outState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import androidx.navigation.ui.setupActionBarWithNavController
import com.simform.custombottomnavigation.Model
import com.sscustombottomnavigation.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
/**
* Kotlin Example for Custom Bottom Navigation
*/
class KotlinActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

Expand All @@ -21,21 +24,24 @@ class MainActivity : AppCompatActivity() {
private const val ID_MESSAGE = 2
private const val ID_NOTIFICATION = 3
private const val ID_ACCOUNT = 4
private const val KEY_ACTIVE_INDEX = "activeIndex"
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

// Uncomment to use the bottom navigation in traditional way, such as without using Navigation Component
//setBottomNavigationInNormalWay(savedInstanceState)
setBottomNavigationWithNavController(savedInstanceState)
}

private fun setBottomNavigationInNormalWay(savedInstanceState: Bundle?) {
val tvSelected = binding.tvSelected
//tvSelected.typeface = Typeface.createFromAsset(assets, "fonts/SourceSansPro-Regular.ttf")
// Uncomment to set the typeface of text
// tvSelected.typeface = Typeface.createFromAsset(assets, "fonts/SourceSansPro-Regular.ttf")

val activeIndex = savedInstanceState?.getInt("activeIndex") ?: 2
val activeIndex = savedInstanceState?.getInt(KEY_ACTIVE_INDEX) ?: ID_MESSAGE

binding.bottomNavigation.apply {

Expand Down Expand Up @@ -97,21 +103,24 @@ class MainActivity : AppCompatActivity() {
}

val bgColor = when (it.id) {
ID_HOME -> ContextCompat.getColor(this@MainActivity, R.color.color_home_bg)
ID_HOME -> ContextCompat.getColor(this@KotlinActivity, R.color.color_home_bg)
ID_EXPLORE -> ContextCompat.getColor(
this@MainActivity,
this@KotlinActivity,
R.color.color_favorite_bg
)
ID_MESSAGE -> ContextCompat.getColor(this@MainActivity, R.color.color_chat_bg)

ID_MESSAGE -> ContextCompat.getColor(this@KotlinActivity, R.color.color_chat_bg)
ID_NOTIFICATION -> ContextCompat.getColor(
this@MainActivity,
this@KotlinActivity,
R.color.color_notification_bg
)

ID_ACCOUNT -> ContextCompat.getColor(
this@MainActivity,
this@KotlinActivity,
R.color.color_profile_bg
)
else -> ContextCompat.getColor(this@MainActivity, R.color.colorPrimary)

else -> ContextCompat.getColor(this@KotlinActivity, R.color.colorPrimary)
}

tvSelected.text = getString(R.string.main_page_selected, name)
Expand Down Expand Up @@ -139,7 +148,7 @@ class MainActivity : AppCompatActivity() {
private fun setBottomNavigationWithNavController(savedInstanceState: Bundle?) {

// If you don't pass activeIndex then by default it will take 0 position
val activeIndex = savedInstanceState?.getInt("activeIndex") ?: 2
val activeIndex = savedInstanceState?.getInt("activeIndex") ?: ID_MESSAGE

val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
Expand All @@ -158,35 +167,35 @@ class MainActivity : AppCompatActivity() {
Model(
icon = R.drawable.ic_home,
destinationId = R.id.navigation_home,
id = 0,
id = ID_HOME,
text = R.string.title_home,
count = R.string.empty_value
),
Model(
R.drawable.ic_favorite_border_black,
R.id.navigation_favorite,
id = 1,
id = ID_EXPLORE,
R.string.title_favorite,
R.string.empty_value
),
Model(
R.drawable.ic_message,
R.id.navigation_chat,
2,
ID_MESSAGE,
R.string.title_chat,
R.string.empty_value
),
Model(
R.drawable.ic_notification,
R.id.navigation_notifications,
3,
ID_NOTIFICATION,
R.string.title_notifications,
R.string.count
),
Model(
R.drawable.ic_account,
R.id.navigation_profile,
4,
ID_ACCOUNT,
R.string.title_profile,
R.string.empty_value
)
Expand All @@ -207,7 +216,7 @@ class MainActivity : AppCompatActivity() {
}

override fun onSaveInstanceState(outState: Bundle) {
outState.putInt("activeIndex", binding.bottomNavigation.getSelectedIndex())
outState.putInt(KEY_ACTIVE_INDEX, binding.bottomNavigation.getSelectedIndex())
super.onSaveInstanceState(outState)
}
}
Loading