Monday, October 4, 2010

How to set height,width for activity or how to make the activity window look in desired size..

This post talks about

- how to display a activity of fixed width & height ?

- why the activity screen is of full screen ? how to reduce the size of activity size?


These were the questions which were running through my mind, when i first started implementing activities in android.

This applies not only to activities it is even for dialogs.How to position the dialog in the desired place in the screen.Sometime we want alert to be displayed on the top right, rather at the center of the screen as user might be reading something.

Lets work on it more to find the answers...!!


1 step - To make the activity to have a desired size rather than full screen, set the theme for your activity as Dialog like below.

android:theme="@android:style/Theme.Dialog"

In AndroidManifest.xml



<activity android:name="urldisplay"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysVisible|adjustPan"
android:theme="@android:style/Theme.Dialog">
</activity>


2 step - We need to get the window from dialog and set the layout attributes, then the window will be positioned accordingly in the screen. This can be done as below.



WindowManager.LayoutParams params = getWindow().getAttributes();
params.x = -100;
params.height = 70;
params.width = 1000;
params.y = -50;

this.getWindow().setAttributes(params);


here is the complete example.




layout.xml (main.xml)



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<EditText android:id="@+id/textbox1"
android:hint="eg. pubs,restuarants "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"/>

<Button android:id="@+id/press"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"/>


</LinearLayout>


urldisplay.java



package com.android.urldisplay;

import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Intent;
import android.view.View;
import android.content.Context;
import android.net.Uri;
import android.app.PendingIntent;
import android.os.Handler;
import android.view.inputmethod.InputMethodManager;

public class urldisplay extends Activity
{
EditText nameText;
private static final int APP_ID = 0;
private NotificationManager mManager;
private Handler mHandler = new Handler();

private Runnable mShowInputMethodTask = new Runnable() {
public void run() {
showInputMethodForQuery();
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

WindowManager.LayoutParams params = getWindow().getAttributes();
params.x = -100;
params.height = 70;
params.width = 1000;
params.y = -50;

this.getWindow().setAttributes(params);
nameText = (EditText) findViewById(R.id.textbox1);

Context context;
context = getApplicationContext();
mManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);

Button press = (Button)findViewById(R.id.press);

press.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v) {

String url = nameText.getText().toString();
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;

Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse(url));
browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mManager.notify(url);
startActivity(browserIntent);
finish();

}
});
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
// Launch the IME after a bit
mHandler.postDelayed(mShowInputMethodTask, 0);
}
}
protected void showInputMethodForQuery() {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(nameText, 0);
}
}
}

6 comments:

  1. Excellent tutorial to reach to the point , U saved my time

    ReplyDelete
  2. Cool, I've been Google, and google for hours, this is exactly what i need ...

    Thanks much for sharing.

    ReplyDelete
  3. Wow! This is amazing. Thanks for sharing :)

    ReplyDelete
  4. brilliant! but is the Theme.Dialog only the theme can be use for this?

    ReplyDelete
  5. Thank you so much. I Change the theme, anyway I had an error in the on Create, what the setContentView is called:

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_dialog_prese);


    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.x = -100;
    params.height = 70;
    params.width = 1000;
    params.y = -50;

    this.getWindow().setAttributes(params);

    this is the error:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.multiplayer/com.example.multiplayer.CustomDialogPrese}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

    Maybe it is necessary in the manifest to put also:
    android:windowSoftInputMode="stateAlwaysVisible|adjustPan"
    ?

    ReplyDelete