getBaseline of TextView returns -1 - android-layout

I am trying to align two TextView one is inside HorizontalScrollView, and both are children of LinerLayout .
When I am trying to call getBaseLine of TextView inside onWindowAttached of RecyclerView Adapter it always returns -1.
Yes, the parent view of TextView, LinerLayout has android:baselineAligned="true"
Edit : Adding Code
Layout
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="6dp"
android:paddingEnd="10dp"
android:baselineAligned="true"
android:paddingStart="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/outerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
<HorizontalScrollView
android:id="#+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/inlineTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
/>
</HorizontalScrollView>
</LinearLayout>
Code in RecyclerView Adapter
#Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder)
{
super.onViewAttachedToWindow(holder);
if(holder instanceof MyHolder)
{
dostuff((MyHolder)holder);
}
}
private void dostuff(QACommentViewHolder holder)
{
int baseline = holder.outerTextView.getBaseline();
}

View is not measured when onViewAttachedToWindow is called, hence it is returning -1.
Better approach would be.
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
.
.
.
viewHolder.itemView.post(new Runnable()
{
//put your code here

Related

Two recyclerview not scrollable in one layout

I created a View that has two recyclerviews inside.
Everything works correctly but I get that the two recyclerviews are scrollable.
I would like the two recyclerview to be shown in its maximum length to show all lines and for the scrolling of the view to work directly
if I set android: nestedScrollingEnabled = "false" the recyclerview is not shown totally long
what do i have to change?
Thanks
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content"
android:fillViewport="true"
android:background="#color/fond"
tools:context=".dossier.FicheDossier">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="115dp"
android:layout_gravity="center|top"
android:gravity="center|top">
<TextView
android:id="#+id/txtNomUsageDossierFragment"
android:layout_width="match_parent"
android:layout_height="86dp"
android:background="#drawable/fond_jaune_top"
android:fontFamily="#font/montserrat_bold"
android:gravity="center_horizontal"
android:onClick="backList"
android:paddingTop="21dp"
android:text="Dossiers"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="normal" />
<ImageView
android:id="#+id/btn_Back"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="23dp"
app:srcCompat="#drawable/icone_arrow_left_blanc" />
<EditText
android:id="#+id/txtFindDocument"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="24dp"
android:layout_marginTop="62dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="4dp"
android:height="48dp"
android:background="#drawable/textedit_blanc_rounded_corners"
android:drawableLeft="#drawable/icone_recherche_jaune"
android:drawablePadding="6dp"
android:elevation="4dp"
android:ems="10"
android:hint="#string/txt_input_recherche"
android:inputType="text"
android:maxLines="1"
android:outlineSpotShadowColor="#color/jauneSombre"
android:paddingLeft="10dp"
android:scrollHorizontally="true"
android:textStyle="italic" />
<ProgressBar
android:id="#+id/progressBarTopListDocument"
style="?android:attr/progressBarStyle"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:layout_marginTop="23dp"
android:layout_marginRight="23dp"
android:theme="#style/colorProgressBarTop"
/>
</RelativeLayout>
<TextView
android:id="#+id/msgListDocument"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="2dp"
android:fontFamily="#font/montserrat"
android:text="#string/txt_msg_dossiers"
android:textColor="#color/neutre"
android:textStyle="bold"
android:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list_classeurs_dossier"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="12dp"
android:layout_marginRight="24dp"
android:divider="#drawable/rectangle_border_bottom"
android:dividerHeight="1dp"
android:isScrollContainer="false"
android:nestedScrollingEnabled="true" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list_docs_dossier"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:divider="#drawable/rectangle_border_bottom"
android:dividerHeight="1dp"
android:isScrollContainer="false"
android:nestedScrollingEnabled="false" />
</LinearLayout>
You have to use only one RecyclerView and use the type to manage which card you want to recall
public class RecyclerViewAdapterClasseursDocs extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final List<Document> documents;
private final List<Classeur> classeurs;
private final Context context;
public RecyclerViewAdapterClasseursDocs(List<Classeur> classeurs, List<Document> documents, Context context) {
this.classeurs = classeurs;
this.context = context;
this.documents = documents;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if (viewType == 0) {
return new ViewHolderClasseur(LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.classeur, parent, false));
}
return new ViewHolderDoc(LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.docs, parent, false));
}
#Override
public int getItemViewType(int position) {
if (position < this.classeurs.size()){
return 0;
} else {
return 1;
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
//Doc
if (holder.getItemViewType() == 0) {
//Classeur
ViewHolderClasseur c = (ViewHolderClasseur) holder;
final Classeur classeur = classeurs.get(position);
c.titleClasseur.setText(classeur.getNom());
} else {
ViewHolderDoc d = (ViewHolderDoc) holder;
final Document document = documents.get(position - classeurs.size());
d.titleDocument.setText(document.getIntitule());
}
}
#Override
public int getItemCount() {
return classeurs.size()+documents.size();
}
public static class ViewHolderClasseur extends RecyclerView.ViewHolder {
private final TextView titleClasseur;
private final View linearLayoutClasseur;
public ViewHolderClasseur(#NonNull View itemView) {
super(itemView);
linearLayoutClasseur = itemView.findViewById(R.id.rowClasseur);
titleClasseur = itemView.findViewById(R.id.txtClasseurTitle);
}
}
public static class ViewHolderDoc extends RecyclerView.ViewHolder {
private final TextView titleDocument;
public ViewHolderDoc(#NonNull View itemView) {
super(itemView);
itemView.findViewById(R.id.rowDocument);
titleDocument = itemView.findViewById(R.id.txtDocTitle);
itemView.findViewById(R.id.txtDocData);
}
}
}

