Why onclick of Sliding Menubar is not coming? - android

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>

Related

Value Of EditText In android Get changed after Typing to initial value

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 ?

Generating QR Code on different activity in android

i have been trying to create a QR Code generator that generates the QR Code on a separate activity on button click. Below is my code but it crushes on button click. Where did i go wrong?
First class is MainActivity.class:
package com.example.profmox.myapplication;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.journeyapps.barcodescanner.BarcodeEncoder;
public class MainActivity extends AppCompatActivity {
TextView tx1;
Button btn1, btn2;
EditText edt1,edt2;
CheckBox ch;
String text2qr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt1 = (EditText)findViewById(R.id.edit1);
edt2 = (EditText)findViewById(R.id.edit2);
btn1 = (Button)findViewById(R.id.login);
btn2 = (Button)findViewById(R.id.signup);
ch = (CheckBox)findViewById(R.id.check);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("EditText", edt1.getText().toString());
Log.v("EditText", edt2.getText().toString());
// text2qr = edt1.getText().toString().trim();
// MultiFormatWriter mfw = new MultiFormatWriter();
// try{
// BitMatrix btm = mfw.encode(text2qr, BarcodeFormat.QR_CODE,250,250);
// BarcodeEncoder ben = new BarcodeEncoder();
// Bitmap bmap = ben.createBitmap(btm);
// UserScreen.qr.setImageBitmap(bmap);
// }catch (WriterException e){
// e.printStackTrace();
// }
startActivity(new Intent(MainActivity.this, UserScreen.class));
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SignUp.class));
}
});
}
}
The layout file for the MainActivity class:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="18dp"
android:paddingRight="18dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#drawable/art_background"
tools:context="com.example.profmox.myapplication.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="300dp"
android:layout_height="230dp"
android:layout_gravity="center_horizontal"
android:padding="16dp"
android:layout_marginTop="20dp"
android:src="#mipmap/logo"/>
<EditText
android:id="#+id/edit1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textEmailAddress"
android:ems="10"
android:background="#android:color/transparent"
android:drawablePadding="12dp"
android:padding="8dp"
android:hint="Username"
android:textColorHint="#fff"
android:maxLines="1"
android:drawableLeft="#drawable/girl"
android:layout_marginTop="70dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#F2CC8F"/>
<EditText
android:id="#+id/edit2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:ems="10"
android:background="#android:color/transparent"
android:drawablePadding="12dp"
android:padding="8dp"
android:hint="Password"
android:textColorHint="#fff"
android:maxLines="1"
android:layout_marginTop="4dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#F2CC8F"/>
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textColor="#fff"
android:text="Remember Me"
android:padding="9dp"
/>
<Button
android:id="#+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/login_art"
android:text="Login"
android:textColor="#3D405B"
android:textAllCaps="false"
android:padding="16dp"
android:clickable="true"
style="#style/Base.TextAppearance.AppCompat.Body1"
android:layout_marginTop="23dp"
android:textSize="18sp"/>
<Button
android:id="#+id/signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/signuo_art"
android:text="Sign up"
android:textColor="#fff"
style="#style/Base.TextAppearance.AppCompat.Body1"
android:textAllCaps="false"
android:textSize="18sp"
android:layout_marginTop="16dp"
android:clickable="true"
android:padding="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forgot your password?"
android:clickable="true"
style="#style/Base.TextAppearance.AppCompat.Body2"
android:padding="16dp"
android:layout_marginBottom="12dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
The UserScreen.class is where i want to generate the QR Code in:
package com.example.profmox.myapplication;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
public class UserScreen extends AppCompatActivity {
static ImageView qr;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_screen);
qr = (ImageView)findViewById(R.id.qrCode);
}
}
The layout file for the UserScreen.class:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/art_background"
android:paddingLeft="18dp"
android:paddingRight="18dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.profmox.myapplication.UserScreen">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/qrCode"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</ScrollView>
</LinearLayout>

BottomSheetDialog setOnClickListener not firing

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

How to pass data from bottom sheet to a fragment in android?

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

