0% found this document useful (0 votes)
4 views

Android 3

Android 3

Uploaded by

mtech rameeja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Android 3

Android 3

Uploaded by

mtech rameeja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Add a font as an XML resource

To add fonts as resources, perform the following steps in Android Studio:


1. Right-click the res folder and go to New > Android resource directory. The New Resource
Directory window appears.
2. In the Resource type list, select font, then click OK.
Note: The name of the resource directory must be font.

Figure 1. Adding the font resource directory.


3. Add your font files in the font folder.
The folder structure below generates R.font.dancing_script, R.font.lobster,
and R.font.typo_graphica.

Figure 2. Adding the font files in the res/font directory.


4. Double-click a font file to preview the file's fonts in the

editor.
Figure 3. Previewing the font file.
Create a font family
A font family is a set of font files along with style and weight details. In Android, you can create a
new font family as an XML resource and access it as a single unit, instead of referencing each style
and weight as separate resources. By doing this, you let the system select the correct font based on
the text style you are using.
To create a font family, perform the following steps in Android Studio:
1. Right-click the font folder and select New > Font resource file. The New Resource File window
appears.
2. Enter the filename, then click OK. The new font resource XML opens in the editor.
3. Enclose each font file, style, and weight attribute in the <font> element. The following XML
illustrates adding font-related attributes in the font resource XML:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/lobster_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="@font/lobster_italic" />
</font-family>

Use fonts in XML layouts


Use your fonts, either a single font file or a font from a font family, in TextView objects or in styles
by using the fontFamily attribute.
Note: When you use a font family, the TextView switches on its own, as needed, to use the font
files from that family.

Add fonts to a TextView


To set a font for a TextView, do one of the following:
 In the layout XML file, set the fontFamily attribute to the font file you want to access.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lobster"/>

 Open the Properties window to set the font for the TextView.
1. Select a view to open the Properties window.
Note: The Properties window is available only when the design editor is open. Select
the Design tab at the bottom of the window.
2. Expand the textAppearance property, and then select the font from the fontFamily list.

3.
Figure 4. Selecting the font from the Properties window.
The Android Studio layout preview, shown in the rightmost pane in Figure 5, lets you preview the
font set in the TextView.
Figure 5. Previewing fonts in layout preview.

Add fonts to a style


Open the styles.xml file and set the fontFamily attribute to the font file you want to access.
<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
<item name="android:fontFamily">@font/lobster</item>
</style>

How to Send Data From One Activity to Second Activity in Android?

In this example, one EditText is used to input the text. This text is sent to the second activity
when the “Send” button is clicked. For this, Intent will start and the following methods will run:
 putExtra() method is used for sending the data, data in key-value pair key is variable name
and value can be Int, String, Float, etc.
 getStringExtra() method is for getting the data(key) that is sent by the above method.
According to the data type of value, there are other methods like getIntExtra(),
getFloatExtra()

How to Create an Android App to Send and Receive the Data between Two Activity

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. The code for that has been given in both Java and Kotlin Programming
Language for Android. This will create an XML file and a Java File. Please refer to the
prerequisites to learn more about this step.
Step 2: Working with the XML Files
Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code
for the activity_main.xml file. Comments are added inside the code to understand the code in
more detail. Open the “activity_first_activity.xml” file and add the following widgets in
a Relative Layout.
 An EditText to Input the message
 A Button to send the data
Also, Assign the ID to each component along with other attributes as shown in the image and the
code below. The assigned ID on a component helps that component to be easily found and used in
the Java/Kotlin files.
Syntax:
android:id="@+id/id_name"
Here the given IDs are as follows:
 Send Button: send_button_id
 input EditText: send_text_id
 XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".first_activity">

<EditText
android:id="@+id/send_text_id"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:hint="Input"
android:textSize="25dp"
android:textStyle="bold" />

<Button
android:id="@+id/send_button_id"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="150dp"
android:text="send"
android:textStyle="bold" />
</RelativeLayout>
This will make the UI of the Application

Step 3: Working with the MainActivity File


