Android 3
Android 3
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>
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.
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
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
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 {
@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
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:
Java
Kotlin
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.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);
}
});
}
}
<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;
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.
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
<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;
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);