Android

Displaying a Dialog Window

Google+ Pinterest LinkedIn Tumblr

1. Using Android Studio, create a new Android project and name it Dialog. When presented with the option, name the main activity DialogActivity.
2. Add the following theme in bold to the AndroidManifest.xml file. Be sure to change all instances of “com.akbansal” to whatever package name your project is using.

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.akbansal.dialog" >

<application

android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" >

<activity

android:name=".DialogActivity" android:label="@string/app_name" android:theme="@style/Theme.AppCompat.Dialog" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />



<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

3. Compare your DialogActivity.java file to this:

 

package com.akbansal.dialog;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar;
import android.view.View; import android.view.Menu;
import android.view.MenuItem;
public class DialogActivity extends AppCompatActivity { 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_dialog);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_dialog, menu); return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId();

//noinspection SimplifiableIfStatement if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

 

4. Press Shift+F9 to debug the application on the Android emulator. Click the button to display the dialog

 

Write A Comment