Understanding of UI :
i. Create an UI such that , one screen have list of all the types of cars.
ii. On selecting of any car name, next screen should show Car details like: name , launched date ,company name, images(using gallery) if available, show different colors in which it is available.
MainActivity.java
i. Create an UI such that , one screen have list of all the types of cars.
ii. On selecting of any car name, next screen should show Car details like: name , launched date ,company name, images(using gallery) if available, show different colors in which it is available.
MainActivity.java
package com.example.assignment11;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity implements OnItemClickListener {
String car_name[]={"Maruti","Hundai","Honda"};
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.list);
ArrayAdapter<String> arr=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,car_name);
lv.setAdapter(arr);
lv.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
Intent inte=new Intent(this,DetailActivity.class);
inte.putExtra("flag",pos);
this.startActivity(inte);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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"
android:background="#FFFFFF"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="494dp"
android:layout_x="2dp"
android:layout_y="72dp"
android:background="#FFFFFF" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="68dp"
android:layout_x="-2dp"
android:layout_y="4dp"
android:background="#FFFFFF"
android:gravity="center"
android:text="CARS LIST"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="#090909" />
</AbsoluteLayout>
DetailActivity.java
package com.example.assignment11;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
public class DetailActivity extends AppCompatActivity implements OnClickListener, OnItemClickListener {
int flag;
Gallery gv;
TextView lblname,lbldate,lblprice,lblcompany,lblcname,lblcl,lblprice1;
Button btnback;
ImageView img;
Integer hundai[]={R.drawable.a,R.drawable.b,R.drawable.v,R.drawable.r,R.drawable.s,R.drawable.t};
Integer maruti[]={R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f,R.drawable.g,R.drawable.h};
Integer honda[]={R.drawable.k,R.drawable.l,R.drawable.m,R.drawable.n,R.drawable.o,R.drawable.p};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
setContentView(R.layout.activity_detail);
gv=(Gallery) findViewById(R.id.gallary);
lblname=(TextView) findViewById(R.id.lblname);
lbldate=(TextView) findViewById(R.id.lbldate);
lblprice=(TextView) findViewById(R.id.lblprice);
lblcompany=(TextView) findViewById(R.id.lblcompany);
lblcname=(TextView) findViewById(R.id.lblcname);
lblcl=(TextView) findViewById(R.id.lblcl);
lblprice1=(TextView) findViewById(R.id.lblprice1);
btnback=(Button) findViewById(R.id.btnback);
img=(ImageView) findViewById(R.id.img);
btnback.setOnClickListener(this);
btnback.setVisibility(-1);
img.setVisibility(-1);
Intent inte=getIntent();
flag=inte.getIntExtra("flag", 0);
if(flag==0)
{
lblcompany.setText("MARUTI");
lblname.setText("Alto 800");
lbldate.setText("23-03-2019");
lblprice.setText("INR 3.5 Lakh to INR 5.5 Lakh");
}
else if(flag==1)
{
lblcompany.setText("HUNDAI");
lblname.setText("i20");
lbldate.setText("11-08-2014");
lblprice.setText("INR 5.59 to INR 9.2 Lakh");
}
else if(flag==2)
{
lblcompany.setText("HONDA");
lblname.setText("Honda City");
lbldate.setText("23-06-2012");
lblprice.setText("INR 9.91 to INR14.31 lakh");
}
gv.setAdapter(new ImageAdapter(this,flag));
gv.setOnItemClickListener(this);
}
@Override
public void onClick(View v) {
visible();
}
private void visible() {
lblcname.setVisibility(0);
lblcl.setVisibility(0);
lblprice1.setVisibility(0);
lblprice.setVisibility(0);
lblname.setVisibility(0);
lbldate.setVisibility(0);
gv.setVisibility(0);
btnback.setVisibility(-1);
img.setVisibility(-1);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
invisible();
if(flag==0)
img.setImageResource(maruti[pos]);
else if(flag==1)
img.setImageResource(hundai[pos]);
else if(flag==2)
img.setImageResource(honda[pos]);
}
private void invisible() {
lblcname.setVisibility(-1);
lblcl.setVisibility(-1);
lblprice1.setVisibility(-1);
lblprice.setVisibility(-1);
lblname.setVisibility(-1);
lbldate.setVisibility(-1);
gv.setVisibility(-1);
btnback.setVisibility(0);
img.setVisibility(0);
}
}
activity_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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"
android:background="#090909"
tools:context=".DetailActivity">
<Gallery
android:id="@+id/gallary"
android:layout_width="328dp"
android:layout_height="222dp"
android:layout_x="36dp"
android:layout_y="254dp" />
<TextView
android:id="@+id/lblcompany"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_x="-5dp"
android:layout_y="0dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FBFBFB" />
<TextView
android:id="@+id/lblcname"
android:layout_width="104dp"
android:layout_height="44dp"
android:layout_x="0dp"
android:layout_y="66dp"
android:text="Name :"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/lblname"
android:layout_width="152dp"
android:layout_height="45dp"
android:layout_x="156dp"
android:layout_y="64dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/lbldate"
android:layout_width="132dp"
android:layout_height="wrap_content"
android:layout_x="156dp"
android:layout_y="125dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/lblprice1"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="187dp"
android:text="Price:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/lblcl"
android:layout_width="131dp"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="125dp"
android:text="Launch Date:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/lblprice"
android:layout_width="114dp"
android:layout_height="wrap_content"
android:layout_x="156dp"
android:layout_y="183dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<ImageView
android:id="@+id/img"
android:layout_width="394dp"
android:layout_height="540dp"
android:layout_x="0dp"
android:layout_y="0dp" />
<Button
android:id="@+id/btnback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="159dp"
android:layout_y="490dp"
android:text="Back" />
</AbsoluteLayout>
ImageAdapter.java
package com.example.assignment11;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
Context c;
Integer hundai[]={R.drawable.a,R.drawable.b,R.drawable.v,R.drawable.r,R.drawable.s,R.drawable.t};
Integer maruti[]={R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f,R.drawable.g,R.drawable.h};
Integer honda[]={R.drawable.k,R.drawable.l,R.drawable.m,R.drawable.n,R.drawable.o,R.drawable.p};
int position=0,flag=0;
public ImageAdapter(DetailActivity c, int flag) {
this.c=c;
this.flag=flag;
}
public int getCount() {
if(flag==0)
return maruti.length;
else if(flag==1)
return hundai.length;
else if(flag==2)
return honda.length;
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
ImageView iv =new ImageView(c);
if(flag==0)
iv.setImageResource(maruti[pos]);
else if(flag==1)
iv.setImageResource(hundai[pos]);
else if(flag==2)
iv.setImageResource(honda[pos]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(160,120));
//Toast.makeText(c,pos+"", Toast.LENGTH_SHORT).show();
return iv;
}
}
No comments:
Post a Comment