Android Development | Problems and Solutions

We all face some kind of problems when coding in Android Development. I am trying to compile some solutions which will help you to speed up the process.

Buttons


How to have a transparent ImageButton: Android?

android:background=”@null”

For Ripple effect:

android:background=”?attr/selectableItemBackgroundBorderless”


Shortcut Keys

https://developer.android.com/studio/intro/keyboard-shortcuts


EditText

EditText1.requestFocus(); // this will focus the cursor on the edittext

TextInputLayout

implementation 'com.google.android.material:material:1.1.0'
<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/enterTextWrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:layout_margin="10dp"
    android:layout_marginTop="4dp">-

    <EditText
        android:id="@+id/txtText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Your Text"
        android:inputType="text" />

</com.google.android.material.textfield.TextInputLayout>
final TextInputLayout enterTextWrapper = (TextInputLayout) findViewById(R.id.enterTextWrapper);
enterTextWrapper.setHint("Enter Your Text");

String strUsername = enterTextWrapper.getEditText().getText().toString();

if(!TextUtils.isEmpty(txtText.getText())) {
    
    enterTextWrapper.setErrorEnabled(false);
   

} else {
    enterTextWrapper.setError("Input required");
    enterTextWrapper.setErrorEnabled(true);
    enterTextWrapper.setFocusable(true);
    txtText.requestFocus();
    return;
}

Share

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType(“text/plain”);
String shareBody = etSMS.getText().toString();
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, “Cash Receipt ” + etSerial.getText());
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, “Share via”));


Copy to Clipboard

ClipboardManager myClipboard;
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

ClipData myClip;
String text = txtResult.getText().toString();
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
Toast.makeText(getApplicationContext(), "Text Copied!", Toast.LENGTH_SHORT).show();

Vertical and Horizontal Center in Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageButton android:id="@+id/btnFindMe" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/findme"></ImageButton>

</RelativeLayout>

Android build.gradle ERROR: ParseError at [row,col]:[65,9] Message: expected start or end tag Affected Modules: app

check for syntax error in the androidmanifest.xml