Pages

Showing posts with label simple. Show all posts
Showing posts with label simple. Show all posts

Friday, May 13, 2016

Simple ListView Android Developer Tutorial Part 16

From exploring the various concepts related to fundamentals and to locations and maps, I would like to look at a few UI elements.


One of the simple views is a List View. In this post, I will explore a very simple list view using all the defaults provided by Android SDK and introduce you to a customized list view in the next blog article.

A List View, by name means being able to display a list of items in an order one below the other. For creating such a view, the first thing we have to do is extend the ‘ListActivity’ (android.app.ListActivity) instead of the normal Activity class.

So, here is how the class declaration should look:

public class MyListView extends ListActivity {
The ListActivity class provides a way of binding a source of data (an array or a cursor) to a ListView object through a ListAdapter.

In android, we all know that views are defined declaratively in XML files. It is the same here too. So, if we were to create our own custom ListView, we would have a declaration of this type:

<ListView android_id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000fff"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>

However, if no ListView is declared, the ListActivity picks up a default ListView which fills the screen. So, in our example we will not declare any list view.

There is one more thing we need to define and that is how each row in the list should show up. This again, there are some defaults available which have names such as simple_list_item_1, simple_list_item_2, and two_line_list_item. So, in our example, I do not declare the layout for the rows but I will be using one of the defaults provided. So, in effect, I have not declared any layout – I am using the default screen layout and row layout.

Now, that we have the layouts out of our way, what else do we need for a List View. Of course, the list of data that needs to be displayed. Just to keep it simple, I will use an array of data for the same. So, here it is:

static final String[] PENS = new String[]{
"MONT Blanc",
"Gucci",
"Parker",
"Sailor",
"Porsche Design",
"Rotring",
"Sheaffer",
"Waterman"
};

Now, how do I bind this data to the default views described earlier? This is aided by a ListAdapter interface hosted by the ListActivity class that we have extended.

We need to use the setListAdapter(..) method of the ListActivity class to bid the two (data and view). Android provides many implementations of the ListAdapter. We will use the simple ArrayAdapter.

What does the ArrayAdapter expect?

It expects the context object, the row layout and the data in an array.

So here it is:

setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, PENS));
With this we are done in creating the ListView.

I have added a small piece of code to show we can handle events on selecting an item in the list, by overriding the protected method shown below:

protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String pen = o.toString();
Toast.makeText(this, "You have chosen the pen: " + " " + pen, Toast.LENGTH_LONG).show();
}

This toasts a message with the item selected.

The complete code is downloadable here.

Read More..

Thursday, April 28, 2016

:p>

tablayout,or,tabbed,view,android,developer,tutorial
Read More..

Tuesday, April 19, 2016

Android Developer Days is reloading for 2013

Android Developer Days(ADD) is now reloading for 2013. ADD 2013 will take place on June 14/15, 2013 in METU Cultural and Convention Center, Ankara, Turkey. 
ADD 2013 is targeted to be the one of the biggest international tech related community organization in EMEA. The first ADD was on May 21/22, 2012. There were 700 participants and more than 30 speakers.  ADD 2013,  June 14/15, 2013,  will have more participants, more fun, more networking and more inspiration. 
Take a look at the About, Call for submissions and Participation Form.

ADD is a community conference organized by:  


  • GDG Ankara,
  • GDG ?stanbul,
  • ODTÜ IEEE, 
  • GDG Baku,
  • GTUG Brussels, 
  • GDG Cluj-Napoca, 
  • GDG Dnipropetrovsk, 
  • GDG Eski?ehir, 
  • GDG Konya, 
  • GDG Kyiv, 
  • GDG Lviv, 
  • GDG Samsun, 
  • GDG Tbilisi, 
  • GDG Timi?oara

Call For Submissions

You can join the conference with the followings:

  • Presentations that is 15-20 minutes or 40-45 minutes long.
  • Quickie: 10 minute presentation in small rooms.
  • Poster: You can show your applications in a place dedicated for posters.
  • Workshop: Presentations with hands on coding, 40 minutes long.
Indicate your contribution in OpenConf and please indicate how much your presentation is going to take when submitting it to OpenConf Deadline of Call for Submissions is 1st May 2013.
Subjects:

  • Future Technologies
  • Mobile World
    • Future of Mobile World
    • New Generation Mobile Devices (Project Glass)
  • Android OS
    • Future Versions
    • Android in Different Architectures
    • Android in Different Areas(Industry, Defense, Home Electronics etc.)
  • Android App Development
    • New Features and Capabilities
    • App Development Best Practises
    • Android SDK Tools
    • UI(User Interface) / UX (User Experience) Design
    • App Monetization
      • Ad Integration
      • In-app Billing
    • User Statistics
  • Android NDK
  • Cross Platform App Development Frameworks
  • HTML5
  • Javascript
  • Game Development
  • Google TV
  • Google Playstore
  • Communication Solutions (NFC, Bluetooth, Wi-fi)
  • 3G, 4G Wide Band Communication
  • Cloud
    • Google Cloud Messaging for Android
    • Google App Engine
  • Augmented Reality
  • Social Media
  • Location Based Services and Maps
  • Mobile Education (Fatih Project)
  • Mobile Payment
  • Security
  • Arduino
  • Software Development Methodologies
  • Success Stories
  • GWT




