• TekArt

    TekArt is an Organisation where people develop Android App through innovative ideas. App for the next Generation....

Tuesday, 8 April 2014

Posted by Unknown
No comments | 23:06
Hello guys, whats up?? Today in this blog we are going to see how to draw graphs in Android. For this purpose we are going to use a library called as "achartengine" . So , lets get started.




1) First of all  create an Android Application Project.

2) Next go to "res/alyout/activity_main.xml" and create a LinearLayout there.

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/label"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

</LinearLayout>

3) Now go to http://www.achartengine.org/content/download.html and download "achartengine" library from there.

4) Right click on the project file in eclipse and go to properties->Java Bild Path-> Libraries-> Export External Jar files and select the downloaded "achartengine" library from there.

5) Now go to "src/your_package_name/MainActivity.java" and paste the following code there.

MainActivity.java

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.*;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.graphics.Paint.Align;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    private GraphicalView mChart;
    private XYSeriesRenderer mCurrentRenderer;
    private XYSeriesRenderer mCurrentRenderer1;
    private XYSeriesRenderer mCurrentRenderer2;
    private XYSeries mCurrentSeries;
    private XYSeries mCurrentSeries1;
    private XYSeries mCurrentSeries2;
    private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
    private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
    private View rootView = null;

    private void addSampleData1() {
        double[] arrayOfDouble = { 20.0D, 30.0D, 67.0D, 80.0D, 85.0D, 94.0D,
                99.0D, 100.0D, 102.0D, 104.0D, 106.0D, 108.0D };
        for (int i = 0;; i++) {
            if (i >= 12) {
                return;
            }
            this.mCurrentSeries.add(i, arrayOfDouble[i]);
        }
    }

    private void addSampleData2() {
        double[] arrayOfDouble = { 30.0D, 55.0D, 80.0D, 90.0D, 91.0D, 100.0D,
                102.0D, 105.0D, 110.0D, 115.0D, 120.0D, 125.0D };
        for (int i = 0;; i++) {
            if (i >= 12) {
                return;
            }
            this.mCurrentSeries1.add(i, arrayOfDouble[i]);
        }
    }

    private void addSampleDatamid() {
        double[] arrayOfDouble = { 25.0D, 50.0D, 75.0D, 85.0D, 87.0D, 99.0D,
                100.0D, 105.0D, 106.0D, 107.0D, 108.0D, 109.0D };
        for (int i = 0;; i++) {
            if (i >= 12) {
                return;
            }
            this.mCurrentSeries2.add(i, arrayOfDouble[i]);
        }
    }

    private void initChart() {
        this.mCurrentSeries = new XYSeries("Min Price");
        this.mCurrentSeries1 = new XYSeries("Max Price");
        this.mCurrentSeries2 = new XYSeries("Predicted Price");
        this.mDataset.addSeries(this.mCurrentSeries);
        this.mDataset.addSeries(this.mCurrentSeries1);
        this.mDataset.addSeries(this.mCurrentSeries2);
        this.mCurrentRenderer = new XYSeriesRenderer();
        this.mCurrentRenderer1 = new XYSeriesRenderer();
        this.mCurrentRenderer2 = new XYSeriesRenderer();
        this.mCurrentRenderer.setColor(-16776961);
        this.mCurrentRenderer1.setColor(-16711936);
        this.mCurrentRenderer2.setColor(-65536);
        this.mCurrentRenderer.setPointStyle(PointStyle.SQUARE);
        this.mCurrentRenderer1.setPointStyle(PointStyle.CIRCLE);
        this.mCurrentRenderer2.setPointStyle(PointStyle.DIAMOND);
        this.mCurrentRenderer.setFillPoints(true);
        this.mCurrentRenderer1.setFillPoints(true);
        this.mCurrentRenderer2.setFillPoints(true);
        this.mRenderer.addSeriesRenderer(this.mCurrentRenderer);
        this.mRenderer.addSeriesRenderer(this.mCurrentRenderer1);
        this.mRenderer.addSeriesRenderer(this.mCurrentRenderer2);
        this.mRenderer.setXTitle("month");
        this.mRenderer.setYTitle("price");
        this.mRenderer.setZoomButtonsVisible(true);
        this.mRenderer.setPointSize(2.0F);
        this.mRenderer.setShowGridX(true);
        String[] arrayOfString = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
        for (int i = 0;; i++) {
            if (i >= arrayOfString.length) {
                this.mRenderer.setXLabels(0);
                this.mRenderer.setXLabelsAlign(Align.CENTER);
                return;
            }
            this.mRenderer.addTextLabel(i, arrayOfString[i]);
        }
    }

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

    }

    public void onResume() {
        super.onResume();
        LinearLayout localLinearLayout = (LinearLayout) findViewById(R.id.label);
        if (this.mChart == null) {
            initChart();
            addSampleData1();
            addSampleData2();
            addSampleDatamid();
            this.mChart = ChartFactory.getLineChartView(this, this.mDataset,
                    this.mRenderer);
            localLinearLayout.addView(this.mChart);
            return;
        }
        this.mChart.repaint();
    }

} 

