Monday, 9 December 2013

How to link a web address in Android's TextView and ImageView (Updated with androidx package)

Once you have developed an application, you will want people contact you regarding feedback or queries about the application. So there has to be some easiest way through which people will reach you. 

For example when the user just clicks a website link, it should take the user to the desired address. This functionality can be achieved with the help of Android's autoLink. Lets see how...



The autoLink will work only for TextViews. What if you want an image to work as a link. In that case Intents are used.

---------------Your xml file will contain the following-----------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#E91E63">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="30sp"
android:textColor="#ffffff"
android:gravity="center"
android:background="#F06292"
android:text="Hit this Url"/>

<TextView
android:layout_gravity="center"
android:id="@+id/la_linkTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:textSize="30sp"
android:text="whatanandroid.blogspot.com"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:weightSum="2"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:background="#F06292"
android:textColor="#ffffff"
android:gravity="center"
android:text="Click this Image"/>

<ImageView
android:id="@+id/la_linkIV"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/andro"
android:autoLink="all"
android:text="whatanandroid.blogspot.com" />

</LinearLayout>

</LinearLayout>
----------------------------------------------------------------------------

---------------------------No need to write anything extra in your Java code for TextView linking----------
package com.app.yourappname;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class LinkActivity extends AppCompatActivity {

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

ImageView imageView = findViewById(R.id.la_linkIV);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent imgLink = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.android.com"));
startActivity(imgLink);
}
});
}
}
------------------------------------------------------------------------------------------

---------------------------- Declare the Activity name in your Manifest file --------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LinkActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Thursday, 5 December 2013

Data passing between activities through Explicit Intents (Updated with androidx package)

In order to pass any data from one Activity to another, there is a very important class provided by Android. It is Intent. There are two types of Intents: Implicit Intents and Explicit Intents. We can pass data from one Activity to another with the help of Explicit Intents.


First we create the layouts for first activity, i.e DataPassingActivity:
----------------------data_passing_activity.xml------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#795548"
android:padding="20dp" >

<TextView
android:id="@+id/textView1"
android:layout_marginBottom="5dp"
android:textSize="25sp"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Activity 1"/>

<EditText
android:id="@+id/enterTextET"
android:gravity="center"
android:textSize="25sp"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

<Button
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:id="@+id/sendBTN"
android:layout_width="match_parent"
android:layout_height="80dp"
android:textColor="#000000"
android:textSize="20sp"
android:background="#A1887F"
android:text="Click Me to send Text." />

</LinearLayout>
-------------------------------------------------------------

---------------------DataPassingActivity.java----------------------

package com.app.yourappname;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class DataPassingActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.data_passing_activity);

Button sendTextBTN;
final EditText sendTextET;
final Intent intent = new Intent();
intent.setClass(this, DataReceivingActivity.class);
sendTextET = findViewById(R.id.enterTextET);
sendTextBTN = findViewById(R.id.sendBTN);
sendTextBTN.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
intent.putExtra("message", sendTextET.getText().toString());
startActivity(intent);
}
});
}
}
------------------------------------------------------------------

Now we create the layout for new activity where we catch the sent data from first activity:
---------------------data_receiving_activity.xml--------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#5D4037"
android:padding="20dp" >

<TextView
android:id="@+id/textView1"
android:layout_marginBottom="5dp"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#ffffff"
android:text="Activity 2"/>

<TextView
android:layout_marginTop="30dp"
android:textSize="25sp"
android:text="Received Text:"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

<TextView
android:layout_marginTop="20dp"
android:id="@+id/receivedTextTV"
android:background="#BCAAA4"
android:textSize="25sp"
android:textColor="#000000"
android:layout_width="fill_parent"
android:layout_height="80dp"/>
</LinearLayout>
---------------------------------------------------------------

------------------------DataReceivingActivity.java---------------------
package com.app.yourappname;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class DataReceivingActivity extends AppCompatActivity {

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

String str = getIntent().getStringExtra("message");

TextView receivedMsgTV = findViewById(R.id.receivedTextTV);
receivedMsgTV.setText(str);
}
}
--------------------------------------------------------------------

---------------------Declare the Activity names Manifest.xml file-------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".DataPassingActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DataReceivingActivity"/>
</application>

</manifest>
-----------------------------------------------------------------

Monday, 2 December 2013

ListActivity (Updated with androidx package)

There is an easy way for listing contents. Also with the help of onListItemClick() method the selected list item can be captured.




-------------------Your Xml code--------------------

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#64B5F6"
android:padding="20dp" >

<TextView
android:id="@+id/textView1"
android:layout_marginBottom="5dp"
android:textStyle="italic|bold"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Android Versions"/>

<TextView
android:id="@+id/showVersionTV"
android:background="#ffffff"
android:gravity="center"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#ff0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>

</LinearLayout>
------------------------------------------------------

  -----------------------Now the Java code------------------------
package com.apps.yourappname;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MyListActivity extends ListActivity {

private TextView showVersionTV;
private String[] items = {"a","b","c","d","e","f","g","h","i","j","k"};
private String[] versions = {"alpha","beta","cup cake","donut","eclare","froyo",
"ginger bread","honey comb","ice cream","jelly bean","kitkat"};

@Override
public void onCreate(Bundle b) {

super.onCreate(b);
setContentView(R.layout.my_list_activity);
setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,items));
showVersionTV = findViewById(R.id.showVersionTV);
}

public void onListItemClick(ListView parent, View v, int position, long id) {
showVersionTV.setText(versions[position]);
}
}
----------------------------------------------------------------------------------

----------------------------------------Changes in the Manifest file------------------------------------------
Just add the ActivityName in the Manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.apps.yourappname">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MyListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

-------------------------No Changes in the build.gradle (Module:app)------------------

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
applicationId "com..app.yourappname"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

ElevenLabs For Audio Models