Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Write an Android-like lockscreen in HTML, CSS, and JS. Make sure it has dark mode. Put 2…

  1. User

    Write an Android-like lockscreen in HTML, CSS, and JS. Make sure it has dark mode. Put 2 demo notifications. Swiping up to trigger the code pin (1234 is the correct code)... if it's correct, display a message "Unlocked", then go back to lockscreen, we're not working on an actual Android custom ROM here, we're just simulating

  2. Assistant

    Sure! Below is a complete example of an Android-like lock screen implemented using HTML, CSS, and JavaScript. This simulation includes dark mode, two demo notifications, and a swipe-up gesture to enter a PIN code (1234). If the correct PIN is entered, it displays an "Unlocked" message before returning to the lock screen.

    Live Demo

    You can try this out by copying the code into an index.html file and opening it in your web browser.

    Code Explanation

    • HTML: Structures the lock screen, notifications, and unlock interface.
    • CSS: Styles the lock screen with dark mode aesthetics and handles the layout.
    • JavaScript: Manages the swipe-up gesture, PIN verification, and unlock message logic.

    Complete Code

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Android-like Lock Screen Simulation</title>
        <style>
            /* Reset default styles */
            * {
                margin: 0;
                padding: 0;
                box-sizing: border-box;
            }
    
            body, html {
                height: 100%;
                font-family: Arial, sans-serif;
                background-color: #121212;
                color: #ffffff;
                overflow: hidden;
            }
    
            .lock-screen {
                position: relative;
                width: 100%;
                height: 100%;
                background: linear-gradient(135deg, #1f1f1f, #2c2c2c);
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
            }
    
            .time {
                font-size: 64px;
                margin-bottom: 10px;
            }
    
            .date {
                font-size: 24px;
                margin-bottom: 30px;
            }
    
            .notifications {
                position: absolute;
                top: 50px;
                width: 90%;
            }
    
            .notification {
                background-color: #1e1e1e;
                padding: 15px;
                border-radius: 10px;
                margin-bottom: 10px;
                box-shadow: 0 2px 5px rgba(0,0,0,0.5);
            }
    
            .notification-title {
                font-weight: bold;
                margin-bottom: 5px;
            }
    
            .notification-body {
                font-size: 14px;
            }
    
            .swipe-area {
                position: absolute;
                bottom: 30px;
                width: 60px;
                height: 60px;
                border: 2px solid #ffffff;
                border-radius: 30px;
                display: flex;
                align-items: center;
                justify-content: center;
                cursor: pointer;
                transition: background-color 0.3s;
            }
    
            .swipe-area:hover {
                background-color: rgba(255, 255, 255, 0.1);
            }
    
            .arrow {
                border: solid white;
                border-width: 0 3px 3px 0;
                display: inline-block;
                padding: 10px;
                transform: rotate(-45deg);
                -webkit-transform: rotate(-45deg);
            }
    
            /* Unlock Overlay */
            .unlock-overlay {
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background-color: rgba(18, 18, 18, 0.95);
                display: none;
                align-items: center;
                justify-content: center;
                flex-direction: column;
                z-index: 10;
            }
    
            .unlock-overlay.active {
                display: flex;
            }
    
            .unlock-title {
                font-size: 32px;
                margin-bottom: 20px;
            }
    
            .pin-input {
                display: flex;
                gap: 10px;
                margin-bottom: 20px;
            }
    
            .pin-input input {
                width: 40px;
                height: 50px;
                font-size: 32px;
                text-align: center;
                border: none;
                border-bottom: 2px solid #ffffff;
                background: transparent;
                color: #ffffff;
            }
    
            .unlock-button {
                padding: 10px 20px;
                font-size: 18px;
                background-color: #1e88e5;
                border: none;
                border-radius: 5px;
                color: #ffffff;
                cursor: pointer;
            }
    
            /* Unlocked Message */
            .unlocked-message {
                position: fixed;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                background-color: #2e7d32;
                padding: 20px 40px;
                border-radius: 10px;
                font-size: 24px;
                color: #ffffff;
                display: none;
                z-index: 20;
            }
        </style>
    </head>
    <body>
        <div class="lock-screen" id="lockScreen">
            <div class="notifications">
                <div class="notification">
                    <div class="notification-title">Email</div>
                    <div class="notification-body">You have a new message from John.</div>
                </div>
                <div class="notification">
                    <div class="notification-title">Calendar</div>
                    <div class="notification-body">Meeting at 3 PM today.</div>
                </div>
            </div>
            <div class="time" id="time">12:00</div>
            <div class="date" id="date">January 1, 2024</div>
            <div class="swipe-area" id="swipeArea">
                <i class="arrow"></i>
            </div>
        </div>
    
        <!-- Unlock Overlay -->
        <div class="unlock-overlay" id="unlockOverlay">
            <div class="unlock-title">Enter PIN</div>
            <div class="pin-input">
                <input type="password" maxlength="1" id="pin1" autocomplete="off"/>
                <input type="password" maxlength="1" id="pin2" autocomplete="off"/>
                <input type="password" maxlength="1" id="pin3" autocomplete="off"/>
                <input type="password" maxlength="1" id="pin4" autocomplete="off"/>
            </div>
            <button class="unlock-button" id="submitPin">Unlock</button>
        </div>
    
        <!-- Unlocked Message -->
        <div class="unlocked-message" id="unlockedMessage">Unlocked</div>
    
        <script>
            // Update Time and Date
            function updateTime() {
                const timeElement = document.getElementById('time');
                const dateElement = document.getElementById('date');
                const now = new Date();
                let hours = now.getHours();
                let minutes = now.getMinutes();
                const ampm = hours >= 12 ? 'PM' : 'AM';
                hours = hours % 12 || 12;
                minutes = minutes < 10 ? '0' + minutes : minutes;
                timeElement.textContent = hours + ':' + minutes;
                const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
                dateElement.textContent = now.toLocaleDateString(undefined, options);
            }
    
            setInterval(updateTime, 1000);
            updateTime();
    
            // Swipe Up to Unlock
            const swipeArea = document.getElementById('swipeArea');
            const unlockOverlay = document.getElementById('unlockOverlay');
            const lockScreen = document.getElementById('lockScreen');
    
            let touchStartY = 0;
            let touchEndY = 0;
    
            swipeArea.addEventListener('touchstart', function(e) {
                touchStartY = e.changedTouches[0].screenY;
            });
    
            swipeArea.addEventListener('touchend', function(e) {
                touchEndY = e.changedTouches[0].screenY;
                handleGesture();
            });
    
            // For desktop: click to open unlock overlay
            swipeArea.addEventListener('click', function() {
                openUnlockOverlay();
            });
    
            function handleGesture() {
                if (touchStartY - touchEndY > 50) { // Swipe up threshold
                    openUnlockOverlay();
                }
            }
    
            function openUnlockOverlay() {
                unlockOverlay.classList.add('active');
                // Clear previous inputs
                document.querySelectorAll('.pin-input input').forEach(input => input.value = '');
                document.getElementById('pin1').focus();
            }
    
            // Handle PIN Input
            const pinInputs = document.querySelectorAll('.pin-input input');
            pinInputs.forEach((input, index) => {
                input.addEventListener('keyup', function(e) {
                    if (e.key >= '0' && e.key <= '9') {
                        if (index < 3) {
                            pinInputs[index + 1].focus();
                        }
                    } else if (e.key === 'Backspace') {
                        if (index > 0) {
                            pinInputs[index - 1].focus();
                        }
                    }
                });
            });
    
            // Submit PIN
            document.getElementById('submitPin').addEventListener('click', function() {
                const enteredPin = Array.from(pinInputs).map(input => input.value).join('');
                if (enteredPin === '1234') {
                    unlockOverlay.classList.remove('active');
                    showUnlockedMessage();
                } else {
                    alert('Incorrect PIN. Try again.');
                    // Clear inputs
                    pinInputs.forEach(input => input.value = '');
                    pinInputs[0].focus();
                }
            });
    
            // Show Unlocked Message
            const unlockedMessage = document.getElementById('unlockedMessage');
    
            function showUnlockedMessage() {
                unlockedMessage.style.display = 'block';
                setTimeout(() => {
                    unlockedMessage.style.display = 'none';
                }, 2000);
            }
        </script>
    </body>
    </html>
    

    How It Works

    1. Lock Screen Display: The main lock screen displays the current time and date, along with two demo notifications. It has a dark background to simulate Android's dark mode.

    2. Swipe Up Gesture:

      • Mobile Devices: Users can swipe up on the circular area at the bottom of the screen to bring up the PIN entry overlay.
      • Desktop Browsers: Since swipe gestures aren't available, clicking the swipe area will open the PIN entry.
    3. PIN Entry Overlay:

      • Users are prompted to enter a 4-digit PIN.
      • The PIN inputs automatically focus on the next field as digits are entered.
      • Pressing backspace moves the focus to the previous field.
    4. PIN Verification:

      • When the "Unlock" button is clicked, the entered PIN is checked.
      • If correct (1234), an "Unlocked" message briefly appears, and the lock screen is re-displayed.
      • If incorrect, an alert notifies the user, and the PIN fields are cleared.
    5. Responsive Design: The lock screen is designed to fit various screen sizes, making it suitable for both mobile and desktop browsers.

    Customization

    • Change Notifications: Modify the content within the .notification divs to display different notifications.
    • Adjust PIN: Change the correct PIN by modifying the comparison in the JavaScript (enteredPin === '1234').
    • Styling: Customize colors, fonts, and layout by editing the CSS section as desired.

    Notes

    • This simulation uses basic JavaScript for handling gestures and PIN verification. For a more robust application, consider adding additional features like security enhancements and better gesture recognition.
    • The swipe gesture is simplified to detect a vertical swipe with a minimum threshold. Touch sensitivity can be further refined based on requirements.

    Feel free to use and modify this code to suit your needs!