Now we are done. Post your questions in the comment. I will be happy to answer those.

For more info visit on facebook https://www.facebook.com/androidcoolstuffs

Thank you
Posted by Unknown
No comments | 05:17
Hello Guys , how are you today? Hope you will be fine.

Have you ever wondered how to write in Hindi or any other language in Android. In this tutorial we are going to see how to develop multilingual android application.



1) First of all, create an Android Application Project.

2) Next go to "res/layout/activity_main.xml" and create a TextView there. The code looks like

activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id = "@+id/text" />

</RelativeLayout>


3) Now you need to download the font file of the language you wish to use. For our case its hindi, so i downloaded the "Mangal.ttf" file. You can google for your respective font file.

4) After downloading the font file save it in "assets/font/font_file.ttf" .

5) Next go to "src/your_package_name/MainActivity.java" and paste the below code.

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        text = (TextView) findViewById(R.id.text);
        Typeface tf = Typeface.createFromAsset(getAssets(), "font/Mangal.ttf");              
        //TextView tv = (TextView) findViewById(R.id.yourtextview);
        text.setTypeface(tf);
        text.setText("यह एक अच्छा एप्लीकेशन है | इ  लव इट |");
    }

}


Note - You can download and use google input tools for converting english to your respective languages. In our case it was Hindi so i used hindi.

Now we are done. Post your questions in the comment. I will be happy to answer those.

For more info visit on facebook https://www.facebook.com/androidcoolstuffs

Thank you

Monday, 7 April 2014

Posted by Unknown
No comments | 22:04
Hello Guys, How are you today? Hope you all be fine..

Today we are going to see how to create sidebar or Navigation Drawer in Android.


1)  Go to "res/layout/activity_main.xml" and create Navigation drawer there. The code looks like

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



<FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />



<ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

2) Create two more Android xml files in the "res/layout" folder . For our case lets name it "drawer_list_item.xml"  and "fragment_planet.xml"  .

drawer_list_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textColor="#fff"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>


fragment_planet.xml

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:gravity="center"
    android:padding="32dp" />

3) In the "res/values/strings.xml" write the following code.

<resources>
    <string name="app_name">Navigation Drawer Example</string>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
        <item>Jupiter</item>
        <item>Saturn</item>
        <item>Uranus</item>
        <item>Neptune</item>
    </string-array>
    <string name="drawer_open">Open navigation drawer</string>
    <string name="drawer_close">Close navigation drawer</string>
    <string name="action_websearch">Web search</string>
    <string name="app_not_available">Sorry, there\'s no web browser available</string>
</resources>

4) Next go to "res/menu/main.xml" .

main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action_websearch"
          android:icon="@drawable/action_search"
          android:title="@string/action_websearch"
          android:showAsAction="ifRoom|withText" />
</menu>

5) Next go to "src/your_package_name/MainActivity.java" and paste the following code.

import java.util.Locale;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mPlanetTitles;

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

        mTitle = mDrawerTitle = getTitle();
        mPlanetTitles = getResources().getStringArray(R.array.planets_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mPlanetTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
         // The action bar home/up action should open or close the drawer.
         // ActionBarDrawerToggle will take care of this.
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action buttons
        switch(item.getItemId()) {
        case R.id.action_websearch:
            // create intent to perform web search for this planet
            Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
            intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
            // catch event that there's no activity to handle intent
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            } else {
                Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    /* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {
        // update the main content by replacing fragments
        Fragment fragment = new PlanetFragment();
        Bundle args = new Bundle();
        args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
        fragment.setArguments(args);

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(mPlanetTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    /**
     * Fragment that appears in the "content_frame", shows a planet
     */
    public static class PlanetFragment extends Fragment {
        public static final String ARG_PLANET_NUMBER = "planet_number";

        public PlanetFragment() {
            // Empty constructor required for fragment subclasses
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
            int i = getArguments().getInt(ARG_PLANET_NUMBER);
            String planet = getResources().getStringArray(R.array.planets_array)[i];

            int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
                            "drawable", getActivity().getPackageName());
            ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
            getActivity().setTitle(planet);
            return rootView;
        }
    }
}

All the image files you can download it from the download link at top of the page which also contains the project file.

Now we are done. Post your questions in the comment. I will be happy to answer those.

For more info visit on facebook https://www.facebook.com/androidcoolstuffs

Thank you

Sunday, 6 April 2014

Posted by Unknown
No comments | 22:13
Hello guys, How are you today? I hope you all be in the best of your life.

