How to draw or write text by hand on image selected from gallery in android? - android-imageview

Actually I have written a code to select image from gallery,but I dont know how to draw or write a text by hand on it.and Edited image should be saved separately in sdcard.Basically I have to make basic paint app where canvas will be my selected image,and my touch is input to draw a circle or anything.
package listdisplay.example.com.photoedit;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.FileDescriptor;
import java.io.IOException;
public class MainActivity_photoedit extends AppCompatActivity {
private static final int RORC =0;
ImageView iview;
Button button,button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_photoedit);
iview= (ImageView) findViewById(R.id.imageView);
button =(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent,RORC);
}
}
);
}
#Override
public void onActivityResult(int requestCode,int resultCode,Intent resultData){
if(requestCode==RORC && resultCode==RESULT_OK){
Uri uri =null;
if(resultData!=null){
uri=resultData.getData();
try {
Bitmap bitmap= getBitmapFromUri(uri);
iview.setImageBitmap(bitmap);
}
catch (IOException e){
e.printStackTrace();
}
}
}
}
private Bitmap getBitmapFromUri(Uri uri) throws IOException{
ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri,"r");
FileDescriptor fileDescriptor= parcelFileDescriptor.getFileDescriptor();
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return bitmap;
}
}

Also, there is another way to do like this is using FingerPaint Api.
For that download code from here,
"https://github.com/nikt/Finger-Paint"
Yes,It also not using Image-view, But it will pick image from gallery and set it into Draw-view and paint on that image.You can also try this.

