Don't bother like my brother

Just another WordPress weblog

 

Android PopupWindow with dynamic layout September 2, 2010

Filed under: Uncategorized — Artem @ 11:54 am

Before I described how a PopupWindow in Android can be created. I used XML to describe the layout. However, sometimes the layout must change depending on various parameters. For example varying number of buttons or text boxes, or other things.

Below is an example of such dynamic layout using PopupWindow.

Public class DynamicPopup {
    private Context context;
    private PopupWindow pw;

    // class constructor
    public DynamicPopup(Context context) {
        this.context = context;
    }

    public showPopup(int type) {
         // create a LinearLayout
         LinearLayout mainLayout = new LinearLayout(context);
         mainLayout.setOrientation(LinearLayout.VERTICAL);
         mainLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
         mainLayout.setGravity(Gravity.CENTER_HORIZONTAL);

         switch(type) {
         case 1:
             // choose the appropriate background size
             mainLayout.setBackgroundResource(R.drawable.popup_background1);
             pw = new PopupWindow(mainLayout, 100, 200, true);
             // create a button
             Button button1 = new Button(context);
             button1.setText("ok");
             button1.setLayoutParams(new LayoutParams(80, 35));
             button1.setOnClickListener(new OnClickListener() {
                 @Override
                 public void onClick(View vv) {
                     pw.dismiss();
                 }
             });
             // add this button to the view
             mainLayout.addView(button1);
             Button button2 = new Button(context);
             button2.setText("ok");
             button2.setLayoutParams(new LayoutParams(80, 35));
             mainLayout.addView(button2);
             pw.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
             break;

         case 2:
             // choose the appropriate background size
             mainLayout.setBackgroundResource(R.drawable.popup_background2);
             pw = new PopupWindow(mainLayout, 200, 200, true);
             // create a button
             Button button1 = new Button(context);
             button1.setText("ok");
             button1.setLayoutParams(new LayoutParams(80, 35));
             button1.setOnClickListener(new OnClickListener() {
                 @Override
                 public void onClick(View vv) {
                     pw.dismiss();
                 }
             });
             // add this button to the view
             mainLayout.addView(button1);
             pw.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
             break;
        }
    }
}

This is it. As simple as that.

 
 

OpenFrameworks object detection September 1, 2010

Filed under: IT, code, software — Artem @ 12:41 pm

I found out about OpenFrameworks from the project Laser Tag by Graffiti Research Lab. Got curious, and the first thing I created with OF was a bit similar to the Laser Tag :-)

I used OpenCV to detect the button on the wall, and then its coordinates to draw the line. Later I will post the source code.

 
 

OpenFrameworks ofProject tab completion

Filed under: IT, code, interesting, tips — Artem @ 11:26 am

I prefer to do many things in console and am used to tab completion in programs. It felt uncomfortable to type the plugin and project names in ofProject, especially when you have many of them. So I have created a small script that looks into appropriate directories and then autocompletes the names when you press tab.

_ofProject()
{
 local cur prev

 COMPREPLY=()
 cur="${COMP_WORDS[COMP_CWORD]}"
 prev="${COMP_WORDS[COMP_CWORD-1]}"
 opts="add"
 local ADDON_DIRS=$(ls -l /home/alius/Code/openframeworks/addons/ | grep '^d' | awk '{ print $8 }')
 local APP_DIRS=$(ls -l /home/alius/Code/openframeworks/apps/myApps/ | grep '^d' | awk '{ print $8 }')

 case "${prev}" in
    add)
        COMPREPLY=( $(compgen -W "${ADDON_DIRS}" -- ${cur}))
        return 0
        ;;
    *)
        COMPREPLY=( $(compgen -W "${APP_DIRS}" -- ${cur}))
        return 0
        ;;
 esac
}
complete -F _ofProject ofProject

What you need is just change the paths and place this script into /etc/bash_completion.d/ as ofProject

 
 

Android PopupWindow example

Filed under: IT, code, tips — Artem @ 11:04 am

