Scroll view hidden due to no view found error - java

I've just implemented a scrollview but then during deployment, I get this error and I don't know why + don't know what's wrong with my code.
No view found for id 0x7f0e006c (com.blah.blah:id/container) for fragment FragmentAbout
fragment_about.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarThumbVertical="#drawable/light_scrollbar">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:contentDescription="#string/app_name"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:src="#drawable/ic_launcher"
/>
<TextView
android:id="#+id/textView1"
android:text="#string/app_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
style="#android:style/TextAppearance.Large"/>
<TextView
android:id="#+id/textView3"
android:text="#string/version_no."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
style="#android:style/TextAppearance.Small"/>
<TextView
android:id="#+id/textView4"
android:text="#string/copyright_info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingBottom="10dp"
style="#android:style/TextAppearance.Small"/>
<TextView
android:id="#+id/textView5"
android:text="#string/developer_info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingBottom="10dp"
style="#android:style/TextAppearance.Small"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
activity_about.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/fragmentabout" >
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/actionBar"
android:elevation="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetEnd="0dp"
app:contentInsetStart="16dp" >
</android.support.v7.widget.Toolbar>
<LinearLayout
android:id="#+id/container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
AboutActivity.java
public class AboutActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Toolbar toolbar = (Toolbar) findViewById(R.id.actionBar);
if (toolbar != null) {
setSupportActionBar(toolbar);
toolbar.setBackgroundColor(Color.parseColor("#212121"));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
}
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new FragmentAbout())
.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_about, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
final Intent intent = NavUtils.getParentActivityIntent(this);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
NavUtils.navigateUpTo(this, intent);
return true;
}
int id = item.getItemId();
if (id == R.id.action_0) {
new AlertDialog.Builder(this)
.setTitle("Hello World")
.setMessage("Hello World")
.show();
}
if (id == R.id.action_1) {
new AlertDialog.Builder(this)
.setTitle("Hello World")
.setMessage("Hello World")
.show();
}
return super.onOptionsItemSelected(item);
}
}

You're setting your content view to the fragment's layout. That's not what you want. You want to set it to the activity's layout, R.id.activity_about

Related

How to make the top bar?

Why when I write this code the application crashing?
Toolbar mToolbar = findViewById(R.id.scroll_top_bar);
setSupportActionBar(mToolbar);
This is the xml code
<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="match_parent"
android:orientation="vertical"
tools:context=".DisplayInformations">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:liftOnScroll="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher_background" />
<androidx.appcompat.widget.Toolbar
android:id="#+id/scroll_top_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
</LinearLayout>
This is the java code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_informations);
Toolbar mToolbar = findViewById(R.id.scroll_top_bar);
setSupportActionBar(mToolbar);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_display_informations,menu);
return true;
}
"You may find a mistake in my question because I am new to the stack over flow platform"
Try this one article: https://guides.codepath.com/android/using-the-app-toolbar
And also this one:
https://www.programcreek.com/java-api-examples/?api=androidx.appcompat.widget.Toolbar

How to set title and back arrow button in toolbar for each fragment?