Today, we are going to see how to show the information in dialog in Android. So , lets get started.



1) First of all, create an Android Application Project.

2) Next go to "res/layout/activity_main.xml" and create a button for which we are going to implement OnClickListener. The code looks like

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        android:id="@+id/click" />

</RelativeLayout>

3) Next make an Android xml file in the "res/layout/" folder and create a TextView in there. For our case the name is "show.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" >
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You are awesome"
        android:textColor="#ffffff"/>
    

</LinearLayout>

4) Next go to "src/your_package_name/MainActivity.java" and implement OnClickListener for the click button. The code looks like

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
    Button click;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        click = (Button) findViewById(R.id.click);
        click.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()) {
        case R.id.click : Intent i = new Intent(this, Show.class);
        startActivity(i);
        break;
        }
    }

}

5) Now, we have to create a class "Show.java" in the "src/your_package_name" . In Show.java we are going to display the show.xml.

import android.app.Activity;
import android.os.Bundle;

public class Show extends Activity{
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
        
    
    }

}

6) Now open AndroidManifest,xml and create an Show Activity with the dialog theme.

For our case its like
<activity android:theme="@android:style/Theme.Dialog" android:name="com.example.dialog_blog.Show" />

Now we are done. Post your questions in the comment. I will be happy to answer those.

For more info visit on facebook https://www.facebook.com/androidcoolstuffs

Thank you
Posted by Unknown
2 comments | 00:07
Hello Guys , How are you?? Its Super Sunday, hope you guys enjoying your weekend.

Today we will see how to Play Videos in Android. So lets get started.



1) First of all create an Android Application Project.

2) Go to "res/layout/activity_main.xml" and create a VideoView. The code looks like

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <VideoView  
        android:id="@+id/videoView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_centerVertical="true" />  

</RelativeLayout>



3) Next go to "src/your_package_name/MainActivity.java" and create set the MediaController for VideoView.

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {

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

        VideoView videoView = (VideoView) findViewById(R.id.videoView1);

        // Creating MediaController
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);

        // specify the location of media file
        Uri uri = Uri.parse("android.resource://com.example.videoplayer_blog/"
                + R.raw.video1);

        // Setting MediaController and URI, then starting the videoView
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(uri);
        videoView.requestFocus();
        videoView.start();
    }

}

Now we are done. Post your questions in the comment. I will be happy to answer those.

For more info visit on facebook https://www.facebook.com/androidcoolstuffs

Thank you

Saturday, 5 April 2014

Posted by Unknown
No comments | 02:54
Hello guys, how are you today? Its super Saturday, i hope you guys will be enjoying your weekend.

In this blog we are going to see how to work with the checkboxes in android. So lets get started.



1) First of all  create an Android Application Project.

2) Go to "res/layout/activity_main.xml" and create 4 checkboxes thereselect, android, windows, ios. The code looks like

<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical" >
    
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select all"
        android:id="@+id/select" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android"
        android:id="@+id/android" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Windows"
        android:id="@+id/win" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="iOS"
        android:id="@+id/ios" />
    

</LinearLayout>


3) Next go to "src/your_package_name/MainActivity.java" and here we will implement OnCheckedChangeListener for all the checkboxes. On clicking the "select all" checkbox all the rest checkboxes should get selected and on unselecting it the rest should get unselected.

The code for MainActivity is given below

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnCheckedChangeListener {
    CheckBox select, android, win, ios;

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

        select = (CheckBox) findViewById(R.id.select);
        android = (CheckBox) findViewById(R.id.android);
        win = (CheckBox) findViewById(R.id.win);
        ios = (CheckBox) findViewById(R.id.ios);

        select.setOnCheckedChangeListener(this);
        android.setOnCheckedChangeListener(this);
        win.setOnCheckedChangeListener(this);
        ios.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
        case R.id.select:
            if (select.isChecked())
                select_all();
            else
                unselect_all();
            break;

        case R.id.android:
            if (android.isChecked())
                Toast.makeText(this, "Android is selected", Toast.LENGTH_SHORT)
                        .show();
            else
                Toast.makeText(this, "Android is unselected",
                        Toast.LENGTH_SHORT).show();
            break;

        case R.id.win:
            if (win.isChecked())
                Toast.makeText(this, "Windows is selected", Toast.LENGTH_SHORT)
                        .show();
            else
                Toast.makeText(this, "Windows is unselected",
                        Toast.LENGTH_SHORT).show();
            break;

        case R.id.ios:
            if (ios.isChecked())
                Toast.makeText(this, "ios is selected", Toast.LENGTH_SHORT)
                        .show();
            else
                Toast.makeText(this, "ios is unselected", Toast.LENGTH_SHORT)
                        .show();
            break;
        }
    }

    private void select_all() {
        // TODO Auto-generated method stub
        android.setChecked(true);
        win.setChecked(true);
        ios.setChecked(true);
    }

    private void unselect_all() {
        // TODO Auto-generated method stub
        android.setChecked(false);
        win.setChecked(false);
        ios.setChecked(false);
    }

}


