- Create project named ActivityAsDialog.
- Add following code in activity_main.xml layout file.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/border_t"
android:padding="20dp"
android:orientation="vertical" >
<TextView
android:id="@+id/tvsmsNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SMS No"
android:textSize="28dp"
android:lines="1"
android:textColor="#fff" />
<TextView
android:id="@+id/tvsmsBody"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:layout_marginTop="15dp"
android:text="Body"
android:maxLines="10"
android:textColor="#fff" />
<Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:textSize="28dp"
android:padding="14dp"
android:layout_marginTop="15dp"
android:scrollbars="vertical"
android:layout_gravity="center" />
</LinearLayout>
NOTE: Change root linear layout background "#000000" or set your image.Otherwise it will throw Exception - NoResourceFound.
- Add following code in MainActivity.java file.
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvNo = (TextView)findViewById(R.id.tvsmsNo);
TextView tvBody = (TextView)findViewById(R.id.tvsmsBody);
Button btnOk = (Button)findViewById(R.id.btnOk);
String body = "Hi, How are you?";
tvNo.setText("SMS:9876543210");
tvBody.setText(body);
tvBody.setMovementMethod(new ScrollingMovementMethod());
btnOk.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MainActivity.this.finish();
}
});
}
}
- Now Add following code in res/values/styles.xml file.
styles.xml
<style name="Theme.UserDialog" parent="android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
- At Last Change your activity theme in menifest.xml as below.
Menifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activityasadialogbox"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.UserDialog" >
<activity
android:name="com.example.activityasadialogbox.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
- Done ! Now Run project.
Thanks!
ReplyDeleteakt