Align the text to center in ListView

I've created a stopwatch app when the user clicks the Lap button just gives the current time the timer is at. My problem is text in ListView displays at the left corner. I'm finding it impossible to center the text in my listview. Here I attached my XML and Java code.
activity_main.xml
<RelativeLayout 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"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="100dp"
android:text="format"
android:textSize="40dp"
android:textStyle="bold" />
<Button
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="110dp"
android:text="Start" />
<Button
android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#id/start"
android:text="Reset" />
<ListView
android:id="#+id/listview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/start"
android:layout_marginLeft="50dp"
android:layout_marginTop="9dp"
android:layout_marginRight="50dp"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll" />
</RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity
{
TextView time;
Button reset, start;
ListView listView;
long startTime, TimeBuff, millisecondsTime, UpdateTime = 0L;
int seconds, milliseconds, minutes;
Handler handler;
List<String> ListElementsArrayList;
ArrayAdapter<String> adapter;
String[] ListElements = new String[]{};///creating arrays of string type
#Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time = (TextView) findViewById(R.id.textView);
start = (Button) findViewById(R.id.start);
reset = (Button) findViewById(R.id.reset);
listView = (ListView) findViewById(R.id.listview1);
handler = new Handler();
//Getting the list of array
ListElementsArrayList = new ArrayList<String>(Arrays.asList(ListElements));
adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,
ListElementsArrayList);
listView.setAdapter(adapter);
}
}
To do it You have to create a layout in Your res/layout folder. With name e.g. list_item_layout.xml. In this layout add this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20sp" />
In this file, You can make custom TextView which You want to display (change font, size etc.). To center text use android:gravity="center"
Next, when You create an ArrayAdapter, use this layout:
adapter = new ArrayAdapter<String>(
MainActivity.this,
R.layout.list_item_layout, // here You use created layout
ListElementsArrayList
);
Result:

Android - Recycler View Focuses Wrong Holder on EditText click

