1. Buatlah 3 Buah Activity dengan nama GridLayout, FullImage dan Image Adapter
2. Buatlah 2 Buah XML dengan nama grid_layout dan full_image.
3. Tambahkanlah script berikut pada manifest agar dapat dibuka secara penuh:
<activity android:name=".FullImage"></activity>
4. Buatlah script grid_layout seperti berikut ini:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:background="#000000">
</GridView>
5. Buatlah script full_image seperti berikut:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:background="#000000">
</GridView>
<?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"
android:background="#000000" >
<ImageView android:id="@+id/full_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
6. Buatlah Activity full_image seperti berikut:
package image.android.imageviewer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class FullImage extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}
7. Buatlah Activity GridLayout seperti berikut:
package image.android.imageviewer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class GridLayout extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImage.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
8. Buatlah Activity ImageAdapter seperti berikut:
package image.android.imageviewer;
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;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic1, R.drawable.pic6, R.drawable.pic11,
R.drawable.pic2, R.drawable.pic7, R.drawable.pic12,
R.drawable.pic3, R.drawable.pic8, R.drawable.pic13,
R.drawable.pic4, R.drawable.pic9, R.drawable.pic14,
R.drawable.pic5, R.drawable.pic10, R.drawable.pic15,
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
Jumat, 06 Januari 2017
widget android
Script xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Belajar Android widget" />
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Demo Dialog"
android:id="@+id/yesno"/>
</LinearLayout>
Script Java
package widget.android.dialog;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.AlertDialog;
import android.content.DialogInterface;
public class DialogActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button yesno = (Button)findViewById(R.id.yesno);
yesno.setOnClickListener(this);
}
public void onClick(View view)
{
if(view == findViewById(R.id.yesno))
{
AlertDialog.Builder dialogue = new
AlertDialog.Builder(this);
dialogue.setMessage("Belajar Dialog...");
dialogue.setPositiveButton("Yes", new
DialogInterface.OnClickListener()
{
@Override
public void
onClick(DialogInterface arg0, int arg1)
{
Toast.makeText(getApplicationContext(),
"Anda memilih tombol YES",Toast.LENGTH_SHORT).show();
}
}
);
dialogue.setNegativeButton("No", new
DialogInterface.OnClickListener() {
@Override
public void
onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "Anda memilih tombol NO", Toast.LENGTH_SHORT).show();
}
});
dialogue.show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Belajar Android widget" />
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Demo Dialog"
android:id="@+id/yesno"/>
</LinearLayout>
Script Java
package widget.android.dialog;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.AlertDialog;
import android.content.DialogInterface;
public class DialogActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button yesno = (Button)findViewById(R.id.yesno);
yesno.setOnClickListener(this);
}
public void onClick(View view)
{
if(view == findViewById(R.id.yesno))
{
AlertDialog.Builder dialogue = new
AlertDialog.Builder(this);
dialogue.setMessage("Belajar Dialog...");
dialogue.setPositiveButton("Yes", new
DialogInterface.OnClickListener()
{
@Override
public void
onClick(DialogInterface arg0, int arg1)
{
Toast.makeText(getApplicationContext(),
"Anda memilih tombol YES",Toast.LENGTH_SHORT).show();
}
}
);
dialogue.setNegativeButton("No", new
DialogInterface.OnClickListener() {
@Override
public void
onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "Anda memilih tombol NO", Toast.LENGTH_SHORT).show();
}
});
dialogue.show();
}
}
}
Membuat SPLASH Screen pada Android
1. Buatlah 2 buah file xml yang diberi nama activity_splash dan activity_bgpattern
2. Buatlah 2 buah file java yang diberi nama SplashActivity dan HomeActivity
3. Tambah sedikit script pada AndroidManifest.xml
Berikut adalah beberapa script pada file Splash Screen Android Eclipse
1. Tambahkan beberapa script agar dapat menampilkan gambar pada activity_splash
(sebelum menambahkan gambar drag file gambar ke mdpi)
<ImageView
android:id="@+id/splashscreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="0.21"
android:src="@drawable/gambar1"
/>
2. Tambahkan beberapa script agar dapat menampilkan gambar pada activity_bgpattern
<ImageView
android:id="@+id/splashscreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="0.21"
android:src="@drawable/gambar2"
/>
Pada file java SplashActivity
splash.android.pembuka;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashActivity extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 5000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = null;
mainIntent = new Intent(SplashActivity.this,
HomeActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
Agar file java HomeActivity dapat dipanggil oleh activity lain maka tambahkan perintah agar
dapat memanggil file xml Activity_bgpattern
Penggalan dari script seperti berikut ini:
setContentView(R.layout.activity_bgpattern);
3. Selanjutnya kita menambhakan AndroidManifest.xml
<application
android:icon="@drawable/pingguin"
android:label="Demo Splashscreen oleh anakmudanya"
>
<activity
android:name="splash.android.pembuka.SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="splash.pembuka.HomeActivity"
>
</activity>
</application>
tambahkan minimum sdk dan target sdk nya
2. Buatlah 2 buah file java yang diberi nama SplashActivity dan HomeActivity
3. Tambah sedikit script pada AndroidManifest.xml
Berikut adalah beberapa script pada file Splash Screen Android Eclipse
1. Tambahkan beberapa script agar dapat menampilkan gambar pada activity_splash
(sebelum menambahkan gambar drag file gambar ke mdpi)
<ImageView
android:id="@+id/splashscreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="0.21"
android:src="@drawable/gambar1"
/>
2. Tambahkan beberapa script agar dapat menampilkan gambar pada activity_bgpattern
<ImageView
android:id="@+id/splashscreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="0.21"
android:src="@drawable/gambar2"
/>
Pada file java SplashActivity
splash.android.pembuka;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashActivity extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 5000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = null;
mainIntent = new Intent(SplashActivity.this,
HomeActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
Agar file java HomeActivity dapat dipanggil oleh activity lain maka tambahkan perintah agar
dapat memanggil file xml Activity_bgpattern
Penggalan dari script seperti berikut ini:
setContentView(R.layout.activity_bgpattern);
3. Selanjutnya kita menambhakan AndroidManifest.xml
<application
android:icon="@drawable/pingguin"
android:label="Demo Splashscreen oleh anakmudanya"
>
<activity
android:name="splash.android.pembuka.SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="splash.pembuka.HomeActivity"
>
</activity>
</application>
tambahkan minimum sdk dan target sdk nya
antar activity eclipse android
Main
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Selamat
Datang"
/>
<Button
android:id="@+id/btnpindah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Target" />
</LinearLayout>
Target 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:gravity="center"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Layout
Target"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/btnmain"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To
Main" />
</LinearLayout>
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="rocky.android.pertemuan5"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true" >
<activity
android:name="rocky.android.pertemuan5.MainActivity"
android:label="@string/app_name" >
<activity android:name="Target"></activity>
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity android:name="target"></activity>
</application>
</manifest>
Main activity
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="rocky.android.pertemuan5"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true" >
<activity
android:name="rocky.android.pertemuan5.MainActivity"
android:label="@string/app_name" >
<activity android:name="Target"></activity>
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity android:name="target"></activity>
</application>
</manifest>
java
package rocky.android.pertemuan5;
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 target extends Activity implements
OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target);
Button move = (Button)findViewById(R.id.btnmain);
move.setOnClickListener(this);
}
public void onClick(View v) {
if(v.getId()==R.id.btnmain) {
Intent i=new Intent(this,MainActivity.class);
startActivity(i);
finish();//menghapus history class ini
}
}
}
Membuat WebView eclipse
1.tambahan Script xml
<WebView
android:id="@+id/webviewku"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
/>
2. Script Activity
public class WebActivity extends Activity {
/** Called when the activity is first created. */
private void tampilweb(String url){
WebView webviewku = (WebView) findViewById(R.id.webviewku);
webviewku.loadUrl(url);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tampilweb("http://www.google.com");
}
}
3.Tambahan AndroidManifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<WebView
android:id="@+id/webviewku"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
/>
2. Script Activity
public class WebActivity extends Activity {
/** Called when the activity is first created. */
private void tampilweb(String url){
WebView webviewku = (WebView) findViewById(R.id.webviewku);
webviewku.loadUrl(url);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tampilweb("http://www.google.com");
}
}
3.Tambahan AndroidManifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
membuat menu tampil pada eclipse
1.Tambahkan sedikit Script di main.xml sebagai perataan objek Button
isi script EDIT TEXT
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
isi dengan script button 1
isi dengan script button 2
</LinearLayout>
isi script EDIT TEXT
2.Nama paket Android
package tampil.android.tekstampil;
3.Library Paket Android
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
4.Fungsi dan Event dari Objek yang akan di pergunakan
public class Activity_tekstampil extends Activity implements View.OnClickListener{
private Button btn_tampil, btn_hapus;
private EditText txt_input;
private TextView txt_output;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt_input = (EditText) findViewById(R.id.txt_input);
txt_output = (TextView) findViewById(R.id.txt_output);
btn_tampil = (Button) findViewById(R.id.btn_tampil);
btn_tampil.setOnClickListener(this);
btn_hapus = (Button) findViewById(R.id.btn_hapus);
btn_hapus.setOnClickListener(this);
}
5.Eksekusi dari objek yang dipergunakan
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_tampil:
txt_output.setText(txt_input.getText());
break;
case R.id.btn_hapus:
txt_output.setText("");
break;
default:
break;
}
}
}
isi script EDIT TEXT
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
isi dengan script button 1
isi dengan script button 2
</LinearLayout>
isi script EDIT TEXT
2.Nama paket Android
package tampil.android.tekstampil;
3.Library Paket Android
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
4.Fungsi dan Event dari Objek yang akan di pergunakan
public class Activity_tekstampil extends Activity implements View.OnClickListener{
private Button btn_tampil, btn_hapus;
private EditText txt_input;
private TextView txt_output;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt_input = (EditText) findViewById(R.id.txt_input);
txt_output = (TextView) findViewById(R.id.txt_output);
btn_tampil = (Button) findViewById(R.id.btn_tampil);
btn_tampil.setOnClickListener(this);
btn_hapus = (Button) findViewById(R.id.btn_hapus);
btn_hapus.setOnClickListener(this);
}
5.Eksekusi dari objek yang dipergunakan
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_tampil:
txt_output.setText(txt_input.getText());
break;
case R.id.btn_hapus:
txt_output.setText("");
break;
default:
break;
}
}
}
membuat dialog di eclipse
saya akan memposting tugas tentang form dialog di eclipse berikut coding nya :
package tugas.rocky;import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button yesno = (Button)findViewById(R.id.yesno);
yesno.setOnClickListener(this);
}
public void onClick(View view)
{
if(view == findViewById(R.id.yesno))
{
AlertDialog.Builder dialogue = new
AlertDialog.Builder(this);
dialogue.setMessage("Belajar Dialog...");
dialogue.setPositiveButton("Yes", new
DialogInterface.OnClickListener()
{
public void
onClick(DialogInterface arg0, int arg1)
{
Intent i=new Intent(getApplicationContext(), webview.class);
startActivity(i);
}
}
);
dialogue.setNegativeButton("No", new
DialogInterface.OnClickListener() {
public void
onClick(DialogInterface arg0, int arg1) {
Intent i=new Intent(getApplicationContext(), facebook.class);
startActivity(i);
}
});
dialogue.show();
}
}
}
Langganan:
Postingan (Atom)