OnItemClickListener doesn't work , OnItemClick is not called

I'm trying to make a Toast message whenever I click an item from the list, For some reason when I click the item nothing happens,
I hope you can help me, thanks.
ProductList.java
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseBooleanArray;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.support.v4.app.FragmentManager;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ProductList extends AppCompatActivity {
private List<myProductsView> myProducts_types = new ArrayList<myProductsView>();
ArrayAdapter<myProductsView> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_productlist);
//Button btnDel = (Button) findViewById(R.id.btnDel);
//adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice,list);
populateProductsList();
populateListView();
}
private void populateProductsList() {
myProducts_types.add(new myProductsView("aaa", "aaa", 1111, 12.90, R.drawable.cereal, 1));
myProducts_types.add(new myProductsView("aaa", "aaaaaa ", 1112, 10.90, R.drawable.cereal, 2));
myProducts_types.add(new myProductsView("aaa", "aaa", 1112, 30.00, R.drawable.cereal, 1));
myProducts_types.add(new myProductsView("aaa", "aaa", 1112, 20.00, R.drawable.cereal, 3));
}
private void populateListView() {
adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.product_list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("BLALBALBLALBLABLAL");
Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT);
}
});
}
public void StartCalck(View view){
Intent intent = new Intent(ProductList.this, SplitBuying.class);
startActivity(intent);
}
public class MyListAdapter extends ArrayAdapter<myProductsView>{
public MyListAdapter(){
super(ProductList.this, R.layout.pop_productlist, myProducts_types);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//make sure we have a view to work with(may have been given null
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.product_item_view, parent, false);
}
//we need to populate the list
//find the product to work with
myProductsView currentProduct = myProducts_types.get(position);
//fill the view
CheckBox checkBox = (CheckBox)itemView.findViewById(R.id.product_checkBox);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(ProductList.this,"BLA",Toast.LENGTH_SHORT);
}
});
TextView productname = (TextView) itemView.findViewById(R.id.product_name);
productname.setText(currentProduct.getProductName());
EditText quantity = (EditText)itemView.findViewById(R.id.edit_text);
quantity.setText(String.valueOf(currentProduct.getQuantity()));
return itemView;
}
}
}
product_item_view.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:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="7dp"
android:layout_weight="1">
<Button
android:id="#+id/btn_minus"
android:layout_width="35dp"
android:layout_height="40dp"
android:text="-" />
<EditText
android:id="#+id/edit_text"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:inputType="number"
android:gravity="center"
android:focusable="false"
android:text="0" />
<Button
android:id="#+id/btn_plus"
android:layout_width="35dp"
android:layout_height="40dp"
android:text="+" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Text"
android:textSize="15dp"
android:textIsSelectable="true"
android:gravity="center"
android:id="#+id/product_name"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/product_checkBox"
android:layout_toStartOf="#+id/product_checkBox" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/product_checkBox" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
pop_productlist.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:layout_height="match_parent"
android:background="#drawable/ightwall"
android:id="#+id/drawerlayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="הרשימה שלי"
android:textAlignment="center"
android:textSize="30dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="#+id/product_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
android:layout_below="#+id/ChooseStore"
>
</ListView>
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/CalckButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calckButton"
android:onClick="StartCalck"
android:layout_gravity="center" />
<Button
android:id="#+id/btnDel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/lblBtnDel"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#D5D4D4"
android:layout_gravity="bottom">
<ImageView
android:id="#+id/mylist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/createlist_mylist"
android:adjustViewBounds="true"
android:maxHeight="90dp"
android:maxWidth="90dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<ImageView
android:id="#+id/freeadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/createlist_freetext"
android:adjustViewBounds="true"
android:maxHeight="90dp"
android:maxWidth="90dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<ImageView
android:id="#+id/favproductadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/createlist_favproduct"
android:adjustViewBounds="true"
android:maxHeight="90dp"
android:maxWidth="90dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</LinearLayout>
</LinearLayout>
You have to call .show() on your toast.
change
Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT);
to
Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT).show();

Categories

Resources