I have find solution for your requirement . So try this,But for that you have to take Drawing-view instead of Image-view to draw or write text on Image which is picked from Gallery.
Hereby, I am posting code.
activity_main.xml :
<LinearLayout 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:gravity="center"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/buttonNew"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/start_new"
android:src="#drawable/new_pic" />
<ImageButton
android:id="#+id/buttonBrush"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/brush"
android:src="#drawable/brush" />
<ImageButton
android:id="#+id/buttonErase"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/erase"
android:src="#drawable/eraser" />
<ImageButton
android:id="#+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/save"
android:src="#drawable/save" />
<ImageButton
android:id="#+id/buttonPickImage"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/save"
android:src="#drawable/save" />
</LinearLayout>
<com.wordpress.priyankvex.paintapp.DrawingView
android:id="#+id/drawing"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:background="#color/white" />
<!-- Color pallet -->
<LinearLayout
android:id="#+id/paint_colors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="2dp"
android:background="#color/skin"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/pallet"
android:tag="#color/skin" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="2dp"
android:background="#color/black"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/pallet"
android:tag="#color/black" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="2dp"
android:background="#color/red"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/pallet"
android:tag="#color/red" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="2dp"
android:background="#color/green"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/pallet"
android:tag="#color/green" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="2dp"
android:background="#color/blue"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/pallet"
android:tag="#color/blue" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="2dp"
android:background="#color/yellow"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/pallet"
android:tag="#color/yellow" />
</LinearLayout>
</LinearLayout>
MainActivity.java :
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.util.UUID;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private DrawingView mDrawingView;
private ImageButton currPaint, drawButton, eraseButton, newButton, saveButton, buttonPickImage;
private float smallBrush, mediumBrush, largeBrush;
private final int RESULT_LOAD_IMAGE = 20;
private Uri pickedImage;
private String imagePath = "";
public Bitmap PaintBitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawingView = (DrawingView) findViewById(R.id.drawing);
// Getting the initial paint color.
LinearLayout paintLayout = (LinearLayout) findViewById(R.id.paint_colors);
// 0th child is white color, so selecting first child to give black as initial color.
currPaint = (ImageButton) paintLayout.getChildAt(1);
currPaint.setImageDrawable(getResources().getDrawable(R.drawable.pallet_pressed));
drawButton = (ImageButton) findViewById(R.id.buttonBrush);
drawButton.setOnClickListener(this);
eraseButton = (ImageButton) findViewById(R.id.buttonErase);
eraseButton.setOnClickListener(this);
newButton = (ImageButton) findViewById(R.id.buttonNew);
newButton.setOnClickListener(this);
saveButton = (ImageButton) findViewById(R.id.buttonSave);
saveButton.setOnClickListener(this);
buttonPickImage = (ImageButton) findViewById(R.id.buttonPickImage);
buttonPickImage.setOnClickListener(this);
smallBrush = getResources().getInteger(R.integer.small_size);
mediumBrush = getResources().getInteger(R.integer.medium_size);
largeBrush = getResources().getInteger(R.integer.large_size);
// Set the initial brush size
mDrawingView.setBrushSize(mediumBrush);
}
/**
* Method is called when color is clicked from pallet.
*
* #param view ImageButton on which click took place.
*/
public void paintClicked(View view) {
if (view != currPaint) {
// Update the color
ImageButton imageButton = (ImageButton) view;
String colorTag = imageButton.getTag().toString();
mDrawingView.setColor(colorTag);
// Swap the backgrounds for last active and currently active image button.
imageButton.setImageDrawable(getResources().getDrawable(R.drawable.pallet_pressed));
currPaint.setImageDrawable(getResources().getDrawable(R.drawable.pallet));
currPaint = (ImageButton) view;
mDrawingView.setErase(false);
mDrawingView.setBrushSize(mDrawingView.getLastBrushSize());
}
}
#Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.buttonBrush:
// Show brush size chooser dialog
showBrushSizeChooserDialog();
break;
case R.id.buttonErase:
// Show eraser size chooser dialog
showEraserSizeChooserDialog();
break;
case R.id.buttonNew:
// Show new painting alert dialog
showNewPaintingAlertDialog();
break;
case R.id.buttonSave:
// Show save painting confirmation dialog.
showSavePaintingConfirmationDialog();
break;
case R.id.buttonPickImage:
// Show save painting confirmation dialog.
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
pickedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
// set Gallery Image to Drawing-view
if (imagePath != null) {
PaintBitmap = getResizeBitmap(imagePath);
BitmapDrawable ob = new BitmapDrawable(getResources(), PaintBitmap);
mDrawingView.setBackgroundDrawable(ob);
}
}
}
}
public static Bitmap getResizeBitmap(String path) {
BitmapFactory.Options op = new BitmapFactory.Options();
op.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bmap = BitmapFactory.decodeFile(path, op);
int nh = (int) (bmap.getHeight() * (1024.0 / bmap.getWidth()));
Bitmap bitmapThumbnail = Bitmap.createScaledBitmap(bmap, 1024, nh, true);
int bitmapWidthMain = bitmapThumbnail.getWidth();
int bitmapHeightMain = bitmapThumbnail.getHeight();
return bitmapThumbnail;
}
private void showBrushSizeChooserDialog() {
final Dialog brushDialog = new Dialog(this);
brushDialog.setContentView(R.layout.dialog_brush_size);
brushDialog.setTitle("Brush size:");
ImageButton smallBtn = (ImageButton) brushDialog.findViewById(R.id.small_brush);
smallBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawingView.setBrushSize(smallBrush);
mDrawingView.setLastBrushSize(smallBrush);
brushDialog.dismiss();
}
});
ImageButton mediumBtn = (ImageButton) brushDialog.findViewById(R.id.medium_brush);
mediumBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawingView.setBrushSize(mediumBrush);
mDrawingView.setLastBrushSize(mediumBrush);
brushDialog.dismiss();
}
});
ImageButton largeBtn = (ImageButton) brushDialog.findViewById(R.id.large_brush);
largeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawingView.setBrushSize(largeBrush);
mDrawingView.setLastBrushSize(largeBrush);
brushDialog.dismiss();
}
});
mDrawingView.setErase(false);
brushDialog.show();
}
private void showEraserSizeChooserDialog() {
final Dialog brushDialog = new Dialog(this);
brushDialog.setTitle("Eraser size:");
brushDialog.setContentView(R.layout.dialog_brush_size);
ImageButton smallBtn = (ImageButton) brushDialog.findViewById(R.id.small_brush);
smallBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawingView.setErase(true);
mDrawingView.setBrushSize(smallBrush);
brushDialog.dismiss();
}
});
ImageButton mediumBtn = (ImageButton) brushDialog.findViewById(R.id.medium_brush);
mediumBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawingView.setErase(true);
mDrawingView.setBrushSize(mediumBrush);
brushDialog.dismiss();
}
});
ImageButton largeBtn = (ImageButton) brushDialog.findViewById(R.id.large_brush);
largeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawingView.setErase(true);
mDrawingView.setBrushSize(largeBrush);
brushDialog.dismiss();
}
});
brushDialog.show();
}
private void showNewPaintingAlertDialog() {
AlertDialog.Builder newDialog = new AlertDialog.Builder(this);
newDialog.setTitle("New drawing");
newDialog.setMessage("Start new drawing (you will lose the current drawing)?");
newDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mDrawingView.startNew();
dialog.dismiss();
}
});
newDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
newDialog.show();
}
private void showSavePaintingConfirmationDialog() {
AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
saveDialog.setTitle("Save drawing");
saveDialog.setMessage("Save drawing to device Gallery?");
saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//save drawing
mDrawingView.setDrawingCacheEnabled(true);
String imgSaved = MediaStore.Images.Media.insertImage(
getContentResolver(), mDrawingView.getDrawingCache(),
UUID.randomUUID().toString() + ".png", "drawing");
if (imgSaved != null) {
Toast savedToast = Toast.makeText(getApplicationContext(),
"Drawing saved to Gallery!", Toast.LENGTH_SHORT);
savedToast.show();
} else {
Toast unsavedToast = Toast.makeText(getApplicationContext(),
"Oops! Image could not be saved.", Toast.LENGTH_SHORT);
unsavedToast.show();
}
// Destroy the current cache.
mDrawingView.destroyDrawingCache();
}
});
saveDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
saveDialog.show();
}
}
Manifest.xml :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Try This code, It include brush for draw text on image with different brush size,eraser and color picker to draw text in different colors on text and yes, It will save image with text in gallery.
Hereby, i am attaching screenshot,
Hope, it will help you. :)

