Sunday, April 28, 2013

Using String as ID to find the control in android.

If you have a string and you want to find the control using it, you can use the folloeing code.


Resources res = getResources();
int id = res.getIdentifier("titleText", "id", getContext().getPackageName());

How to finish all the activities in android application stack?

Android maintains activity stack for all the activities running on android device. If you want to perform logout like functionality, or want to finish all the activity of your application write the following statements, 

Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);



How to Change Background Color of Tab Control in android?

In Android, you can change Tab Control's background color. You can specify different color for selected and non selected tabs.

To set default color for all tabs of tab view and make first child selected, write the following code in "onCreate" method of activity class,


for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(color.Blue);
}
tabHost.getTabWidget().setCurrentTab(1);
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.White);

To change the tab color on selection write the following code in "onTabChange" method.

@Override
public void onTabChanged(String tabId) {


for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(color.Blue));
}

tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(color.White);

}

Wednesday, April 10, 2013

how can use Checkbox with ListView?



activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
   <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:gravity="center"
     android:orientation="vertical" >
     <Button
       android:id="@+id/button1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:onClick="click"
       android:text="getCount" />
   </LinearLayout>
   <ListView
     android:id="@+id/listView1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" >
   </ListView>
 </LinearLayout>

row_photo.xml


<?xml version="1.0" encoding="UTF-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >
   <TextView
     android:id="@+id/thumbImage"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="5dp"
     android:layout_alignParentLeft="true"
     android:text="sample text"
     android:textSize="12dp"
     android:textAppearance="?android:attr/textAppearanceMedium" />
   <CheckBox
     android:id="@+id/itemCheckBox"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentRight="true" />

   <TextView
       android:id="@+id/txtindexid"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBaseline="@+id/itemCheckBox"
       android:layout_alignBottom="@+id/itemCheckBox"
       android:layout_toRightOf="@+id/thumbImage"
       android:text="-1" />
   
 </RelativeLayout>


NameBean.java


public class NameBean {

private String name;
private String id;

public NameBean(){}

public NameBean(String name, String id) {
this.name = name;
this.id = id;
}

@Override
public String toString() {
return getId();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
}
MainActivity.java


public class MainActivity extends Activity {
private ListView listview;
     ArrayList<NameBean> items = new ArrayList<NameBean>();
     private int count;
     private boolean[] thumbnailsselection;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          fillarray();
          count = items.size();
          thumbnailsselection = new boolean[count];
          listview = (ListView) findViewById(R.id.listView1);
          listview.setAdapter(new ImageAdapter(MainActivity.this));
     }
     private void fillarray() {
          // TODO Auto-generated method stub
          items.clear();
          items.add(new NameBean("Android alpha1","1"));
          items.add(new NameBean("Android alpha2","2"));
          items.add(new NameBean("Android alpha3","3"));
          items.add(new NameBean("Android alpha4","4"));
          items.add(new NameBean("Android alpha5","5"));
          items.add(new NameBean("Android alpha6","6"));
          items.add(new NameBean("Android alpha7","7"));
          items.add(new NameBean("Android alpha8","8"));
           
     }
     @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;
     }
     public class ImageAdapter extends BaseAdapter {
          private LayoutInflater mInflater;
          private Context mContext;
          public ImageAdapter(Context context) {
               mContext = context;
          }
          public int getCount() {
               return count;
          }
          public Object getItem(int position) {
               return position;
          }
          public long getItemId(int position) {
               return position;
          }
          public View getView(int position, View convertView, ViewGroup parent) {
               ViewHolder holder;
               if (convertView == null) {
                    holder = new ViewHolder();
                    convertView = LayoutInflater.from(mContext).inflate(
                              R.layout.row_photo, null);
                    holder.textview = (TextView) convertView
                              .findViewById(R.id.thumbImage);
                   
                    holder.textIndexId = (TextView) convertView
                            .findViewById(R.id.txtindexid);
                   
                    holder.checkbox = (CheckBox) convertView
                              .findViewById(R.id.itemCheckBox);
                   
                    convertView.setTag(holder);
               } else {
                    holder = (ViewHolder) convertView.getTag();
               }
               holder.checkbox.setId(position);
               holder.textview.setId(position);
               holder.textIndexId.setId(position);
               holder.checkbox.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                         // TODO Auto-generated method stub
                         CheckBox cb = (CheckBox) v;
                         int id = cb.getId();
                         if (thumbnailsselection[id]) {
                              cb.setChecked(false);
                              thumbnailsselection[id] = false;
                         } else {
                              cb.setChecked(true);
                              thumbnailsselection[id] = true;
                         }
                    }
               });
               holder.textview.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                         // TODO Auto-generated method stub
                         int id = v.getId();
                    }
               });
               holder.textview.setText(items.get(position).getName());
               holder.textIndexId.setText(items.get(position).getId());
               holder.checkbox.setChecked(thumbnailsselection[position]);
               holder.id = position;
               return convertView;
          }
     }
     class ViewHolder {
          TextView textview , textIndexId;
          CheckBox checkbox;
          int id;
     }
     public void click(View v) {
          if (v.getId() == R.id.button1) {
               final ArrayList<NameBean> posSel = new ArrayList<NameBean>();
               posSel.clear();
               boolean noSelect = false;
               for (int i = 0; i < thumbnailsselection.length; i++) {
                    if (thumbnailsselection[i] == true) {
                         noSelect = true;
                         Log.e("sel pos thu-->", "" + i);
                         posSel.add(items.get(i));
                         // break;
                    }
               }
               if (!noSelect) {
                    Toast.makeText(MainActivity.this, "Please Select Item!",
                              Toast.LENGTH_SHORT).show();
               } else {
                    Toast.makeText(MainActivity.this,
                              "Selected Items:" + posSel.toString(),
                              Toast.LENGTH_LONG).show();
               }
          }
     }
}







Thursday, April 4, 2013

how to programmatically hide a button in android sdk?

Button b1=(Button)findViewById.(R.id.Button);

b1.setVisiblity(View.INVISIBLE);

b1.setVisiblity(View.VISIBLE);



Wednesday, April 3, 2013

DP - Pixel - DP




public static float convertDpToPixel(float dp,Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi/160f);
    return px;
}



public static float convertPixelsToDp(float px,Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return dp;

}


Monday, April 1, 2013

How disable / remove android activity Title bar?


<activity android:name=".Activity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>