I have a Recycler View to show a list of data and have edit text inside of Recycler View to allow user edit some number.
My app also resizing activity when the keyboard is shown/hidden.
The app works great unless I want to edit items which are placed at the bottom. When I click to edit text, it resizes the screen, shows keyboard, focuses holder but wrong one: usually one before and sometimes 2.
I tried to track issue, and understand that LinearLayoutMnager's onFocusSearchFailed function returns the wrong view.
Any idea why could it be?
Adapter
private final LinearLayoutManager linearLayoutManager;
public Observable<double[][]> observable;
private ObservableEmitter<double[][]> observe;
List<Invoice> invoices = Collections.emptyList();
double[][] amounts;
Context context;
public AdjustInvoiceAdapter(List<Invoice> list, Context context, LinearLayoutManager linearLayoutManager) {
this.invoices = list;
this.context = context;
this.linearLayoutManager = linearLayoutManager;
amounts = new double[invoices.size()][1];
for (int i = 0; i < list.size(); i++) {
amounts[i][0] = invoices.get(i).getAmountDue();
}
observable = Observable.create(new ObservableOnSubscribe<double[][]> (){
#Override
public void subscribe(ObservableEmitter<double[][]> emitter) throws Exception {
observe = emitter;
}
});
}
#Override
public InvoiceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_adjust_invoice_payment_amount, parent, false);
InvoiceHolder holder = new InvoiceHolder(view);
return holder;
}
#Override
public void onBindViewHolder(InvoiceHolder holder, final int position) {
holder.update(invoices.get(position), amounts[position][0]);
holder.amount.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() == 0 || (s.length() == 1 && (s.toString().charAt(0)) == '-'))
amounts[position][0] = 0.0;
else
amounts[position][0] = Double.parseDouble(s.toString());
observe.onNext(amounts);
}
});
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getItemCount() {
return invoices.size();
}
Holder
public class InvoiceHolder extends RecyclerView.ViewHolder {
EditText amount;
TextView invoiceAmountDue;
TextView invoiceId;
TextView invoiceDueDate;
TextInputLayout textInputLayout;
private NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.US);
public InvoiceHolder(View itemView) {
super(itemView);
invoiceId = (TextView) itemView.findViewById(R.id.invoice_number_text_view);
invoiceDueDate = (TextView) itemView.findViewById(R.id.invoice_due_text_view);
invoiceAmountDue = (TextView) itemView.findViewById(R.id.invoice_amount_due_text_view);
amount = (EditText) itemView.findViewById(R.id.editText);
textInputLayout = (TextInputLayout) itemView.findViewById(R.id.text_input_layout);
}
void update(Invoice invoice, double currentAmount) {
textInputLayout.setHint("(" + numberFormat.format(invoice.getAmountDue())+")");
invoiceId.setText(invoice.getinvoiceId());
invoiceDueDate.setText(invoice.getInvoiceDueDate());
amount.setText(currentAmount);
}
}
Layout (RecyclerView doesn't have any attribute)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:focusableInTouchMode="true"
android:focusable="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:gravity="center_vertical">
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_main_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5">
<TextView
android:id="#+id/invoice_number_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="TextView"
android:textColor="#android:color/black"
android:textSize="18sp"
tools:layout_editor_absoluteX="7dp"
tools:layout_editor_absoluteY="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="#+id/red_warning_image_view"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginRight="5dp"
android:backgroundTint="#color/ebiz_red"
android:tint="#color/ebiz_red"
android:visibility="gone"
app:srcCompat="#drawable/ic_error_black_24dp" />
<TextView
android:id="#+id/invoice_due_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1"
android:text="TextView"
android:textColor="#color/ebiz_gray_medium"
android:textSize="12sp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="#+id/linearLayout4"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.511">
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_layout"
android:layout_width="100dp"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="143dp"
tools:layout_editor_absoluteY="0dp">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="numberDecimal"
android:text="$100"
android:focusableInTouchMode="true"
android:textColor="#android:color/black"
android:textSize="18sp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Radio Button and TextField Android-studio

How can I make a radio button group and a TextField, so that whenever I click on TextField the radio button automatically changes from one to another?
Please give me a block of code for this.
If I understood your question correctly, you want the radioButton checked status change once the textView click. You can achieve by this way.
public class MainActivity extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton,male,female;
private TextView btnDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioSexGroup=(RadioGroup)findViewById(R.id.radioGroup);
btnDisplay=(TextView)findViewById(R.id.textView3);
male = (RadioButton)findViewById(R.id.radioButton);
female=(RadioButton)findViewById(R.id.radioButton2);
btnDisplay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedId=radioSexGroup.getCheckedRadioButtonId();
radioSexButton=(RadioButton)findViewById(selectedId);
if(radioSexButton.getText().toString().equals("Male"))
{
female.setChecked(true);
}
else
{
male.setChecked(true);
}
}
});
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_marginTop="58dp"
android:weightSum="1"
android:id="#+id/radioGroup"
android:layout_alignRight="#+id/textView3"
android:layout_alignEnd="#+id/textView3">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="55dp"
android:text="Male"
android:id="#+id/radioButton"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="#+id/radioButton2"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp"
android:layout_weight="0.13" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me "
android:id="#+id/textView3"
android:textSize="35dp"
android:layout_below="#+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"></TextView>
</RelativeLayout>
https://www.tutorialspoint.com/android/android_radiogroup_control.htm
check this link , to use radio group with textviews