Go to the MainActivity File and refer to the following code. Below is the code for the
MainActivity File. Comments are added inside the code to understand the code in more detail.
Now, after the UI, this step will create the Backend of the App. For this, open the “first_activity”
file and instantiate the components made in the XML file (EditText, send Button) using
findViewById() method. This method binds the created object to the UI Components with the
help of the assigned ID.
Syntax: General
ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);
Syntax: for components used is as follows:
Button send_button= findViewById(R.id.send_button_id);
send_text = findViewById(R.id.send_text_id);
Setting up the Operations for the Sending and Receiving of Data.

These Operations are as follows:


Add the listener to the send button and this button will send the data.
This is done as follows:
send_button.setOnClickListener(v -> {}
after clicking this button following operation will be performed. Now create the String type
variable to store the value of EditText which is input by the user. Get the value and convert it to a
string.
This is done as follows:
String str = send_text.getText().toString();
Now create the Intent object First_activity.java class to Second_activity class.
This is done as follows:
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
where getApplicationContext() will fetch the current activity. Put the value in the putExtra
method in the key-value pair then start the activity.
This is done as follows:
intent.putExtra("message_key", str);
startActivity(intent);
where “str” is the string value and the key is “message_key” this key will use to get the str value

 Java
 Kotlin
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class first_activity extends AppCompatActivity {

// define the variable


Button send_button;
EditText send_text;

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

send_button = findViewById(R.id.send_button_id);
send_text = findViewById(R.id.send_text_id);
// add the OnClickListener in sender button after clicked this button following Instruction will
run
send_button.setOnClickListener(v -> {
// get the value which input by user in EditText and convert it to string
String str = send_text.getText().toString();
// Create the Intent object of this class Context() to Second_activity class
Intent intent = new Intent(getApplicationContext(), Second_activity.class);
// now by putExtra method put the value in key, value pair key is
// message_key by this key we will receive the value, and put the string
intent.putExtra("message_key", str);
// start the Intent
startActivity(intent);
});
}
}
Step 4: Creating Second_Activity to Receive the Data.
The steps to create the second activity are as follows:
android project > File > new > Activity > Empty Activity

Step 5: Working with the Second XML File


Add TextView to display the received messages. assign an ID to Textview.

 XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="org.geeksforgeeks.navedmalik.sendthedata.Second_activity">

<TextView
android:id="@+id/received_value_id"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:textSize="40sp"
android:textStyle="bold"
android:layout_marginStart="40dp" />
</RelativeLayout>
The Second Activity is shown below:

Step 6: Working with the SecondActivity File


Define the TextView variable, use findViewById() to get the TextView as shown above.
receiver_msg = (TextView) findViewById(R.id.received_value_id);
Now In the second_activity.java file create the object of getIntent to receive the value in String
type variable by the getStringExtra method using message_key.
Intent intent = getIntent();
String str = intent.getStringExtra("message_key");
The received value set in the TextView object of the second activity XML file
receiver_msg.setText(str);

 Java
 Kotlin
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class Second_activity extends AppCompatActivity {

TextView receiver_msg;

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

receiver_msg = findViewById(R.id.received_value_id);
// create the get Intent object
Intent intent = getIntent();
// receive the value by getStringExtra() method and
// key must be same which is send by first activity
String str = intent.getStringExtra("message_key");
// display the string into textView
receiver_msg.setText(str);
}
}

Output:

Background Image
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/rl"
tools:context=".MainActivity">
<Button
android:text="Set Drawable"
android:id="@+id/btnSetDrawable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>
</RelativeLayout>

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
Button button;
RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout = findViewById(R.id.rl);
button = findViewById(R.id.btnSetDrawable);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
relativeLayout.setBackgroundResource(R.drawable.image);
}
});
}
}

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.com.sample">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

How to build a simple music player app using Android Studio

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical"
android:theme="@style/Theme.AppCompat"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="430dp"
android:background="@drawable/download"
android:contentDescription="@string/todo" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:background="@color/colorAccent"
android:orientation="horizontal"
android:padding="10dp">

<Button
android:id="@+id/pause"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="125dp"
android:layout_height="match_parent"
android:background="@android:drawable/ic_media_pause"
android:onClick="musicpause" />

<Button
android:id="@+id/start"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="125dp"
android:layout_height="match_parent"
android:background="@android:drawable/ic_media_play"
android:onClick="musicplay" />

