Friday, 12 June 2015

How to set custom ActionBar color / style?


I Have this Way :


You Can Do it Like Belove Way :

Method 1:

<resources>
    <style name="MyCustomTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
    </style>

    <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">ANY_HEX_COLOR_CODE</item>
    </style>
</resources>
Then set "MyCustomTheme" as the Theme of your Activity that contains the ActionBar.

Method 2:

You can also set a color for the ActionBar like this:
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.RED)); // set your desired color

How do I close a SearchView programmatically?


//Stuff With SearchView Widget

// One Method Used for Auto Searching or When cleared Text From SearchView

searchview.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
    @Override
     public boolean onQueryTextChange(String newQueryText)
    {
    Log.d("alert","onQueryTextChange : " + newQueryText);
           //Do stuff

           hideKeyboard();            // Call to close or Remove Focus of SearchView

           return true;
    }

   // Used When Submit Button of SearchView Clicked and Searching Begin
     @Override
     public boolean onQueryTextSubmit(String query)
    {
        Log.d("alert","onQueryTextSubmit : " + query);
       //Do Stuff

         hideKeyboard();            // Call to close or Remove Focus of SearchView
        return true;
     }
}

//This Method used To catch close Event of SearchView
searchview.setOnCloseListener(new OnCloseListener()
{
@Override
public boolean onClose()
{
Log.d("alert", "closed");
          // doStuff
          hideKeyboard();            // Call to close or Remove Focus of SearchView
             
              return true;
    }
}



/**
 *    Hide Soft KeyBoard When Click on crossIcon of SearchView 
 */

private boolean hideKeyboard()
{
try
{
if(searchview.hasFocus())
{
Log.d("alert", "hiding and removing focus");
searchview.clearFocus();

searchview.setIconified(true);
             //   used to again closed searchview  after clear text from click on cross button
}

InputMethodManager imm =
        (InputMethodManager)     myContext.getSystemService(Context.INPUT_METHOD_SERVICE); 

imm.hideSoftInputFromWindow(searchview.getWindowToken(), 0);

return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}

Wednesday, 10 June 2015

How to change color of ListView items on focus and on click.

In Custom ListItem Layout :


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/NevDrawerItemLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:layout_gravity="center_vertical">

//Do put all controls like textbox..images
</LinearLayout >

now Create Xml file in Drawable Folder :

list_background.xml

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

         <item android:state_focused="true" android:drawable="@color/list_background" />
        <!-- focused -->

         <item android:state_pressed="true" android:drawable="@color/list_background_pressed" />
        <!-- pressed -->

        <item android:drawable="@color/list_background" />
        <!-- default -->

</selector>

How to trigger onClickListener for HorizontalScrollView?


Method 1 :

 HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView);

    final GestureDetector detector = new GestureDetector(this, new OnGestureListener() {

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // Do stuff.
            return false;
        }

        // Note that there are more methods which will appear here 
        // (which you probably don't need).
    });


    scrollView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            detector.onTouchEvent(event);
            return false;
        }
    });

Method 2 :


HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView);

    scrollView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                // Do stuff
            }
            return false;
        }
    });

Monday, 8 June 2015

Create a circle bitmap in Android

Very often in today's modern applications, we can see a profile picture in a circle shape.
Today, we' ll create a method that takes square bitmap and later it returns circle shaped bitmap. 
What we'll do first is add some random image in our drawable folder. Let's call it simple_image.jpg. 
Next we'll create the activity_main.xml layout that will display the modified image and will be used in MainActivity. 
The activity_main.xml layout is very simple, it contains only ImageView.

ctivity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@android:color/darker_gray"
    tools:context="com.example.circlebitmaptest.MainActivity" >
    
    <ImageView 
        android:id="@+id/image"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:scaleType="fitXY"/>

</LinearLayout>

Now let's create the method that will create a circle shaped bitmap. 
As you can see the method receives a bitmap object as a parameter.We'll send that bitmap later from the onCreate method.

private Bitmap getCircleBitmap(Bitmap bitmap) {
 final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
  bitmap.getHeight(), Bitmap.Config.ARGB_8888);
 final Canvas canvas = new Canvas(output);

 final int color = Color.RED;
 final Paint paint = new Paint();
 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
 final RectF rectF = new RectF(rect);

 paint.setAntiAlias(true);
 canvas.drawARGB(0, 0, 0, 0);
 paint.setColor(color);
 canvas.drawOval(rectF, paint);

 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
 canvas.drawBitmap(bitmap, rect, rect, paint);

 bitmap.recycle();

 return output;
}

The last thing we need to do is to decode the image resource from our drawable folder into a bitmap object, find the ImageView that will be used to display the image, and add the rounded bitmap to the ImageView.We'll do all of this in the onCreate() method.

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 // create bitmap from resource
 Bitmap bm = BitmapFactory.decodeResource(getResources(),
  R.drawable.simple_image);

 // set circle bitmap
 ImageView mImage = (ImageView) findViewById(R.id.image);
 mImage.setImageBitmap(getCircleBitmap(bm));

}

    Friday, 5 June 2015

    ActionBarDrawerToggle ( three line ) Button not opening Drawer ???

    You`ll Have to Use :getActionBar().setDisplayShowHomeEnabled(true);

    // enabling action bar app icon and behaving it as toggle button  

     getActionBar().setDisplayHomeAsUpEnabled(true);   getActionBar().setHomeButtonEnabled(true);

    &&

    /**
     * This method required to enable icon to open drawer from click @hits
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
          // Pass the event to ActionBarDrawerToggle, if it returns
          // true, then it has handled the app icon touch event
          
          if (mDrawerToggle.onOptionsItemSelected(item))
          {
              return true;   // this line important
          }
            
           // Handle your other action bar items...
           return super.onOptionsItemSelected(item);
    }

    Is possible to increase the size of ActionBarDrawerToggle (Drawer menu)?

    When you construct a new ActionBarDrawerToggle one of the parameters is the drawerImageRes.

     If you want this resource to be larger, try editing this resource (usually R.drawable.ic_drawer) and increasing its size.

    I found the answer, I need research about Action Bar first.
    Add this :
    <style name="Theme.white_style" parent="@android:style/Theme.Holo.Light.DarkActionBar">
            <item name="android:actionBarSize">64dp</item>
            <item name="actionBarSize">64dp</item>
    </style> 
    It worked!
    p/s : According to Iconography, define height matched the specification for the action bar icons, which is 32 x 32 dp.
    mdpi - 32 dp = 32 px
    hdpi - 32 dp * 1.5 = 48 px
    xxhdpi - 32 dp * 2 = 64 px

    open the navigation Drawer when you click the home screen icon with the three line icon

    you need to explicitly define this action, it is not provided automatically by the nav drawer API. Add the following to your activity:


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            if(mDrawerLayout.isDrawerOpen(drawerList)) {
                mDrawerLayout.closeDrawer(drawerList);
            }
            else {
                mDrawerLayout.openDrawer(drawerList);
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }