Saturday 29 September 2012

android: How to make first letter capital in String

String s = "hello";
Integer x = s.length();
String s2 = s.substring(0,1).toUpperCase().concat(s.substring(1, x));

Wednesday 26 September 2012

android: Alert Dialog with two buttons


AlertDialog.Builder builder2=new AlertDialog.Builder(MyAlertdialogActivity.this);
  builder2.setMessage("Do you want to continue");
  builder2.setPositiveButton("Yes",new DialogInterface.OnClickListener() {

  @Override
   public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

  Toast.makeText(getApplicationContext(), "U Clicked OK", Toast.LENGTH_LONG).show();

  }

  });

  builder2.setNegativeButton("No", new DialogInterface.OnClickListener() {

@Override
  
  public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

  Toast.makeText(getApplicationContext(), "U Clicked Cancel ", Toast.LENGTH_LONG).show();

  }

  });

  builder2.show();

android: AutoCompleteTextView with addTextChangedListener


 public class CountriesActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.countries);

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_dropdown_item_1line, COUNTRIES);
         AutoCompleteTextView textView = (AutoCompleteTextView)
                 findViewById(R.id.countries_list);
         textView.setAdapter(adapter); 
textView.addTextChangedListener(new TextWatcher() {
    
 @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
 if(adapter.getCount() == 0) {
Toast.makeText(getApplicationContext(), "Adapter : 0", Toast.LENGTH_SHORT).show();
    }
     
  }
    
 @Override
public void beforeTextChanged(CharSequence s, int start, int count,
      int after) {
// TODO Auto-generated method stub
     
    }
    
   
@Override
public void afterTextChanged(Editable s) {
     
    }
   });
     }

     private static final String[] COUNTRIES = new String[] {
         "Belgium", "France", "Italy", "Germany", "Spain"
     };
 }

Monday 24 September 2012

android: Add params to already existing View in the XML


 CheckBox cb12=(CheckBox)child.findViewById(R.id.subtext);
 RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)cb12.getLayoutParams();
  params.setMargins(37, 0, 0, 0);
 cb12.setLayoutParams(params);

Sunday 23 September 2012

android: Splash screen


Splash.java

public class Splash extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.splashscreen,
(ViewGroup) findViewById(R.id.splash));
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();


new Handler().post(new Runnable() {
@Override
public void run() {
Splash.this.startActivity(new Intent(Splash.this,
MainActivity.class));
Splash.this.finish();
}
});


}
}

splashscreen.xml 


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/splash"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="@+id/splashscreen"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/splashbig" android:scaleType="fitXY"
android:layout_gravity="center" />
</FrameLayout>

android: Dynamic Menus with out XML


private static final int MENU_EDITLIST = 0;
private static final int MENU_HELP = 1;
private static final int MENU_ABOUT = 2;


public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_EDITLIST, 0, "Edit List").setIcon(
R.drawable.ic_launcher);
menu.add(0, MENU_HELP, 0, "Help").setIcon(R.drawable.ic_launcher);
menu.add(0, MENU_ABOUT, 0, "About us").setIcon(R.drawable.ic_launcher);
return true;
}


public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case MENU_EDITLIST:
Intent intent1 = new Intent(this, Help.class);
startActivity(intent1);
return true;
case MENU_HELP:
Intent intent1 = new Intent(this, Help.class);
startActivity(intent1);
return true;
case MENU_ABOUT:
Intent intent2 = new Intent(this, About.class);
startActivity(intent2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}


android: Capture the BACK button keypress from the emulator


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent intent1 = new Intent(this, MainActivity.class);
startActivity(intent1);
finish();
}
return super.onKeyDown(keyCode, event);
}

Thursday 20 September 2012

android: Listview -set visibility of checkboxes in all the listitems


ListView list=getListView();
int count = list.getChildCount();
for (int i = 0; i < count; i++) {
   View child = list.getChildAt(i);
   CheckBox cb11=(CheckBox)child.findViewById(R.id.checkBox1);
  cb11.setVisibility(View.VISIBLE);
}

Friday 14 September 2012

android: Custom Alert Dialog





layout/custom_dialog.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:background="@drawable/alertstyle"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="List Name"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#FFFFFF" />
    
     <EditText
        android:id="@+id/text"
        android:hint="Enter List Name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" />

    <Button
        android:id="@+id/restart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" 
        android:layout_gravity="center_horizontal"/>

</LinearLayout>

HomeActivity

package com.kubical.customalert.app;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class HomeActivity extends Activity {
private static final int DIALOG_GAME_RESTART = 0;
Dialog dialog;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ((Button) findViewById(R.id.button1))
.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
showDialog(DIALOG_GAME_RESTART);
}
});
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
       switch (id) {
       case DIALOG_GAME_RESTART:
         Context context=HomeActivity.this;
         dialog=new Dialog(context);
         dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
         dialog.setContentView(R.layout.custom_dialog);
         dialog.setTitle("hai");
         
         Button restart=(Button)dialog.findViewById(R.id.restart);

         restart.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
            dialog.dismiss();

            //whatever code you want to execute on restart
         }
         });
       break;

       default: break;
       }
       return dialog;
    }
}



drawable/alertstyle.xml


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient android:angle="90" android:endColor="#88a912" android:startColor="#88a912" />

</shape>



android: Listview with radio button (only one radio button selected when we click on listview Item)



