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);
}
}
Related
I am trying to apply a setOnClickListener on an EditText View in a Fragment and for some reason, It is not working, the code in the listener doesn't execute when I click on the EditText.
The fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<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="16dp"
tools:context=".add_payment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/testpay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:ems="10"
android:inputType="time" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Table:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Amount:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_amm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" />
</LinearLayout>
</LinearLayout>
The fragment code:
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_payment_fragment, container, false);
view.findViewById(R.id.add_pay_time).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "aaaa", Toast.LENGTH_LONG).show();
}
});
return view;
}
Edit text needs focus to call onClick().
Solution 1
request focus when you set listener:
EditText editText = view.findViewById(R.id.add_pay_time);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "aaaa", Toast.LENGTH_LONG).show();
}
});
editText.requestFocus(); //request focus
Solution 2
set on touch listener:
EditText editText = view.findViewById(R.id.add_pay_time);
editText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction())
Toast.makeText(getContext(), "aaaa", Toast.LENGTH_LONG).show();
return false;
}
});
Solution 3
you can use onClickListener and onFocusChangeListener together:
EditText editText = view.findViewById(R.id.add_pay_time);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showToast();//call your method
}
});
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
showToast();//call your method
}
});
EditText edText = view.findViewById(R.id.add_pay_time);
edText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "aaaa", Toast.LENGTH_LONG).show();
}
});
Use getActivity().getApplicationContext() instead
I implemented Bottom navigation bar in my project. When I declare and initialize buttons and then set onClickListener for them. The onclicklistener is not working fine without any error in logcat. I think its logical mistake that I'm unable to understand please see the code and guide me. (If you think the question is not according to community standards then I'm sorry in advance because I'm a beginner in java)
I searched a lot of questions related to this but nothing works for me.
Here is my main activity:
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
//I added this if statement to keep the selected fragment when rotating the device
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
}
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_notifications:
selectedFragment = new NotificationFragment();
break;
case R.id.nav_search:
selectedFragment = new SearchFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
}
Here is my First Fragment named HomeFragment:
public class HomeFragment extends Fragment implements View.OnClickListener {
Button btnFertilizers;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
btnFertilizers = (Button)v.findViewById(R.id.btnFertilizers);
return inflater.inflate(R.layout.fragment_home, container, false);
}
#Override
public void onClick(View view) {
btnFertilizers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),Fertilizers.class);
getActivity().startActivity(intent);
}
});
}
Logcat shows zero error.
I believe that here is the problem area. This is because the click function is not working properly for Fertilizers.class:
#Override
public void onClick(View view) {
btnFertilizers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),Fertilizers.class);
getActivity().startActivity(intent);
}
});
}
Here is my FragmentHome.xml :
<RelativeLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_light">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="230dp"
android:orientation="vertical"
android:gravity="center"
android:background="#drawable/agriculture_home">
<ImageView
android:layout_width="110dp"
android:layout_height="110dp"
android:src="#drawable/e_agriculture_logo"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-Agriculture"
android:textColor="#ffffff"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="29sp"
android:layout_marginTop="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="215dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="21dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="0dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/fertilizers"
android:layout_width="144dp"
android:layout_height="113dp"
android:layout_centerInParent="true"
android:src="#drawable/fertilizers_png" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fertilizers"
android:text="Fertilizers"
android:textSize="20sp"
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="0dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnFertilizers"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/seeds"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/seed" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/seeds"
android:text="Seeds"
android:gravity="center"
android:textColor="#color/colorBlack"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btn_seeds"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="21dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="0dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/deseases"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/deseases" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/deseases"
android:text="Diseases"
android:textSize="20sp"
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btn_deseases"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewPesticides"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/pesticides" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewPesticides"
android:text="Pesticides"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnPesticides"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="21dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="0dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewWeatherForecast"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/weatherforecast" />
<TextView
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewWeatherForecast"
android:text="Weather Forecast"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnWeatherForecast"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewArticles"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/video" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewArticles"
android:text="Videos"
android:textSize="20sp"
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnArticles"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="21dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="0dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewFeedback"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/feedback" />
<TextView
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewFeedback"
android:text="Feedback"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnFeedback"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewLocation"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/location_icon" />
<TextView
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewLocation"
android:text="Location"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
tools:ignore="NotSibling" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnLocation"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Here is my MainActivity.xml file:
<RelativeLayout 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=".HomeActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/bottom_navigation"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_navigation"
app:itemIconTint="#color/colorWhite"
app:itemTextColor="#color/colorWhite"
android:background="#color/colorGreen"/>
</RelativeLayout>
How I should setOnClickListener to access my wanted activity?
Move this code into onCreateView.
btnFertilizers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),Fertilizers.class);
getActivity().startActivity(intent);
}
});
remove this
implements View.OnClickListener
remove this
#Override
public void onClick(View view) { }
You are calling another onClick lisnter inside the onclick method so it will no work.You should have to change the code like this:
#Override
public void onClick(View view) {
if (view.getId() == R.id.btnFertilizers) {
Intent intent = new Intent(getActivity(),Fertilizers.class);
getActivity().startActivity(intent);
}
Return v in your onCreateViewMethod .
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
btnFertilizers = (Button)v.findViewById(R.id.btnFertilizers);
return v;
}
I want the code in the Java file to set the background color of whatever button was clicked, and keep it, even after you let go of the button. Thanks in advance
I don't really get the formatting on here. I hope it's clear what belongs where.
Java:
package mika.actual;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class accordion extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accordian);
Button btnProfile = (Button) findViewById(R.id.btnProfile);
Button btnSettings = (Button) findViewById(R.id.btnSettings);
Button btnPrivacy = (Button) findViewById(R.id.btnPrivacy);
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
btnProfile.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.VISIBLE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
}
});
btnSettings.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.VISIBLE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
}
});
btnPrivacy.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.VISIBLE);
}
});
}
}
XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"
android:orientation="vertical">
<Button
android:id="#+id/btnProfile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Profile"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="#+id/panelProfile"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF">
<LinearLayout
android:id="#+id/panelProfile1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="#+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelProfile2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname" />
<EditText
android:id="#+id/txtSurname"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/btnSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="#+id/panelSettings"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/panelSettings1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="e-mail" />
<EditText
android:id="#+id/txtMail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelSettings2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone" />
<EditText
android:id="#+id/txtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/btnPrivacy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Privacy"
android:textColor="#FFFFFFFF" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/panelPrivacy"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<CheckBox
android:id="#+id/checkFacebook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Facebook"
android:textColor="#ff355689">
</CheckBox>
<CheckBox
android:id="#+id/checkLinkedIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LinkedIn"
android:textColor="#ff355689">
</CheckBox>
<CheckBox
android:id="#+id/checkTwitter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Twitter"
android:textColor="#ff355689">
</CheckBox>
</LinearLayout>
</ScrollView>
</LinearLayout>
If I understood correctly, what you want to do is, when clicking a button and expanding its sub-menu, change the background of that button until it is clicked again. You can do this with a Tag on the Button:
btnSettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
//if button is not selected, change background and tag the View as selected
if(v.getTag() == null || v.getTag().equals("not_selected")) {
v.setBackgroundColor(Color.RED);
v.setTag("selected");
//show sub-menu
}
//if button is already selected, reset background and tag the View as not selected
else{
v.setBackgroundResource(android.R.drawable.btn_default);
v.setTag("not_selected");
//hide sub menu
}
}
});
Notice that v.setBackgroundResource(android.R.drawable.btn_default); is resetting the Button background to an android drawable. You may want to change this to a custom drawable or color.
You could have used an ExpandableListView to implement this kind of menu.
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
view.setBackgroundColor(ContextCompat.getColor(context, R.color.presed_color));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
view.setBackgroundColor(ContextCompat.getColor(context, R.color.unpresed_color));
}
return false;
}
});
I'm trying to create an application with 5 buttons on the main page. For some reason the ListenerOnButton is not working and the app will close before showing xml buttons layout.
Here's my MainActivity:
public class MainListActivity extends Activity {
ImageButton news;
ImageButton weather;
ImageButton counter;
ImageButton fakoi;
ImageButton gyalia;
ImageButton uvindex;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
addListenerOnButton();
}
public void addListenerOnButton() {
news = (ImageButton) findViewById(R.id.news);
weather = (ImageButton) findViewById(R.id.weather);
fakoi = (ImageButton) findViewById(R.id.fakoi);
gyalia = (ImageButton) findViewById(R.id.gyalia);
uvindex = (ImageButton) findViewById(R.id.uvindex);
news.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), MyOrasisNews.class);
startActivity(intent);
}
});
weather.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
gyalia.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), GyaliaActivity.class);
startActivity(intent);
}
});
fakoi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), FakoiActivity.class);
startActivity(intent);
}
});
uvindex.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), UvMainActivity.class);
startActivity(intent);
}
});
}
}
And this is the xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/db1_root"
android:background="#drawable/bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/news"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:singleLine="false"
android:text="News"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/news"
android:layout_centerInParent="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/weather"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:singleLine="false"
android:text="Weather"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/weather"
android:layout_centerInParent="true"/>
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/uvindex"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="Uv Index"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/uvindex"
android:layout_centerInParent="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/fakoi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="S Fakwn"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sfakon"
android:layout_centerInParent="true"/>
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/gyalia"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="S gyalion"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sgyalion"
android:layout_centerInParent="true"/>
</RelativeLayout>
</TableRow>
</TableLayout>
</LinearLayout>
Any help will be appreciated.
you defined Buttons in your xml but you are trying to cast them to ImageButton. change type of your ImageButtons to Button.
You try to cast Button to ImageButton.
In your XML file change the Button to ImageButton or in your activity change the ImageButton to Button.
here is the picture. i want to click signs & Symptoms from the table of contents and by clicking i want it to jumps directly to the Signs and Symptoms paragraph. Please solve my this problem. here is my code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Atherosclerosis"
tools:ignore="UselessParent,ScrollViewCount" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/tablecontentlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#EFECCB" >
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textStyle="bold"
android:textColor="#000000"
android:text="#string/table" />
<TextView
android:id="#+id/definitionid"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/Definition" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/RiskFactors" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/Pathophysiology" />
<TextView
android:id="#+id/Signandsymptompsid"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/SignsandSymptoms" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/Diagnosis" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/Treatment" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/Prevention" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/Complications" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tablecontentlayout"
android:layout_marginTop="15dp"
android:orientation="vertical" >
<TextView
android:id="#+id/atherodefinition"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Definition"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/atherosclerosisdefinition"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/RiskFactors"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Modifiable"
android:textStyle="bold"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/modifiableatherosclerosis"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Nonmodifiable"
android:textStyle="bold"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/nonmodifiableatherosclerosis"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Pathophysiology"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/pathophysiologyatherosclerosis"
android:textSize="15sp"/>
<TextView
android:id="#+id/Signandsymptomsparagraph"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/SignsandSymptoms"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/atherosclerosissignsandsymptoms"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Diagnosis"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Diagnosisatherosclerosis"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Treatment"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/lifestylemodification"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/treatmentatherosclerosis"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Prevention"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Preventionatherosclerosis"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Complications"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/complicationatherosclerosis"
android:textSize="15sp"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
my java code for this layout:
public class Atherosclerosis extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.atherosclerosis);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.atherosclerosis, menu);
return true;
}
}
Use android:onClick="" and android:Clickable="true"
to make the Textview as a button
You can use OnClickListener on TextView and then use the following method on the OnClick, where textViewAbove is the TextView that is above the one you need to jump:
scrollView.post(
new Runnable() {
#Override
public void run() {
new CountDownTimer(1500, 20) {
#Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (textViewAbove.getBottom()-millisUntilFinished));
}
#Override
public void onFinish() {
}
}.start();
}
}
);
Hope it helps!
You can have OnclickListener on TextView
Here is the code but still give me error till me where I am wrong?
public class Atherosclerosis extends Activity{
ScrollView scrollView;
TextView tvabove=(TextView)findViewById(R.id.pathophysiology_id);
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.atherosclerosis);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#6B8E23")));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
TextView tv=(TextView)findViewById(R.id.Signandsymptompsid);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
#Override
public void run() {
new CountDownTimer(1500, 20) {
#Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (tvabove.getBottom()-millisUntilFinished));
}
#Override
public void onFinish() {
}
}.start();
}
}
);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.atherosclerosis, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.id_search:
Intent newActivity0 = new Intent(this,Search.class);
startActivity(newActivity0);
return true;
case R.id.id_favorit:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}