Payday Loan Payday Loan

Author Archive

Hi,
in this post i want to show you the simplest method to get the current battery level of the battery in Android.
Implement the following snippet:

public void onCreate(Bundle savedInstanceState) {
    ...
    this.registerReceiver(this.myBatteryReceiver , new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    ...
}
 
private BroadcastReceiver myBatteryReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        //here you can use level as you wish.
    }
};
||||| 0 I Like It! |||||

Hi,
Today i want to show you something very interesting in Android. I see that on the internet there are no tutorials for helping you to implement from A – Z your own listener.
Let me give an use case.
So you have two classes. One extends Activity in which you have a Button, and in the second one you need to listen to events, in our case you will listen to the event that a click was done in some Activity.
You will need to implement an interface and a singleton class like this:

 
public interface EventListener
{
	public void onEventHappened();
}

This interface must be in a separate file (Interface)called EventListener
Now the class:

 
public class MyEvent
{
	private EventListener listener;
	private static MyEvent instance;
 
	private MyEvent(){
 
	}
	public static MyEvent getInsance(){
		if (instance == null)
			instance = new MyEvent();
		return instance;
	}
	public void setEventListener(EventListener listen)
	{		
		listener = listen;
	}
 
	public void eventHappened()
	{		
		if (listener != null)
		{
			listener.onEventHappened();
		}
	}
}

Now that you have those two classes copied in your project let me tell you how to use them.
So in the Activity where you have the Button in onClick method you put the code:

MyEvent.getInstance().eventHappened();

Now let s say that in any of your other classes you want to be notified about this event.
You will put in your other class the following code:

MyEvent.getInsance().setEventListener(new EventListener()
		{
 
			@Override
			public void onEventHappened()
			{
                            //here is your code that will happen when the button from the other Activity was pressed
                        }
                 });
||||| 0 I Like It! |||||

Hi,
Below i will show you a method to delete all cache saved by your app. You will clear shared preferences, files and folders from data/data/ folder, databases, and all you got in cache.
This is the method:

public void clearApplicationData()
	{
		File cache = getCacheDir();
		File appDir = new File(cache.getParent());
		if (appDir.exists())
		{
			String[] children = appDir.list();
			for (String s : children)
			{
				if (!s.equals("lib"))
				{
					deleteDir(new File(appDir, s));
					Log.i("TAG", "File /data/data/APP_PACKAGE/" + s + " DELETED ");
				}
			}
		}
	}
||||| 0 I Like It! |||||

Hi, this is a simple and efficient working example of how you can know when device received a message.
So in your launch activity put the code:

registerReceiver(new BroadcastReceiver() {
	        @Override
	        public void onReceive(Context context, Intent intent) {
	           Log.d("MyTag", "You have received a message");
	        }
	    }, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));

And in your manifest put the permission:

<uses-permission android:name="android.permission.RECEIVE_SMS" >
||||| 0 I Like It! |||||

In your element:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2) In your element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):

<receiver android:name="com.example.MyBroadcastReceiver">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver>
public class MyBroadcastreceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, MyService.class);
        context.startService(startServiceIntent);
    }
}
||||| 0 I Like It! |||||

Hi,
I want to show you a method that can help you to show the three dots(Menu button) on Action Bar.
This button is not shown on the ActionBar if the device HAS ALREADY A MENU BUTTON.
To overcome this please call in your onCreate() method the following method:

private void getOverflowMenu() {
 
     try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if(menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
||||| 0 I Like It! |||||

Hi, i want to show you how you can add implement a simple GridView to your app.
There is a widget in Android called GridView. You will need just to add it in to your xml and create an adapter like on a ListView and things will work. Please look on example below:

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView1"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="100dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
</GridView>

res/layout/custom_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >
 
    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="50px"
        android:layout_height="50px"
        android:layout_marginRight="10px"
        android:src="@drawable/android_logo" >
    </ImageView>
 
    <TextView
        android:id="@+id/grid_item_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:layout_marginTop="5px"
        android:textSize="15px" >
    </TextView>
 
</LinearLayout>

You must put in drawable folder one icon called android_logo.png
Now the CustomAdapter class:

package com.myapp.android;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
import com.mkyong.android.R;
 
public class ImageAdapter extends BaseAdapter {
	private Context context;
	private final String[] mobileValues;
 
	public ImageAdapter(Context context, String[] mobileValues) {
		this.context = context;
		this.mobileValues = mobileValues;
	}
 
	public View getView(int position, View convertView, ViewGroup parent) {
 
		LayoutInflater inflater = (LayoutInflater) context
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
		View gridView;
 
		if (convertView == null) {
 
			gridView = new View(context);
 
			// get layout from custom_adapter.xml
			gridView = inflater.inflate(R.layout.custom_adapter, null);
 
			// set value into textview
			TextView textView = (TextView) gridView
					.findViewById(R.id.grid_item_label);
			textView.setText(mobileValues[position]);
 
			// set image based on selected text
			ImageView imageView = (ImageView) gridView
					.findViewById(R.id.grid_item_image);
 
			String mobile = mobileValues[position];
 //you will need to add this icons in your drawable folder
			if (mobile.equals("Windows")) {
				imageView.setImageResource(R.drawable.windows_logo);
			} else if (mobile.equals("iOS")) {
				imageView.setImageResource(R.drawable.ios_logo);
			} else if (mobile.equals("Blackberry")) {
				imageView.setImageResource(R.drawable.blackberry_logo);
			} else {
				imageView.setImageResource(R.drawable.android_logo);
			}
 
		} else {
			gridView = (View) convertView;
		}
 
		return gridView;
	}
 
	@Override
	public int getCount() {
		return mobileValues.length;
	}
 
	@Override
	public Object getItem(int position) {
		return null;
	}
 
	@Override
	public long getItemId(int position) {
		return 0;
	}
 
}
package com.myapp.android;
 
import com.myapp.android.ImageAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
 
public class GridViewActivity extends Activity {
 
	GridView gridView;
 
	static final String[] MOBILE_OS = new String[] { 
		"Android", "iOS","Windows", "Blackberry" };
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
 
		gridView = (GridView) findViewById(R.id.gridView1);
 
		gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));
 
		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				Toast.makeText(
				   getApplicationContext(),
				   ((TextView) v.findViewById(R.id.grid_item_label))
				   .getText(), Toast.LENGTH_SHORT).show();
 
			}
		});
 
	}
 
}

How it looks:
gridview android

||||| 0 I Like It! |||||

Hi,
After the API 3.0 has appeared, now we need to handle a new Theme: Halo Theme.
But if you want to use Halo theme you must have minSDK Version 11.
But there is a way to have minSDKVersion<11, like 7 or 8 or whatever you want.
You must create your style so that you can extend the Halo Theme or Normal Theme in function of device API.

You need to create beside res/values/ folder , other 2 folders called values-v11 and values-v14 like in image:

After creating these two folders please modify the styles.xml file in the values-v11 and values-v14 folder…
So styles.xml in res/values folder will look like this:

<resources>
    <style name="Theme.White" parent="@android:style/Theme.Light">
    </style>
</resources>

styles.xml in res/values-v11 folder will look like this:

<resources>
    <style name="Theme.White" parent="@android:style/Theme.Holo.Light">
    </style>
</resources>

styles.xml in res/values-v14 folder will look like this:

<resources>
    <style name="Theme.White" parent="@android:style/Theme.Holo.Light">
    </style>
</resources>

As you can saw all the styles have the same name,but extends a diferent parent in function of device API VERSION.
Now to set universal theme for your app write in your Manifest file

 <application android:label="@string/app_name">
        <activity android:name=".MyActivity"
                  android:theme="@style/Theme.White"
                  android:label="@string/app_name">
        </activity>

Also for Halo theme to work, do not forget to targetSdkVersion=14.
and minSdkVersion can be whatever.

||||| 1 I Like It! |||||

How to resize a drawable in Android

Hi, this is a snippet with how you can really resize a Drawable.

 Drawable drawable = getResources().getDrawable(android.R.drawable.ic_search_category_default);
 
                    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                    Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 30, 30, true));
||||| 0 I Like It! |||||

How to make a layout animation in android

Hi,
I want to show you how to animate a layout in android.
Let’s say you want to animate a group of views and not only a view. I will give you example a LinearLayout.
For referring to a LinearLayout you need to have an id for it in xml file like this:

<LinearLayout   android:id="@+id/myLayoutId"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
</LinearLayout>

Then you will search for this layout in your java code and apply TransitionAnimation:

onCreate(){
toolbarWidget = (LinearLayout)findViewById(R.id.toolbarWidget);
toolbarWidget.startAnimation(inFromRightAnimation());
}
 private Animation inFromRightAnimation() {
 
        Animation inFromRight = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
                Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        inFromRight.setDuration(1000);
        inFromRight.setInterpolator(new AccelerateInterpolator());
        return inFromRight;
    }

Hope this is usefull for you.

||||| 1 I Like It! |||||

Search

Popular

Sponsors