What is Uri.parse() in Android?
Last Updated :
15 May, 2023
Uri stands for Uniform Resource Identifier. Uri is the sequence of the characters used to identify resources uniquely over the internet. In this article, we are going to see what is Uri.parse() method in Android.
Step-by-Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
android:gravity = "center"
tools:context = ".MainActivity" >
< ImageView
android:id = "@+id/gfg"
android:layout_width = "match_parent"
android:layout_height = "500dp"
android:src = "@drawable/gfg"
tools:layout_editor_absoluteX = "7dp"
tools:layout_editor_absoluteY = "0dp" />
< Button
android:id = "@+id/btnUri"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "URI" />
< TextView
android:id = "@+id/txtUri"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "Uri here :" />
</ LinearLayout >
|
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file.
Java
package com.anas.gfguri;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btnUri;
TextView txtUri;
ImageView gfg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnUri=findViewById(R.id.btnUri);
txtUri=findViewById(R.id.txtUri);
gfg=findViewById(R.id.gfg);
btnUri.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse( "android.resource://" + getPackageName() + "/" + gfg.getId());
txtUri.setText(uri.toString().);
}
});
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...