<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shakibaenur</title>
    <description>The latest articles on DEV Community by Shakibaenur (@shakibaenur).</description>
    <link>https://dev.to/shakibaenur</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1020798%2F04d01fbd-6e59-485c-8024-40ba1f1bd093.png</url>
      <title>DEV Community: Shakibaenur</title>
      <link>https://dev.to/shakibaenur</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shakibaenur"/>
    <language>en</language>
    <item>
      <title>How to turn on charge only mode programatically if the mode if file transfer in android?</title>
      <dc:creator>Shakibaenur</dc:creator>
      <pubDate>Sun, 08 Oct 2023 07:20:23 +0000</pubDate>
      <link>https://dev.to/shakibaenur/how-to-turn-on-charge-only-mode-programatically-if-the-mode-if-file-transfer-in-android-56oe</link>
      <guid>https://dev.to/shakibaenur/how-to-turn-on-charge-only-mode-programatically-if-the-mode-if-file-transfer-in-android-56oe</guid>
      <description>&lt;p&gt;I will have to show this type of dialog if my device is connected to usb and I want to access the app. Here this dialog shows -Click to Charge Only will automatically set the USB protocol to Charge only mode and i can not access files from my phone&lt;/p&gt;

&lt;p&gt;Can Anyone help me to achieve this feature?&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uLnDNF6y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmm578cfldl142q6v4hy.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uLnDNF6y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmm578cfldl142q6v4hy.jpeg" alt="Image description" width="718" height="615"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have used to get device list but device list is empty. But still i dont thing this will solve my problem&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (entry in usbDeviceList.entries) {
            val usbDevice = entry.value

            // Check the USB device's connection mode
            val usbConfiguration = usbDevice.getConfiguration(0)
            val usbInterface = usbConfiguration.getInterface(0)
            val usbInterfaceClass = usbInterface.interfaceClass

            // Check if the interface class indicates MTP (Media Transfer Protocol)
            if (usbInterfaceClass == 6) {
                // The device is in MTP mode (File Transfer mode)
                // You can perform file transfer operations here
                Toast.makeText(applicationContext, "(File Transfer mode)", Toast.LENGTH_LONG).show()
            } else {
                // The device is not in MTP mode
                // Handle the mode accordingly
                Toast.makeText(applicationContext, "MTP mode", Toast.LENGTH_LONG).show()
            }
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Android — Drag and Drop</title>
      <dc:creator>Shakibaenur</dc:creator>
      <pubDate>Fri, 17 Feb 2023 17:21:00 +0000</pubDate>
      <link>https://dev.to/shakibaenur/android-drag-and-drop-3k3e</link>
      <guid>https://dev.to/shakibaenur/android-drag-and-drop-3k3e</guid>
      <description>&lt;p&gt;Today i want to talk about my one of the most favourite features in android development that is &lt;strong&gt;"Drag and Drop"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In modern user interfaces, drag and drop has become an essential interaction pattern for enabling users to move objects around a screen with a touch gesture.&lt;/p&gt;

&lt;p&gt;The drag and drop framework in Android Kotlin provides a simple and flexible way to implement drag and drop functionality in your app. It allows users to drag a view or item from one location to another by simply tapping and holding the item, and then dragging it to its desired destination. The drag and drop framework is based on three key components: the &lt;strong&gt;DragEvent&lt;/strong&gt;, the &lt;strong&gt;DragShadow&lt;/strong&gt;, and the &lt;strong&gt;ClipData object&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DragEvent:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The DragEvent class is responsible for managing the entire drag and drop lifecycle. It provides a set of constants that represent different states of the drag and drop operation. These states include &lt;strong&gt;ACTION_DRAG_STARTED&lt;/strong&gt;, &lt;strong&gt;ACTION_DRAG_ENTERED&lt;/strong&gt;, &lt;strong&gt;ACTION_DRAG_LOCATION&lt;/strong&gt;, &lt;strong&gt;ACTION_DRAG_EXITED&lt;/strong&gt;, &lt;strong&gt;ACTION_DROP&lt;/strong&gt;, and &lt;strong&gt;ACTION_DRAG_ENDED&lt;/strong&gt;. These states are used to identify when a drag operation is starting, when it's in progress, and when it's completed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DragShadow:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The DragShadow class is responsible for creating a visual representation of the item being dragged. It is an optional component that can be used to provide a custom visual representation of the item being dragged. It's also possible to use the default drag shadow, which is a simple opaque image of the view being dragged.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ClipData:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;ClipData&lt;/strong&gt; class is responsible for storing the data associated with the item being dragged. This data can be anything that you want to associate with the item, such as text, images, or other types of data. You can use the &lt;strong&gt;ClipData.newPlainText()&lt;/strong&gt; method to create a new &lt;strong&gt;ClipData object&lt;/strong&gt; that contains plain text data, or you can create a custom ClipData object that contains any type of data.&lt;/p&gt;

&lt;p&gt;These were the fundamental parts. Now let's think about a Drag and Drop game you want to built for kids. To built this you must know about &lt;strong&gt;View.setOnTouchListener()&lt;/strong&gt; and &lt;strong&gt;View.OnDragEventListener()&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Note that i am just sharing the basic concept. Assume i want to match a shape. Let's call it &lt;strong&gt;"Match the Shape"&lt;/strong&gt;. I this application I have a triangle shaped object and want to match it with the shape. &lt;/p&gt;