//Here is my fragment
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white">
<RelativeLayout
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="#dimen/_420sdp"
android:background="#EAEAEA"
>
<RelativeLayout
android:id="#+id/home_bg_layout"
android:layout_width="match_parent"
android:layout_height="#dimen/_400sdp"
android:background="#drawable/sama">
<Button
android:id="#+id/bookAppointmentBtn"
android:layout_width="match_parent"
android:layout_height="#dimen/_55sdp"
android:layout_marginLeft="#dimen/_30sdp"
android:layout_marginRight="#dimen/_30sdp"
android:fontFamily="#string/roboto_light"
android:textSize="#dimen/_20sdp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="#dimen/_10sdp"
android:background="#drawable/book_appointmentbg"
android:textColor="#color/white"
android:text="#string/bookappointment" />
</RelativeLayout>
</RelativeLayout>
<Button
android:id="#+id/ContactNoBtn"
android:layout_width="match_parent"
android:layout_height="#dimen/_46sdp"
android:text="#string/shop_number"
android:textColor="#ffffff"
android:textSize="#dimen/_25sdp"
android:layout_below="#+id/discountsTv"
android:textAllCaps="false"
android:layout_marginRight="#dimen/_30sdp"
android:layout_marginTop="#dimen/_5sdp"
android:layout_marginLeft="#dimen/_30sdp"
android:background="#drawable/black_background"/>
<LinearLayout
android:id="#+id/servicesLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:layout_marginLeft="#dimen/_20sdp"
android:layout_marginRight="#dimen/_20sdp"
android:orientation="horizontal"
android:layout_below="#+id/servicesupperview"
>
<LinearLayout
android:id="#+id/menLayout"
android:layout_width="#dimen/_108sdp"
android:layout_height="#dimen/_27sdp"
android:layout_margin="#dimen/_10sdp"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#string/roboto_medium"
android:textColor="#535353"
android:layout_marginStart="#dimen/_17sdp"
android:layout_marginTop="#dimen/_5sdp"
android:textSize="#dimen/_12sdp"
android:text="#string/mens"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/womenLayout"
android:layout_width="#dimen/_108sdp"
android:layout_height="#dimen/_27sdp"
android:layout_margin="#dimen/_10sdp"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#string/roboto_medium"
android:textSize="#dimen/_12sdp"
android:layout_marginStart="#dimen/_10sdp"
android:layout_marginTop="#dimen/_5sdp"
android:textColor="#535353"
android:text="#string/womens"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/childrenLayout"
android:layout_width="#dimen/_108sdp"
android:layout_height="#dimen/_27sdp"
android:layout_margin="#dimen/_10sdp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/_12sdp"
android:fontFamily="#string/roboto_medium"
android:textColor="#535353"
android:layout_marginStart="#dimen/_20sdp"
android:layout_marginTop="#dimen/_5sdp"
android:text="#string/children_caps"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
I am having toolbar in MainActivity. This is my fragment i want to set title, image and back button inside the toolbar. Here i am using bottom navigation to change the fragments. Each and every fragment i want to set different title and image. I have to set title as center of the toolbar and back button as right corner of the toolbar.
In your MainActivity add these lines in OnCreate() method
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle("My Activity Title");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
To use Back arrow you can write this code outside onCreate()
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Toast.makeText(this, "Back button pressed", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
In you fragment you can change title of Toolbar
getActivity().setTitle("My fragment Title");
You can Create custom Toolbar with ImageView and TextView with your arrow .
include_tool_bar.xml
<com.mastercard.wallet.ui.widget.CustomTextView
android:id="#+id/toolbar_title"
style="#style/Toolbar.TitleText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|left"
android:textSize="#dimen/px_48"
app:fontName="InterStateLight"
customviews:fontName="InterStateLight" />
<ImageView
android:id="#+id/toolbar_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="#string/adaCitiPayLogoText"
android:src="#drawable/ab_logo"
android:visibility="gone" />
And Add Toolbar
Toolbar toolbar = (Toolbar) appCompatActivity.findViewById(R.id.toolbar);
ImageView logo = (ImageView) toolbar.findViewById(R.id.toolbar_logo);
CustomTextView textView = (CustomTextView) toolbar.findViewById(R.id.toolbar_title);
logo.setVisibility(View.VISIBLE);
logo.setImageResource(logoResourceId);
textView.setVisibility(View.GONE);
appCompatActivity.setSupportActionBar(toolbar);
appCompatActivity.getSupportActionBar().setDisplayShowTitleEnabled(false);
title of the toolbar can be changed from fragment calling this in onActivityCreated
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((MainActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.menu_about_us));
((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((MainActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
}
in your activity make sure to add a toolbar like this.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
and set it in your activity
setSupportActionBar(findViewById(R.id.toolbar));
and for the back press just add a listener and do the needful.
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// pop up fragment or take another action
}
});

How to show shopping cart icon in activity?

I made the shopping cart app and I use the navigation drawer and in this navigation drawer I use the shopping cart icon.
Now, When i click on the particular item. It's open in new activity but its not showing the shopping cart icon, So how will it show? So,i see the item in the cart?
ItemDetailsActivity. java (This is the .java file of this image where i am unable to see the shopping cart icon, so i am unable to see how many items add in the cart)
public class ItemDetailsActivity extends AppCompatActivity {
int imagePosition;
String stringImageUri;
TextView textViewshare, textViewmap;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_details);
SimpleDraweeView mImageView = (SimpleDraweeView)findViewById(R.id.image1);
TextView textViewAddToCart = (TextView)findViewById(R.id.text_action_bottom1);
TextView textViewBuyNow = (TextView)findViewById(R.id.text_action_bottom2);
textViewshare = (TextView) findViewById(R.id.text_action1);
textViewmap = (TextView) findViewById(R.id.text_action3);
TextView textViewBuyNowwithpayment = (TextView) findViewById(R.id.text_action_bottom2);
//Getting image uri from previous screen
if (getIntent() != null) {
stringImageUri = getIntent().getStringExtra(ImageListFragment.STRING_IMAGE_URI);
imagePosition = getIntent().getIntExtra(ImageListFragment.STRING_IMAGE_URI,0);
}
Uri uri = Uri.parse(stringImageUri);
mImageView.setImageURI(uri);
mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ItemDetailsActivity.this, ViewPagerActivity.class);
intent.putExtra("position", imagePosition);
startActivity(intent);
}
});
textViewAddToCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ImageUrlUtils imageUrlUtils = new ImageUrlUtils();
imageUrlUtils.addCartListImageUri(stringImageUri);
Toast.makeText(ItemDetailsActivity.this,"Item added to cart.",Toast.LENGTH_SHORT).show();
MainActivity.notificationCountCart++;
NotificationCountSetClass.setNotifyCount(MainActivity.notificationCountCart);
}
});
textViewBuyNow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ImageUrlUtils imageUrlUtils = new ImageUrlUtils();
imageUrlUtils.addCartListImageUri(stringImageUri);
MainActivity.notificationCountCart++;
NotificationCountSetClass.setNotifyCount(MainActivity.notificationCountCart);
startActivity(new Intent(ItemDetailsActivity.this, CartListActivity.class));
}
});
// payment.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// Intent i = new Intent(ItemDetailsActivity.this, PayPalCheckoutActivity.class);
// startActivity(i);
// }
// });
textViewBuyNowwithpayment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ItemDetailsActivity.this, PayPalCheckoutActivity.class);
startActivity(i);
}
});
textViewshare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
intent.putExtra(Intent.EXTRA_TEXT,"Extra Text");
startActivity(intent);
}
});
textViewmap.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(ItemDetailsActivity.this, Placepicker.class);
startActivity(in);
}
});
}
}
activity_item_details.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/activity_item_details"
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:orientation="vertical"
android:weightSum="10"
tools:context="com.codeexpertise.eshop.product.ItemDetailsActivity">
<ScrollView android:id="#+id/scrollbar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9.5"
android:scrollbars="none"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.facebook.drawee.view.SimpleDraweeView xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:id="#+id/image1"
android:layout_width="match_parent"
android:layout_height="200.0dp"
fresco:placeholderImage="#color/stay_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Denim Shirt"
android:textSize="16dp"
android:textColor="#color/gen_black"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Rs. 1,979"
android:textSize="20dp"
android:textColor="#color/gen_black"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FREE Delivery"
android:textSize="12dp"
android:layout_marginTop="4dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="8dp">
<TextView android:id="#+id/text_ratings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/green_light"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:text="4.3 *"
android:textSize="12dp"
android:textColor="#color/gen_white"
android:textStyle="bold"/>
<TextView android:id="#+id/text_ratings_reviews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:text="50 ratings \u0026 15 reviews"
android:textSize="12dp"/>
</LinearLayout>/
<View android:layout_width="match_parent"
android:layout_height="#dimen/view_width_small"
android:background="#color/grey_light"
android:layout_marginTop="8dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:layout_marginTop="8dp"
android:weightSum="3">
<LinearLayout android:id="#+id/layout_action1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="2">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_share_black_18dp"/>
<TextView android:id="#+id/text_action1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="8dp"
android:text="Share"
android:showAsAction="ifRoom"
android:textSize="12dp"
android:textColor="#color/gen_black"
android:gravity="left"
android:actionProviderClass=
"android.widget.ShareActionProvider"/>
</LinearLayout>
<View android:layout_width="#dimen/view_width_small"
android:layout_height="match_parent"
android:background="#color/grey_light"/>
<LinearLayout android:id="#+id/layout_action2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="2">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_filter_none_black_18dp"/>
<TextView android:id="#+id/text_action2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="8dp"
android:text="Similar"
android:textSize="12dp"
android:textColor="#color/gen_black"
android:gravity="left"/>
</LinearLayout>
<View android:layout_width="#dimen/view_width_small"
android:layout_height="match_parent"
android:background="#color/grey_light"/>
<LinearLayout android:id="#+id/layout_action3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="2">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_favorite_border_black_18dp"/>
<TextView android:id="#+id/text_action3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_marginLeft="8dp"
android:text="Store Locator"
android:textSize="12dp"
android:textColor="#color/gen_black"
android:gravity="left"/>
</LinearLayout>
</LinearLayout>
<View android:layout_width="match_parent"
android:layout_height="#dimen/view_width_small"
android:background="#color/grey_light"
android:layout_marginTop="8dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Details"
android:textSize="16dp"
android:textColor="#color/gen_black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="\u2022 Regular fit, full sleeve"
android:textSize="12dp"
android:textColor="#color/gen_black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="\u2022 Fabric: Cotton"
android:textSize="12dp"
android:textColor="#color/gen_black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="\u2022 Pattern: printed"
android:textSize="12dp"
android:textColor="#color/gen_black"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="horizontal"
android:weightSum="2"
android:elevation="30dp"
android:background="#color/gen_black">
<TextView android:id="#+id/text_action_bottom1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/gen_white"
android:text="ADD TO CART"
android:textSize="14dp"
android:textColor="#color/gen_black"
android:textStyle="bold"
android:gravity="center"/>
<TextView android:id="#+id/text_action_bottom2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#4dc3ff"
android:text="BUY NOW"
android:textSize="14dp"
android:textColor="#color/gen_white"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
Main.xml (In this xml i use the menu item which show in navigation drawer bar)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/ic_search_white_24dp"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="#+id/action_notifications"
android:title="#string/action_notifications"
app:showAsAction="always"
android:icon="#drawable/ic_notifications_white_24dp"/>
<item android:id="#+id/action_cart"
android:title="#string/action_cart"
app:showAsAction="always"
android:icon="#drawable/ic_menu_notifications"/>
</menu>
Please Override this Method in your ItemDetailsActivity to Show Menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
verride this Method in your ItemDetailsActivity to Handle Click Events
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_cart:
dosomething();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Java Android : onItemLongClick for listView doesn't respond

