I am having issues firing the onClick method of the setOnClickListener of the BottomSheetDialog menu.
This is the xml fragment layout or the BottomSheetDialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/fragment_grocery_menu_bottom"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#ffffff"
android:orientation="vertical"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:id="#+id/fragment_grocery_bottom_sheet_search"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
app:srcCompat="#drawable/ic_search_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Search Item" />
</LinearLayout>
<LinearLayout
android:id="#+id/fragment_grocery_bottom_sheet_got"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
app:srcCompat="#drawable/ic_done_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Mark Item as Gotten" />
</LinearLayout>
<LinearLayout
android:id="#+id/fragment_grocery_bottom_sheet_delete"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
app:srcCompat="#drawable/ic_delete_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Remove Item" />
</LinearLayout>
</LinearLayout
The java class code for the Fragment is below. So for example if I click on the got menu (LinearLayout) the onClick method is not fired and the Log is not written also the SnackBar is not displayed
import android.os.Bundle;
import android.app.Fragment;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class GroceryItemsMenu extends Fragment {
public GroceryItemsMenu() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_grocery_items_menu, container, false);
LinearLayout delete = (LinearLayout) view.findViewById(R.id.fragment_grocery_bottom_sheet_delete);
LinearLayout got = (LinearLayout) view.findViewById(R.id.fragment_grocery_bottom_sheet_got);
LinearLayout search = (LinearLayout) view.findViewById(R.id.fragment_grocery_bottom_sheet_search);
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
got.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Item: ", "Got");
Snackbar.make(getActivity().findViewById(R.id.groceryItemsCoordinatorLayout), "Got this Item", Snackbar.LENGTH_SHORT)
.setAction("Undo", new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}).show();
}
});
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Delete code here;
}
});
return view;
}
}
What am I doing wrong?
use : public class GroceryItemsMenu extends Fragment implements View.OnClickListener
Related
I have the following layout.xml containing one ScrollView with some elements(picture and text)
<?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"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/clickable"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_gravity="center"
android:src="#drawable/crown" />
<TextView
android:id="#+id/n1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="some text" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_gravity="center"
android:src="#drawable/medal" />
<TextView
android:id="#+id/n2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="some text" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_gravity="center"
android:src="#drawable/star" />
<TextView
android:id="#+id/n3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="some text" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity{
LayoutInflater inflater;
RelativeLayout MyLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inflater = LayoutInflater.from(this);
MyLayout = (RelativeLayout) inflater.inflate(R.id.layout, null, false);
MyLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("TAG"," I'm clicked! ");
}
});
Click Listener above does not work when I click screen!!!
But if I change code to this - everything is working
MyLayout.findViewById(R.id.clickable).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("TAG"," I'm clicked! ");
}
});
So why should I set click listener to LinearLayout inside ScrollView inside parent RelativeLayout?
Why click listener does not work for root xml element RelativeLayout???
The problem is that your ScrollView is intercepting the touch events and not propagating them to parent layouts (in this case your RelativeLayout).
This thread might have some useful information about this topic.
Just add android:clickable="false" in your ScrollView. Then change your MainActivity like below:
public class MainActivity extends Activity {
LinearLayout myLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myLayout = (LinearLayout) findViewById(R.id.layout);
myLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("TAG"," I'm clicked! ");
}
});
}
}
Note: activity_main.xml is xml file which you are loading.
Now your OnClick will work as expected.
Value is automatically changed after user started typing in EditText.Value is automatically changed with initial value.
Please Help me what is causing this issue
This layout is USed for adapter layout the complete layout file and adapter code is given below here is EditTextbox part that is causing issue
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
style="#style/booking_heading"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Enter Amount" />
<EditText
android:layout_width="0dp"
android:editable="true"
android:inputType="number"
android:layout_weight="1"
android:background="#fff"
android:padding="8dp"
android:text="00"
android:layout_height="match_parent"
android:id="#+id/booking_price" />
</LinearLayout>
Code Of My Adapter class is
package com.visionprecis.vikrant;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.visionprecis.vikrant.DriverService.Booking;
import com.visionprecis.vikrant.LocationProvider.TrackingActivity;
import com.visionprecis.vikrant.StaticData.StaticInfo;
import java.util.ArrayList;
import java.util.HashMap;
public class BookingArrayAdapter extends ArrayAdapter {
ArrayList<Booking> bookingArrayList;
Context context;
public BookingArrayAdapter(Context context, int resource,ArrayList<Booking> objects) {
super(context, resource, objects);
this.context=context;
this.bookingArrayList=objects;
}
#Override
public View getView(int position, View convertView,ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.single_booking_layout,null);
final Booking booking=bookingArrayList.get(position);
TextView orgin=view.findViewById(R.id.booking_origin);
TextView destination=view.findViewById(R.id.booking_destination);
TextView status=view.findViewById(R.id.booking_status);
final EditText price=view.findViewById(R.id.booking_price);
Button trackbtn=view.findViewById(R.id.booking_track_btn);
Button cancel=view.findViewById(R.id.booking_cancel);
Button confirm=view.findViewById(R.id.booking_confirm);
orgin.setText(booking.getOrigin().getAdress());
destination.setText(booking.getDestination().getAdress());
status.setText(booking.getStatus());
price.setText(booking.getPrice());
if(booking.getStatus().equals(StaticInfo.RIDE_REQUESTED)){
confirm.setText("Send Amount");
confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendbidamound(booking,price.getText().toString());
Log.i(StaticInfo.TAG,"Price kis "+price.getText());
}
});
}else if(booking.getStatus().equals(StaticInfo.RIDE_BID_PROVIDED)){
// confirm.setText("Confirmed");
confirm.setText("Send Amount");
confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendbidamound(booking,price.getText().toString());
Log.i(StaticInfo.TAG,"Price kis "+price.getText());
}
});
}
trackbtn.setEnabled(true);
trackbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
trackuser(booking.getUserid());
}
});
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oncancelpressed(booking);
}
});
return view;
}
private void trackuser(String userid) {
Intent intent=new Intent(context,TrackingActivity.class);
intent.putExtra("userid",userid);
context.startActivity(intent);
}
private void oncancelpressed(Booking booking) {
DatabaseReference reference=FirebaseDatabase.getInstance().getReference().child(StaticInfo.USER_BOOKING_REQUEST_TABLE)
.child(booking.getUserid())
.child(booking.getBookingid());
reference.child("status").setValue(StaticInfo.RIDE_CANCELED).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Toast.makeText(context,"Booking caceled Succcessfully",Toast.LENGTH_LONG).show();
}
});
}
private void sendbidamound(final Booking booking, final String price) {
if(validateprice()){
DatabaseReference userreference=FirebaseDatabase.getInstance().getReference().child(StaticInfo.USER_BOOKING_REQUEST_TABLE)
.child(booking.getUserid())
.child(booking.getBookingid());
HashMap<String,Object> updatevalue=new HashMap<>();
updatevalue.put("status",StaticInfo.RIDE_BID_PROVIDED);
updatevalue.put("price",price);
userreference.updateChildren(updatevalue).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
DatabaseReference driverreference=FirebaseDatabase.getInstance().getReference().child(StaticInfo.DRIVER_BOOKING_REQUEST_TABLE)
.child(booking.getDriverid())
.child(booking.getBookingid());
HashMap<String,Object> updatechild=new HashMap<>();
updatechild.put("status",StaticInfo.RIDE_BID_PROVIDED);
updatechild.put("price",price);
driverreference.updateChildren(updatechild).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Toast.makeText(context,"Bid amount Placed Succcesfully Awaiting User Confirmation And You Will Be Notified back",Toast.LENGTH_LONG).show();
context.startActivity(new Intent(context,MainActivity.class));
}
});
}
});
}
}
private boolean validateprice() {
return true;
}
}
Complete Layout Of The adapter is
<!-- Begning Of child Layout horizental-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
style="#style/booking_heading"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Origin" />
<TextView
style="#style/booking_value"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/booking_origin"
android:text="Shimla Himachal Pradesh India" />
</LinearLayout>
<!-- End Of child Layout horizental-->
<!-- Begning Of child Layout horizental-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
style="#style/booking_heading"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Destination" />
<TextView
style="#style/booking_value"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/booking_destination"
android:text="Shimla Himachal Pradesh India" />
</LinearLayout>
<!-- End Of child Layout horizental-->
<!-- Begning Of child Layout horizental-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
style="#style/booking_heading"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Ride Status" />
<TextView
style="#style/booking_value"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/booking_status"
android:text=" " />
</LinearLayout>
<!-- End Of child Layout horizental-->
<!-- Begning Of child Layout horizental-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
style="#style/booking_heading"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Enter Amount" />
<EditText
android:layout_width="0dp"
android:editable="true"
android:inputType="number"
android:layout_weight="1"
android:background="#fff"
android:padding="8dp"
android:layout_height="match_parent"
android:id="#+id/booking_price" />
</LinearLayout>
<!-- End Of child Layout horizental-->
<!-- Begning Of child Layout horizental-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<Button
style="#style/booking_heading"
android:textColor="#fff"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#273e70"
android:gravity="center"
android:textStyle="bold"
android:textAlignment="center"
android:text="Cancel"
android:layout_margin="4dp"
android:id="#+id/booking_cancel"/>
<Button
style="#style/booking_heading"
android:layout_width="0dp"
android:textColor="#fff"
android:layout_height="match_parent"
android:background="#273e70"
android:gravity="center"
android:textStyle="bold"
android:textAlignment="center"
android:text="Confirm"
android:layout_margin="4dp"
android:id="#+id/booking_confirm" />
</LinearLayout>
<!-- End Of child Layout horizental-->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Track User"
android:layout_marginTop="8dp"
android:textSize="22sp"
android:enabled="false"
android:id="#+id/booking_track_btn"
/>
</LinearLayout>
<!-- End Of main Layout vertical-->
</android.support.constraint.ConstraintLayout>
The Activity layout is
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RequestedRidesListActivity">
<ListView
android:layout_width="match_parent"
android:id="#+id/booking_list_view"
android:layout_height="wrap_content">
</ListView>
</android.support.constraint.ConstraintLayout>
Layout of the File is
Have you set textwatcher to your edit text ?
I am trying to pass values from a bottom sheet which contains some TextViews to a fragment.
My fragment contains an EditText field and a Floating action button.When the user clicks on the floating action button, the bottom sheet shows up which has a number of TextViews, when the user clicks on any of the textViews on the bottom sheet, the value or the string of that TextView should be displayed in the editText field of the fragment and the bottom sheet should be dismissed.
I have tried implementing the onClickListener inside the setOnShowListener method but it doesn't seem to work.
Here is my code:
Fragment_TextPropert1_EditText.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sumitroy.TextProperty1_EditText"
>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="15dp"
android:padding="15dp"
android:layout_below="#+id/view"
android:layout_marginTop="10dp"
app:cardUseCompatPadding="true"
android:id="#+id/view2">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:hint="Enter Your Comments Here.."
android:id="#+id/userText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</android.support.v7.widget.CardView>
<ImageButton
android:layout_width="65dp"
android:layout_height="65dp"
android:id="#+id/example_Ads"
android:background="#drawable/oval"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="20dp"
android:layout_marginRight="15dp"
android:src="#drawable/double_plus"
/>
</RelativeLayout>
TextProperty1_bottomsheet.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="15dp"
android:padding="15dp"
android:layout_marginTop="15dp"
app:cardUseCompatPadding="true"
android:id="#+id/view1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Comment1"
android:id="#+id/example_Ad1"
android:padding="10dp"
android:textStyle="bold"
android:textSize="14sp"
android:layout_below="#+id/view"
android:layout_alignParentStart="true" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="15dp"
android:padding="15dp"
android:layout_marginTop="10dp"
app:cardUseCompatPadding="true"
android:layout_below="#+id/view1"
android:id="#+id/view2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Comment 2"
android:id="#+id/example_Ad2"
android:padding="10dp"
android:textStyle="bold"
android:textSize="14sp"
android:layout_below="#+id/view"
/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="15dp"
android:padding="15dp"
android:layout_marginTop="15dp"
app:cardUseCompatPadding="true"
android:id="#+id/view3"
android:layout_below="#+id/view2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Comment 3 "
android:id="#+id/example_Ad3"
android:padding="10dp"
android:textStyle="bold"
android:textSize="14sp"
android:layout_below="#+id/view"
/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="15dp"
android:padding="15dp"
android:layout_marginTop="15dp"
app:cardUseCompatPadding="true"
android:id="#+id/view4"
android:layout_below="#+id/view3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Comment 4 "
android:id="#+id/example_Ad4"
android:padding="10dp"
android:textStyle="bold"
android:textSize="14sp"
android:layout_below="#+id/view"
/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="15dp"
android:padding="15dp"
android:layout_marginTop="15dp"
app:cardUseCompatPadding="true"
android:id="#+id/view5"
android:layout_below="#+id/view4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Comment 5 "
android:id="#+id/example_Ad5"
android:padding="10dp"
android:textStyle="bold"
android:textSize="14sp"
android:layout_below="#+id/view"
/>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
TextProperty1_EditText.java
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
* A simple {#link Fragment} subclass.
*/
public class TextProperty1_EditText extends Fragment {
View bottomSheetView;
EditText editText1;
TextView t1;
BottomSheetDialog bottomSheetDialog;
BottomSheetBehavior bottomSheetBehavior;
ImageButton floatButton;
RelativeLayout backgroundLayout;
public TextProperty1_EditText() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootview= inflater.inflate(R.layout.fragment_text_property1__edit_text, container, false);
editText1=(EditText) rootview.findViewById(R.id.userText);
floatButton=(ImageButton)rootview.findViewById(R.id.example_Ads);
floatButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(v.getContext(),"Floating Button Works",Toast.LENGTH_SHORT).show();
bottomSheetView=getActivity().getLayoutInflater().inflate(R.layout.textproperty1_bottomsheet,null);
bottomSheetDialog=new BottomSheetDialog(rootview.getContext());
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetBehavior=BottomSheetBehavior.from((View) bottomSheetView.getParent());
bottomSheetDialog.show();
bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
t1=(TextView)bottomSheetView.findViewById(R.id.example_Ad1);
t1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String take1=t1.getText().toString();
//Toast.makeText(bottomSheetView.getContext(),"Floating Button Works",Toast.LENGTH_SHORT).show();
editText1.setText(take1);
bottomSheetDialog.dismiss();
}
});
}
});
bottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
}
});
bottomSheetDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//Toast.makeText(bottomSheetView.getContext(),"Floating Button Works",Toast.LENGTH_SHORT).show();
return false;
}
});
}
});
return rootview;
}
}
As a simple solution you may use LocalBroadcastManager.
public static final String SOME_INTENT_FILTER_NAME = "SOME_INTENT_FILTER_NAME";
In your fragment:
private BroadcastReceiver someBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//TODO extract extras from intent
}
};
#Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(getContext()).registerReceiver(someBroadcastReceiver,
new IntentFilter(SOME_INTENT_FILTER_NAME));
}
#Override
public void onPause() {
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(someBroadcastReceiver);
super.onPause();
}
In your bottomsheet:
Intent someIntent = new Intent(SOME_INTENT_FILTER_NAME);
//TODO put extras to your intent
LocalBroadcastManager.getInstance(context).sendBroadcast(someIntent);
In the following code Sliding drawer menu bar is coming but when i click on the menu items nothing happens.I am looking forward to open the sliding drawer on button click of the buttonmenu.I dont know how to open a sliding drawer on button click.When i click on the drawer it disappers to the left hand side.I want to write the code like this if i click on menu item (home) it should open Mainactivity.class.I have not written the code for that.
package com.bar.start;
import com.bar.barapp.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button loc,buttonmenu;
String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.homescreen);
// getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
menu = new String[]{"Home","Android","Windows","Linux","Raspberry Pi","WordPress","Videos","Contact Us"};
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dList = (ListView) findViewById(R.id.left_drawer);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
dList.setAdapter(adapter);
dList.setSelector(android.R.color.holo_blue_dark);
dList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
dLayout.closeDrawers();
Bundle args = new Bundle();
args.putString("Menu", menu[position]);
Fragment detail = new Menu_view();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
}
});
loc=(Button)findViewById(R.id.location);
loc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent in =new Intent(MainActivity.this,LocationActivity.class);
startActivity(in);
}
});
buttonmenu=(Button)findViewById(R.id.menubutton);
buttonmenu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
}
});
}
}
homescreen.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="0dp"
android:background="#800000"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/startupscreen">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/menulayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:paddingTop="30dp"
android:orientation="horizontal">
<Button
android:id="#+id/menubutton"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.3"
android:text="#string/menu"
/>
<TextView
android:layout_weight="0.2"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="0.2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/search"
android:textSize="14sp"
android:textColor="#color/black"/>
<EditText
android:layout_weight="0.3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:focusable="false"
android:focusableInTouchMode="true"/>
</LinearLayout>
<ExpandableListView
android:layout_below="#+id/menulayout"
android:id="#+id/searchres"
android:layout_width="fill_parent"
android:layout_height="350dp"
android:clipChildren="true"
android:groupIndicator="#null"
android:background="#color/gray"
/>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="50dp"
android:weightSum="1.5"
android:orientation="horizontal"
android:layout_below="#+id/searchres">
<Button
android:id="#+id/location"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:text="#string/location"
android:background="#drawable/btn_image"
/>
<Button
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:visibility="invisible"
/>
<Button
android:id="#+id/barype"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:text="#string/bartype"
android:background="#drawable/btn_image"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_below="#+id/buttons">
<Button
android:id="#+id/joggle"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#drawable/btn_image"
android:text="#string/joggle"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Menuview.java
public class Menu_view extends Fragment{
TextView text;
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
View view = inflater.inflate(R.layout.menu_detail, container, false);
String menu = getArguments().getString("Menu");
text= (TextView) view.findViewById(R.id.detail);
text.setText(menu);
return view;
}
}
menu_detail.xml
<?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:gravity="center"
android:background="#5ba4e5"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40px"
android:textColor="#000000"
android:layout_gravity="center"
android:id="#+id/detail"/>
</LinearLayout>
I have the following activity. It is an simplification of a draggable map. The problem is that mytext2 is not clickable, even if it is visible. Can anybody tell me how to make it clickable? And for some reason the inner-RelativeLayout gets not bigger than screen size, even when i set this high dp.
<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="#0000ff"
android:clipChildren="false"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:layout_width="100000dp"
android:layout_height="100000dp"
android:background="#00ff00"
android:translationX="-100dp" >
<TextView
android:id="#+id/mytext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:translationX="400dp"
android:translationY="100dp" />
<TextView
android:id="#+id/mytext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:translationX="200dp"
android:translationY="100dp" />
</RelativeLayout>
</RelativeLayout>
class
package com.example.relativatest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.mytext1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("HELLO!1");
}
});
findViewById(R.id.mytext2).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("HELLO!2");
}
});
}
}
Use this 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="#0000ff"
android:clipChildren="false"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:layout_width="100000dp"
android:layout_height="100000dp"
android:background="#00ff00"
android:translationX="-100dp" >
<TextView
android:id="#+id/mytext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:focusable="true"
android:padding="20dp"
android:translationX="400dp"
android:clickable="true"
android:translationY="100dp" />
<TextView
android:id="#+id/mytext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:translationX="200dp"
android:clickable="true"
android:padding="20dp"
android:focusable="true"
android:translationY="100dp" />
</RelativeLayout>
</RelativeLayout>