csdevbin

csdevbin

Android coding experiences

Hot

Post Top Ad

Post Top Ad

Saturday, 24 November 2018

Android CollapsingToolbarLayout

November 24, 2018 0

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
   
android:fitsSystemWindows="true"
   
tools:context=".DhikrActivity">

    <
android.support.design.widget.AppBarLayout
       
android:id="@+id/app_bar"
       
android:layout_width="match_parent"
       
android:layout_height="160dp"
       
android:fitsSystemWindows="true"
       
android:theme="@style/AppTheme.AppBarOverlay">

        <
android.support.design.widget.CollapsingToolbarLayout
           
android:id="@+id/toolbar_layout"
           
android:layout_width="match_parent"
           
android:layout_height="match_parent"
           
android:fitsSystemWindows="true"
           
app:contentScrim="?attr/colorPrimary"
           
app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <
ImageView
               
android:id="@+id/expandedImage"
               
android:layout_width="match_parent"
               
android:layout_height="200dp"
               
android:scaleType="centerCrop"
               
android:src="@drawable/dhikr"
               
app:layout_collapseMode="parallax"
               
app:layout_collapseParallaxMultiplier="0.7" />

            <
android.support.v7.widget.Toolbar
               
android:id="@+id/toolbar"
               
android:layout_width="match_parent"
               
android:layout_height="?attr/actionBarSize"
               
app:layout_collapseMode="pin"
               
app:popupTheme="@style/AppTheme.PopupOverlay" />

        </
android.support.design.widget.CollapsingToolbarLayout>
    </
android.support.design.widget.AppBarLayout>

    <
android.support.v4.widget.NestedScrollView
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <
android.support.v7.widget.RecyclerView
           
android:id="@+id/rec_dhikr"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content">

        </
android.support.v7.widget.RecyclerView>
    </
android.support.v4.widget.NestedScrollView>

</
android.support.design.widget.CoordinatorLayout>



  • Java Parts

onCreate()  add these two lines
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
Read More

Tuesday, 20 November 2018

How to take a screenshot in Android Studio

November 20, 2018 0

Take a screenshot

On many Android devices, you can capture a screenshot with a key-combination: Simultaneously press-and-hold Power and Volume-down. You can also capture a screenshot with Android Studio as follows:
  1. Run your app on a connected device or emulator. If using a connected device, be sure you have enabled USB debugging.
  2. In Android Studio, select View > Tool Windows > Logcat to open Logcat.
  3. Select the device and a process from the drop-down at the top of the window.
  4. Click Screen Capture  on the left side of the window.
    The screenshot appears in a Screenshot Editor window.
Figure 1. Screenshot editor

   5.change the image:
  • Recapture: Take a new screenshot.
  • Rotate Left: Rotate the image 90 degrees counter-clockwise.
  • Rotate Right: Rotate the image 90 degrees clockwise.
  • Frame Screenshot: Choose a device to wrap your screenshot with real device artwork. Select Drop ShadowScreen Glare, or both to add these effects to your image.
    6. Click Save, specify the location and filename, and then click OK.

https://developer.android.com
Read More

Thursday, 11 October 2018

Custom Toast in Android

October 11, 2018 0
Custom Toast in Android

public void customToast() {

    // Get the custom layout view.    //  View toastView = getLayoutInflater().inflate(R.layout.activity_toast_custom_view, null);
    View view = LayoutInflater.from(this).inflate(R.layout.activity_splash, null);
    Toast toast = new Toast(this);
    toast.setView(view);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();
}

public void toastWithPic() {
    Toast toast = Toast.makeText(getApplicationContext(), "Show Image In Toast", Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 0, 0);
    LinearLayout toastContentView = (LinearLayout) toast.getView();
    ImageView imageView = new ImageView(getApplicationContext());
    imageView.setImageResource(R.drawable.apple);
    toastContentView.addView(imageView, 0);
    toast.show();
}

Read More

Sunday, 9 September 2018

Image libraries for android

September 09, 2018 0
 Major image loading libraries in android are :

1.Picasso 
2.Glide 
3.Universal Image Loader
4.Fresco 

1.Picasso 
-----------------------

GRADLE

implementation 'com.squareup.picasso:picasso:2.71828'
Picasso.get()
    .load(url)
    .placeholder(R.drawable.user_placeholder)
    .error(R.drawable.user_placeholder_error)
    .into(imageView);

2.Glide 
---------------------
Gradle dependency:
repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.8.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}
Glide.with(getApplication()).load(url)
        .centerCrop()
        .crossFade()
        .error(R.mipmap.ic_launcher)
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .into(imageView);

3.Universal Image Loader
---------------------------------------

Gradle dependency:
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
ImageLoader imageLoader = ImageLoader.getInstance(); // Get singleton instance
// Load image, decode it to Bitmap and display Bitmap in ImageView (or any other view 
// which implements ImageAware interface)
imageLoader.displayImage(imageUri, imageView);
// Load image, decode it to Bitmap and return Bitmap to callback
imageLoader.loadImage(imageUri, new SimpleImageLoadingListener() {
 @Override
 public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
  // Do whatever you want with Bitmap
 }
});
// Load image, decode it to Bitmap and return Bitmap synchronously
Bitmap bmp = imageLoader.loadImageSync(imageUri);
For more Android-Universal-Image-Loader
  4.Fresco 
---------------------
Gradle dependency:

         implementation 'com.facebook.fresco:fresco:1.10.0'