I'm developing an Android application.
I want to display a list of contacts (using listview) and I want to add 2 alerts dialogs: one by onItemClickListener and the other by onItemLongClickListener.
But the onItemLongClickListener is never called, just the onItemClickListener.
I already did it for another application, but here it doesn't work and I don't find why :/
My code:
public class ContactActivity extends AppCompatActivity {
private ListView listView;
private ContactAdapter adapter; //my adapter
private ArrayList<Contact> contacts; //my list of contacts
//...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
//here i init my "listView" and "contacts"
adapter = new ContactAdapter(this, contacts);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("longClick", "ok");
return true;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> a, View v, final int position, long id) {
//here i display a Dialog
}
});
//some code
So, when I "simple click" on an item of the list, the dialog appears (ok).
But when I "long click" on an item of the list, the dialog appears and i don't see the Log...
(The item that i use for my ListView got just 2 EditText)
PS: the XML where my listView is shown
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#color/white">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:id="#+id/screen">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:background="#drawable/w_aff2" />
</FrameLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="20"
android:weightSum="100">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="80">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="14dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_back"
android:layout_gravity="center"
android:background="#drawable/grey_button"
android:src="#drawable/i_back_blue" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="14dp">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="14dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_add"
android:src="#drawable/i_add_blue"
android:background="#drawable/grey_button"
android:layout_gravity="center" />
</FrameLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
PS2: Don't look at the Log, I put that for example, but if i put anything else nothing happens, just the dialog appears...
PS3: The XML i use for each item of the listView
<?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="wrap_content"
android:id="#+id/item_screen"
android:background="#color/white">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
android:id="#+id/item_layout"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:padding="7dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/layout_label">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="#+id/item_label"
android:layout_weight="1"
android:textColor="#color/grey_dark"
android:gravity="center_vertical"
android:textStyle="bold"
android:textSize="16dp" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/empty_layout">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/layout_value"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="#+id/item_value"
android:textColor="#color/grey_dark"
android:textStyle="bold"
android:gravity="center_vertical|center_horizontal"
android:textSize="16dp" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
SOLUTION
I had a onSwipeRight() on my listener, it blocked the onItemLongClickListener
here is the code that was blocking the listener
listView.setOnTouchListener(new OnSwipeTouchListener(getApplicationContext()) {
public void onSwipeRight(){
//some code
}
});
Big thanks to ddb for his time :)
why are you passing a new AdapterView.OnItemLongClickListener?
just do like this below instead
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("longClick", "ok");
return true;
}
});
Do not set listView.setLongClickable(true);, listView.setClickable(true); in your java.
Neither
android:longClickable="true"
android:clickable="true"
Inside your xml

