//jo aama bundle kadi kadiye to normal profile bnu jse drawer ma kso change nai karanu
khale profile na fragment no code j nakhvano and budle jetle jgiya pr che ae nikade deva
itele normal profile bni jse
//profile fragment aama image firebase ma update karvanu and
name,phone,address,description aatle information firebase ma update tse
//aama bundle no use kreyo che ie bundle no use ie kre yo km ke profile vada
fragment ma photo to dekhay and bhar navigation drawer.xml(bhar ie sv thi upar) ma pn pic dekhay
//drawer ma profile name aapi devu
step 1:profilefragment.java
step 2:profileframent.xml
step 3:drawer.java
Step 1:profilefragment.java
profile fragment.java
public class profileFragment 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 profileFragment() {
// 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_profile, container, false);
//Get Bundle here
final Bundle bundle=this.getArguments();
if (bundle != null) {
image = bundle.getString("avatarUrl");
name = bundle.getString("name");
address = bundle.getString("address");
description = bundle.getString("description");
phone = bundle.getString("phone");
}
//Init Firebase storage
firebaseStorage = FirebaseStorage.getInstance();
storageReference = firebaseStorage.getReference();
database = FirebaseDatabase.getInstance();
userReference = database.getReference().child("DriversInformation").child("A1SFB0RsqnWFKwTKsnhgnVbCToJ3");
//DriversLnformation ie firebase ma aa database na name thi save tse
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 fragment.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#000000"
tools:context="demoproject.aalap.com.ideal.profileFragment">
<!-- 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="@drawable/rectangleborder"
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="@drawable/rectangleborder"
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="@drawable/rectangleborder"
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="@drawable/rectangleborder"
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.ideal;
import static demoproject.aalap.com.ideal.Common.PICK_IMAGE_REQUEST;
public class drawer extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private FirebaseDatabase database;
private DatabaseReference userReference;
//Firebase storage
FirebaseStorage firebaseStorage;
StorageReference storageReference;
View headerView;
String tv, image;
CircleImageView imageView;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
firebaseStorage = FirebaseStorage.getInstance();
storageReference = firebaseStorage.getReference();
database = FirebaseDatabase.getInstance();
userReference = database.getReference().child("DriversInformation").child("A1SFB0RsqnWFKwTKsnhgnVbCToJ3");
userReference.keepSynced(true);
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);
headerView = navigationView.getHeaderView(0);
tv1 = headerView.findViewById(R.id.tv1);
imageView=headerView.findViewById(R.id.imageView);
navigationView.setNavigationItemSelectedListener(this);
loadUser();
tv1.setText(tv);
Picasso.with(drawer.this).load(image).placeholder(R.drawable.ic_menu_user).into(imageView);
}
private void showProfileFragment() {
profileFragment accountFragment = new profileFragment();
Bundle bundle=new Bundle();
bundle.putString("avatarUrl",image);
bundle.putString("tv",tv);
accountFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
//.add(new HomeFragment(),"HomeFragment")
.addToBackStack(null)
.replace(R.id.maincontainer,accountFragment)
.commit();
}
@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.drawer, 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.home) {
// Handle the camera action
} else if (id == R.id.profile) {
showProfileFragment();
} else if (id == R.id.payment) {
PAYMENT payment=new PAYMENT();
getSupportFragmentManager().beginTransaction()
//.add(new profileFragment(),"PAYMENT fragment")
.addToBackStack("null")
.replace(R.id.maincontainer,payment)
.commit();
} else if (id == R.id.share) {
} else if (id == R.id.help) {
} else if (id == R.id.histories) {
} else if (id == R.id.as_driver) {
As_DRIVER as_driver=new As_DRIVER();
getSupportFragmentManager().beginTransaction()
//.add(new profileFragment(),"AS_DRIVER fragment")
.addToBackStack("null")
.replace(R.id.maincontainer,as_driver)
.commit();
}else if (id==R.id.signout){
Intent intent=new Intent(drawer.this,activitymainloginandregis.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void loadUser() {
userReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
tv = dataSnapshot.child("name").getValue().toString();
image = dataSnapshot.child("avatarUrl").getValue().toString();
Picasso.with(drawer.this).load(image).placeholder(R.drawable.ic_menu_user).into(imageView);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(drawer.this, ""+databaseError, Toast.LENGTH_SHORT).show();
}
});
}
private void updateUserInfo() {
Map<String, Object> updateInfo = new HashMap<>();
//Check Validation
if (!TextUtils.isEmpty(tv))
updateInfo.put("tv", tv);
userReference.updateChildren(updateInfo).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful())
Toast.makeText(drawer.this, "Information uploaded !", Toast.LENGTH_SHORT).show();
else
Toast.makeText(drawer.this, "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(drawer.this);
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(drawer.this, "Uploaded !", Toast.LENGTH_SHORT).show();
else
Toast.makeText(drawer.this, "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+"%");
}
});
}
}
}
}
Comments
Post a Comment