main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
   
        <ListView
            android:id="@+id/lv1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:cacheColorHint="#f8f8ff"
            android:divider="#000000"
            android:fadeScrollbars="true"
            android:choiceMode="singleChoice"
            android:fastScrollEnabled="true"
            android:footerDividersEnabled="true"
            android:textFilterEnabled="true"
            android:textStyle="normal" />
       
        <Button  android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="get"
            android:id="@+id/Button01" />
   

</LinearLayout>


list_single_check.xml 


?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativeLayout1"
     android:layout_width="fill_parent" android:layout_height="wrap_content"  
     xmlns:android="http://schemas.android.com/apk/res/android">
   
     <LinearLayout android:layout_centerVertical="true"
         android:layout_width="fill_parent" android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:orientation="vertical" android:gravity="center_vertical">
       
         <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content" android:id="@+id/tv_MainText"
              android:textAppearance="?android:attr/textAppearanceMedium" />
         <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content" android:id="@+id/tv_SubText"
              android:textAppearance="?android:attr/textAppearanceSmall" />
     </LinearLayout>

     <RadioButton android:layout_height="wrap_content"
         android:id="@+id/rb_Choice" android:layout_width="wrap_content"  android:layout_centerVertical="true"
         android:layout_alignParentRight="true" android:gravity="center_vertical"
         android:focusable="false"  android:clickable="false"
        ></RadioButton>

HomeActivity

public class HomeActivity extends Activity {
HomeActivity m_this;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
  
    m_this = this;
    
    //begin init test data
    final ArrayList<HashMap<String, Object>> m_data = new ArrayList<HashMap<String, Object>>();
    
    HashMap<String, Object> map1 = new HashMap<String, Object>();
    map1.put("maintext", "Large text 1");
    map1.put("subtext", "small text 1A");        
    m_data.add(map1);
    
    HashMap<String, Object> map2 = new HashMap<String, Object>();
    map2.put("maintext", "Large text 2");// no small text of this item!
    m_data.add(map2);
    
    HashMap<String, Object> map3 = new HashMap<String, Object>();
    map3.put("maintext", "Large text 3");
    map3.put("subtext", "small text 3");
    m_data.add(map3);
    
    for (HashMap<String, Object> m :m_data) //make data of this view should not be null (hide )
m.put("checked", false);
  //end init data
    
    
    final ListView lv = (ListView) m_this.findViewById(R.id.lv1);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    
    
    final SimpleAdapter adapter = new SimpleAdapter(m_this,
    m_data,
            R.layout.listitem_single_check,
            new String[] {"maintext", "subtext", "checked"}, 
            new int[] {R.id.tv_MainText, R.id.tv_SubText, R.id.rb_Choice}); 
    
    adapter.setViewBinder(new ViewBinder()
{
    public boolean setViewValue(View view, Object data, String textRepresentation)
    {
if (data == null) //if 2nd line text is null, its textview should be hidden 
{
view.setVisibility(View.GONE);
return true;
}
view.setVisibility(View.VISIBLE);
return false;
}

});
    

    // Bind to our new adapter.
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
RadioButton rb = (RadioButton) v.findViewById(R.id.rb_Choice);
if (!rb.isChecked()) //OFF->ON
{
for (HashMap<String, Object> m :m_data) //clean previous selected
m.put("checked", false);
m_data.get(arg2).put("checked", true);
adapter.notifyDataSetChanged();
}
}        
});
    
    //show result
    ((Button)m_this.findViewById(R.id.Button01)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int r = -1;
for (int i = 0; i < m_data.size(); i++) //clean previous selected
{
HashMap<String, Object> m = m_data.get(i);
Boolean x = (Boolean) m.get("checked");
if (x == true)
{
r = i;
break; //break, since it's a single choice list
}
}
new AlertDialog.Builder(m_this).setMessage("you selected:"+r).show();
}
});
    
}  
}



Wednesday 5 September 2012

android: ListVew with Radio button setOnCheckedChangeListener() with setViewBinder()



SimpleAdatper adapter;

adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, final Object data, String textRepresentation){
                if (view.getId() == R.id.langtext) {
                    RadioButton b=(RadioButton) view;
                    b.setText((String)data);
                    b.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton cb,
boolean isChecked) {
if (cb.isChecked()) {

Toast.makeText(
EditItemActivity.this,
"checked "
+ (String)data,
Toast.LENGTH_SHORT).show();

}
}
});
                    return true;
               }
                return false;
            }
}
);

Tuesday 4 September 2012

android: Hasp Map With String Array Grouped by category in android?


ListView lv1 = (ListView) findViewById(R.id.elist);

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
for (int k = 0; k < rowe.length; k++) {
map = new HashMap<String, String>();
map.put("ENG", rowe[k]);
map.put("LNG", "English");
mylist.add(map);
}
for (int k = 0; k < rowt.length; k++) {
map = new HashMap<String, String>();
map.put("ENG", rowt[k]);
map.put("LNG", "Telugu");
mylist.add(map);
}
for (int k = 0; k < rowh.length; k++) {
map = new HashMap<String, String>();
map.put("ENG", rowh[k]);
map.put("LNG", "Hindi");
mylist.add(map);
}


SimpleAdapter mSchedule = new SimpleAdapter(this, mylist,
R.layout.langlistview, new String[] { "ENG",
"LNG" }, new int[] { R.id.langtext,
R.id.textlang});
lv1.setAdapter(mSchedule);


android: How use HashMap in ArrayList in android?


ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map = new HashMap<String, String>();
map.put("ENG", "Name");
map.put("LNG", "language");
mylist.add(map);