profile fragment firebase ie image and information vadu page update tay firebase ma

//profile fragment aama image firebase ma update karvanu and
name,phone,address,description aatle information firebase ma update tse
//aama bundle no use nthi kreyo aama profile na badle galary name aapiyu che
and drawer na badle Main2Activity.java name aapiyu che joi aatlo change che yad rakhvu
//drawer ma profile name aapi devu

step 1:profilefragment.java
step 2:profileframent.xml
step 3:drawer.java

Step 1:profilefragment.java

package demoproject.aalap.com.delete


/**
 * A simple {@link Fragment} subclass.
 */
public class GalleryFragment extends Fragment {
    View view;

    private FirebaseDatabase database;
    private DatabaseReference userReference;

    //Firebase storage
    FirebaseStorage firebaseStorage;
    StorageReference storageReference;

    String name, address, phone, description, image;
    CircleImageView imgProfile;
    EditText edtname, edtdescription, edtphone,edtaddress;
    Button btnUpdate;

    //public static final int PICK_IMAGE_REQUEST=9999;
    public GalleryFragment() {
        // Required empty public constructor
    }


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



        //Init Firebase storage
        firebaseStorage = FirebaseStorage.getInstance();
        storageReference = firebaseStorage.getReference();

        database = FirebaseDatabase.getInstance();
        userReference = database.getReference().child("DriversInformation").child("A1SFB0RsqnWFKwTKsnhgnVbCToJ3");
        userReference.keepSynced(true);

        imgProfile = view.findViewById(R.id.imgProfile);
        edtname = view.findViewById(R.id.edtname);
        edtaddress = view.findViewById(R.id.edtaddress);
        edtphone = view.findViewById(R.id.edtphone);
        edtdescription = view.findViewById(R.id.edtdescription);
        btnUpdate = view.findViewById(R.id.btnUpdate);

        edtname.setText(name);
        edtaddress.setText(address);
        edtphone.setText(phone);
        edtdescription.setText(description);

        imgProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chooseImage();

            }
        });


        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                updateUserInfo();
                {
                    Toast.makeText(getActivity(), "Please check your connection", Toast.LENGTH_SHORT).show();
                }
            }
        });

        return view;
    }

    private void updateUserInfo() {

        String name = edtname.getText().toString();
        String phone = edtphone.getText().toString();
        String address = edtaddress.getText().toString();
        String description = edtdescription.getText().toString();

        Map<String, Object> updateInfo = new HashMap<>();

        //Check Validation
        if (!TextUtils.isEmpty(name))
            updateInfo.put("name", name);

        if (!TextUtils.isEmpty(phone))
            updateInfo.put("phone", phone);

        if (!TextUtils.isEmpty(address))
            updateInfo.put("address", address);

        if (!TextUtils.isEmpty(description))
            updateInfo.put("description", description);

        userReference.updateChildren(updateInfo).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {

                if (task.isSuccessful())
                    Toast.makeText(getActivity(), "Information uploaded !", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(getActivity(), "Information uploaded failed !", Toast.LENGTH_SHORT).show();
            }
        });

    }


    private void chooseImage() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,"Select picture"),PICK_IMAGE_REQUEST);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode ==PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null)
        {
            Uri saveUri = data.getData();
            if (saveUri!=null)
            {
                final ProgressDialog mDialog = new ProgressDialog(getActivity());
                mDialog.setMessage("Uploading...");
                mDialog.show();

                String image = UUID.randomUUID().toString();    //Random name image upload
                final StorageReference imageFolder =storageReference.child("images/"+image);
                imageFolder.putFile(saveUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                        mDialog.dismiss();

                        imageFolder.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {
                                //Upload this url to avtar property of user
                                //First you need to add avtar property on user model
                                Map<String,Object> avatarUpdate = new HashMap<>();
                                avatarUpdate.put("avatarUrl",uri.toString());


                                userReference.updateChildren(avatarUpdate)
                                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Void> task) {

                                                if (task.isSuccessful())
                                                    Toast.makeText(getActivity(), "Uploaded !", Toast.LENGTH_SHORT).show();
                                                else
                                                    Toast.makeText(getActivity(), "Uploaded Error!", Toast.LENGTH_SHORT).show();
                                            }
                                        });
                            }
                        });
                    }
                })
                        .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {

                                double progress = (100.0* taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                                mDialog.setMessage("Uploading : "+progress+"%");
                            }
                        });
            }
        }
    }
}

step 2:profile.xml

<ScrollView 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"
    android:background="#000000"
    tools:context="demoproject.aalap.com.delete.GalleryFragment">

    <!-- TODO: Update blank fragment layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="16dp"
        android:gravity="center_horizontal">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/imgProfile"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginTop="0dp"
            android:scaleType="centerCrop"
            android:layout_marginBottom="16dp"
            android:paddingTop="@dimen/nav_header_vertical_spacing"
            android:gravity="center_horizontal"
            android:src="@mipmap/ic_launcher"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Name"
            android:id="@+id/txtName"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:textSize="15dp"/>
        <EditText

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_gravity="center_horizontal"
            android:background="#da1313"
            android:textColor="#ffffff"
            android:id="@+id/edtname"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Phone"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="5dp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:textSize="15dp"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="#da1313"
            android:textColor="#ffffff"
            android:id="@+id/edtphone"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Address"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="5dp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:textSize="15dp"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"

            android:background="#da1313"
            android:textColor="#ffffff"
            android:id="@+id/edtaddress"/>


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Description"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="5dp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:textSize="15dp"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="#da1313"
            android:textColor="#ffffff"
            android:id="@+id/edtdescription"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnUpdate"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="5dp"
            android:text="SUBMIT"
            android:layout_gravity="center_horizontal"/>


    </LinearLayout>

</ScrollView>

Step 3:drawer.java

package demoproject.aalap.com.delete;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class Main2Activity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.gallery) {
            GalleryFragment galleryFragment=new GalleryFragment();
            getSupportFragmentManager().beginTransaction()
                    //.add(new profileFragment(),"PAYMENT fragment")
                    .addToBackStack("null")
                    .replace(R.id.maincontainer,galleryFragment)
                    .commit();

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

Comments

Popular posts from this blog

Seaborn

Payment ideal