ListView with image and text
In android studio, I am creating a listView which has image in each item of the list.
Each item of the list has same image(a wall clock image), but each list in ListView has different text(week days).
Now, some addition to it. I added switch on right side of each list, and sub Item to each view.
it appears as-
XML code for a list is-
On touching subitem TextView a time picker Dialog should appear, but I am not getting it.
Above codes will help u getting a LISTVIEW which has a Image to its left and a switch on its right.
Heading and a subItem related to it.
Java code-(MainActivity class)
(baseadapter class)
Each item of the list has same image(a wall clock image), but each list in ListView has different text(week days).
Now, some addition to it. I added switch on right side of each list, and sub Item to each view.
it appears as-
XML code for a list is-
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="70dp" android:layout_height="80dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:src="@android:drawable/btn_star_big_on" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="11dp" android:layout_toEndOf="@+id/imageView" android:text="TextView" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/switch1" android:layout_below="@+id/textView" android:layout_toEndOf="@+id/imageView" android:text="TextView" android:clickable="true" /> <TextView android:id="@+id/textView3" android:text="textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toEndOf="@+id/arrow" android:layout_below="@+id/textView" android:clickable="true" /> <ImageView android:id="@+id/arrow" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_alignTop="@+id/textView3" android:layout_toEndOf="@+id/textView" android:src="@drawable/ic_action_arrow_right" /> <Switch android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_below="@+id/textView" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView3" android:layout_toEndOf="@+id/textView2" android:text="Button" /> </RelativeLayout>
------------------------------------------------------------------------------------------------
main XML will contain only a listView which is taken from inbuilt templates--
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.csc.hours.MainActivity"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp" /> </RelativeLayout>
---------------------------------------------------------------------------------------------------------------------------------
On touching subitem TextView a time picker Dialog should appear, but I am not getting it.
Above codes will help u getting a LISTVIEW which has a Image to its left and a switch on its right.
Heading and a subItem related to it.
Java code-(MainActivity class)
import android.app.Activity; import android.app.Dialog; import android.app.DialogFragment; import android.app.FragmentManager; import android.app.TimePickerDialog; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.text.format.DateFormat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.TimePicker; import java.util.Calendar; import java.util.zip.Inflater; public class MainActivity extends Activity { ListView listView; String[] week={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView)findViewById(R.id.listview); customized ca=new customized(getApplicationContext(),week); listView.setAdapter(ca); } } ----------------------------------------------------------------------------------------------------
(baseadapter class)
class customized extends BaseAdapter { String[] week; Main2Activity obj=new Main2Activity(); Context context; LayoutInflater inflator ; public customized(Context context,String[] week) { this.week = week; this.context=context; inflator=(LayoutInflater.from(context)); } @Override public int getCount() { return week.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = inflator.inflate(R.layout.activity_main2, null); TextView days = (TextView) convertView.findViewById(R.id.textView); ImageView clock = (ImageView) convertView.findViewById(R.id.imageView); TextView from = (TextView) convertView.findViewById(R.id.textView2); TextView to = (TextView) convertView.findViewById(R.id.textView3); Button bt=(Button)convertView.findViewById(R.id.button); from.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { obj.fromtime(); } }); to.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { obj.totime(); } }); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { obj.totime(); } }); days.setText(week[position]); clock.setImageResource(R.drawable.ic_action_clock); /*from.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } });*/ return convertView; } /*public void fromtime(View view) { DialogFragment dFragment = new BlankFragment(); // Show the time picker dialog fragment } public void totime(View view) { DialogFragment dFragment = new BlankFragment(); } */}
Comments
Post a Comment