Sunday 29 December 2013

Custom SeekBar for lower versions


Custom SeekBar with the holo theme for lower versions of android


Hello Coders,
                    Today I am sharing the tutorial for custom seek bar (see snapshot) for lower versions(>2.2) with some small steps .
  • Firstly copy the below images into your drawable folder:
            save these images and rename it with the following name:
          red_scrubber_control_disabled_holo.png

         red_scrubber_control_focused_holo.png

          red_scrubber_control_normal_holo.png


          red_scrubber_control_pressed_holo.png


              red_scrubber_primary_holo.9.png

              red_scrubber_secondary_holo.9

              red_scrubber_track_holo_light.9

  • Now make an xml file in drawable folder named: red_scrubber_control.xml and then copy the below code in that layout:

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

    <item android:drawable="@drawable/red_scrubber_control_disabled_holo" android:state_enabled="false"/>
    <item android:drawable="@drawable/red_scrubber_control_pressed_holo" android:state_pressed="true"/>
    <item android:drawable="@drawable/red_scrubber_control_focused_holo" android:state_selected="true"/>
    <item android:drawable="@drawable/red_scrubber_control_normal_holo"/>
</selector>

  • Again make another xml file in drawable folder named: red_scrubber_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@android:id/background"
        android:drawable="@drawable/red_scrubber_track_holo_light"/>
    <item android:id="@android:id/secondaryProgress">
        <scale
            android:drawable="@drawable/red_scrubber_secondary_holo"
            android:scaleWidth="100%" />
    </item>
    <item android:id="@android:id/progress">
        <scale
            android:drawable="@drawable/red_scrubber_primary_holo"
            android:scaleWidth="100%" />
    </item>

</layer-list>

  • Next step is in the "layout" folder in activity_main.xml, use seek bar like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@android:color/black" >
    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
     android:layout_centerVertical="true"
        android:layout_margin="10dp"
        android:progressDrawable="@drawable/red_scrubber_progress"
    android:thumb="@drawable/red_scrubber_control"
        android:layout_marginBottom="18dp" />

</RelativeLayout>

  • Now run the project to see the seek bar with holo theme.







Custom ListView with multiple click in widgets with holder


Hello Friends,
                     Today I am sharing new tutorial for clicking multiple widgets on a single list view item.


  • Create a new project in the activity_main.xml,copy the below code:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/bg" >

    <ListView

        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
     android:cacheColorHint="#00000000"
     android:divider="@android:color/transparent"
       android:listSelector="@android:color/transparent" >
    </ListView>

</RelativeLayout>


  • Now create a new xml layout named : row_layout.xml. And copy the below code in that:

<?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" >
    
<RelativeLayout
    android:id="@+id/relative_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="5dp"
        android:background="@drawable/border_background" >
        <TextView
            android:id="@+id/textproduct_editdeals"
            android:layout_width="match_parent"
            android:maxLines="2"
            android:layout_height="wrap_content"
             android:layout_marginRight="80dp"
          android:layout_centerVertical="true"
            android:text="Medium Text"
            android:textSize="15dp"
            android:textColor="@android:color/black"
            android:layout_marginLeft="5dp"
            android:textAppearance="?android:attr/textAppearanceMedium" />

          <Button
             android:id="@+id/imagedelete_editdeals"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
         android:layout_alignParentRight="true"
              android:layout_marginRight="10dp"
              android:layout_marginLeft="5dp"
              android:layout_centerVertical="true"
            android:background="@drawable/delete"
             
            />
          
          <Button
             android:id="@+id/imageedit_editdeals"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
          android:layout_toLeftOf="@+id/imagedelete_editdeals"
              android:layout_marginRight="10dp"
              android:layout_marginLeft="5dp"
              android:layout_centerVertical="true"
            android:background="@drawable/edit"
            
            />
    </RelativeLayout>
</LinearLayout>
  • Then move to the src folder in that make an adapter named CustomListAdapter.java ,in that class follow the below code:
package com.example.customlistviewblog;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CustomListAdapter extends BaseAdapter{
Context mContext;
String[] mstrArray =  new String[5];
public CustomListAdapter(Context applicationContext, String[] strArray) {
this.mContext = applicationContext;
this.mstrArray = strArray;
}
 static class ViewHolder {
   public TextView text;
   public Button btn_view,btn_edit,btn_delete;
   public RelativeLayout layout_parent;
 
 }

@Override
public int getCount() {
// TODO Auto-generated method stub
return mstrArray.length;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mstrArray[arg0];
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
ViewHolder holder = null;
       LayoutInflater mInflater = (LayoutInflater) mContext
               .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
       if (convertView == null) {
           convertView = mInflater.inflate(R.layout.row_layout, null);
           holder = new ViewHolder();
           holder.layout_parent = (RelativeLayout)convertView.findViewById(R.id.relative_layout);
           holder.text = (TextView) convertView.findViewById(R.id.textproduct_editdeals);
         holder.btn_delete = (Button)convertView.findViewById(R.id.imagedelete_editdeals);
         holder.btn_edit = (Button)convertView.findViewById(R.id.imageedit_editdeals);
        
    convertView.setTag(holder);
   }
       
       else
           holder = (ViewHolder) convertView.getTag();
 
       holder.text.setText(mstrArray[position]);
       
       holder.layout_parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"Layout>"+position,Toast.LENGTH_LONG).show();
}
});
       holder.btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(mContext,"Delete>"+position,Toast.LENGTH_LONG).show();
}
});

 holder.btn_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(mContext,"Edit>"+position,Toast.LENGTH_LONG).show();
}
});
  return convertView;
}
}

  • Next step is to move to the MainActivity.java, write the following code:
package com.example.customlistviewblog;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {

ListView list;
CustomListAdapter adapter;
String[] strArray =  new String[]{"Product 0","Product 1","Product 2","Product 3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.listView1);
adapter =  new CustomListAdapter(getApplicationContext(),strArray);
list.setAdapter(adapter);
}
}

  • Now run the project.

Advanced Kotlin Coroutines : Introduction

 Hi,  Today I am unwraping the topic in Kotin world i.e. Coroutine . If you want to get started with Kotlin coroutine and ease your daily de...