Read More..

Thursday, March 24, 2016

Android Simple Image Gallery

In this post we are going to use the gallery control t construct a simple image gallery.
the layout is like the previous post.

remeber that the Gallery control is populated by an adapter, so we will create a custom adapter that contains ImageViews to display the images.
our custom adapter must inherit from BaseAdapter class
package mina.android.GalleryDemo;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
Context Con;
//array to hold the values of image resources
int [] Resources;
List views;

public ImageAdapter(Context con,int[] resources)
{
Con=con;
Resources=resources;
views=new ArrayList(resources.length);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return Resources.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return views.get(position);
//return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
//return views.get(position).getId();
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView img=new ImageView(Con);
img.setImageResource(Resources[position]);
views.add(img);

return img;
}

}


then in our activity onCreate Method:
int []res=new int[]{R.drawable.wc,R.drawable.wc2,R.drawable.wc3,R.drawable.wc4,R.drawable.wc5};
ItemsInGallery=res.length;
ImageAdapter imgAdapter=new ImageAdapter(this, res);
gallery.setAdapter(imgAdapter);

and that was our simple Image gallery.
download the application from here
Read More..

Wednesday, February 17, 2016

Simple HTTP server running on Android

Its my old exercise "Implement simple HTTP server running on Android". Somebody report its not work, so I re-try it on Android Studio. Actually the code is same as before, and work as expected. Please notice both the server and client have to connect in the same WiFi network.


Updated@2015-11-26:
This code NOT work on Android 5, may be all 5+ version.
Fixed, refer next post "socket.getInetAddress() return null on Android 5".


Work on Xiaomi Redmi 2, running Android 4.4.4


MainActivity.java
package com.blogspot.android_er.androidhttpserver;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;

public class MainActivity extends AppCompatActivity {

EditText welcomeMsg;
TextView infoIp;
TextView infoMsg;
String msgLog = "";

ServerSocket httpServerSocket;

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

welcomeMsg = (EditText) findViewById(R.id.welcomemsg);
infoIp = (TextView) findViewById(R.id.infoip);
infoMsg = (TextView) findViewById(R.id.msg);

infoIp.setText(getIpAddress() + ":"
+ HttpServerThread.HttpServerPORT + " ");

HttpServerThread httpServerThread = new HttpServerThread();
httpServerThread.start();
}

@Override
protected void onDestroy() {
super.onDestroy();

if (httpServerSocket != null) {
try {
httpServerSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();

if (inetAddress.isSiteLocalAddress()) {
ip += "SiteLocalAddress: "
+ inetAddress.getHostAddress() + " ";
}

}

}

} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + " ";
}

return ip;
}

private class HttpServerThread extends Thread {

static final int HttpServerPORT = 8888;

@Override
public void run() {
Socket socket = null;

try {
httpServerSocket = new ServerSocket(HttpServerPORT);

while(true){
socket = httpServerSocket.accept();

HttpResponseThread httpResponseThread =
new HttpResponseThread(
socket,
welcomeMsg.getText().toString());
httpResponseThread.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

private class HttpResponseThread extends Thread {

Socket socket;
String h1;

HttpResponseThread(Socket socket, String msg){
this.socket = socket;
h1 = msg;
}

@Override
public void run() {
BufferedReader is;
PrintWriter os;
String request;

try {
is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
request = is.readLine();

os = new PrintWriter(socket.getOutputStream(), true);

String response =
"<html><head></head>" +
"<body>" +
"<h1>" + h1 + "</h1>" +
"</body></html>";

os.print("HTTP/1.0 200" + " ");
os.print("Content type: text/html" + " ");
os.print("Content length: " + response.length() + " ");
os.print(" ");
os.print(response + " ");
os.flush();
socket.close();

msgLog += "Request of " + request
+ " from " + socket.getInetAddress().toString() + " ";
MainActivity.this.runOnUiThread(new Runnable() {

@Override
public void run() {

infoMsg.setText(msgLog);
}
});

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return;
}
}
}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout


android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
tools_context="com.blogspot.android_er.androidhttpserver.MainActivity">

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold" />

<EditText
android_id="@+id/welcomemsg"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Welcome from Android-er" />

<TextView
android_id="@+id/infoip"
android_layout_width="wrap_content"
android_layout_height="wrap_content" />

<ScrollView
android_layout_width="match_parent"
android_layout_height="match_parent" >

<TextView
android_id="@+id/msg"
android_layout_width="wrap_content"
android_layout_height="wrap_content" />
</ScrollView>
</LinearLayout>


Permission of "android.permission.INTERNET" is needed in AndroidManifest.xml.

download filesDownload the files (Android Studio Format) .
Read More..