java - Android start new activity when clicking widget -
i new coding java , xml android applications , wanted know how start/open new activity when clicking on something. in case using relative layout keep image , text 1 object. want when click on it start/open new activity. how do this? tell me step step since quite new this.
first of all, if want layout act (relativelayout
) button (do not handle onclick
on layout child components) firstly set in xml layout file relativelayout
attribute
android:clickable="true"
or can directly in code (in oncreate
method)
relativelayout.setclickable(true);
than need set onclicklistener
layout.
can creating anonymous class
relativelayout.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent startactivityintent = new intent(getapplicationcontext(),yourdesiredactivity.class); startactivity(startactivityintent); } }
update
layout defined in xml file, of course in android can in code ,but better use xml file.
in idea have folder res->layout
here should place layout files. example layout name `
relative_root_layout.xml
<xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relative_layout" android:layout_height="wrap_content" android:layout_width="wrap_content"> <imageview android:id="@+id/image_view"> android:layout_width="wrap_content" android:src="@drawable/icon" android:layout_height="wrap_content" android:layout_alignparenttop="true" imageview> <textview android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:layout_torightof="@+id/image_view" android:text="relative layout"> textview> relativelayout>
but in case have text , image better use
<button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableleft="@android:drawable/btn_image" android:text="button image" android:gravity="left" android:drawablepadding="10dp"> button>
how can access widgets ?
basic thing have know if developing android, essential part. please read documentation, read books, watch tutorial or whatever.
in short need inflate layout in activity oncreate()
method relativelayout mrelativelayout;
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.relative_root_layout); mrelativelayout = (relativelayout) findviewbyid(r.id.relative_layout); mrelativelayout.setonclicklistener(.....) }
but again basic things must know.
Comments
Post a Comment