Free Android Tutorials, Android Tips, Android Developments, Free Android Codings., Free Android App Examples, Open Source Code for Android
Tuesday, 21 July 2015
Monday, 20 July 2015
Android ActionBar Tab Color
For anyone wants to change actionbar color/background in code, you can do something like this
final ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_bg));
To change the tab bar color under the actionbar:
actionBar.setStackedBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.color_brown_dark)));
To change tab bar background:
actionBar.setStackedBackgroundDrawable(getResources().getDrawable(
R.drawable.coupon_header));
GridView with Auto Resized Images on Android
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <GridView android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:verticalSpacing="0dp" android:horizontalSpacing="0dp" android:stretchMode="columnWidth" android:numColumns="2" /> </FrameLayout>
package com.rogcg.gridviewexample;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class SquareImageView extends ImageView
{
public SquareImageView(Context context)
{
super(context);
}
public SquareImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
Context context=getContext();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y-80;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
if(Config.orientation==1)
setMeasuredDimension(getMeasuredWidth(), height/3); //Snap to width
else
setMeasuredDimension(getMeasuredWidth(), height/2); //Snap to width }
}
And now we make a layout for a grid item using this SquareImageView class and set the scaleType to centerCrop
. Let's call this filegridview_item.xml
.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.rogcg.gridviewexample.SquareImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:layout_gravity="bottom"
android:textColor="@android:color/white"
android:background="#55000000"
/>
</FrameLayout>
package com.mafiree;
import java.util.ArrayList;
import java.util.List;
import com.mafiree.source.Config;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class DashBoard extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dash_board_demo);
Config.orientation=getScreenOrientation();
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(this));
//String[] name={"Current Health","Outstanding Issues","Architecture","Tickets","Backup","Activity"};
}
private class MyAdapter extends BaseAdapter
{
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item("Current Health", R.drawable.test));
items.add(new Item("Outstanding Issues", R.drawable.test));
items.add(new Item("Architecture", R.drawable.test));
items.add(new Item("Tickets", R.drawable.test));
items.add(new Item("Backup", R.drawable.test));
items.add(new Item("Activity", R.drawable.test));
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i)
{
return items.get(i);
}
@Override
public long getItemId(int i)
{
return items.get(i).drawableId;
}
@SuppressLint("NewApi")
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View v = view;
ImageView picture;
TextView name;
if(v == null)
{
v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
Item item = (Item)getItem(i);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
int orentation=getScreenOrientation();
System.out.println("orientation: "+getScreenOrientation());
// picture.getLayoutParams().height = 20;
// picture.requestLayout();
DashBoard.this.getResources().getConfiguration();
System.out.println();
android.view.ViewGroup.LayoutParams layoutParams = picture.getLayoutParams();
if(orentation==2){
layoutParams.width = width/3;
layoutParams.height = 50;
picture.setLayoutParams(layoutParams);
System.out.println("setting orientation");
}else{
System.out.println("nothing");
}
System.out.println("width :"+width);
System.out.println("height :"+height);
picture.setImageResource(item.drawableId);
name.setText(item.name);
picture.setId(i);
picture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "click-"+v.getId(), Toast.LENGTH_SHORT).show();
}
});
return v;
}
private class Item
{
final String name;
final int drawableId;
Item(String name, int drawableId)
{
this.name = name;
this.drawableId = drawableId;
}
}
}
public int getScreenOrientation()
{
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = Configuration.ORIENTATION_UNDEFINED;
if(getOrient.getWidth()==getOrient.getHeight()){
orientation = Configuration.ORIENTATION_SQUARE;
} else{
if(getOrient.getWidth() < getOrient.getHeight()){
orientation = Configuration.ORIENTATION_PORTRAIT;
}else {
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
return orientation;
}
}
Subscribe to:
Posts (Atom)