Related

Problem with Recyclerview not always loading items

Greetings to all the experts.
I have the following problem, I have implemented Billing Subscriptions in Android, the code works well, the problem is that the Recyclerview sometimes loads and many others it doesn't, sometimes it loads first, sometimes you have to enter the atricity a few times, I don't understand why it makes that bug, if you could check the code and guide me I would appreciate it
suscription.java
public class suscripcion extends AppCompatActivity implements PurchasesUpdatedListener {
private TextView statuspremium, rangopremium;
private BillingClient billingClient;
private AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener;
private RecyclerView recyclerView_sub;
private DocumentReference documentReference;
private FirebaseFirestore mFirestore;
private FirebaseUser user;
private FirebaseAuth fAuth;
private String userID;
private MySubAdapter adapter;
private Button cargarpremium;
private ImageView ayudapremium, txtclose;
private String status;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_suscripcion);
this.setFinishOnTouchOutside(false);
mFirestore = FirebaseFirestore.getInstance();
fAuth = FirebaseAuth.getInstance();
user = fAuth.getCurrentUser();
ayudapremium = findViewById(R.id.ayudapremium);
ayudapremium.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(suscripcion.this, ofrecepremium.class));
}
});
if (user !=null)
{
userID = fAuth.getCurrentUser().getUid();
}
documentReference = mFirestore.collection("users").document(userID);
documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
#Override
public void onEvent(#Nullable DocumentSnapshot value, #Nullable FirebaseFirestoreException error) {
status = value.getString("status");
}
});
setupSubClient();
iniciador();
txtclose = findViewById(R.id.txtclosesub);
txtclose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
documentReference.update("premiumverificar", "no");
finish();
startActivity(new Intent(suscripcion.this, MainActivity.class));
}
});
}
private void iniciador() {
statuspremium = findViewById(R.id.statuspremium);
rangopremium = findViewById(R.id.rangopremium);
recyclerView_sub = findViewById(R.id.recyclerview_sub);
recyclerView_sub.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView_sub.setLayoutManager(layoutManager);
recyclerView_sub.addItemDecoration(new DividerItemDecoration(this, layoutManager.getOrientation()));
cargarpremium = findViewById(R.id.cargarpremium);
cargarpremium.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
startActivity(getIntent());
}
});
}
private void loadAllSubPackage() {
if (billingClient.isReady())
{
Log.d("ERROR20", "onSkuDetailsResponse: ");
SkuDetailsParams params = SkuDetailsParams
.newBuilder()
.setSkusList(Arrays.asList("vf_sub_unmes", "vf_sub_tresmes"))
.setType(BillingClient.SkuType.SUBS)
.build();
billingClient.querySkuDetailsAsync(params, new SkuDetailsResponseListener() {
#Override
public void onSkuDetailsResponse(#NonNull BillingResult billingResult, #Nullable List<SkuDetails> list) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK)
{
adapter = new MySubAdapter(suscripcion.this, list, billingClient);
recyclerView_sub.setAdapter(adapter);
recyclerView_sub.setVisibility(View.VISIBLE);
Log.d("ERROR5", "onSkuDetailsResponse: " + billingResult.getResponseCode());
}else
{
Log.d("ERROR6", "onSkuDetailsResponse: " + billingResult.getResponseCode());
}
}
});
}else
{
loadAllSubPackage();
Log.d("ERROR7", "loadAllSubPackage: Billing cliente not ready" );
}
}
private void setupSubClient() {
acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() {
#Override
public void onAcknowledgePurchaseResponse(#NonNull BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK)
{
Log.d("ERROR1", "onAcknowledgePurchaseResponse: " +
billingResult.getResponseCode());
// rangopremium.setVisibility(View.VISIBLE);
// tiemposub.setVisibility(View.VISIBLE);
}else
{
Log.d("ERROR21", "onAcknowledgePurchaseResponse: " +
billingResult.getResponseCode());
}
}
};
billingClient = BillingClientSetup.getInstance(this, this);
billingClient.startConnection(new BillingClientStateListener() {
#Override
public void onBillingServiceDisconnected() {
setupSubClient();
Log.d("ERROR2", "onBillingServiceDisconnected: " );
}
#Override
public void onBillingSetupFinished(#NonNull BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK)
{
Log.d("ERROR3", "onBillingSetupFinished: " +
billingResult.getResponseCode());
// //Query
// List<Purchase> purchases = billingClient.queryPurchases(BillingClient.SkuType.SUBS)
// .getPurchasesList();
billingClient.queryPurchasesAsync(BillingClient.SkuType.SUBS, new PurchasesResponseListener() {
#Override
public void onQueryPurchasesResponse(#NonNull BillingResult billingResult, #NonNull List<Purchase> list) {
if (list.size() > 0)
{
for (Purchase purchase:list)
handleSubAlreadyPurchase(purchase);
Log.d("ERROR16", "onBillingSetupFinished: " +
billingResult.getResponseCode());
}else
{
loadAllSubPackage();
documentReference.update("status", "user");
statuspremium.setVisibility(View.VISIBLE);
Log.d("ERROR17", "onBillingSetupFinished: " +
billingResult.getResponseCode());
// rangopremium.setVisibility(View.GONE);
// tiemposub.setVisibility(View.GONE);
// statuspremium.setVisibility(View.VISIBLE);
// recyclerView_sub.setVisibility(View.VISIBLE);
}
}
});
}
else
{
Log.d("ERROR4", "onBillingSetupFinished: " +
billingResult.getResponseCode());
}
}
});
}
private void handleSubAlreadyPurchase(Purchase purchases) {
if (purchases.getPurchaseState() == Purchase.PurchaseState.PURCHASED)
{
documentReference = mFirestore.collection("users").document(userID);
documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
#Override
public void onEvent(#Nullable DocumentSnapshot value, #Nullable FirebaseFirestoreException error) {
if (value != null && value.exists()) {
status = value.getString("status");
rangopremium.setVisibility(View.VISIBLE);
rangopremium.setText(status);
}
}
});
Log.d("ERROR11", "handleSubAlreadyPurchase: ");
if (!purchases.isAcknowledged())
{
Log.d("ERROR12", "handleSubAlreadyPurchase: ");
AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchases.getPurchaseToken())
.build();
billingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
}else
{
Log.d("ERROR13", "handleSubAlreadyPurchase: ");
}
}
if (purchases.getPurchaseState() == Purchase.PurchaseState.UNSPECIFIED_STATE){
Log.d("ERROR14", "handleSubAlreadyPurchase: ");
}
}
#Override
public void onPurchasesUpdated(#NonNull BillingResult billingResult, #Nullable List<Purchase> list) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && list !=null)
{
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss", Locale.getDefault());
String currentDateandTime = formatter.format(new Date());
documentReference = mFirestore.collection("users").document(userID);
documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
#Override
public void onEvent(#Nullable DocumentSnapshot value, #Nullable FirebaseFirestoreException error) {
if (value != null && value.exists()){
status = value.getString("status");
rangopremium.setVisibility(View.VISIBLE);
rangopremium.setText(status);
}
}
});
statuspremium.setVisibility(View.GONE);
recyclerView_sub.setVisibility(View.GONE);
documentReference.update("status", "PREMIUM");
documentReference.update("bloqueo", 3);
documentReference.update("premiumfecha", currentDateandTime);
Log.d("ERROR8", "onPurchasesUpdated: " + billingResult.getResponseCode());
for (Purchase purchase:list)
handleSubAlreadyPurchase(purchase);
}else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED)
{
Log.d("ERROR9", "onPurchasesUpdated: " + billingResult.getResponseCode());
rangopremium.setVisibility(View.GONE);
statuspremium.setVisibility(View.VISIBLE);
recyclerView_sub.setVisibility(View.VISIBLE);
documentReference.update("status", "user");
}else
{
Log.d("ERROR10", "onPurchasesUpdated: " + billingResult.getResponseCode());
}
}
#Override
public void onBackPressed() {
}
}
MySubAdapter.java
public class MySubAdapter extends RecyclerView.Adapter<MySubAdapter.MyViewHolder> {
AppCompatActivity appCompatActivity;
List<SkuDetails> skuDetailsList;
BillingClient billingClient;
public MySubAdapter(AppCompatActivity appCompatActivity, List<SkuDetails> skuDetailsList, BillingClient billingClient) {
this.appCompatActivity = appCompatActivity;
this.skuDetailsList = skuDetailsList;
this.billingClient = billingClient;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(appCompatActivity.getBaseContext())
.inflate(R.layout.listado_sub, parent, false));
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.txt_sub_name.setText(skuDetailsList.get(position).getTitle());
holder.txt_precio_sub.setText(skuDetailsList.get(position).getPrice());
holder.setListener(new IReciclerClickListener() {
#Override
public void onClick(View view, int position) {
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetailsList.get(position))
.build();
int reponse = billingClient.launchBillingFlow(appCompatActivity, billingFlowParams)
.getResponseCode();
switch(reponse)
{
case BillingClient.BillingResponseCode.BILLING_UNAVAILABLE:
Log.d("ERROR11", "BILLING_UNAVAILABLE");
break;
case BillingClient.BillingResponseCode.DEVELOPER_ERROR:
Log.d("ERROR12", "DEVELOPER_ERROR");
break;
case BillingClient.BillingResponseCode.FEATURE_NOT_SUPPORTED:
Log.d("ERROR13", "FEATURE_NOT_SUPPORTED");
break;
case BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED:
Log.d("ERROR14", "ITEM_ALREADY_OWNED");
break;
case BillingClient.BillingResponseCode.SERVICE_DISCONNECTED:
Log.d("ERROR15", "SERVICE_DISCONNECTED");
break;
case BillingClient.BillingResponseCode.SERVICE_TIMEOUT:
Log.d("ERROR16", "SERVICE_TIMEOUT");
break;
case BillingClient.BillingResponseCode.ITEM_UNAVAILABLE:
Log.d("ERROR17", "ITEM_UNAVAILABLE");
break;
default:
break;
}
}
});
}
#Override
public int getItemCount() {
return skuDetailsList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView txt_sub_name, txt_precio_sub;
IReciclerClickListener listener;
public void setListener(IReciclerClickListener listener) {
this.listener = listener;
}
public MyViewHolder(#NonNull View itemView) {
super(itemView);
txt_sub_name = itemView.findViewById(R.id.txt_sub_name);
txt_precio_sub = itemView.findViewById(R.id.txt_precio_sub);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
listener.onClick(v, getBindingAdapterPosition());
}
}
}
activity_suscription
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/Snackbar_apoyo"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/fondocurva"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center">
<ImageView
android:contentDescription="#string/app_name"
android:id="#+id/txtclosesub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_close_black"
android:layout_gravity="start"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/statuspremium"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:gravity="center"
android:shadowColor="#color/negro"
android:shadowDx="2"
android:shadowDy="5"
android:visibility="gone"
android:shadowRadius="10"
android:text="#string/no_premium"
android:textColor="#color/colorAccent"
android:textSize="25sp"
android:textStyle="bold|italic" />
<com.hanks.htextview.rainbow.RainbowTextView
android:id="#+id/rangopremium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:visibility="gone"
android:textSize="25sp"
android:textStyle="bold"
app:colorSpace="150dp"
app:colorSpeed="10dp"/>
<ImageView
android:contentDescription="#string/app_name"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/vinoayuda"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/haste_premium"
android:textSize="18sp"
android:textColor="#color/colorAccent"
android:layout_gravity="center"
android:gravity="center"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_marginBottom="20dp"
android:textStyle="bold"
android:fontFamily="sans-serif-smallcaps"
android:shadowColor="#color/negro"
android:shadowRadius="10"
android:shadowDx="2"
android:shadowDy="5"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:gravity="center"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/que_ofrece_el_premium"
android:textSize="16sp"
android:textColor="#color/blanco"
android:layout_gravity="center"
android:gravity="center"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:textStyle="bold"
android:fontFamily="sans-serif-smallcaps" />
<ImageView
android:id="#+id/ayudapremium"
android:background="#drawable/curvas"
android:contentDescription="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="10dp"
android:src="#drawable/ic_baseline_help"/>
</LinearLayout>
<Button
android:id="#+id/cargarpremium"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/cargar_botones"
android:padding="10dp"
android:textColor="#color/colorPrimary"
android:background="#drawable/btn_desing"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview_sub"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="20dp">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
PD: I have placed a button to reload the Activity, and only then sometimes it loads and sometimes it doesn't, just to half solve that bug, but the ideal is that the recyclerview loads me the first time

