Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
14 kB
6
Indexable
Never
AndroidManifest <uses-permission android:name="android.permission.INTERNET"/>
package com.example.myapplication;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;

public class FavoriteFragment extends Fragment {

    public FavoriteFragment(){
        // require a empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_favorite, container, false);
    }
}


package com.example.myapplication;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;

public class GalleryFragment extends Fragment {

    public GalleryFragment(){
        // require a empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_gallery, container, false);
    }
}


package com.example.myapplication;

import static android.content.ContentValues.TAG;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

//import com.android.volley.AuthFailureError;
//import com.android.volley.Request;
//import com.android.volley.RequestQueue;
//import com.android.volley.Response;
//import com.android.volley.VolleyError;
//import com.android.volley.toolbox.JsonObjectRequest;
//import com.android.volley.toolbox.StringRequest;
//import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class GenerateFragment extends Fragment {
    EditText prompt;
    Button btDraw, btSave, btShare;
    ImageView imgView;

//    private RequestQueue mRequestQueue;
//    private StringRequest mStringRequest;
    private String url = "https://api.deepai.org/api/text2img";
    public GenerateFragment(){
        // require a empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_generate, container, false);
        prompt = view.findViewById(R.id.prompt);
        imgView = view.findViewById(R.id.imgView);
        btDraw = view.findViewById(R.id.btDraw);
        btSave = view.findViewById(R.id.btSave);
        btShare = view.findViewById(R.id.btShare);

//        prompt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
//                if(i== EditorInfo.IME_ACTION_SEND){
//                    String text = prompt.getText().toString().trim();
//                    if(text.length()>0){
////                        try {
////                            getResponse(text);
////                        } catch (JSONException e) {
////                            throw new RuntimeException(e);
////                        }
//                    }
//                    else {
//                        Toast.makeText(getContext(), "Please enter something", Toast.LENGTH_SHORT).show();
//                    }
//                    return true;
//                }
//                return false;
//            }
//        });
//
        return view;
    }
//    private void getResponse(String text) throws JSONException {
//        mRequestQueue = Volley.newRequestQueue(getContext());
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("text",text);
////        jsonObject.put("api-key","quickstart-QUdJIGlzIGNvbWluZy4uLi4K");
//
//        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
//            @Override
//            public void onResponse(JSONObject response) {
//                try {
//                    String imgUrl = (String) response.getString("output_url");
//                    System.out.println("image: "+imgUrl);
//                } catch (JSONException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//
//        },new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                error.printStackTrace();
//            }
//        }){
//            @Override
//            public Map<String, String> getHeaders() throws AuthFailureError {
//                Map<String, String>  params = new HashMap<String, String>();
//                params.put("api-key", "quickstart-QUdJIGlzIGNvbWluZy4uLi4K");
//                return params;
//            }
//        };
//        mRequestQueue.add(jsonObjectRequest);
//    }

}

package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    GenerateFragment generateFragment = new GenerateFragment();
    GalleryFragment galleryFragment = new GalleryFragment();
    FavoriteFragment favoriteFragment = new FavoriteFragment();
    BottomNavigationView bottomNavigationView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
        bottomNavigationView.setSelectedItemId(R.id.generate);

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        if(item.getItemId()==R.id.generate) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.flFragment, generateFragment)
                    .commit();
            return true;
        }
        else if(item.getItemId()==R.id.gallery) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.flFragment, galleryFragment)
                    .commit();
            return true;
        }
        else if(item.getItemId()==R.id.favorite){
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.flFragment, favoriteFragment)
                    .commit();
            return true;
        }
        return false;
    }
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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">

    <FrameLayout
        android:id="@+id/flFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu"/>

</androidx.constraintlayout.widget.ConstraintLayout>

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FavoriteFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="100dp"
        android:text="Favorite" />

</FrameLayout>

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GalleryFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="100dp"
        android:text="Gallery" />

</FrameLayout>

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GenerateFragment">

    <!-- TODO: Update blank fragment layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="10">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="Create AI Art with our free AI image generator."
            android:textSize="17dp"
            android:textAlignment="center"/>
        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/pprompt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_margin="5dp"
            android:hint="Enter your query to generate image"
            android:padding="5dp"
            android:textColorHint="@color/white">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/prompt"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/black"
                android:drawableEnd="@drawable/ic_send"
                android:drawableTint="@color/white"
                android:ems="10"
                android:imeOptions="actionSend"
                android:importantForAutofill="no"
                android:inputType="textEmailAddress"
                android:textColor="@color/white"
                android:textColorHint="@color/white"
                android:textSize="14sp" />
        </com.google.android.material.textfield.TextInputLayout>
        <Button
            android:id="@+id/btDraw"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="Draw"
            android:textSize="20dp">
        </Button>
        <ImageView
            android:id="@+id/imgView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="5">
        </ImageView>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="2">
            <Button
                android:id="@+id/btSave"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="Save"
                android:textSize="20dp">
            </Button>
            <Button
                android:id="@+id/btShare"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="Share"
                android:textSize="20dp">
            </Button>
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/generate"
        android:title="Generate"
        android:icon="@drawable/ic_generate"/>
    <item
        android:id="@+id/gallery"
        android:title="Gallery"
        android:icon="@drawable/ic_gallery"/>
    <item
        android:id="@+id/favorite"
        android:title="Favorite"
        android:icon="@drawable/ic_favorite"/>
</menu>
Themes.xml
<style name="Base.Theme.MyApplication" parent="Theme.Material3.DayNight.NoActionBar">