Show PopupWindow on ListItem Click dynamically change ContentView data based on item click - android-popupwindow

I have a ListView in SomeActivity conatins employee names. On item list click i want to popup a window which displays employee details of clicked item.
i tried this popupwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="First Name"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Last Name"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/firstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/lastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/firstName" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text=" email address"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<TextView
android:id="#+id/emailAdress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="#+id/textView6"
app:layout_constraintTop_toBottomOf="#+id/lastName" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="phone number"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6" />
<TextView
android:id="#+id/phoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="#+id/textView8"
app:layout_constraintTop_toBottomOf="#+id/emailAdress" />
In SomeActivity i wrote code like this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
String poName = ((TextView)view.findViewById(R.id.rowPOName)).getText().toString();
// View layout = inflater.inflate(R.layout.detailspopup, null);
initiatePopupWindow(poName);
}
});
private void initiatePopupWindow(String name) {
try {
Cursor cursor2 = null;
LayoutInflater inflater = (LayoutInflater) SomeSample.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.detailspopup, null);
String[] selectedArgs = {name};
try {
cursor2 = db.rawQuery("my query to get employee details", selectedArgs);
if (cursor2.moveToFirst()) {
String firstname = cursor2.getString(cursor2.getColumnIndex("firstname"));
String lastname = cursor2.getString(cursor2.getColumnIndex("lastname"));
String email = cursor2.getString(cursor2.getColumnIndex("email"));
String phone = cursor2.getString(cursor2.getColumnIndex("phone"));
TextView fNname = layout.findViewById(R.id.firstName);
TextView lName = layout.findViewById(R.id.lastName);
TextView emailAdd = layout.findViewById(R.id.emailAdress);
TextView phoneNumber = layout.findViewById(R.id.phoneNumber);
fNname.setText(firstname);
lName.setText(lastname);
emailAdd.setText(email);
phoneNumber.setText(phone);
}
} catch (Exception e) {
Log.d(TAG, e.getMessage());
} finally {
if (cursor2 != null && !cursor2.isClosed()) {
cursor2.close();
}
}
popupWindow = new PopupWindow(layout, 370, 450, true);
popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
After executing code I got popup window without my employee details populated i.e TextViews are not displaying employee data. It displays only default texts i.e TextView.
need help to solve this

Related

android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>

Here is my fooddetail activity codes
public class FoodDetail extends AppCompatActivity {
TextView food_name,food_price,food_description;
ImageView food_image;
CollapsingToolbarLayout collapsingToolbarLayout;
FloatingActionButton btnCart;
ElegantNumberButton numberButton;
String foodId="";
FirebaseDatabase database;
DatabaseReference food;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_detail);
database = FirebaseDatabase.getInstance();
food = database.getReference("Food");
numberButton = (ElegantNumberButton)findViewById(R.id.number_button);
btnCart = (FloatingActionButton)findViewById(R.id.btnCart);
food_name = (TextView)findViewById(R.id.food_name);
food_description = (TextView)findViewById(R.id.food_description);
food_price = (TextView)findViewById(R.id.food_price);
food_image = (ImageView) findViewById(R.id.img_food);
collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBar);
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBar);
if (getIntent() != null)
foodId = getIntent().getStringExtra("FoodId");
if(!foodId.isEmpty())
{
getDetailFood(foodId);
}
}
private void getDetailFood(String foodId)
{
food.child(foodId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Food food = dataSnapshot.getValue(Food.class);
Picasso.with(getBaseContext()).load(food.getImage()).into(food_image);
collapsingToolbarLayout.setTitle(food.getName());
food_price.setText(food.getPrice());
food_name.setText(food.getName());
food_description.setText(food.getDescription());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
and here is my xml
<android.support.design.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="com.order.m.jersonsordering.FoodDetail">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
android:id="#+id/app_bar_layout">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/collapsing"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="#0e0d0e">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#null"
app:layout_collapseMode="parallax"
android:id="#+id/img_food"
android:scaleType="centerCrop" />
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="parallax"
android:id="#+id/toolbar"
app:title="Food Name">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnCart"
android:src="#drawable/ic_shopping_cart_black_24dp"
android:backgroundTint="#android:color/white"
android:elevation="6dp"
app:pressedTranslationZ="12dp"
app:layout_anchor="#id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end"
app:useCompatPadding="true"/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/nestedScrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/food_name"
android:layout_marginTop="8dp"
android:padding="12dp"
android:textColor="#000000"
android:text="Food Name"
android:textSize="21sp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layout_price"
android:orientation="horizontal">
<TextView
android:layout_width="1dp"
android:layout_weight="9"
android:layout_height="wrap_content"
android:id="#+id/food_price"
android:layout_marginTop="8dp"
android:padding="12dp"
android:textColor="#000000"
android:text="1,000PHP"
android:textStyle="bold"
android:textSize="15sp"/>
</LinearLayout>
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:layout_width="100dp"
android:layout_height="30dp"
android:id="#+id/number_button"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="18dp"
app:textSize="8sp"
app:backGroundColor="#color/colorAccent"
app:textColor="#000000"
app:initialNumber="1"
app:finalNumber="20">
</com.cepheuen.elegantnumberbutton.view.ElegantNumberButton>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/food_description"
android:layout_marginTop="12dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="Description"
android:textColor="#000000"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
I dont know what is causing the error and i cant resolve it I tried to rebuild and clean the project
I also tried searching for similar error but none is showing
the app keeps on closing and then thats the error(attached photo)
pls help me thank you :)
First make sure that you have included design library implementation 'com.android.support:design:26.+' in your application level build.gradle.
Then replace:
<android.support.design.widget.FloatingActionButton
...
...
android:backgroundTint
/>
with
<android.support.design.widget.FloatingActionButton
...
...
app:backgroundTint
/>

How to set color in odd and even position in recyclerview?

Position break in recyclerview
(https://drive.google.com/file/d/1EZADHjrpn8ZwryvQ6qPf1xU1hcG0-Mgf/view?usp=drivesdk)
You can get this by following manner.
Add this code to your onBindViewHolder() method of adapter. Like this:
#Override
public void onBindViewHolder(ViewHolder holder, int position)
{
if(position % 2 == 0)
{
//holder.rootView.setBackgroundColor(Color.BLACK);
holder.rootView.setBackgroundResource(R.color.black);
}
else
{
//holder.rootView.setBackgroundColor(Color.WHITE);
holder.rootView.setBackgroundResource(R.color.white);
}
}
After that surround your layout xml file that contains card design for example like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="72dp"
android:background="#color/my_white"
android:orientation="horizontal">
<ImageView
android:id="#+id/folder_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:src="#drawable/folder_icon" />
<TextView
android:id="#+id/folder_name"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="The Mills"
android:textColor="#color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/folder_content_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:src="#drawable/folder_content_icon" />
<TextView
android:id="#+id/content_number"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:gravity="left"
android:text="3"
android:textColor="#color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
After that create variable in your View Holder class of adapter like this:
public static class ViewHolder extends RecyclerView.ViewHolder
{
LinearLayout rootView;//newly added field
public ViewHolder(View view)
{
super(view);
rootView=(LinearLayout)view.findViewById(R.id.rootView);
}
}
It will work for you to get the required results.

Android Grid Menu Layout

i am a beginner in android programming.( i am using android studio for coding)
I am trying to design a dashboard for my android application taking guidance from the following link
and its working well, but i want to make it as per
Like This Image
i need 2 column layout with icon image, Title 1 and title 2 with a Background image.
can anyone help me.
thank you
you have to use customGridView using BaseAdapter. In customGridView show the ImageView with TextView for each list item.
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" >
<GridView
android:id="#+id/gridViewCustom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</RelativeLayout>
grid_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
CustomGridViewMainActivity.java
public class CustomGridViewMainActivity extends Activity
{
GridView gridView;
GridViewCustomAdapter grisViewCustomeAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView=(GridView)findViewById(R.id.gridViewCustom);
// Create the Custom Adapter Object
grisViewCustomeAdapter = new GridViewCustomAdapter(this);
// Set the Adapter to GridView
gridView.setAdapter(grisViewCustomeAdapter);
// Handling touch/click Event on GridView Item
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
String selectedItem;
if(position%2==0)
selectedItem="Facebook";
else
selectedItem="Twitter";
Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();
}
});
}
}
then set the adapter with your customized view
GridViewCustomAdapter.java
public class GridViewCustomAdapter extends ArrayAdapter
{
Context context;
public GridViewCustomAdapter(Context context)
{
super(context, 0);
this.context=context;
}
public int getCount()
{
return 24;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
TextView textViewTitle = (TextView) row.findViewById(R.id.textView);
ImageView imageViewIte = (ImageView) row.findViewById(R.id.imageView);
if(position%2==0)
{
textViewTitle.setText("Facebook");
imageViewIte.setImageResource(R.drawable.facebook);
}
else
{
textViewTitle.setText("Twitter");
imageViewIte.setImageResource(R.drawable.twitter);
}
}
return row;
}
}
Output:
At last, I found the solution...
your XML File`
<?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="#drawable/bg"
android:padding="16dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:layout_marginBottom="5dp"
android:textColor="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:layout_marginBottom="5dp"
android:textColor="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
`
then AndroidButtonWithIconAndText.java class
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class AndroidButtonWithIconAndText extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.android_button_with_icon_text);
}
}
and finally it may look like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
</LinearLayout>

