how to create about dialog box in android studio example
In this tutorial, today learn how to create about dialog box in android studio. about box used in android studios like your information and version shows. follow the how to create about dialog box in android studio example.
1.MainActivity.java:
package com.About.BoxDemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
AboutBox.Show(MainActivity.this);
}
});
}
}
2.activity_main.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About" />
</RelativeLayout>
create a class AboutBox.java
3.AboutBox.java:
package com.About.BoxDemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.text.SpannableString;
import android.text.util.Linkify;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class AboutBox {
static String VersionName(Context context) {
try {
return context.getPackageManager().getPackageInfo(
context.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
return "Unknown";
}
}
public static void Show(Activity callingActivity) {
//Use a Spannable to allow for links highlighting
SpannableString aboutText = new SpannableString("Version " +
VersionName(callingActivity) + "\n\n" +
callingActivity.getString(R.string.about));
// views to pass to AlertDialog.Builder and to set the text
View about;
TextView tvAbout;
try {
//Inflate the custom view
LayoutInflater inflater = callingActivity.getLayoutInflater();
about = inflater.inflate(R.layout.aboutbox,
(ViewGroup) callingActivity.findViewById(R.id.aboutView));
tvAbout = (TextView) about.findViewById(R.id.aboutText);
} catch (InflateException e) {
// Unchecked exception, unlikely, but default to TextView if it occurs
about = tvAbout = new TextView(callingActivity);
}
//Set the about text
tvAbout.setText(aboutText);
// Now Linkify the text
Linkify.addLinks(tvAbout, Linkify.ALL);
//Build and show the dialog
new AlertDialog.Builder(callingActivity)
.setTitle("About " + callingActivity.getString(R.string.app_name))
.setCancelable(true)
.setIcon(R.drawable.ic_launcher)
.setPositiveButton("OK", null)
.setView(about)
.show(); //Builder method returns allow for method chaining
}
}
create a XML for AboutBox class :
4.aboutbox.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/aboutView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/aboutLayout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TextView android:id="@+id/aboutText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#888"/>
</LinearLayout>
</ScrollView>
5.dimens.xml:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
6.strings.xml:
<resources>
<string name="app_name">AboutBoxDemo</string>
<string name="about">about</string>
</resources>
7.AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.About.BoxDemo">
<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>
Output:
No comments:
Post a Comment