Sunday, 28 October 2012

OnClickListener to the layout of the listview item and displays the toast


 Displays the list of items in the form of list view and onclick of listitem its respective toast appears
Main point here is onclick listener is implemented on the layout

package com.example.weeknamesdisplay;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {

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

        String[] sports = getResources().getStringArray(R.array.sports_array);

        final ListView lv1 = (ListView) findViewById(R.id.listV_main);
        lv1.setAdapter(new ItemListBaseAdapter(this, sports));
    }

}


package com.example.weeknamesdisplay;

import java.io.Serializable;
import java.util.Arrays;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ItemListBaseAdapter extends BaseAdapter {

    private Context context;
    private List<String> displayitems;
    private LayoutInflater l_Inflater;

    public ItemListBaseAdapter(MainActivity mainActivity, String[] sports) {
        // TODO Auto-generated constructor stub

        l_Inflater = LayoutInflater.from(mainActivity);
        this.displayitems = Arrays.asList(sports);
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = l_Inflater.inflate(R.layout.item_details_view, null);
            holder = new ViewHolder();
            holder.txt_itemName = (TextView) convertView
                    .findViewById(R.id.text1);
            holder.row = (RelativeLayout) convertView

            .findViewById(R.id.row);

            holder.itemImage = (ImageView) convertView
                    .findViewById(R.id.image1);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txt_itemName.setText(displayitems.get(position));

        holder.itemImage.setImageResource(R.drawable.arrow);

        /*
         * holder.row.setOnClickListener(new OnClickListener() {
         *
         * @Override public void onClick(View arg0) { // TODO Auto-generated
         * method stub // Toast.makeText(ItemListBaseAdapter.this,
         * "You have chosen : " + " " + displayitems.get(position),
         * Toast.LENGTH_LONG).show();
         *
         *
         *
         * } });
         */

        View ll = (RelativeLayout) holder.txt_itemName.getParent();
        ll.setFocusable(true);
        ll.setSelected(true);
        ll.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 250
                Toast.makeText(v.getContext(), displayitems.get(position), 250)
                        .show();
                // notifyDataSetChanged();
            }
        });

        return convertView;
    }

    static class ViewHolder {
        TextView txt_itemName;
        RelativeLayout row;
        ImageView itemImage;
    }

}


Activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/row" >

   
     <ListView
         android:layout_height="wrap_content"
         android:layout_width="fill_parent" android:id="@+id/listV_main"/>

</RelativeLayout>





Items_Detail.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layercontainer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:padding="10dp"
        android:textColor="#000000"
        android:textSize="16sp"
        android:typeface="sans" />

    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />

</RelativeLayout>

Wednesday, 24 October 2012

Android to draw border at top or bottom of textview

package com.example.testscrollview;

import java.util.HashMap;

import java.util.Map;


import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
    ImageView image ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView sample = (TextView) findViewById(R.id.textView1);
        sample.setText("hello test");

      sample.setBackgroundResource(R.layout.linesample);
      
    } 
}


Place this in layout folder

linesample.xml





<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="line">
        <stroke android:width="1dp" android:color="#FFFF00" />
        <solid android:color="#00000000" />

        <padding android:top="25dp" />
      
       

    </shape>
</item>
</layer-list>

Top and Bottom Border to textview

package com.example.testscrollview;

import java.util.HashMap;

import java.util.Map;


import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
    ImageView image ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView sample = (TextView) findViewById(R.id.textView1);
        sample.setText("hello test"); 

    }   
}

activity_main.xml






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

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:text="TextView"
            android:paddingTop="25dp"
            android:background="@drawable/textlines"
           />
      
    </LinearLayout>

</ScrollView>

Place the following in the drawable folder

textlines.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>

        <shape android:shape="rectangle" >

            <stroke
                android:width="1dp"
                android:color="#65000B" />

            <solid android:color="#ff215f8b" />
        </shape>
    </item>

    <item
        android:bottom="1dp"
        android:top="1dp">

        <shape android:shape="rectangle" >

            <stroke
                android:width="1dp"
                android:color="#65000B" />

            <solid android:color="#ff215f8b" />
        </shape>
    </item>

</layer-list>

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>

        <shape android:shape="rectangle" >

            <stroke
                android:width="1dp"
                android:color="#65000B" />

            <solid android:color="#ff215f8b" />
        </shape>
    </item>

    <item
        android:bottom="1dp"
        android:top="1dp">

        <shape android:shape="rectangle" >

            <stroke
                android:width="1dp"
                android:color="#65000B" />

            <solid android:color="#ff215f8b" />
        </shape>
    </item>

</layer-list>

Thursday, 18 October 2012

Dynamically load image in activity

package com.example.imagedynamicallyadding;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
       
        LinearLayout layout = new LinearLayout(getApplicationContext());
        LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        ImageView img = new ImageView(getApplicationContext());
        img.setPadding(0, 0, 0, 0);
        img.setImageResource(R.drawable.ic_launcher);
        layout.addView(img);
        setContentView(layout);

       
    }

  
}

Sunday, 14 October 2012

Passing data using Intents

package com.example.callingintent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
   
    public String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    protected static final int REQUEST_CODE = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText edt1 = (EditText) findViewById(R.id.editText1);
        Button button1 = (Button) findViewById(R.id.button1);
        EditText et1 = (EditText) findViewById(R.id.editText1);
        et1.setText(getName());
        button1.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
               
                Intent myintent = new Intent(MainActivity.this,secondActivity.class);
                myintent.putExtra("name", "namratha");
                myintent.putExtra("Id", 1);
                //startActivity(myintent);
                startActivityForResult(myintent, REQUEST_CODE);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String returnString;
           if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
              if (data.hasExtra("returnString1")) {
                 returnString = data.getExtras().getString("returnString1");
                 System.out.println("return string is   "+returnString);
                 setName(returnString);
                
            }
           }
       
       
       
    }

 
}


