Friday 26 September 2014

Customized ListView Example

Configuration Of Activities:

You can simply copy from outside image and add into under res/drawable-hdpi/



Your  MainActivity.java like this;

package com.example.customizedlistview;

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

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {

ListView myList;
String[] images;
String[] mobiles;
String[] names;
String[] tags;
String strImage;
ArrayList<HashMap<String, String>> tempList = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> sendList = new ArrayList<HashMap<String, String>>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList = (ListView) findViewById(R.id.myList);

names = new String[] { "Apple", "HTC", "LG", "Micromax", "Samsung",
"Sony", "Windows" };
images = new String[] { "apple", "htc", "lg", "micromax", "samsung",
"sony", "windows" };
tags = new String[] { "Think Different!", "Quietly Brilliant",
"Life's Good", "Nothing like anything", "Next is What?",
"make belive", " Do more with less" };
mobiles = new String[] { "iphonem", "htcm", "lgm", "micromaxm",
"samsungm", "sonym", "windowsm" };

for (int i = 0; i < tags.length; i++) {
HashMap<String, String> tempName = new HashMap<String, String>();
tempName.put("title", tags[i]);

tempList.add(tempName);
}

String[] from = { "title" };

int[] to = { R.id.tag };

SpecialAdapter adapter = new SpecialAdapter(this, tempList,
R.layout.row_cell, from, to);
myList.setAdapter(adapter);
myList.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
HashMap<String, String> sendmobile = new HashMap<String, String>();
sendmobile.put("mobile", mobiles[pos]);
Intent pass = new Intent(MainActivity.this, Companyname.class);

pass.putExtra("hello", sendmobile);

startActivity(pass);

return false;
}

});
myList.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub

HashMap<String, String> send = new HashMap<String, String>();
send.put("name", names[pos]);
send.put("image", images[pos]);
send.put("tag_line", tags[pos]);
Intent pass = new Intent(MainActivity.this, Description.class);

pass.putExtra("hello", send);

startActivity(pass);

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

public class SpecialAdapter extends SimpleAdapter {
public SpecialAdapter(Context context,
List<HashMap<String, String>> items, int resource,
String[] from, int[] to) {
super(context, items, resource, from, to);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);

TextView txt_companyName = (TextView) view
.findViewById(R.id.companyName);
txt_companyName.setText(names[position]);

ImageView thumbImage = (ImageView) view
.findViewById(R.id.thumbImage);
ImageView arraowimg = (ImageView) view.findViewById(R.id.arrowimg);
arraowimg.setImageResource(R.drawable.arrow);

strImage = images[position];

Resources res = getResources();
String mDrawableName = strImage;
int resID = res.getIdentifier(mDrawableName, "drawable",
getPackageName());
Drawable drawable = res.getDrawable(resID);
thumbImage.setImageDrawable(drawable);

return view;

}
}

}

2) Companyname.java activity:

package com.example.customizedlistview;

import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class Companyname extends Activity {
ImageView imgmobile;
String strImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_companyname);
Intent getval = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>) getval.getSerializableExtra("hello");
imgmobile=(ImageView)findViewById(R.id.longimage);
strImage=hashMap.get("mobile").toString();
Resources res = getResources();
String mDrawableName = strImage;
int resID = res.getIdentifier(mDrawableName, "drawable",
getPackageName());
Drawable drawable = res.getDrawable(resID);
imgmobile.setImageDrawable(drawable);
imgmobile.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Mobiles", Toast.LENGTH_LONG).show();

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_companyname, menu);
return true;
}


}

3) Description.java activity:

package com.example.customizedlistview;

import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

public class Description extends Activity {

ImageView imageView;
String name, tagline;
TextView txt_name;
TextView tag;
String strImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_description);
Intent getval = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>) getval
.getSerializableExtra("hello");
// HashMap<String, String> map = new HashMap<String, String>();

txt_name = (TextView) findViewById(R.id.name);
tag = (TextView) findViewById(R.id.tagline);

imageView = (ImageView) findViewById(R.id.image);
name = hashMap.get("name").toString();
tagline = hashMap.get("tag_line").toString();
strImage = hashMap.get("image").toString();
Resources res = getResources();
String mDrawableName = strImage;
int resID = res.getIdentifier(mDrawableName, "drawable",
getPackageName());
Drawable drawable = res.getDrawable(resID);

imageView.setImageDrawable(drawable);
txt_name.setText(name);
tag.setText(tagline);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_description, menu);
return true;
}


}

Your Layout XML files are:




Your activity_main like this;

<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"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/myList"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>


2) activity_companyname.xml;

<LinearLayout 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:gravity="center"
    tools:context=".Companyname" >

<ImageView 
    android:id="@+id/longimage"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    
    android:layout_gravity="center_horizontal"/>
   

</LinearLayout>

3) activity_description.xml;

