Delicious Digg Facebook Favorites More Stumbleupon Twitter
Blog seputar hobby

Monday, May 19, 2014

Posted by Maulana yusuf 1:06 AM
No comments

Oke, kali ini saya akan mencoba berbagi kepada teman-teman mengenai aplikasi sederhana "Cetak ganjil genap".

Aplikasi sederhanan ini berfungsi untuk mengetahui bilangan ganjil dan genap dari rentang bilangan yang sudah ditentukan oleh pengguna. Pengguna tinggal memasukan batas awal dan batas akhir kemudian pilih jenis bilangan yg ingin diketahui (Ganjil/ Genap).

Berikut adalah penjelasan mengenai cara membuat aplikasi.

Buat tampilan seperti gambar dibawah ini:


Berikut adalah source code nya.

package com.example.cetakbilangan;

import java.util.ArrayList;
import java.util.Arrays;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @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 boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
    public Button bCetak, bReset;
    public MediaPlayer mp;
    public RadioGroup groupJenisBilangan;
    public TextView tDari, tSampai;
    public ListView listBilangan;
    public RadioButton opGanjil, opGenap;
    public String pilihanBilangan="ganjil";
   
    int dari=0;
    int sampai=0;
    //LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
    ArrayList<String> listItems =  new ArrayList<String>(Arrays.asList(""));

    ArrayAdapter<String> adapter;
        
        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            
            listBilangan=(ListView) rootView.findViewById(R.id.listBilangan);
            adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, listItems);
            listBilangan.setAdapter(adapter);
            
            tDari = (TextView) rootView.findViewById(R.id.tDari);
            tSampai = (TextView) rootView.findViewById(R.id.tSampai);
            
            bCetak = (Button) rootView.findViewById(R.id.bCetak);
            bCetak.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
dari = Integer.parseInt(tDari.getText().toString());
sampai = Integer.parseInt(tSampai.getText().toString());
adapter.setNotifyOnChange(true);
adapter.clear();
mp = MediaPlayer.create(getActivity(), R.raw.tone);
mp.setLooping(false);
mp.start();
Toast.makeText(getActivity(), "Anda memilih bilangan: " + pilihanBilangan , Toast.LENGTH_LONG).show();
          for (int bilangan=dari ; bilangan<= sampai; bilangan++) {
               if(pilihanBilangan.equals("ganjil")){
if((bilangan%2)!=0){
//bilangannya[bilangan]= String.valueOf(bilangan).toString();
adapter.add(Integer.toString(bilangan).toString());
}
}else if(pilihanBilangan.equals("genap")){
if((bilangan%2)==0){
adapter.add(Integer.toString(bilangan).toString());
}
}
          }
          
          //adapter.notifyDataSetChanged();
 
/*for(int bilangan=dari;dari<=sampai;bilangan++){
if(pilihanBilangan.equals("ganjil")){
if(bilangan%2!=0){
//bilangannya[bilangan]= String.valueOf(bilangan).toString();
//adapter.add(Integer.toString(bilangan));
}
}else if(pilihanBilangan.equals("genap")){
if(bilangan%2==0){
//adapter.add(Integer.toString(bilangan));
}
}
}*/
// adapter.notifyDataSetChanged();
}
});
            
           
            bReset = (Button) rootView.findViewById(R.id.bReset);
            bReset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tDari.setText("");
tSampai.setText("");
adapter.clear();
opGanjil.setChecked(false);
opGenap.setChecked(false);
}
});
            
           groupJenisBilangan = (RadioGroup) rootView.findViewById(R.id.groupJenisBilangan);
           opGanjil = (RadioButton) rootView.findViewById(R.id.opGanjil);
           opGenap = (RadioButton) rootView.findViewById(R.id.opGenap);
           
           groupJenisBilangan.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.opGanjil:
Toast.makeText(getActivity(), "Anda memilih bilangan: " + opGanjil.getText().toString() , Toast.LENGTH_LONG).show();
pilihanBilangan = "ganjil";
break;
case R.id.opGenap:
Toast.makeText(getActivity(), "Anda memilih bilangan: " + opGenap.getText().toString() , Toast.LENGTH_LONG).show();
pilihanBilangan = "genap";
break;
}
}
});
            return rootView;
        }
    }

}

sekian. Terimakasih.


0 comments:

Post a Comment

Search

Our Sponsors