package com.example.callingintent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;

public class secondActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        EditText et1 = (EditText) findViewById(R.id.editText1);
        EditText et2 = (EditText) findViewById(R.id.editText2);
        Bundle mydata = getIntent().getExtras();
        if(mydata!=null)
        {
            String name = mydata.getString("name");
            int id =mydata.getInt("Id");
            System.out.println("id is "+id);
           
            et1.setText(name);
            //et1.setText(id);
       
        }
       
    }       
       
        public void finish() {
            Intent data = new Intent();
           
            data.putExtra("returnString1", "Message to parent activity");
            setResult(RESULT_OK, data);
            super.finish();
       
       
    }

}

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="44dp"
        android:text="Button" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_marginTop="33dp"
        android:layout_toLeftOf="@+id/textView1"
        android:ems="10" >

        <requestFocus />
    </EditText>

</RelativeLayout>

Second.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Hello Second Activity"
        tools:context=".MainActivity" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="36dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginLeft="21dp"
        android:layout_marginTop="20dp"
        android:ems="10" />

</RelativeLayout>

ListView with its Customised BaseAdapter

Following example displays the list of items and on selection of particular item this item is passed to second activity using intents


package com.example.listadapter;

import java.util.ArrayList;

import java.util.List;



import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends Activity {
    protected static final int requestCode = 0;
    VideoGet getObj = new VideoGet();
    ArrayList<VideoGet> sample = new ArrayList<VideoGet>();

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

        getObj.setId("1");
        getObj.setName("one");

        sample.add(getObj);
        for (VideoGet display : sample) {
            System.out.println("inside for loop " + display.getId() + " "
                    + display.getName());

        }
        VideoGet getObj1 = new VideoGet();
        getObj1.setId("2");
        getObj1.setName("two");
        sample.add(getObj1);

        for (VideoGet display : sample) {
            System.out.println("inside second for loop " + display.getId()
                    + " " + display.getName());

        }
        VideoGet getObj2 = new VideoGet();
        getObj2.setId("3");
        getObj2.setName("three");
        sample.add(getObj2);

        VideoGet getObj3 = new VideoGet();
        getObj3.setId("4");
        getObj3.setName("four");
        sample.add(getObj3);

        System.out
                .println("getobj name is in mainactivity " + getObj.getName());

        final ListView lv1 = (ListView) findViewById(R.id.ListView01);

        lv1.setAdapter(new MyCustomAdapter(this, sample));

        lv1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> a, View v, int position,
                    long id) {
                // TODO Auto-generated method stub
               
               
               
                Object o = lv1.getItemAtPosition(position);
                VideoGet video_items = (VideoGet)o;
                Toast.makeText(MainActivity.this, "selected item is " +" "+video_items.getName()+" "+video_items.getId() , Toast.LENGTH_LONG).show();
                Intent my_intent = new Intent(MainActivity.this,Detail_list.class);
                my_intent.putExtra("name", video_items.getName());
                //startActivity(my_intent);
                startActivityForResult(my_intent, requestCode);
               
               

            }

        });
    }

}




package com.example.listadapter;

import java.util.ArrayList;


import java.util.List;





import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

public class MyCustomAdapter extends BaseAdapter {
   
    private int imgid = R.drawable.arrow;
           
           

    private LayoutInflater l_Inflater;
    private ArrayList<VideoGet> listdisplay;

    public MyCustomAdapter(Context context, ArrayList<VideoGet> sample) {
        // TODO Auto-generated constructor stub

        l_Inflater = LayoutInflater.from(context);
        listdisplay = sample;
    }

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

    @Override
    public Object getItem(int position) {
        return listdisplay.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

   

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
           

            convertView = l_Inflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
           
            holder.txt_itemName = (TextView) convertView.findViewById(R.id.TextView01);
            holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.TextView02);
            holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);
           
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        holder.txt_itemName.setText(listdisplay.get(position).getName()+"  ");
        holder.txt_itemDescription.setText(listdisplay.get(position).getId());
        holder.itemImage.setImageResource(imgid);
       
        return convertView;
        }
   
   
   
    static class ViewHolder {
        TextView txt_itemName;
        TextView txt_itemDescription;
        TextView txt_itemPrice;
        ImageView itemImage;
    }
}



package com.example.listadapter;

public class VideoGet {
    String name;
    String id;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }


}


package com.example.listadapter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Detail_list extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_list);
        TextView tv1 = (TextView) findViewById(R.id.textView1);
       
        Bundle mydata = getIntent().getExtras();
       
        String name = mydata.getString("name");
        tv1.setText(name);
       
    }
}







activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/ListView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</RelativeLayout>



detail_list.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello detail activity"
        android:textColor="#0099CC"
        android:textSize="20px"
        android:textStyle="bold" >
    </TextView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextView01"
        android:textColor="#0099CC"
        android:textSize="15px"
        android:textStyle="bold"
       
        android:text="TextView" />

</RelativeLayout>


 list_item.xml

 


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView
           android:id="@+id/photo"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
        android:layout_marginLeft="150px"
        android:paddingTop="10px" />
    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50px"
        android:textColor="#0099CC"
        android:textSize="20px"
        android:textStyle="bold" >
    </TextView>
   
    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10px"
        android:textColor="#0099CC"
        android:textSize="20px"
        android:textStyle="bold" >
    </TextView>
   
   
  
</RelativeLayout>




Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...