&lt;p&gt;Now proceed of making this little application.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Click on &lt;strong&gt;File&lt;/strong&gt;, then &lt;strong&gt;New&lt;/strong&gt; =&amp;gt; &lt;strong&gt;New Project&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;After that include the Kotlin support and click on next.&lt;/li&gt;
&lt;li&gt;Select the minimum SDK as per convenience and click the next button.&lt;/li&gt;
&lt;li&gt;Then select the &lt;strong&gt;Empty&lt;/strong&gt; activity =&amp;gt; &lt;strong&gt;next&lt;/strong&gt; =&amp;gt; &lt;strong&gt;finish&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;View Binding&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I have used view binding in this project. To enable view binding in a module, set the &lt;strong&gt;viewBinding&lt;/strong&gt; build option to true in the module-level &lt;strong&gt;build.gradle&lt;/strong&gt; file, as shown in the following example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

buildFeatures {
    viewBinding = true
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;activity_main.xml file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the xml file we have two ImageView one is for showing the shape &lt;strong&gt;ivTriangle&lt;/strong&gt; and another one &lt;strong&gt;ivTriangleDrag&lt;/strong&gt; to drag it on the shape.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;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:orientation="vertical"
    tools:context=".MainActivity"&amp;gt;

    &amp;lt;TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:text="Match the Shape"
        android:textAlignment="center"
        android:textSize="24sp" /&amp;gt;

    &amp;lt;ImageView
        android:id="@+id/ivTriangle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="12dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:src="@drawable/ic_triangle" /&amp;gt;

    &amp;lt;ImageView
        android:id="@+id/ivTriangleDrag"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="12dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:src="@drawable/ic_triangle_drag" /&amp;gt;

&amp;lt;/LinearLayout&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;MainActivity.kt&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will implement the &lt;strong&gt;onTouch&lt;/strong&gt; and &lt;strong&gt;onDrag&lt;/strong&gt; functions of &lt;strong&gt;View.OnTouchListener&lt;/strong&gt; and &lt;strong&gt;View.OnDragListener&lt;/strong&gt; correspondingly.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class MainActivity : AppCompatActivity(), View.OnTouchListener, View.OnDragListener{

..........
 override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        ........
  }

 override fun onDrag(v: View?, event: DragEvent?): Boolean {
        .........
  }

}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here in &lt;strong&gt;onCreate&lt;/strong&gt; function we will initialise the &lt;strong&gt;setOnTouchListener&lt;/strong&gt; to &lt;strong&gt;ivTriangleDrag&lt;/strong&gt; and &lt;strong&gt;setOnDragListener&lt;/strong&gt; to &lt;strong&gt;ivTriangle&lt;/strong&gt; . As we want to touch &lt;strong&gt;ivTriangleDrag&lt;/strong&gt; then drag it over &lt;strong&gt;ivTriangle&lt;/strong&gt; and drop.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    mViewBinding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(mViewBinding.root)

    mViewBinding.ivTriangleDrag.setOnTouchListener(this)
    mViewBinding.ivTriangle.setOnDragListener(this)

}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now we can see the &lt;strong&gt;Motion.ACTION_DOWN&lt;/strong&gt; event of &lt;strong&gt;ivTriangleDrag&lt;/strong&gt; will trigger when we will touch it and drop it in the screen anywhere.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

override fun onTouch(v: View?, event: MotionEvent?): Boolean {
    return if (event!!.action == MotionEvent.ACTION_DOWN) {
        val data = ClipData.newPlainText("", "")
        val shadowBuilder = DragShadowBuilder(v)
        v?.startDragAndDrop(data, shadowBuilder, v, 0)
        true
    } else {
        false
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And the onDrag function will trigger the DragEvent callbacks. Now when we will drop &lt;strong&gt;ivTriangleDrag&lt;/strong&gt; over &lt;strong&gt;ivTriangle&lt;/strong&gt; then &lt;strong&gt;ACTION_DROP&lt;/strong&gt; will trigger and as we wanted we will change the background &lt;strong&gt;ivTriangle&lt;/strong&gt; and will &lt;strong&gt;INVISIBLE&lt;/strong&gt; &lt;strong&gt;ivTriangleDrag&lt;/strong&gt; Imageview.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

override fun onDrag(v: View?, event: DragEvent?): Boolean {
    when (event?.action) {
        ACTION_DRAG_STARTED -&amp;gt; {
            Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");
        }
        ACTION_DRAG_ENTERED -&amp;gt; {
            Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED")
        }
        ACTION_DRAG_EXITED -&amp;gt; {
            Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED")
        }
        ACTION_DRAG_LOCATION -&amp;gt; {
            Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION")
        }
        ACTION_DRAG_ENDED -&amp;gt; {
            Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED")
        }
        ACTION_DROP -&amp;gt; {
            Log.d(msg, "ACTION_DROP event")
            mViewBinding.ivTriangle.setImageResource(R.drawable.ic_triangle_drag)
            mViewBinding.ivTriangleDrag.visibility = INVISIBLE
            Toast.makeText(this@MainActivity,"You have matched the shape!!",Toast.LENGTH_SHORT).show()
        }

    }
    return true
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here is a preview of "Match the Shape" App. And it's Ready.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqzkml4j7sefsx0vxova.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqzkml4j7sefsx0vxova.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope this article helps you. To check out Full Code, here is the &lt;em&gt;&lt;strong&gt;&lt;a href="https://github.com/Shakibaenur/Drag_Drop.git" rel="noopener noreferrer"&gt;Repository&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt; .&lt;/p&gt;

&lt;p&gt;If you like the article do &lt;strong&gt;follow&lt;/strong&gt; me for more interesting topics on &lt;strong&gt;Android Development&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Happy Coding! :)&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