Android: Spinner in fragments

I don't know what is happening here, I am new to Android. Actually when I click on the spinner it doesn't show anything, the first value is selected by default and when I click the spinner nothing happens. I have tried it different devices and avd too still I don't know what to do. I have tried another method too (Implementing OnItemSelectedListener) but still same result. There is no error but the code doesn't seem to work and I have debugged the application too and the adapter has the 7 items but still no use.
Result Image:
When I opened the fragment the spinner with selected value and Toast:
import static android.R.layout.simple_spinner_dropdown_item;
import static android.R.layout.simple_spinner_item;
public class Attendance_fragment extends android.app.Fragment {
public Attendance_fragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_attendance_layout, container, false);
Spinner spinner;
spinner =(Spinner)view.findViewById(R.id.spinnerattendancedates);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(getActivity(),R.array.attendance_dates, simple_spinner_item);
adapter.setDropDownViewResource(simple_spinner_dropdown_item);
adapter.setNotifyOnChange(true);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(getActivity(), "Selected " + item, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getActivity(), "Selected " , Toast.LENGTH_LONG).show();
}
});
return view;
}
fragment_attendance_layout.xml
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:id="#+id/spinnerattendancedates"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="bottom"
android:padding="10dp"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:layout_width="match_parent"
android:layout_height="440dp"
android:layout_gravity="bottom|center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="150dp"
android:layout_height="50dp"
android:text="#string/period1"
android:id="#+id/textView"
android:layout_column="0"
android:layout_gravity="center" />
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/absentorpresent"
android:id="#+id/textView2"
android:layout_column="1"
android:layout_gravity="center_horizontal" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="150dp"
android:layout_height="50dp"
android:text="#string/period2"
android:id="#+id/textView3"
android:layout_column="0"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/absentorpresent"
android:id="#+id/textView4"
android:layout_column="1"
android:layout_gravity="center_horizontal" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="150dp"
android:layout_height="50dp"
android:text="#string/period3"
android:id="#+id/textView5"
android:layout_column="0"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/absentorpresent"
android:id="#+id/textView11"
android:layout_column="1"
android:layout_gravity="center_horizontal" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="150dp"
android:layout_height="50dp"
android:text="#string/period4"
android:id="#+id/textView6"
android:layout_column="0"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/absentorpresent"
android:id="#+id/textView12"
android:layout_column="1"
android:layout_gravity="center_horizontal" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="150dp"
android:layout_height="50dp"
android:text="#string/period5"
android:id="#+id/textView7"
android:layout_column="0"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/absentorpresent"
android:id="#+id/textView13"
android:layout_column="1"
android:layout_gravity="center_horizontal" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="150dp"
android:layout_height="50dp"
android:text="#string/period6"
android:id="#+id/textView8"
android:layout_column="0"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/absentorpresent"
android:id="#+id/textView14"
android:layout_column="1"
android:layout_gravity="center_horizontal" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="150dp"
android:layout_height="50dp"
android:text="#string/period7"
android:id="#+id/textView9"
android:layout_column="0"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/absentorpresent"
android:id="#+id/textView15"
android:layout_column="1"
android:layout_gravity="center_horizontal" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="150dp"
android:layout_height="50dp"
android:text="#string/period8"
android:id="#+id/textView10"
android:layout_column="0"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/absentorpresent"
android:id="#+id/textView16"
android:layout_column="1"
android:layout_gravity="center_horizontal" />
</TableRow>
</TableLayout>
</ScrollView>
</RelativeLayout>
My strings.XML
<string-array name="attendance_dates">
<item>Date 1</item>
<item>Date 2</item>
<item>Date 3</item>
<item>Date 4</item>
<item>Date 5</item>
<item>Date 6</item>
<item>Date 7</item>
</string-array>
Finally corrected it. My problem was i have an "android.support.v4.widget.NestedScrollView" in Navigation Drawer Activity from where i open this Fragment. When I removed this. It worked. This is also similar for buttons which is not clickable at some circumstances.

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