Full details from here :Fresco
Read More

Tuesday, 28 August 2018

Networking with Volley in Android

August 28, 2018 0

Volley is a networking library for Android that manages network requests. It bundles the most important features you’ll need, such as accessing JSON APIs, loading images and String requests in an easier-to-use package.
By using Volley for network operations you avoid the standard way to handle networking, HttpURLConnection. Another reason is asynchronicity. Volley handles asynchronicity by itself, there is no need to create Asynctask manually.

Import Volley, add Permissions

dependencies { ...... implementation'com.android.volley:volley:1.0.0' }
------------------------------------------------------------------------
In AndroidManifest.xml add the internet permission.
<uses-permission android:name="android.permission.INTERNET" />
------------------------------------------------------------------------
In MainActivity’s onCreate() method, initiate the RequestQueue.
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState)
{
//...
requestQueue = Volley.newRequestQueue(this); }
------------------------------------------------------------------------
Volley has Requests for JSONObject and JSONArray. The structure of JSONRequest
and most of the request classes included in Volley uses constructors like
the following.
JsonObjectRequest request JsonObjectRequest(RequestMethod,
URL,
null,
new ResponseListener(),
new ErrorListener());
----------------------------------------------------------------------------
JsonObjectRequest
/*Json Request*/
String url = "https://json_url/";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
url,
null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) { }
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) { }
});
//add request to queue
requestQueue.add(jsonObjectRequest);

JsonArrayRequest
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,
url,
null,
new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response) { }
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) { }
});
//add request to queue
requestQueue.add(jsonArrayRequest);
-----------------------------------------------------
Read More

Sunday, 26 August 2018

Fetching JSON simple form

Sunday, 22 April 2018

Thursday, 28 December 2017

Shared Preferences in Android. Save Data

December 28, 2017 0

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout 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="com.arham.csdevbin.downloadimagefrominternet.SharedPreferencesDemo">
    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="103dp"
        android:layout_marginStart="103dp"
        android:layout_marginTop="136dp"
        android:text="CheckBox" />
</RelativeLayout>



package com.arham.csdevbin.downloadimagefrominternet;

  import android.content.SharedPreferences;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.widget.CheckBox;
  import android.widget.CompoundButton;
  import android.widget.Toast;

  public class SharedPreferencesDemo extends AppCompatActivity implements
 CompoundButton.OnCheckedChangeListener {
    CheckBox checkBox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);
        checkBox = findViewById(R.id.checkBox);
        checkBox.setOnCheckedChangeListener(this);
    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            checkBox.setText("The CheckBox is Checked");
        } else {
            checkBox.setText("The CheckBox is not Checked");
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
        SharedPreferences sharedPreferences = getPreferences(0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("CHECK_BOX_VALUE", checkBox.getText().toString());
        editor.putBoolean("CH", checkBox.isChecked());
        editor.commit();
    }
    @Override
    protected void onResume() {
        super.onResume();
        Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
        SharedPreferences sharedPreferences = getPreferences(0);
        boolean checkBoxValue = sharedPreferences.getBoolean("CH", false);
        String chBoxString = sharedPreferences.getString("CHECK_BOX_VALUE", "This is my CheckBox");
        checkBox.setChecked(checkBoxValue);
        checkBox.setText(chBoxString);
    }
}


Read More

Downloading Image From Internet in Android

December 28, 2017 0

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
   
android:orientation="vertical"
   
tools:context="com.arham.csdevbin.downloadimagefrominternet.MainActivity">

    <
ImageView
       
android:id="@+id/imageView"
       
android:layout_width="300dp"
       
android:layout_height="400dp"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="30dp"
       
app:layout_constraintLeft_toLeftOf="parent"
       
app:layout_constraintRight_toRightOf="parent"
       
app:srcCompat="@mipmap/ic_launcher"
       
tools:layout_editor_absoluteY="45dp" />

    <
Button
       
android:id="@+id/button"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="20dp"
       
android:text="Download Image"
       
app:layout_constraintLeft_toLeftOf="parent"
       
app:layout_constraintRight_toRightOf="parent"
       
tools:layout_editor_absoluteY="152dp" />
</
LinearLayout>


MainActivity.java
package com.arham.csdevbin.downloadimagefrominternet;

  import android.app.ProgressDialog;
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.os.AsyncTask;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.ImageView;
  import java.io.InputStream;
  import java.net.URL;

  public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView imageView;
    Button btnD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);
        btnD = findViewById(R.id.button);
        btnD.setOnClickListener(MainActivity.this);
    }
    @Override
    public void onClick(View v) {
        DownloadImageTask downloadImageTask = new DownloadImageTask(MainActivity.this);
        downloadImageTask.execute("http://www.gettyimages.ca/gi-resources" +
                "/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg");
    }  

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

        ProgressDialog progressDialog;
        Context context;

        public DownloadImageTask(Context context) {
            this.context = context;
            progressDialog = new ProgressDialog(context);
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog.setMessage("Downloading Image... Please Wait");
            progressDialog.show();
        }
        @Override
        protected Bitmap doInBackground(String... params) {
            String stringUrl = params[0];
            Bitmap bitmap = null;

            try {
                URL url = new URL(stringUrl);
                InputStream inputStream = url.openStream();
                bitmap = BitmapFactory.decodeStream(inputStream);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);

            imageView.setImageBitmap(bitmap);
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }
    }
}



Read More

Post Top Ad

Your Ad Spot