<LinearLayout 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:gravity="center_horizontal"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_marginTop="20dp"
        android:clickable="true" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_marginTop="30dp"
        android:text="CompanyName"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tagline"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:text="tagline" />

</LinearLayout>

4) row_cell.xml;

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

    <ImageView
        android:id="@+id/thumbImage"
        android:clickable="true"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginLeft="10dp" />

    <LinearLayout
        android:layout_width="160dp"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/companyName"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginLeft="10dp"
            android:text="CompanyName"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tag"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_marginLeft="10dp"
            android:text="tagline"/>
    </LinearLayout>
     <ImageView
        android:id="@+id/arrowimg"
        android:clickable="true"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp" />

</LinearLayout>

App like this;





On Simple Click:


On Long Click:











Seekbar bind with Progressbar and Rattingbar

Your activity_main.xml like this;

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <SeekBar
        android:id="@+id/sb"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:max="100" />

    <TextView
        android:id="@+id/txt_dis"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="Display" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <RatingBar
        android:id="@+id/rtbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:numStars="7"
        android:rating="0"
        android:stepSize="0.5" />

</LinearLayout>

Your MainActivity.java like this;

package com.example.seekbar;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
SeekBar sb;
TextView txt_dis;
ProgressBar pb;
RatingBar rtb;
int int_pro;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (ProgressBar) findViewById(R.id.progressBar1);
rtb = (RatingBar) findViewById(R.id.rtbar);
sb = (SeekBar) findViewById(R.id.sb);

sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
txt_dis = (TextView) findViewById(R.id.txt_dis);
txt_dis.setText("" + progress);
pb.setProgress(progress);
float pro = progress / 12;
rtb.setRating(pro);

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


}

App like this;


Rattingbar simple example

Your activity_main like this;

<LinearLayout 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:background="@drawable/blueworld"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RATE ME"
            android:textColor="@android:color/background_light" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >

        <RatingBar
            android:id="@+id/rate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="5"
            android:rating="2.0"
            android:stepSize="0.5" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_Rate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RATE" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txt_result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RESULT:"
            android:textColor="@android:color/background_light" />

        <TextView
            android:id="@+id/txt_display"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"
            android:textColor="@android:color/background_light" />
    </LinearLayout>

</LinearLayout>

Your MainActivity.java like this;

package com.example.rattingbar;

import java.security.PublicKey;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
TextView txt_display;
Button btn_rate;
RatingBar rate;

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

addListenerOnRattingBar();

btn_rate = (Button) findViewById(R.id.btn_Rate);

btn_rate.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Set",
Toast.LENGTH_LONG).show();
}
});

}

private void addListenerOnRattingBar() {
// TODO Auto-generated method stub
txt_display = (TextView) findViewById(R.id.txt_display);
rate = (RatingBar) findViewById(R.id.rate);
rate.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub
txt_display.setText(String.valueOf(rating));

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


}

App like this;




Progressbar simple example

Your activity_main like this;

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >
       
        <Button
            android:id="@+id/btn_download"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Download"/>
       
    </LinearLayout>

</LinearLayout>

You MainActivity.java like this;

package com.example.progressbar;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity {
ProgressDialog pb;
Button btn_download;
int pbstatus;
Handler pbhandler = new Handler();
long filesize = 0;

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

}

private void addListenerOnButton() {
// TODO Auto-generated method stub
btn_download = (Button) findViewById(R.id.btn_download);
btn_download.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
pb = new ProgressDialog(v.getContext());
pb.setCancelable(true);
pb.setMessage("File is Downloading...");
pb.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pb.setMax(100);
pb.setProgress(0);
pb.show();
pbstatus = 0;
filesize = 0;
new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
while (pbstatus < 100) {
pbstatus = incr();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
pbhandler.post(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
pb.setProgress(pbstatus);

}
});

}
if (pbstatus >= 100) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
pb.dismiss();
}
}

}).start();
}
});
}

private int incr() {
// TODO Auto-generated method stub
while (filesize <= 1000000) {
filesize++;
if (filesize == 100000) {
return 10;
} else if (filesize == 200000) {
return 20;
} else if (filesize == 300000) {
return 30;
}
}
return 100;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

App like this;




Set Image Through Button

You can add images in your project through simple copy your image and then past in
res/drawable.



Now your activity_main.xml like this;

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/img"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Change" />
    </LinearLayout>

</LinearLayout>


Your MainActivity.java like this:

package com.example.imageview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
Button btn_change;
ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image=(ImageView)findViewById(R.id.img);
        btn_change=(Button)findViewById(R.id.btn);
        btn_change.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
// TODO Auto-generated method stub
image.setImageResource(R.drawable.cloudy);

}
});
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
}

App like this:

  







Runtime Set TextView Example

package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        TextView txt_hello=new TextView(this);
        txt_hello.setText("Hello World");
        Toast.makeText(getApplicationContext(), "Hello is not world",Toast.LENGTH_LONG).show();
        setContentView(txt_hello);
       
        return true;
    }
   
}
 App like this: