Android - Recycler View Focuses Wrong Holder on EditText click - keyboard

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>

Related

the fragments do not replace each other but are superimposed android studio

I'm trying to replace a fragment in a tabbed activity with another, when I search online I find people using FragmentLayout or View but I can't find a solution that works. This is my first application, sorry if the code is not optimized.
MainActivity.java :
`
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public ViewPager viewPager;
public FragmentAbstract fragmentActuel;
public FragmentAbstract fragmentAncien;
public VPAdapter vpAdapter;
public String message;
public ViewModelFactory viewModelFactory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//on récupere le container des tableau par son id
TabLayout tabLayout = findViewById(R.id.tabs);
//page de base
viewPager = findViewById(R.id.view_pager);
//on definit la page de base
tabLayout.setupWithViewPager(viewPager);
viewModelFactory = new ViewModelFactory(this);
//on ajouter les fragments dans la page de base
this.vpAdapter = new VPAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
this.fragmentActuel = new Fragment_repetition_home(this);
this.fragmentAncien = fragmentActuel;
vpAdapter.addfragment(fragmentActuel,"REPETITION");
vpAdapter.addfragment(new Fragment_technique_home(this),"TECHNIQUE");
vpAdapter.addfragment(new Fragment_figure_home(this),"FIGURE");
viewPager.setAdapter(vpAdapter);
//pour dire que l'on veut 3 pages en arrière plan
viewPager.setOffscreenPageLimit(3);
}
}
VPAdapter.java :
public class VPAdapter extends FragmentPagerAdapter {
private ArrayList<FragmentAbstract> fragmentArrayList = new ArrayList<>();
private final ArrayList<String> fragmentTitle = new ArrayList<>();
public VPAdapter(#NonNull FragmentManager fm, int behavior) {
super(fm, behavior);
}
#NonNull
#Override
public Fragment getItem(int position) {
return fragmentArrayList.get(position);
}
#Override
public int getCount() {
return fragmentArrayList.size();
}
public void addfragment(FragmentAbstract fragment, String title){
fragmentArrayList.add(fragment);
fragmentTitle.add(title);
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return fragmentTitle.get(position);
}
//pour changer les fragments dans la liste
public void changeFragment(MainActivity mainActivity) {
if (!mainActivity.fragmentAncien.equals(mainActivity.fragmentActuel)) {
int index = fragmentArrayList.indexOf(((FragmentAbstract) mainActivity.vpAdapter.getItem(mainActivity.viewPager.getCurrentItem())));
fragmentArrayList.set(index,mainActivity.fragmentActuel);
}
}
}
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.MonProgramme.AppBarOverlay">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="?actionBarSize"
android:padding="#dimen/appbar_padding"
android:text="#string/app_name"
android:textAppearance="#style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REPETITION"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIGURE"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TECHNIQUE"/>
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
fragment_figure_home.xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/figure_home"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/selection_figure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ListView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/button_plus_figure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="#dimen/fab_margin"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_plus"
android:backgroundTint="#color/grey_2"
android:onClick="onClick"/>
</FrameLayout>
Do you have a idea for replace the fragment not superimposed ?

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);
}
}
}

Recyclerview animation not working in activity

i have an activity that contains 2 fragments
StepsFragment contains a Recyclerview but the Recyclerview's animation is not working
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:background="#B3D1DB"
tools:context=".StepsFragment">
<TextView
android:id="#+id/inputEntred"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="invisible" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/RV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:layoutAnimation="#anim/layou_animation_fall_down"
android:paddingTop="10dp"
android:paddingBottom="30dp" />
</RelativeLayout>
layou-animation-fall-down
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="#anim/fall_down"
android:animationOrder="normal"
android:delay="15%">
fall-down
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="6000">
<translate
android:fromYDelta="-20%"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:toYDelta="0"
android:duration="1000" />
<alpha
android:fromAlpha="0"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:toAlpha="3"
android:duration="1000"
/>
<scale
android:fromXScale="105%"
android:fromYScale="105%"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="100%"
android:toYScale="100%" android:duration="1000"
/>
</set>
activity layout
<androidx.constraintlayout.widget.ConstraintLayout 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=".Sorting_activity">
<RelativeLayout
android:id="#+id/triLayout"
android:layout_width="0dp"
android:layout_height="300dp"
app:layout_constraintBottom_toTopOf="#+id/stepsLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
</RelativeLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/stepsLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/triLayout"></androidx.coordinatorlayout.widget.CoordinatorLayout>
StepsFragment
public class StepsFragment extends Fragment {
private OnFragmentInteractionListener mListener;
private static TextView inputEntred ;
private static RecyclerView RV ;
private Handler mhandler = new Handler();
public StepsFragment() {
// Required empty public constructor
}
public void setTextView(String textentered){
inputEntred.setText(textentered);
String[] numberList = inputEntred.getText().toString().split(",");
final Integer[] numbers = new Integer[numberList.length];
SelectionSort m1 = new SelectionSort();
ArrayList<String> mystepsList = m1.steps(numbers);// returns the steps numbers
final StepListAdapter adapter = new StepListAdapter() ;
RV.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
RV.setLayoutManager(llm);
adapter.setList(mystepsList);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_steps, container, false);
inputEntred =view.findViewById(R.id.inputEntred);
RV =view.findViewById(R.id.RV);
return view;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
// mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}}
Could you try this? I have to remove unnecessary duration in the XML. Thanks
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="250">
<translate
android:fromYDelta="-20%"
android:toYDelta="0"
android:interpolator="#android:anim/decelerate_interpolator"
/>
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:interpolator="#android:anim/decelerate_interpolator"
/>
<scale
android:fromXScale="105%"
android:fromYScale="105%"
android:toXScale="100%"
android:toYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="#android:anim/decelerate_interpolator"
/>
</set>

getBaseline of TextView returns -1

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

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