Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Program 1-Hello Toast: Strings

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Program 1- Hello Toast

XML

Strings:
<resources>
<string name="app_name">My Application</string>
<string name="button_label_toast">TOAST</string>
<string name="button_label_count">COUNT</string>
<string name="count_initial_value">0</string>
<string name="toast_message">Hello Toast!</string>
<string name="name">Shrinivas</string>
<string name="reg_no">2047123</string>
</resources>

Colors:

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


<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>

Toast:

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="#8BC34A">

<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#FFF" />
</LinearLayout>

activity_main:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:background="#673AB7"
tools:context=".MainActivity">

<TextView
android:id="@+id/regno"
android:layout_width="232dp"
android:layout_height="105dp"
android:gravity="center_horizontal|center_vertical"
android:text="@string/reg_no"
android:textColor="#0F0F10"
android:textSize="20sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toTopOf="@+id/show_count"
app:layout_constraintEnd_toStartOf="@+id/imageView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name"
app:layout_constraintVertical_bias="0.0" />

<TextView
android:id="@+id/show_count"
android:layout_width="411dp"
android:layout_height="wrap_content"
android:background="#0DBF14"
android:gravity="center"
android:text="@string/count_initial_value"
android:textColor="@color/purple_700"
android:textSize="160sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/btn2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/btn1"
android:layout_width="414dp"
android:layout_height="63dp"
android:background="@color/purple_700"
android:onClick="show_toast"
android:text="@string/button_label_toast"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btn2"
android:layout_width="414dp"
android:layout_height="63dp"
android:background="@color/purple_700"
android:onClick="count_up"
android:text="@string/button_label_count"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<TextView
android:id="@+id/name"
android:layout_width="234dp"
android:layout_height="116dp"
android:gravity="center_horizontal|center_vertical"
android:text="@string/name"
android:textColor="#111112"
android:textSize="20sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toTopOf="@+id/show_count"
app:layout_constraintEnd_toStartOf="@+id/imageView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn1"
app:layout_constraintVertical_bias="0.291" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="177dp"
android:layout_height="386dp"
app:layout_constraintBottom_toTopOf="@+id/show_count"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn1"
app:srcCompat="@drawable/pic" />

</androidx.constraintlayout.widget.ConstraintLayout>

JAVA

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private int count = 0;
private TextView showc;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showc = (TextView) findViewById(R.id.show_count);
}

public void show_toast(View view) {

LayoutInflater inflater = getLayoutInflater();


View layout = inflater.inflate(R.layout.toast,
(ViewGroup) findViewById(R.id.toast_layout));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello Toast!");

Toast toast = new Toast(getApplicationContext());


toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}

public void count_up(View view) {


++count;
if (showc != null) {
showc.setText(Integer.toString(count));
}
}

}
Screen shots:

With toast:

You might also like