android - How to initialize two layout Button in same activity? -
i used (include) using 2 layout in 1 activity. , visible 2nd layout clicking event. when initialize 2nd layout button, android studio don't id of 2nd layout button. how can solve problem. , how initialize 2nd layout button in activity.
main layout
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ebcf1f28" > <include android:id="@+id/player" android:layout="@layout/player" android:visibility="gone"/> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <textview android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="15dp" android:layout_marginleft="145dp" android:layout_marginbottom="10dp" android:background="#ebcf1f28" android:text="back" android:textsize="25dp" android:textcolor="#1d1d28" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:id="@+id/textcolor" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor = "#262323" /> <listview android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" > </listview> </linearlayout> </linearlayout> 2nd layout
<?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:orientation="vertical" android:background="#00ffff" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <seekbar android:id="@+id/seekbar" android:layout_margintop="50dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:color:"#000000"/> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <button android:id="@+id/backsong" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="25dp" android:layout_marginleft="33dp" android:text="|<" android:textsize="30dp" /> <button android:id="@+id/playsong" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="25dp" android:layout_marginleft="25dp" android:text="| |" android:textsize="30dp" /> <button android:id="@+id/nextsong" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="25dp" android:layout_marginleft="25dp" android:text=">|" android:textsize="30dp" /> </linearlayout> </linearlayout> my activity code:
package com.example.anupam.favouritesonglist; import android.content.intent; import android.media.mediaplayer; import android.net.uri; import android.os.environment; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.text.layout; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.button; import android.widget.listview; import android.widget.textview; import java.io.file; import java.util.arraylist; public class songlist extends appcompatactivity implements view.onclicklistener{ arraylist<file> songfile; string[] songitem; listview lv; mediaplayer mp ; view player ; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.song_list); init(); lv.setonclicklistener(new adapterview.onitemclicklistener() { onitemclick(); }); } public void init() { lv = (listview) findviewbyid(r.id.list_view); player = (view)findviewbyid(r.id.player) ; setsongfile(); textview = (textview) findviewbyid(r.id.back); back.setonclicklistener(this); } private void setsongfile() { songfile = findsong(environment.getexternalstoragedirectory()); songitem = new string[songfile.size()]; (int = 0; < songfile.size(); i++) { songitem[i] = songfile.get(i).getname(); } arrayadapter<string> songadb = new arrayadapter<>(getapplicationcontext(), android.r.layout.simple_list_item_1, songitem); lv.setadapter(songadb); } public arraylist<file> findsong(file root) { arraylist<file> song = new arraylist<file>(); file[] musicfilem = root.listfiles(); (file singlefile : musicfilem) { if (singlefile.isdirectory() && !singlefile.ishidden()) { song.addall(findsong(singlefile)); } else if (singlefile.getname().endswith(".mp3")) { song.add(singlefile); } } return song; } @override public void onclick(view v) { int id = v.getid(); switch (id) { case r.id.back: { intent intent = new intent(getapplicationcontext(),mainactivity.class); startactivity(intent); finish(); } } } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { player.setvisibility(view.visible); playerinit(); uri u = uri.parse(songfile.get(position).tostring()); mp = mediaplayer.create(getapplicationcontext(),u); mp.start(); } public void playerinit() { button backsong = (button)findviewbyid(r.id.backsong); button backsong = (button)findviewbyid(r.id.playsong); button backsong = (button)findviewbyid(r.id.nextsong); } } please check last 2 function used 2nd layout initialization
Comments
Post a Comment