0% found this document useful (0 votes)
26 views4 pages

MAD Programs

The document discusses several Android UI components including text-to-speech, grid views, and image views. It provides code examples for activities and layout files to implement these components. For text-to-speech, it shows how to convert text to speech by clicking a button. For grid views, it demonstrates populating a grid with string items from an array. And for image views, it displays centering an image resource in a layout.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

MAD Programs

The document discusses several Android UI components including text-to-speech, grid views, and image views. It provides code examples for activities and layout files to implement these components. For text-to-speech, it shows how to convert text to speech by clicking a button. For grid views, it demonstrates populating a grid with string items from an array. And for image views, it displays centering an image resource in a layout.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1) Text To Speech

➢ MainActivity.java:

package com.example.sohamsakpal;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;

public class MainActivity extends AppCompatActivity


{
private TextToSpeech textToSpeech;
private EditText editText;
private Button buttonSpeak;

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

editText = findViewById(R.id.editText);
buttonSpeak = findViewById(R.id.buttonSpeak);

textToSpeech = new TextToSpeech(this, status ->


{
if (status == TextToSpeech.SUCCESS)
{
textToSpeech.setLanguage(Locale.US);
}
});

buttonSpeak.setOnClickListener(v ->
{
String text = editText.getText().toString();
if (!text.isEmpty())
{
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
}
});
}

@Override
protected void onDestroy()
{
if (textToSpeech != null)
{
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onDestroy();
}
}

Activity_main.xml:

<LinearLayout
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:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:hint="Enter text here"
android:importantForAutofill="no" />

<Button
android:id="@+id/buttonSpeak"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Speak" />
</LinearLayout>

AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
4) Grid View

MainActivity,java:

package com.example.gridviewexample;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


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

GridView gridView = findViewById(R.id.gridView);

String[] items =
{
"Item 1", "Item 2", "Item 3",
"Item 4", "Item 5", "Item 6",
"Item 7", "Item 8", "Item 9"
};

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,


android.R.layout.simple_list_item_1, items);

gridView.setAdapter(adapter);
}
}

Activity_main.xml:

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

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3" />

</RelativeLayout>

5) ImageView

Activity_main.xml

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

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/your_image_resource" />

</RelativeLayout>

MainActivity.java:

package com.example.imageviewexample;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{

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

You might also like