How to change xml source of svg image in a imageview

I Found lots of questions for image changing in imageview with another image but I have different problem.
I have to two svg images in xml format in drawable folder.
I want to toggle between images on click of imageview. Like if image a.xml is there then on click on it image b.xml should be displayed, to do that I have to fetch the current xml image source of imageview nd set it to the other one.
How to do that ?
MainActivity.java :
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageView);
iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v == iv){
if(/* How to check current current source of imagevie here */ ){
/* How to set new xml svg source to imagevie here */
}
}
}
});
}
}
xml :
<FrameLayout 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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
tools:context="com.abcd.MainActivity">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:srcCompat="#android:drawable/alert_dark_frame"
android:src="#drawable/a"
android:layout_centerHorizontal="true"
android:id="#+id/imageView"
android:layout_alignParentTop="true" />
</FrameLayout>
drawable folder containing two svg sources
figured out the partial solution because I can set the new svg source to imageview but still can't the current source :
if(v == iv){
int k = 10;
switch (count) {
case 1 :
iv.setImageResource(R.drawable.a);
k = 0 ;
break;
case 0 :
iv.setImageResource(R.drawable.b);
k=1 ;
break;
}
count = k ;
}

RecyclerView and Glide messing up the layout

I am running into a strange issue with the RecyclerView loading images dynamically with Glide. Looks like race condition and I looked at this thread but it did not solve the problem:
Recyclerview Adapter and Glide - same image every 4-5 rows
Here's how it looks like screenshot and the red arrows shows the image glitch, notice that the user name text ALWAYS exist.
The code itself looks pretty straight forward:
RV Adapter:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
int viewType = getItemViewType(position);
bindSubscribersCell((SubscribersListViewHolder) viewHolder, position);
}
binding view:
private void bindSubscribersCell(SubscribersListViewHolder holder, int position) {
SubscribersListRowTypeUser subscriberRow = (SubscribersListRowTypeUser) mItems.get(position);
String userFullName = SDKUserHelper.MakeFullName(subscriberRow.getUser());
holder.getUsername().setText(userFullName);
holder.getUsername().setVisibility(View.VISIBLE);
CircleImageView userImageView = holder.getAvatarImage();
Drawable defaultIcon = ContextCompat.getDrawable(
userImageView.getContext(),
R.drawable.avatar_guest);
RecylerViewHelper.loadImageView(
subscriberRow.getUser().getPhotoUrl(),
userImageView,
defaultIcon,
true);
}
the image load helper using the same technique described on the thread I mentioned above:
public static void loadImageView(
String imageUrl,
ImageView imageView,
Drawable drawable,
boolean icon) {
Context context = imageView.getContext();
if (imageUrl != null && !imageUrl.isEmpty()) {
if (icon) {
Glide.with(context).using(CloudinaryHelper.getUrlLoaderPresetPOIIcon(context)).
load(imageUrl).
centerCrop().
crossFade().
into(imageView);
} else {
Glide.with(context).
load(imageUrl).
crossFade().
into(imageView);
}
} else {
Glide.clear(imageView);
imageView.setImageDrawable(drawable);
}
}
the inner layout of the cell:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_avatar"
android:layout_width="wrap_content"
android:layout_height="#dimen/splash_icon_height"
android:layout_centerVertical="true"
android:src="#drawable/avatar_guest" />
<TextView
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="40dp"
android:layout_toEndOf="#+id/user_avatar"
android:ellipsize="end"
android:gravity="center"
android:visibility="gone"
android:maxLines="1"
android:text=""
android:textSize="14sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="20dp"
android:src="#drawable/arrow_forward_gray" />
</RelativeLayout>
the wrapper class to fetch the layout controls:
public class SubscribersListViewHolder extends RecyclerView.ViewHolder {
final private CircleImageView mAvatarImage;
final private TextView mUsername;
final private View mViewHolder;
public SubscribersListViewHolder(View itemView) {
super(itemView);
mViewHolder = itemView;
mUsername = (TextView) itemView.findViewById(R.id.user_name);
assert mUsername != null;
mAvatarImage = (CircleImageView) itemView.findViewById(R.id.user_avatar);
assert mAvatarImage != null;
}
public TextView getUsername() { return mUsername; }
public CircleImageView getAvatarImage() { return mAvatarImage; }
public View getViewHolder() { return mViewHolder; }
}
If anyone can spot the race condition, I really appreciate.
thank you!
The fix was to set the icon width and height to a fixed value, so instead of "wrap_content" make some fixed dp (in my case 49dp). In other words:
WRONG (race condition)
android:layout_width="wrap_content"
android:layout_height="#dimen/splash_icon_height"
CORRECT:
android:layout_width="#dimen/splash_icon_height"
android:layout_height="#dimen/splash_icon_height"
But the question now becomes why? I have no idea.