<Button
android:id="@+id/stop"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="125dp"
android:layout_height="match_parent"
android:background="@android:drawable/ic_delete"
android:onClick="musicstop" />
</LinearLayout>
</LinearLayout>

package com.example.amusinz;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity


extends AppCompatActivity {

// Instantiating the MediaPlayer class


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

// Adding the music file to our


// newly created object music
music = MediaPlayer.create(
this, R.raw.sound);
}

// Playing the music


public void musicplay(View v)
{
music.start();
}

// Pausing the music


public void musicpause(View v)
{
music.pause();
}

// Stopping the music


public void musicstop(View v)
{
music.stop();
music
= MediaPlayer.create(
this, R.raw.sound);
}
}

How to build a Video Player in Android


Steps to build a Video Player:
In creating Frontend we just need one component, i.e VideoView.
The icons like play, rewind, forward will only come when we touch on VideoView and they will
only come for just 3 seconds and then they will disappear. It is provided by Google and it is its
default behaviour.

Coming to back-end part i.e Java coding, we are getting media controls by:
vw.setMediaController(new MediaController(this));
Then, adding the videos of the raw folder in ArrayList and making a call to a method called
setVideo() by giving an argument to it of the first video.

// big video songs are not running


videolist.add(R.raw.faded);
videolist.add(R.raw.aeroplane);
setVideo(videolist.get(0));
Now in setVideo() defining, we need an Uri object so as to pass to a method called as
setVideoURI(). Therefore,

String uriPath = “android.resource://” + getPackageName() +”/” + id ;


Uri uri = Uri.parse(uriPath);
vw.setVideoURI(uri);
vw.start();

Note: First video will start playing as soon as application gets launch. This is because we are giving
call to setVideo() from inside onCreate() and then inside setVideo(), it is calling vw.start(), where
vw is VideoView.
Now, code of generating a dialog box is done inside the method called onCompletion(). Please
refer to this article for how to generate Dialog Box

// It is creating object of AlertDialog


AlertDialog.Builder obj = new AlertDialog.Builder(this);
At last, we have handled the coding of user’s action, i.e what the user has click (Replay or next).
The simple logic is used such as increment and decrement.
public void onClick(DialogInterface dialog, int which) {
if (which == -1) {
vw.seekTo(0);
vw.start();
}
else {
++currvideo;
if (currvideo == videolist.size())
currvideo = 0;
setVideo(videolist.get(currvideo));
}
}

<?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=".MainActivity">

<VideoView
android:id="@+id/vidvw"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>

package com.example.videoapp_demo;

import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

import java.util.ArrayList;

public class MainActivity


extends AppCompatActivity
implements MediaPlayer.OnCompletionListener {

VideoView vw;
ArrayList<Integer> videolist = new ArrayList<>();
int currvideo = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vw = (VideoView)findViewById(R.id.vidvw);
vw.setMediaController(new MediaController(this));
vw.setOnCompletionListener(this);

// video name should be in lower case alphabet.


videolist.add(R.raw.middle);
videolist.add(R.raw.faded);
videolist.add(R.raw.aeroplane);
setVideo(videolist.get(0));
}
public void setVideo(int id)
{
String uriPath
= "android.resource://"
+ getPackageName() + "/" + id;
Uri uri = Uri.parse(uriPath);
vw.setVideoURI(uri);
vw.start();
}

public void onCompletion(MediaPlayer mediapalyer)


{
AlertDialog.Builder obj = new AlertDialog.Builder(this);
obj.setTitle("Playback Finished!");
obj.setIcon(R.mipmap.ic_launcher);
MyListener m = new MyListener();
obj.setPositiveButton("Replay", m);
obj.setNegativeButton("Next", m);
obj.setMessage("Want to replay or play next video?");
obj.show();
}

class MyListener implements DialogInterface.OnClickListener {


public void onClick(DialogInterface dialog, int which)
{
if (which == -1) {
vw.seekTo(0);
vw.start();
}
else {
++currvideo;
if (currvideo == videolist.size())
currvideo = 0;
setVideo(videolist.get(currvideo));
}
}
}
}
Output:

 Playing the first video:


first song “faded”
 Dialogue box after the first video:

After completion of first song,dialog box is getting generated


 Playing the second video:

When we click on “NEXT”,then the second video starts running

You might also like