Showing posts with label create. Show all posts
Showing posts with label create. Show all posts

Monday, March 31, 2014

How to create menu in Android using xml file

This code will show how to create menu in android, how to perform action when we click on menu item and how to set icon in menu item.

Just create a new project and give "rl" id to layout or paste below code in layout->main xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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=".TestMenu"
    android:id="@+id/rl">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

 
Now open menu folder and create test_menu xml file and paste below code


    <item android:id="@+id/red"
        android:title="RED"
        android:icon="@drawable/lion">
       
    </item>
      <item android:id="@+id/blue"
        android:title="BLUE">
       
    </item>
      <item android:id="@+id/green"
        android:title="GREEN">
       
    </item>
      <item android:id="@+id/yellow"
        android:title="YELLOW">
       
    </item>
      <item android:id="@+id/black"
        android:title="BLACK">
      </item>
           <item android:id="@+id/gray"
        android:title="GREY">
        </item>
      
           <item android:id="@+id/pink"
        android:title="PINK">
      </item>
         <item android:id="@+id/white"
        android:title="WHITE">
      </item>
         <item android:id="@+id/close"
        android:title="CLOSE">
      </item>
</menu>

Now open Main java file and paste below code

package test.menutest; //your package name

importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.graphics.Color;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.widget.RelativeLayout;

public class TestMenu extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

 //This function will call when we click on menu button
    @Override
    public booleanonCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.test_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
//This function will perform action when we click on menu item

    public booleanonOptionsItemSelected(MenuItem item)
    {
      RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl);
      switch(item.getItemId())
      {
      case R.id.red:rl.setBackgroundColor(Color.RED); break;
      case R.id.blue:rl.setBackgroundColor(Color.BLUE); break;
      case R.id.gray:rl.setBackgroundColor(Color.GRAY); break;
      case R.id.yellow:rl.setBackgroundColor(Color.YELLOW); break;
      case R.id.pink:rl.setBackgroundColor(Color.MAGENTA); break;
      case R.id.white:rl.setBackgroundColor(Color.WHITE); break;
      case R.id.green:rl.setBackgroundColor(Color.GREEN); break;
      case R.id.close:finish(); break;
      default:rl.setBackgroundColor(Color.BLACK);
     
      }
     
      return true;
    }  
}

Run code and Enjoy...

Read More..

Sunday, March 30, 2014

Create wifi hotspot on Ubuntu 12 04 12 10

Ok, its sad that Android devices dont support connection to ad-hoc wifi but there are some methods. For example, for Windows 7 there is Connectify, its true its not free, but it exist! But for Linux? Im a Linux fan, I work a lot on Linux and I want to have the posibility to connect my gadgets to a hotspot but Linux creates from default ad-hoc connections. But there is a solutions and its FREE!
All you have to do is to open terminal and enter some commands. And Ill publish the steps!

  1. open terminal and type: sudo lshw | less ; find -netwok section and make sure that driver is ath5k or ath9k. If you made a hotspot with Connectify on Windows7 and it worked, you can skip this step.
  2. install hostapd and dnsmasq by typing: sudo apt-get install hostapd dnsmasq
  3. stop those services and prevent them from starting on system start up:
    • sudo service hostapd stop
    • sudo service dnsmasq stop
    • sudo update-rc.d hostapd disable
    • sudo update-rc.d dnsmasq disable
  4. configure dnsmasq:sudo gedit /etc/dnsmasq.conf and add:
    1. # Bind to only one interface
    2. bind-interfaces
    3. # Choose interface for binding
    4. interface=wlan0
    5. # Specify range of IP addresses for DHCP leasses
    6. dhcp-range=192.168.150.2,192.168.150.10
  5. configure hostapd:sudo gedit /etc/hostapd.conf and add:
    1. # Define interface
    2. interface=wlan0
    3. # Select driver
    4. driver=nl80211
    5. # Set access point name
    6. ssid=myhotspot
    7. # Set access point harware mode to 802.11g
    8. hw_mode=g
    9. # Set WIFI channel (can be easily changed)
    10. channel=6
    11. # Enable WPA2 only (1 for WPA, 2 for WPA2, 3 for WPA + WPA2)
    12. wpa=2
    13. wpa_passphrase=mypassword
  6. create a file named start.sh and add the following lines:
    1. #!/bin/bash
    2. # Start
    3. # Configure IP address for WLAN
    4. sudo ifconfig wlan0 192.168.150.1
    5. # Start DHCP/DNS server
    6. sudo service dnsmasq restart
    7. # Enable routing
    8. sudo sysctl net.ipv4.ip_forward=1
    9. # Enable NAT
    10. sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
    11. # Run access point daemon
    12. sudo hostapd /etc/hostapd.conf
    13. # Stop
    14. # Disable NAT
    15. sudo iptables -D POSTROUTING -t nat -o ppp0 -j MASQUERADE
    16. # Disable routing
    17. sudo sysctl net.ipv4.ip_forward=0
    18. # Disable DHCP/DNS server
    19. sudo service dnsmasq stop
    20. sudo service hostapd stop
  7. start hotspot by starting your script "start.sh"
PS: Im not the author of this tutorial but it helped me and Im sure that it works!
Read More..