The application may be doing too much work on its main thread. Even using AsyncTask

I am creating a android application.
In my app I have one listView and one gridView and when I set the addapter of those view I get the The application may be doing too much work on its main thread
I have try to put those setadapter on a AsyncTask but nothing change.. I do not understand.
My Activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showToolbarTitle(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
toolbarTitle.setText("PARALLAX + FADING IMAGEVIEW");
toolbarTitle.setVisibility(View.INVISIBLE);
scrollview.setScrollViewCallbacks(this);
listView = (ListView) findViewById(R.id.listview);
gridView = (GridView) findViewById(R.id.gridView);
new LongOperation().execute(); //I call the AsyncTask
gridView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true;
}
return false;
}
});
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
CustomList adapter = new
CustomList(ParallaxLikeIoshedActivity.this, test, imageId, false);
// listView.setAdapter(adapter); // If I do this I will get the error
CustomList adapterGrid = new
CustomList(ParallaxLikeIoshedActivity.this, test, imageId, true);
// gridView.setAdapter(adapterGrid); // If I do this I will get the error
return "Executed";
}
#Override
protected void onPostExecute(String result) {
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... values) {}
}
This is the layout of the Activity :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
android:id="#+id/scrollview"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/image_container"
android:layout_width="match_parent"
android:layout_height="250dp">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#000000"/>
<!--<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/moviethumbnail"
tools:ignore="ContentDescription" />-->
</FrameLayout>
<LinearLayout
android:id="#+id/header"
android:layout_below="#id/image_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:background="#222"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/linearLayout">
<TextView
android:textAppearance="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MHeader 1 !!!"
android:textSize="40sp"
android:textColor="#color/white"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:background="#ff7f7f7f"
android:showDividers="middle">
<com.example.edouard.NonScrollListView
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingTop="20dp"
android:divider="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listview"
android:visibility="visible"/>
<GridView
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingTop="20dp"
android:divider="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/gridView"
android:visibility="invisible"/>
</FrameLayout>
</RelativeLayout>
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>
</FrameLayout>

Categories