Payday Loan Payday Loan

Archive for the ‘Widgets’ Category

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! |||||

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! |||||

Hi,
For making an gradient background you need to write the following xml called background_gradient.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient
            android:startColor="#E9E9E9"
            android:endColor="#F4F4F4"
            android:angle="90"/>
</shape>

This code you must put it in drawable folder…and call it like this…
let s say you want a background gradient color for your button.

button.setBackgroundResource(R.drawable.background_gradient);
||||| 0 I Like It! |||||

Hi,
I want to show you how you can make a RotateAnimation in android. This rotate animation it will be around the self center.

ImageView refresh = (ImageView)view.findViewById(R.id.refreshimage);
            RotateAnimation anim = new RotateAnimation(0f, 360f,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            anim.setInterpolator(new LinearInterpolator());
            anim.setRepeatCount(Animation.INFINITE);       
            anim.setDuration(1000);
            refresh.startAnimation(anim);
||||| 0 I Like It! |||||

How to animate a view in android

Hi,
This is a very simple and basic animation of a view. Let’s say you have a button and on clicking the button, this will start moving. Also you can move any other views from your layout, but this example shows how to animate this clicked button:

mButton.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View view)
    {
        TranslateAnimation animate = new TranslateAnimation(0.0f, 1.0f, 0.0f, 1.0f);
        view.startAnimation(animate);
    }
});
||||| 0 I Like It! |||||

Search

Popular

Sponsors