Membuat Kontak dengan Android Studio

Pada kesempatan kali ini saya akan membuat aplikasi untuk menyimpan kontak tanpa database. check this out!!!

mainactivity.java

package com.aplikasiuas;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

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

        Intent intent;
        TabHost tabHost = getTabHost();
        TabHost.TabSpec tabSpec;

        intent = new Intent(this, TambahActivity.class);
        tabSpec = getTabHost().newTabSpec("tab1").setIndicator(
                "TAMBAH").setContent(intent);
        tabHost.addTab(tabSpec);

        intent = new Intent(this, TampilkanActivity.class);
        tabSpec = getTabHost().newTabSpec("tab2").setIndicator(
                "TAMPILKAN").setContent(intent);
        tabHost.addTab(tabSpec);
    }
}

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

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/linearLayout1" >

        <TabHost
            android:id="@android:id/tabhost"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >
                </TabWidget>

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

                    <LinearLayout
                        android:id="@+id/tab1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal">
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/tab2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal">
                    </LinearLayout>
                </FrameLayout>
            </LinearLayout>
        </TabHost>
    </LinearLayout>
</RelativeLayout>


Screenshot_2017-07-22-23-36-04

subactivity.java

package com.aplikasiuas;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by shan on 22/07/2017.
 */

public class SubActivity extends AppCompatActivity {

    EditText nama, nohp;
    TextView notif;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);

        nama = (EditText)findViewById(R.id.et_nama);
        nohp = (EditText)findViewById(R.id.et_nohp);
        notif = (TextView)findViewById(R.id.tv_notif);
    }

    public void save(View view){
        nama.setText("");
        nohp.setText("");
        notif.setText("Data Tersimpan");
    }
}

activitysub.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_nama"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nama       :"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="150dp"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"/>

    <TextView
        android:id="@+id/tv_nohp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No.HP      :"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:layout_below="@id/tv_nama"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"/>

    <EditText
        android:id="@+id/et_nama"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv_nama"
        android:layout_marginTop="150dp"/>

    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv_nohp"
        android:layout_marginTop="200dp"
        android:id="@+id/et_nohp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="save"
        android:textAllCaps="true"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/et_nohp"
        android:layout_alignRight="@+id/et_nohp"
        android:layout_alignEnd="@+id/et_nohp"
        android:onClick="save"
        android:id="@+id/btn_save" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_below="@+id/btn_save"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:id="@+id/tv_notif"/>
</RelativeLayout>

Screenshot_2017-07-22-23-36-08

tambahactivity.java

package com.aplikasiuas;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

/**
 * Created by shan on 22/07/2017.
 */

public class TambahActivity extends AppCompatActivity {

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

    public void tambah(View view){
        Intent intent = new Intent(TambahActivity.this, SubActivity.class);
        startActivity(intent);
    }
}

tambah.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="tambah"
        android:textAllCaps="true"
        android:layout_centerInParent="true"
        android:onClick="tambah"/>

</RelativeLayout>

 

Screenshot_2017-07-22-23-37-55

tampilkanactivity.java

package com.aplikasiuas;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by shan on 22/07/2017.
 */

public class TampilkanActivity extends AppCompatActivity {

    EditText nama, nohp;
    TextView notif;
    String nm,nhp;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tampilkan);

        nama = (EditText)findViewById(R.id.et_nama);
        nohp = (EditText)findViewById(R.id.et_nohp);
        notif = (TextView)findViewById(R.id.tv_notif);
    }

    public void save(View view){
        nm=nama.getText().toString().trim();
        nhp=nohp.getText().toString().trim();
        notif.setText(""+nm +"\n"+nhp);
    }
}

tampilkan.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_nama"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nama       :"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="150dp"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"/>

    <TextView
        android:id="@+id/tv_nohp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No.HP      :"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:layout_below="@id/tv_nama"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"/>

    <EditText
        android:id="@+id/et_nama"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv_nama"
        android:layout_marginTop="150dp"/>

    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv_nohp"
        android:layout_marginTop="200dp"
        android:id="@+id/et_nohp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TAMPIL"
        android:textAllCaps="true"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/et_nohp"
        android:layout_alignRight="@+id/et_nohp"
        android:layout_alignEnd="@+id/et_nohp"
        android:onClick="save"
        android:id="@+id/btn_save" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_below="@+id/btn_save"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:id="@+id/tv_notif"/>

</RelativeLayout>

 

Screenshot_2017-07-22-23-36-45

Membuat Pie Chart tanpa JFreeChart di Netbeans

Pada kali ini saya akan coba membuat pie chart secara manual seperti contoh di bawah ini :

chart

 

Copas source berikut ke dalam project anda.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Arc2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

/**
*
* @author acy
*/
public class PieChart extends JPanel {

public void paintComponent( Graphics g )
{
super.paintComponent( g );
Graphics2D g2 = ( Graphics2D ) g;

//antialis untu hasil lebih baik
g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

//frame background
Shape bg = new Rectangle2D.Float(10, 10, 250, 200);
g2.setColor(Color.LIGHT_GRAY);
g2.fill(bg);
g2.setColor(Color.GRAY);
g2.draw(bg);

//field untu menyimpan data
String [] nilaiUjian = {“A”, “B”, “C”, “D”, “E”};
int [] Data = {3, 10, 23, 5, 2};
Color [] warna = {Color.BLUE, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.RED};

//hitung total
float total = 0.0f;
for (int k = 0; k < Data.length; k++)
{total += Data[k];}

float sudut, awal = 90; //sudut pie
float lx = 220, ly = 70, lw = 10, lh = 10; //legend
for (int k = 0; k < Data.length; k++)
{
//hitung besaran sudut setiap sektor
sudut = 360.0f * Data[k] / total;
Shape sektor = new Arc2D.Float(30, 30, 150, 150, awal, sudut, Arc2D.PIE);

//tampilkan render
g2.setColor(warna[k]);
g2.fill(sektor);
awal += sudut;

//legend
g2.fill(new Rectangle2D.Float(lx, ly, lw, lh));
g2.setColor(Color.BLACK);
g2.drawString(nilaiUjian[k], lx + lw + 5, ly + lh);
ly += (lh + 5);
}

//signature
g2.setColor(Color.GRAY);
g2.drawString(“Created by : ACY”, 15, 205);
}
public static void main(String[] args)
{
JFrame frame = new JFrame(“PIE CHART”);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{System.exit(0);}
});

//tambahkan panel
PieChart canvas = new PieChart();
frame.getContentPane().add(canvas);
frame.pack();
frame.setVisible(true);
}

}
}