Thursday, January 2, 2014

R.java files not generated in Android / r.java missing in android project in Android

Android will not generate the R.java after a clean if any of the XML files have errors in them. Check for any errors and fix them. R.java will then be built.
If that doesn't work, sometimes Eclipse looses track of the file. Click your project and hit F5 to Refresh the project.

if you run a clean on the project it should regenerate all the generated java files, namely R.
...and...

In Eclipse, under the Project menu, is an option build automatically. That would help you build the R.java file everytime modifications are made. The Clean... option is also there under Project.

Gridview in android / Gridview example in android / Grid layout in android

In res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp" <!-- coloumn width  -->
    android:numColumns="auto_fit" <!-- number of coloumn will be automatically -->
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"

/>

Open HelloGridView.java and insert the following code for the onCreate() method:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
}

ImageAdapter class which will feed the data to the gridview.

//here we have added a.jpg,b.jpg,c.jpg,d.jpg,e.jpg,f.jpg,g.jpg,h.jpg in the res/drawable folder

package com.example.test;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

private Context mContext;
private Integer[] mThumbIds = {
           R.drawable.a, R.drawable.b,
           R.drawable.c, R.drawable.d,
           R.drawable.e, R.drawable.f,
           R.drawable.g, R.drawable.h
   };

    public ImageAdapter(Context c) {
        mContext = c;
    }
@Override
public int getCount() {
// TODO Auto-generated method stub
        return mThumbIds.length;

}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
}

}

Wednesday, January 1, 2014

match_parent vs fill_parent android / what is the difference between match_parent and fill_parent property in android

FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)


fill_parent: The view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by match_parent.

how to remove title bar in android / How to hide the title bar for an Activity in XML / Remove Android App Title Bar


1. By XML

You need to modify the style.xml file
Inside values/styles.xml
<style name="Theme.Default" parent="@android:style/Theme"></style>
<style name="Theme.NoTitle" parent="@android:style/Theme.NoTitleBar"></style>
<style name="Theme.FullScreen" parent="@android:style/Theme.NoTitleBar.Fullscreen"></style>

Inside values-v11/styles.xml
<style name="Theme.Default" parent="@android:style/Theme.Holo"></style>
<style name="Theme.NoTitle" parent="@android:style/Theme.Holo.NoActionBar"></style>
<style name="Theme.FullScreen" parent="@android:style/Theme.Holo.NoActionBar.Fullscreen"></style>

Inside values-v14/styles.xml
<style name="Theme.Default" parent="@android:style/Theme.Holo.Light"></style>
<style name="Theme.NoTitle" parent="@android:style/Theme.Holo.Light.NoActionBar"></style>
<style name="Theme.FullScreen" parent="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"></style>

In the application tag of the Manifest file I have added an attribute of:

android:theme="@android:style/Theme.NoTitleBar"

2. By Coding

You can archive the same via coding. Here is a corresponding snippet
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // hide titlebar of application
    // must be before setting the layout
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // hide statusbar of Android
    // could also be done later
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);
    text = (EditText) findViewById(R.id.EditText01);


}