How to Add a Fixed Button after a ListView in Android?

In android development there’s a common scenario where you want a list of items and select from them.So, you may want a ListView with the items and and a button always fixed at the bottom. I googled a lot while i tried for the first time but failed to manage a good answer.So, this article may help the beginners.

Solution is very simple.Here’s my code:

package com.example.smsreceiver;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.backup.SharedPreferencesBackupHelper;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

	private Button btn;
	private ListView listView;
	private ArrayList<String> musicList=new ArrayList<String>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.musiclistfragment);

		listView=(ListView)findViewById(R.id.listViewMusic);
		btn=(Button)findViewById(R.id.btnok);

		musicList.add("Music 1");
		musicList.add("Music 2");
		musicList.add("Music 3");
		musicList.add("Music 4");
		musicList.add("Music 5");
		musicList.add("Music 6");
		musicList.add("Music 7");
		musicList.add("Music 8");
		musicList.add("Music 9");
		musicList.add("Music 10");
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_expandable_list_item_1,musicList);
		listView.setAdapter(adapter);

	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.btncpanel)
		{
			Intent intent=new Intent(getApplicationContext(),MessageDetails.class);
			startActivity(intent);
		}

	}

}

Here is the xml file:

I haven’t customize my ListView.Just kept it simple.You can  change the weight of  the components to your desired proportions if you wish.


<?xml version="1.0" encoding="utf-8"?>
<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" >

    <ListView
        android:id="@+id/listViewMusic"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:background="#000000"
        android:layout_weight="5" >
    </ListView>

    <Button
        android:id="@+id/btnok"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:text="Done" />

</LinearLayout>

Here’s a screenshot of the output:

output
output

‘Battery Charging Statistics’ an Android App in SlideME Marketplace

Battery Charging statistics is an android app which helps the user to track his battery charging history.Sometimes you have know idea how much time is needed to charge your phone.But this will help you to have an idea of that.
Here’s a screen shot of the app
Screenshot_2014-01-05-23-23-11
Get the Battery Charging Statistics Android app from SlideME.

How to Get SHA1 Fingerprint?

SHA1 finger print is needed to get google api key for google map in android or to get api key for google cloud messaging system.Here is the process:

If you have java installed.Using commad line GO to C:\-> Program files-> java->jre1.6.0->bin

Then enter the following commad:

keytool -v -list -alias androiddebugkey -keystore c:\users\your_user_name\.android\debug.keystore -storepass android -keypass android

c:\users\your_user_name\.android is the path to debug keystore.

How to Get Current Location Using Google Location Services?

Google Location services can be used to get users current location. Using this  tutorial provied by google developer, I tried to get user’s location.

But in real device I found that the function below is not getting called.

// Define the callback method that receives location updates
@Override
public void onLocationChanged(Location location) {
// Report to the UI that the location was updated
String msg = "Updated Location: " +
Double.toString(location.getLatitude()) + "," +
Double.toString(location.getLongitude());
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

To get the user’s current location we need to changed the settings of the device. Settings-> Location  Services ->Enable Google’s Location Service. Now we will easily get user’s location data.

How to show a part of your webpage in android webview?

My webpage

The  image above is the sample webpage.I want show only the 2nd <div> labeled with “Div visible in my  Webview”. Here is  the code for for it.


package com.tanzeer.webview;

import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

private WebView webView;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 String primeDiv="div2";

webView = (WebView) findViewById(R.id.webView_act_viewer);
 webView.getSettings().setJavaScriptEnabled(false);
 //new thread
 new LoadData().execute();
 }
 private class  LoadData extends AsyncTask<Void,Void,Void>
 {
 String primeDiv="div2";
 String html=new String();
 Document doc = null;
 @Override
 protected Void doInBackground(Void... params) {

try {

doc = Jsoup.connect("http://10.0.2.2/foodbookdhaka/webview.html").timeout(100000).get();
 } catch (IOException e) {
 e.printStackTrace();
 }
 //get total document

Elements alldivs=doc.select("div");
 ArrayList<String> list=new ArrayList<String>();

for(org.jsoup.nodes.Element e: alldivs)
 {
 if(!e.id().equals(""))
 list.add(e.id());
 }
 //removing all <div> without "div2"
 for(int i=0;i<list.size();i++)
 {
 if(!list.get(i).equals(primeDiv))
 doc.select("div[id="+list.get(i)+"]").remove();
 }

html=alldivs.outerHtml();

return null;
 }
 @Override
 protected void onPostExecute(Void result) {

super.onPostExecute(result);
 webView.loadDataWithBaseURL(null,doc.html(),
 "text/html", "utf-8", null);

}
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);
 return true;
 }

}

Here is the html code(webview.html):

<!DOCTYPE html>
 <html>
 <head>
 <title></title>
 <style type="text/css">

#para0
 {
 text-align:center;
 color:#333333;
 margin:10px 20%;
 }
 #para1
 {
 text-align:center;
 color:#555555;
 margin:10px 20%;
 }

#para2
 {
 text-align:center;
 color:green;
 margin:10px 20%;
 }

#para3
 {
 text-align:center;
 color:#555555;
 margin:10px 20%;
 }
 </style>
 </head>
 <body style="text-align:center; width: 80%">
 <div id="div0" p="">Among these  DIV; I want to show only one DIV to Android Webview
 <p>&nbsp;</p>
 </div>

<div id="div1" style="font-family:verdana;padding:20px;border-radius:10px;border:5px solid #444444;background-color:#cccccc">
 <h3>  Div  Not Visible in Webview </h3>
 </div>

<div id="div2" style="font-family:verdana;padding:20px;border-radius:10px;border:5px solid #444444;background-color:#cccccc">
 <h3>  Div Visible to Webview </h3>

<div style="color:#0000ff;">Including the border style...</div>
 </div>

<div id="div3" style="font-family:verdana;padding:20px;border-radius:10px;border:5px solid #444444;background-color:#cccccc">
 <h3> Div  Not Visible in Webview</h3>
 </div>
 </body>
 </html>

Here is the output:

Output in webview
Output in webview

Here is the xml file(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/linearLayout_act_viewer"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"

android:fillViewport="true"
 android:scrollbars="vertical" >

<WebView
 android:id="@+id/webView_act_viewer"
 android:layout_width="fill_parent"
 android:layout_height="0dp"
 android:layout_weight="1"/>

</LinearLayout>