Now we are done. Post your questions in the comment. I will be happy to answer those.

For more info visit on facebook https://www.facebook.com/androidcoolstuffs

Thank you

Thursday, 3 April 2014

Posted by Unknown
2 comments | 23:27
Hello Guys , how are you today? Its super Friday.

In this blog e are going to see how to make an image slideshow with button and music controls in Android. So ,lets get started.



1) First of all create an Android Application Project.

2) Go to "res/layout/activity_main.xml" and create two image buttons there. This code looks like

<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"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity"

    android:background="@drawable/costume1"

    android:id ="@+id/back" >



    <ImageButton 

        android:layout_height="wrap_content"

        android:layout_width = "wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_centerInParent="true"

        android:layout_centerHorizontal="true"

        android:src="@drawable/left"

        android:background="@null"

        android:id="@+id/left"

        android:alpha="0.5"

        />

    

    <ImageButton 

        android:layout_height="wrap_content"

        android:layout_width = "wrap_content"

        android:layout_alignParentRight="true"

        android:layout_centerInParent="true"

        android:layout_centerHorizontal="true"

        android:src="@drawable/right"

        android:background="@null"

        android:id="@+id/right"

        android:alpha="0.5"/>



    <ImageButton

        android:id="@+id/music"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:layout_alignParentRight="true"

        android:background="@null"

        android:src="@drawable/mute1" />



</RelativeLayout>


The images used here can be get through the project file in the download link above.

3) Now go to "src/your_package_name/MainActivity.java" and copy paste the below code.

import java.util.ArrayList;

import android.media.MediaPlayer;

import android.os.Bundle;

import android.app.Activity;

import android.content.res.Resources;

import android.graphics.drawable.Drawable;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ImageButton;

import android.widget.RelativeLayout;



public class MainActivity extends Activity implements OnClickListener {

    

    ImageButton left,right,music;

    MediaPlayer mp;

    RelativeLayout back;

    ArrayList<Integer> imagearray;

    Resources res;Drawable drawable;

    int track=0;boolean flag=false;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        imagearray = new ArrayList<Integer>();

        

        back = (RelativeLayout) findViewById(R.id.back);

        left= (ImageButton) findViewById(R.id.left);

        right= (ImageButton) findViewById(R.id.right);

        music= (ImageButton) findViewById(R.id.music);

        //left.setAlpha(45);

        //right.getBackground().setAlpha(45);

        left.setOnClickListener(this);

        right.setOnClickListener(this);

        music.setOnClickListener(this);

        setup();

        res = getResources(); 

    }



    private void setup() {

        // TODO Auto-generated method stub

        imagearray.add(R.drawable.costume1);

        imagearray.add(R.drawable.costume2);

        imagearray.add(R.drawable.costume3);

        imagearray.add(R.drawable.costume4);

        imagearray.add(R.drawable.costume5);

        

        if(track==0) {

            left.setEnabled(false);

            left.setVisibility(View.GONE);

        }

        

        mp=MediaPlayer.create(this, R.raw.music);

        mp.start();

    }



    @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;

    }



    @Override

    public void onClick(View v) {

        // TODO Auto-generated method stub

        switch(v.getId()) {

        case R.id.left: if(track!=0)track--;

        checkview();

        //drawable = res.getDrawable(imagearray.get(track));

        back.setBackgroundResource(imagearray.get(track));

        break;

        

        case R.id.right: if(track!=imagearray.size()-1) track++;

        checkview();

        //drawable = res.getDrawable(imagearray.get(track));

        back.setBackgroundResource(imagearray.get(track));

        break;

        

        case R.id.music : if(!flag){

            mp.pause();

            music.setImageResource(R.drawable.sound1);

            flag = true;

        }else{

            flag = false;

            mp.start();

            music.setImageResource(R.drawable.mute1);

        }

        }

    }



    private void checkview() {

        // TODO Auto-generated method stub

        if(track==0) {

            left.setEnabled(false);

            left.setVisibility(View.GONE);

        }else {

            left.setEnabled(true);

            left.setVisibility(View.VISIBLE);

        }

        if(track==imagearray.size()-1) {

            right.setEnabled(false);

            right.setVisibility(View.GONE);

        }else {

            right.setEnabled(true);

            right.setVisibility(View.VISIBLE);

        }

    }

    



}


In this code we are traversing the arraylist using a variable track and changing the background of relativelayout accordingly.

Now we are done. Post your questions in the comment. I will be happy to answer those.

For more info visit on facebook https://www.facebook.com/androidcoolstuffs

Thank you