Showing posts with label item. Show all posts
It is sometimes useful to have your own custom list, here we are going to create a custom layout with a switch, button and an image.
1. first, we need to create the xml file of our layout res/layout/sliding_item.xml
2. in you res/layout/activity_main.xml
Notice that the name of our id is viewPager
3. now we will create our SlidingItem class
Note: we create our page viewer object and our SlidingItem class object, and pass through our array to our SlidingItem constructor.
I hope this article was clear
1. first, we need to create the xml file of our layout res/layout/sliding_item.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:background="@drawable/head_light_banner" android:orientation="vertical" android:id="@+id/car_light_banner"> <ImageView android:id="@+id/car_light" android:layout_width="100dp" android:layout_height="100dp"/> <TextView android:id="@+id/car_light_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:textColor="#fff" android:textSize="20dp" android:text="Car Lights" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:background="@null" android:orientation="horizontal"> <Switch android:id="@+id/switch_plug" android:layout_width="wrap_content" android:layout_margin="10dp" android:layout_height="wrap_content" android:textSize="10dp" android:trackTint="#fff" android:checked="false" android:colorForeground="#fff"/> <Button android:id="@+id/detail_button" android:layout_width="wrap_content" android:layout_height="40dp" android:textSize="10dp" android:text="Detail" /> </LinearLayout> </LinearLayout>
2. in you res/layout/activity_main.xml
<android.support.v4.view.ViewPager android:id="@+id/slider_view" android:layout_width="match_parent" android:layout_height="223dp" android:layout_weight="1" android:gravity="center" />
Notice that the name of our id is viewPager
3. now we will create our SlidingItem class
package com.designspaza.carcontroller; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.TextView; import android.widget.LinearLayout; import android.widget.Switch; import android.widget.Toast; public class SlidingItem extends PagerAdapter { Context context; int car_light[]; int car_light_banner[]; int car_set_banner = -1; String car_light_title[]; LayoutInflater layoutInflater; public CarLights(Context context, int car_light[],int car_light_banner[], String car_light_title[]) { this.context = context; this.car_light = car_light; this.car_light_banner = car_light_banner; this.car_light_title = car_light_title; layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return car_light.length; } @Override public boolean isViewFromObject(View view, Object object) { return view == ((LinearLayout) object); } @Override public Object instantiateItem(ViewGroup container, final int position) { View CarLightView = layoutInflater.inflate(R.layout.car_lights, container, false); final ImageView CarLight = (ImageView) CarLightView.findViewById(R.id.car_light); CarLight.setImageResource(car_light[position]); final LinearLayout CarLightBanner = (LinearLayout) CarLightView.findViewById(R.id.car_light_banner); CarLightBanner.setBackgroundResource(car_light_banner[position]); TextView CarLightTitle = (TextView) CarLightView.findViewById(R.id.car_light_title); CarLightTitle.setText(car_light_title[position]); Switch SwitchPlug = (Switch) CarLightView.findViewById(R.id.switch_plug); Button DetailButton = (Button) CarLightView.findViewById(R.id.detail_button); container.addView(CarLightView); //listening to image click CarLight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if( car_set_banner != car_light[position]) { CarLightBanner.setBackgroundResource(car_light[position]); car_set_banner = car_light[position]; } else { CarLightBanner.setBackgroundResource(car_light_banner[position]); car_set_banner = car_light_banner[position]; } Toast.makeText(context, "you clicked image " + (position + 1), Toast.LENGTH_LONG).show(); } }); //listening to switch flip //SwitchPlug.setChecked(false); SwitchPlug.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) { if (bChecked) { CarLight.setBackgroundColor(Color.rgb(255,255,255)); CarLight.setPadding(3,3,3,3); Toast.makeText(context, "turn on " + (position + 1), Toast.LENGTH_LONG).show(); } else { CarLight.setBackgroundColor(Color.rgb(0,0,0)); CarLight.setPadding(0,0,0,0); Toast.makeText(context, "turn off " + (position + 1), Toast.LENGTH_LONG).show(); } } }); //details direction for popup DetailButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { //car lights intent Intent i = new Intent(context.getApplicationContext(), PopActivity.class); i.putExtra("car_light_title", car_light_title[position]); i.putExtra("car_light", car_light[position]); context.startActivity(i); } }); return CarLightView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((LinearLayout) object); } }
4. now lets edit your MainActivity.java class
public class MainActivity extends AppCompatActivity { //custom slider int car_light[] = {R.drawable.image_1, R.drawable.image_2, R.drawable.image_3}; int car_light_banner[] = {R.drawable.head_light_banner, R.drawable.rear_light_banner, R.drawable.rear_light_banner}; String car_light_title[] = {"Head Light","Rear Light","Stop Lights"}; SlidingItem SlidingItem; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //car lights ViewPager viewPager = (ViewPager)findViewById(R.id.slider_view);
SlidingItem = new SlidingItem(MainActivity.this, car_light,car_light_banner,car_light_title); viewPager.setAdapter(SlidingItem);
Note: we create our page viewer object and our SlidingItem class object, and pass through our array to our SlidingItem constructor.
I hope this article was clear
How To create custom Horizontal Slider using ViewPager
This is a useful tool, especial if you have a complex UI. the ability to go back to your previously opened Activity.
1. first you need to import the menuItem class on your opened Second Activity.
2. next add the following code under
3. now paste the follow code.
Its that simple, i hope this article was helpful.
1. first you need to import the menuItem class on your opened Second Activity.
import android.view.MenuItem;
2. next add the following code under
public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_activity); //add back button getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
3. now paste the follow code.
@Overridepublic boolean onOptionsItemSelected(MenuItem item){ int id = item.getItemId(); if(id == android.R.id.home){ this.finish(); } return super.onOptionsItemSelected(item); }
Its that simple, i hope this article was helpful.
How to add a Back button on Activity
This will be one of the features you will use the most, so its best you get the handle of it now.
create a file under res/menu/app_menu_bar.xml and paste the code below.
looking at the above code
ifRoom: menu icon will only if the is room on the menu bar.
Never: menu item will only show if you click on the ... vertical icon.
now open you MainActivity.java and paste the following code under the MainActivity class.
create a file under res/menu/app_menu_bar.xml and paste the code below.
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/harzard_menu" android:title="Harzard" android:icon="@mipmap/harzard_sign" app:showAsAction="ifRoom" /> <item android:id="@+id/indicator_left" android:title="Indicator Left" android:icon="@mipmap/indicator_left_on" app:showAsAction="ifRoom" /> <item android:id="@+id/indicator_right" android:title="Indicator right" android:icon="@mipmap/indicator_left_on" app:showAsAction="never" /> </menu>
looking at the above code
ifRoom: menu icon will only if the is room on the menu bar.
Never: menu item will only show if you click on the ... vertical icon.
@mipmap/: This is where the compiler will look for the item's icon. res/mipmap
now open you MainActivity.java and paste the following code under the MainActivity class.
@Overridepublic boolean onCreateOptionsMenu(Menu menu){ MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.app_bar_menu, menu); return true; } @Overridepublic boolean onOptionsItemSelected(MenuItem item){ int id = item.getItemId(); if(id == R.id.harzard_menu){ //your code here } else if(id == R.id.indicator_left){ //your code here } else{ //your code here } return false; }
We have two functions on the above code, first we override the onCreateOptionsMenu
function and inflate our app_bar_menu.xml layout.
New we override the onOptionsItemSelected function to help us identify which menu item has
been clicked.
READ >> How to make a clickable items to a new Activity