Audio starts on list item click how to stop?

Audio is played when the user selects an item from the list, i have three buttons one is to stop the playing audio and the other two are one to play the next item on the list and one to play the previous
How would i achieve this? i have made the buttons to be clickable and then i try writing the code eg. mp.stop(); to stop the music but this is not working? and also how would i get the other buttons to play next and previous items on the list?
Below is my .java file
public class Nasheeds extends ListActivity {
//ArrayList holds the data (as HashMaps) to load into the ListView
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
//SimpleAdapter does the work to load the data in to the ListView
private SimpleAdapter sa;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nasheeds2);
//HashMap links each line of data to the correct TextView
HashMap<String,String> item;
for(int i=0;i<Nasheed.length;i++){
item = new HashMap<String,String>();
item.put( "line1", Nasheed[i][0]);
item.put( "line2", Nasheed[i][1]);
list.add( item );
}
sa = new SimpleAdapter(this, list,
R.layout.nasheeds1,
new String[] { "line1","line2" },
new int[] {R.id.displayname, R.id.title});
setListAdapter(sa);
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switch (arg2)
{
case 0:
System.out.println("User selected option 1");
MediaPlayer mp = MediaPlayer.create(Nasheeds.this, R.raw.mok);
mp.start();
TextView tv=(TextView) findViewById(R.id.selectedfile);
tv.setText("Playing "+ "Mountains of Mekkah, Zain Bikha");
break;
case 1:
System.out.println("User selected option 2");
case 2:
break;
}
}
});
}
private String[][] Nasheed =
{{"Mountains of Mekkah","Zain Bikha"},
{"Hadith 2","....add hadith...."},
{"Hadith 3",".....add hadith"},};
}
and this is my one of the nasheed2.xml file:
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:layout_weight="1.0"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/screen_background_light"
android:padding="10dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/selectedfile"
android:text="Not file selected"
android:textColor="#android:color/black"
android:gravity="center_horizontal"
android:singleLine="true"
android:ellipsize="middle"/>
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/seekbar"
android:max="100"
android:paddingBottom="10dip"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#android:drawable/screen_background_light">
<TextView
android:id="#+id/songCurrentDurationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/prev"
android:src="#android:drawable/ic_media_previous"
android:onClick="doClick"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/play"
android:src="#android:drawable/ic_media_play"
android:onClick="doClick"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/next"
android:src="#android:drawable/ic_media_next"
android:onClick="doClick"/>
<TextView
android:id="#+id/songTotalDurationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
this is the other nasheed1.xml file:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/displayname"
android:textSize="18dip"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="end"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/title"
android:textSize="15dip"
android:singleLine="true"
android:ellipsize="end"
android:layout_weight="1.0"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/duration"
android:gravity="right"
android:textSize="15dip"
android:singleLine="true"
android:ellipsize="end"/>
</LinearLayout>
</LinearLayout>
Try this below code on play button click event.
public boolean istrue = true;
btnplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (istrue) {
mp.pause();
istrue = false;
} else {
mp.start();
istrue = true;
}
}
});
or create one other button for stop audio.
on click that stop button use this
mp.stop();

Resources