I’ve struggled for some time to get the PopupWindow to work on Android. For all of those who have trouble making it to work here is a short tutorial.

First, create an XML layout, name it popup_layout.xml and in the res/layout/ folder:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_menu_root"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button android:id="@+id/popup_menu_button1"
        android:text="close"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Button android:id="@+id/popup_menu_button2"
        android:text="ok2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Button android:id="@+id/popup_menu_button3"
        android:text="ok3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Here I have created a linear layout and put three buttons inside. So that the popup is visible I have made the background white.

Now to the code. You probably already have a class where you need to create a popup. You can declare it wherever you need. I needed to show the popup window after user clicks a button, so I made it class-level and declared in the beginning:

private PopupWindow pw;

Next in the appropriate onClickListener I create the PopupWindow and show it:

// get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopupWindowClass.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate our view from the corresponding XML file
View layout = inflater.inflate(R.layout.popup_menu, (ViewGroup)findViewById(R.id.popup_menu_root));
// create a 100px width and 200px height popup window
pw = new PopupWindow(layout, 100, 200, true);
// set actions to buttons we have in our popup
Button button1 = (Button)layout.findViewById(R.id.popup_menu_button1);
button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View vv) {
        // close the popup
        pw.dismiss();
    }
});
Button button2 = (Button)layout.findViewById(R.id.popup_menu_button2);
button2.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View vv) {
        Toast.makeText(PopupWindowClass.this, "Hello", Toast.LENGTH_LONG).show();
    }
});
Button button3 = (Button)layout.findViewById(R.id.popup_menu_button3);
button3.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View vv) {
        finish();
    }
});
// finally show the popup in the center of the window
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);

Hope this helps you :-)

 
 

Quote August 19, 2010

Filed under: IT, interesting — Artem @ 11:31 am

“If I plug the machine into a d/port with two monitors connected, it posts on one monitor and the OS X desktop loads on the other. Now that’s almost as messed up as Barack Obama winning a Nobel Peace Prize.”

By leppy700m at insanelymac.com

 
 

Android Secret Codes July 6, 2010

Filed under: IT, interesting, tips — Artem @ 11:41 pm

There are some secret codes that you can dial in you Android dialer app. All these codes have the following mask: *#*#SOME_NUMBERS#*#*

  • *#*#4636#*#* - Testing application, which has phone information and some settings (imei, network name, type, location, etc), battery information including history, and usage statistics
  • *#*#8351#*#* - Turn on voice dial logging
  • *#*#8350#*#* - Turn off voice dial logging
  • *#*#7262626#*#* - FieldTest application
  • *#*#225#*#* - Calendar information
 
 

Ad world March 28, 2010

Filed under: interesting — Artem @ 5:45 pm

The world we live in

 
 

Funny February 25, 2010

Filed under: personal — Artem @ 11:21 pm

From Guardian:

The Indonesian government’s policy… simply weakens the software industry and undermines its long-term competitiveness by creating an artificial preference for companies offering open source software and related services, even as it denies many legitimate companies access to the government market.
Rather than fostering a system that will allow users to benefit from the best solution available in the market, irrespective of the development model, it encourages a mindset that does not give due consideration to the value to intellectual creations.
As such, it fails to build respect for intellectual property rights and also limits the ability of government or public-sector customers (e.g., State-owned enterprise) to choose the best solutions.

I wonder how much time will it take when GNU, Linux foundation, *BSD, and other organizations will become terrorist organizations. And it is also strange that these guys said nothing about France and Germany, Sweden and some US states that promote FOSS. We clearly need the change of that old generation of non-thinkers.

 
 

Temperature November 26, 2009

Filed under: interesting — Artem @ 2:11 am

I never thought of that:

Temperature is a quantity that determines the number of thermodynamically possible states of a system within an energy range.

 
 

Bacterial orchestra November 10, 2009

Filed under: interesting — Artem @ 12:54 am

Project’s page. It would be fun to have one cell.