How to Make View Pager
By Unknown - Februari 02, 2018
View Pager
Ok today we gonna make some components name is VIEW PAGER, ok so lets go broπππππ.
firstly open the android studio create a new project the name is up to you and choose the empty activity then finish.
secondly make a new layout like thisπππ.
Third we make the java code like this πππ.
but if you want to make different app you just change the "keterangan, gambar, judul".
fourth if there is a error in the viewpageradapter you just click Alt + Enter and choose the "Create class 'viewpageradapter' then enter.
fifth we need to make code for ViewPagerAdapter like this πππ.
result for the app will be like this πππ.
you just swipe the app and will be change.
secondly make a new layout like thisπππ.
then make a code like this πππ.
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imgview" android:layout_width="match_parent" android:layout_height="300dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_weight="0.83" android:src="@drawable/niat" android:layout_alignParentTop="true" /> <ScrollView android:id="@+id/scrolljudul" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/imgview"> <TextView android:id="@+id/judul" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Medium Text" android:textSize="30sp" android:layout_marginTop="13dp" /> </ScrollView> <ScrollView android:id="@+id/scrollketerangan" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/scrolljudul"> <TextView android:id="@+id/keterangan" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="Keterangan" android:textSize="18sp" /> </ScrollView> </RelativeLayout>
then back to xml activity_main and make code like thisπππ.
<android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/pager"></android.support.v4.view.ViewPager>
package com.blogspot.rzwei.tatacarawudhu; import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle; import static com.blogspot.rzwei.tatacarawudhu.R.id.judul;import static com.blogspot.rzwei.tatacarawudhu.R.id.pager; public class PictureActivity extends AppCompatActivity { String[] judul={ "Niat" ,"kedua telapak" ,"Mulut" ,"Hidung" ,"Wajah" ,"Kedua Tangan" ,"Kepala" ,"Kedua Kaki" }; int gambar[] ={ R.drawable.niat ,R.drawable.wudhu1 ,R.drawable.wudhu2 ,R.drawable.wudhu8 ,R.drawable.wudhu3 ,R.drawable.wudhu4 ,R.drawable.wudhu5 ,R.drawable.wudhu7 }; String keterangan[] ={ "1.Berniat wudhu (dalam hati) untuk menghilangkan hadats.\n" + "2.Mengucapkan basmalah (bacaan bismillah)." ,"3.Membasuh dua telapak tangan sebanyak 3 kali." ,"4.Mengambil air dengan tangan kanan kemudian memasukkannya ke dalam mulut dan" ,"5.hidung untuk berkumur-kumur dan istinsyaq (memasukkan air dalam hidung). Kemudian beristintsar (mengeluarkan air dari hidung) dengan tangan kiri sebanyak 3 kali." ,"6.Membasuh seluruh wajah dan menyela-nyelai jenggot sebanyak 3 kali." ,"7.Membasuh tangan kanan hingga siku bersamaan dengan menyela-nyelai jemari sebanyak 3 kali kemudian dilanjutkan dengan yang kiri." ,"8.Menyapu seluruh kepala dengan cara mengusap dari depan ditarik ke belakang, lalu ditarik lagi ke depan, dilakukan sebanyak 1 kali, dilanjutkan menyapu bagian luar dan dalam telinga sebanyak 1 kali." ,"9.Membasuh kaki kanan hingga mata kaki bersamaan dengan menyela-nyelai jemari sebanyak 3 kali kemudian dilanjutkan dengan kaki kiri." }; ViewPager viewPager; PagerAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_picture); viewPager = (ViewPager)findViewById(R.id.pager); adapter = new ViewPagerAdapter (PictureActivity.this,judul,keterangan,gambar); viewPager.setAdapter(adapter); } }
but if you want to make different app you just change the "keterangan, gambar, judul".
fourth if there is a error in the viewpageradapter you just click Alt + Enter and choose the "Create class 'viewpageradapter' then enter.
fifth we need to make code for ViewPagerAdapter like this πππ.
package com.blogspot.rzwei.tatacarawudhu; import android.content.Context;import android.support.v4.view.PagerAdapter;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView; /** * Created by ammarkhasali on 1/30/2018 AD. */ class ViewPagerAdapter extends PagerAdapter { String judul[]; String keterangan[]; int gambar[]; PictureActivity context; LayoutInflater inflater; public ViewPagerAdapter(PictureActivity pictureActivity, String[] judul, String[] keterangan, int[] gambar) { this.context = pictureActivity; this.judul = judul; this.gambar =gambar; this.keterangan =keterangan; } @Override public int getCount() { return gambar.length; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } public Object instantiateItem(ViewGroup container, int position){ inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.item,container, false); TextView jdl = view.findViewById(R.id.judul); TextView ktrng = view.findViewById(R.id.keterangan); ImageView img = view.findViewById(R.id.imgview); jdl.setText(judul[position]); ktrng.setText(keterangan[position]); img.setImageResource(gambar[position]); container.addView(view); return view; } @Override public void destroyItem(ViewGroup container,int position, Object object){ container.removeView((LinearLayout)object); } }after that you can try it .
result for the app will be like this πππ.
you just swipe the app and will be change.
thank you for your joining my blog i hope it can be useful for you and for everyone.
0 komentar