Gaps and unexpcted moves in RecyclerView

I'm using RecyclerView along with StaggeredGridLayoutManager for showing items with slightly different appearance. in items I use AspectRatioImageView by JakeWharton (link) to maintain the imageView sizes before the image actually downloaded and shown by Picasso. RV (RecyclerView) is populated by server data in infinite scroll pattern.
The problem is (as shown in included pictures), when scolling, some gaps appear on top and bottom of RV (in picture and sometimes items moves between the columns! (taking screenshot is hard :D)
Edit 2: A really important thing I forgot, is that I use the same layout for all of the items but regarding to adapter items some part of the layouts change their visibility to Gone or vice versa.
My question: Why RV behave like this? Am I doing something wrong? Is this normal in RV?
The code is large and I don't know which part is useful to put here. If you can narrow down the cause I will put some code here.
EDIT: Code parts related to problem
xml:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_texture"
android:padding="#dimen/padding_lvlhalf"
android:scrollbarStyle="outsideOverlay"/>
initializing view:
#Bind(R.id.recycler_view) RecyclerView mRecyclerView;
.
.
.
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(columnCount, direction));
mRecyclerView.setAdapter(
new RecyclerViewAdapter<>(ctx, new ViewFiller<Post, DashboardPostViewHolder>() {
#Override
public void fill(Post post, DashboardPostViewHolder holder) {
holder.fill(post);
}
#Override
public DashboardPostViewHolder getViewHolder(View view) {
return new DashboardPostViewHolder(view, receiverName);
}
#Override
public int getLayoutID() {
return R.layout.post_dashboard;
}
});)
My custom Adapter:
public RecyclerViewAdapter(Context context, ViewFiller filler) {
super();
this.filler = filler;
mLayoutInflater = LayoutInflater.from(context);
items = new ArrayList<>();
}
#Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = mLayoutInflater.inflate(filler.getLayoutID(), parent, false);
return filler.getViewHolder(v);
}
#Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
filler.fill(getItem(position), holder);
}
#Override
public int getItemCount() {
return items.size();
}
#Override
public long getItemId(int position) {
return position;
}
public T getItem(int position){
return items.get(position);
}
public void addItem(int position, T item){
items.add(position, item);
notifyItemInserted(position);
}
public void addItem(T item){
items.add(item);
notifyItemInserted(items.size() - 1);
}
public void addItems(List<T> data){
int oldSize = items.size();
items.addAll(data);
notifyItemRangeInserted(oldSize, data.size());
}
public void addItemsToListHead(List<T> data){
items.addAll(0, data);
notifyItemRangeInserted(0, data.size());
}
public void resetData(List<T> data) {
this.items = data;
notifyDataSetChanged();
}
public void removeItem(int position){
items.remove(position);
notifyItemRemoved(position);
}
public void clear(){
this.items.clear();
notifyDataSetChanged();
}
public List<T> getItems(){
return items;
}
ViewFiller interface
public interface ViewFiller<D, VH extends BaseViewHolder>{
void fill(D data, VH holder);
VH getViewHolder(View view);
#LayoutRes int getLayoutID();
}
Edit 2 (Cont.): My items xml and how I fill them..
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/margin_lvlhalf"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/post"
android:background="#color/poinila_background">
<TextView
android:layout_gravity="right"
android:text="#string/test_title"
android:layout_margin="#dimen/margin_lvl1"
style="#style/match_wrap.large_text.centered_right"
android:id="#+id/post_title"
android:visibility="gone"/>
<!--Post Image-->
<com.shaya.poinila.android.presentation.view.AspectRatioImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:contentDescription="#string/post_content_description"
android:id="#+id/post_image"
android:visibility="gone"
app:aspectRatioEnabled="true"
app:dominantMeasurement="width"
/>
<!--Content-->
<TextView
android:id="#+id/post_content"
style="#style/match_wrap.medium_text.three_line.end_ellipsize"
android:layout_margin="#dimen/margin_lvl1"/>
<!--android:text="#string/test_long"-->
<include layout="#layout/horizontal_line"
/>
<!--Post Author-->
<include android:id="#+id/post_author"
layout="#layout/circle_image_title_subtitle_reverse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_lvl1"/>
<include layout="#layout/horizontal_line"
/>
<!--Post Stats (favs, comments, etc) -->
<include android:id="#+id/post_stats"
layout="#layout/stats"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_lvlhalf"
android:layout_marginBottom="#dimen/margin_lvlhalf"/>
<include layout="#layout/horizontal_line"/>
<!--Post Collection Info-->
<include layout="#layout/rounded_image_title_subtitle_reverse"
android:id="#+id/post_collection"/>
</LinearLayout>
</android.support.v7.widget.CardView>
PostViewHolder class
public class PostViewHolder extends RecyclerView.ViewHolder
public void fill(Post post) {
if (post.type == PostType.IMAGE) {
postImage.setVisibility(View.VISIBLE);
postName.setVisibility(TextUtils.isEmpty(post.name) ? View.VISIBLE : View.GONE);
/*if (TextUtils.isEmpty(post.summary))
postContent.setVisibility(View.GONE);*/
}else{ //post.type == PostType.TEXT
postImage.setVisibility(View.GONE);
postName.setVisibility(View.VISIBLE);
postContent.setVisibility(View.VISIBLE);
}
// contetnt
if (TextUtils.isEmpty(post.summary) && !TextUtils.isEmpty(post.contentUrl)) {
DataRepository.getInstance().getPostContent(post.contentUrl, postContent);
}
else{
setText(postContent, post.summary);
}
.
.
.
}
}
Sorry for the long post!
You probably have margins in your root ViewGroup of item.
manager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
Do this ,spacing between the grid items stay same
private boolean flagDecoration = false;
if(!flagDecoration)
{
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(getContext(),(R.dimen.spacing));
recyclerView.addItemDecoration(itemDecoration);
flagDecoration = true;
}

