Posts with the tag android:

How to Draw Rectangle on Imageview ?

If you already have a bitmap image in your code, just use this code snippet. bmp = bmp.copy(Bitmap.Config.RGB_565, true); That code will recreate the bitmap image as a mutable. So you can edit that bitmap image on canvas as you like. But if you do not have a bitmap image in code, create a new one using this code snippet. Bitmap bmp=Bitmap.createBitmap(img.getHeight(),img.getWidth(),Bitmap.Config.RGB_565); where img is another bitmap image you want to make a new one with the height and width of it. If you want to create one from scratch here is the code snippet. Bitmap bmp=Bitmap.createBitmap(400,700,Bitmap.Config.RGB_565); After that, create a canvas and pass the bitmap image you have.

Adding GIF image in an ImageView in android

use android-gif-drawable library add the following dependency to build.gradle file of your project. dependencies { implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.19' } Use the view in XML file like this. <pl.droidsonroids.gif.GifImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/src_anim" android:background="@drawable/bg_anim" /> For more customization and code, see the docs here. use VideoView on Android You can use VideoView to show the GIF image. But the library is more efficient and smooth. use ImageView on Android Use ImageView and Split the GIF file into several images and then apply animation to it. It is complicated and not worth it because the library is more efficient and elegant. use Glide library add the dependency to build.

How to make imageview over another view in Android ?

There are at least 3 ways to put imageview on another imageview. Simple Order This way is just put the imageview after the other imageview in XML file. It is simple like that. The first view will be underneath the second one. Using FrameLayout <FrameLayout xmlns:android="" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/t" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </FrameLayout> The second image will be on top. Put imageview on another imageview exactly <RelativeLayout xmlns:android="" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/white" > <ImageView android:id="@+id/inside_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dip" android:layout_marginTop="5dip" android:src="@drawable/frame" /> <ImageView android:id="@+id/outside_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/inside_imageview" android:layout_alignBottom="@id/inside_imageview" android:layout_alignLeft="@id/inside_imageview" android:layout_alignRight="@id/inside_imageview" android:scaleType="fitXY" /> </RelativeLayout> As you can see, the second imageview has those properties.

How to get currently running applications in android ?

Use this code in your app’s `src/MainActivity.kt’ file. import import import android.content.Context import android.os.Bundle import android.widget.TextView import import java.util.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" val textView: TextView = findViewById( val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager val recentTasks: List<RunningTaskInfo> = Objects.requireNonNull(activityManager).getRunningTasks(Int.MAX_VALUE) for (i in recentTasks.indices) { textView.text = "Application executed: ${recentTasks[i].baseActivity!!.toShortString()}ID: ${recentTasks[i].id}" } } } Make sure you have a TextView with id textView in your XML layout. All the currently running applications on your smartphone will be shown in the textview.

How do you animate the change of background color of a view on Android ?

The idea is to use this code snippet. ColorDrawable[] colorDrawables = { new ColorDrawable(Color.RED), new ColorDrawable(Color.BLUE), new ColorDrawable(Color.GREEN) }; TransitionDrawable transitionDrawable = new TransitionDrawable(colorDrawables); textView.setBackground(transitionDrawable); transitionDrawable.startTransition(2000); In this code snippet, we created a list of colorDrawables, then feed them to the TransitionDrawable. And now we have an animating color change, so we pass it to the setBackground to get the animating colors in the background. Got the idea ? let’s create a complete code example. use this xml code in res/layout/activity_main.xml. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/relativeLayout" android:padding="8dp" tools:context=".MainActivity"> <Button android:id="@+id/button" android:text="Animate background color" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/textView" android:layout_centerInParent="true" android:layout_marginBottom="15dp"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="36sp" android:textStyle="bold" android:text="Changing Background color of this view.

How to change background color of selected item in listview in android ?

The main idea is to use view.setBackgroundColor(getColor(R.color.colorAccent)) to the selected view (list item). Let’s see a complete example. use this xml code in res/layout/activity_main.xml. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" tools:context=".MainActivity"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> use this Kotlin code in src/MainActivity.kt. import android.os.Build import import android.os.Bundle import android.widget.ArrayAdapter import android.widget.ListView class MainActivity : AppCompatActivity() { var operatingSystem: Array<String> = arrayOf("Android", "IPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" val listView: ListView = findViewById( val adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, operatingSystem) listView.

How to Change Listview Item Color in Android

The idea is to create an a list item xml, and give it the design choices you want. If you want to change the list item color, use this xml code android:textColor="@android:color/holo_orange_dark". If you want to change the text to italic or bold, use this xml android:textStyle="italic|bold". And even if you want to change the font itself, you can by this font-family property in xml android:fontFamily="sans-serif-condensed". Now, you got the idea. Let’s build a sample application to illustrate this more practically. create Android app in Android Studio use this XML as a res/layout/activity_main.xml. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="sans-serif-condensed" android:textColor="@android:color/holo_orange_dark" android:textSize="24sp" android:textStyle="italic|bold" /> </LinearLayout> use this Kotlin code in your src/MainActivity.