ANDROID: Database connection, check data, login page

I'm doing Major Project on my final year and I'm very new to Android plus I;m not good at codings. I need help with my login page. I've created something like a database connection java file which is this:
package one.two;
import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
public class UserDB
{
public String KEY_USERNAME = "Username";
public String KEY_PASSWORD = "Password";
public String KEY_LNAME = "LastName";
public String KEY_FNAME = "FirstName";
private static final String DATABASE_NAME = "Users";
private static final String DATABASE_TABLE = "User";
private static final int DATABASE_VERSION = 1;
private static Context context;
private static DatabaseHelper DBHelper;
private static SQLiteDatabase db;
public UserDB(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, "Users", null, 1);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
#Override
public void onCreate(SQLiteDatabase db)
{
}
}//end DatabaseHelper
// ---opens the database---
public UserDB open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close()
{
DBHelper.close();
}
}
I've already created a database for users using SQLite. The database name is Users and the table is called User. The records inside the table are Username, Password, LastName, FirstName. I've inserted one user's info into the database. The problem is I do not know whether my UserDB.java is correct.
And I've also created login.java. Hardcoded Login page:
package one.two;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
public class Login extends Activity implements OnClickListener{
UserDB db = new UserDB(this);
/** Called when the activity is first created. */
private EditText etUsername;
private EditText etPassword;
private Button btnLogin;
//private Button btnRegister;
private TextView lblResult;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Get the EditText and Button References
etUsername = (EditText)findViewById(R.id.usernametxt);
etPassword = (EditText)findViewById(R.id.passwordtxt);
btnLogin = (Button)findViewById(R.id.btnLogin);
//btnRegister = (Button)findViewById(R.id.btnRegister);
lblResult = (TextView)findViewById(R.id.msglbl);
//Cursor c = (Cursor) db.getAllTitles();
Button btnArrival = (Button) findViewById(R.id.btnRegister);
btnArrival.setOnClickListener(this);
// Set Click Listener
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Check Login
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
if(username.equals("guest") && password.equals("guest")){
lblResult.setText("Login successful.");
} else {
lblResult.setText("Login failed. Username and/or password doesn't match.");
}
}
});
}
public void onClick(View v)
{
Intent intent = new Intent(this, DatabaseActivity.class);
startActivity(intent);
}
}
So I want to know how I should apply the database connection on the login.java. Should I insert database connection something like db.Open();? I studied ASP.Net a few months back and I kind of forget most of what I've learnt.
So how should I open the database connection on login.java and how to check with database whether the user enters the right username and password?
Just incase you need my xml, here's the code:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="#+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background = "#drawable/image"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="#+id/widget32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ferry Hub"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#ff009999"
android:layout_x="97px"
android:layout_y="15px"
>
</TextView>
<TextView
android:id="#+id/widget35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textColor="#ff100999"
android:layout_x="116px"
android:layout_y="48px"
>
</TextView>
<EditText
android:layout_width="168px"
android:layout_height="42px"
android:text=""
android:textSize="18sp"
android:layout_x="133px"
android:layout_y="146px"
android:id="#+id/passwordtxt">
</EditText>
<EditText
android:layout_width="169px"
android:layout_height="41px"
android:text=""
android:textSize="18sp"
android:layout_x="132px"
android:layout_y="93px"
android:id="#+id/usernametxt">
</EditText>
<TextView
android:id="#+id/widget31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:"
android:textColor="#ff000000"
android:layout_x="32px"
android:layout_y="102px"
>
</TextView>
<TextView
android:id="#+id/widget32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:textColor="#ff000000"
android:layout_x="32px"
android:layout_y="155px"
>
</TextView>
<Button
android:id="#+id/btnLogin"
android:layout_width="109px"
android:layout_height="34px"
android:text="Login"
android:layout_x="38px"
android:layout_y="272px"
>
</Button>
<Button
android:id="#+id/btnRegister"
android:layout_width="110px"
android:layout_height="35px"
android:text="Register"
android:layout_x="159px"
android:layout_y="272px"
>
</Button>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click 'Register' if you do not have an account."
android:textSize="13px"
android:textColor="#ff000000"
android:layout_x="3px"
android:layout_y="305px"
android:id="#+id/msglbl">
</TextView>
</AbsoluteLayout>
Any help is appreciated. I need to learn how to do it so that I can apply for other pages for example Register.java page.
-Dayne
the first, Are you want to check whether or not record in User table. Ok you can. In Eclipse, Open File Explorer-->data-->data-->package of your app (i think one.two). after that click icon pull a file from the device (icon like FDD driver).
After that install SQLite Expert Professional to open this file. Ok you will know.
To make database in Android. See this site:
http://developer.android.com/resources/tutorials/notepad/index.html
hope helpful for you.
For the database: You should have a primary key that is autoincrement on user table. and try putting codes in onCreate() and onUpgrade(). here's example:
#Override
public void onCreate(SQLiteDatabase db) {
// Creating Tables
String CREATE_EMPLOYEES_TABLE = "CREATE TABLE " + TABLE_EMPLOYEES + "("
+ KEY_EMPID + " INTEGER PRIMARY KEY autoincrement," + KEY_FIRSTNAME
+ " TEXT not null," + KEY_LASTNAME + " TEXT not null," + KEY_DEPARTMENTNAME
+ " TEXT not null," + KEY_EMAIL + " TEXT not null," + KEY_PHONE
+ " TEXT not null," + KEY_PASSWORD + " TEXT not null " + ")";
db.execSQL(CREATE_EMPLOYEES_TABLE);
String CREATE_DEPARTMENTS_TABLE = "CREATE TABLE " + TABLE_DEPARTMENTS
+ "(" + KEY_DEPARTMENTID + " INTEGER PRIMARY KEY autoincrement,"
+ KEY_DEPARTMENTNAME + " TEXT not null"
+ ")";
db.execSQL(CREATE_DEPARTMENTS_TABLE);
}
// Upgrading database
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_EMPLOYEES);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DEPARTMENTS);
// Create tables again
onCreate(db);
}`
For the login, try this:
#Override
public void onClick(View v) {
if(et1.getText().toString().equals("admin") &&
et2.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(),
"Redirecting...",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Login.this, AdminHome.class);
startActivity(intent);
finish();
}else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",Toast.LENGTH_SHORT).show();
}
}
});