Unexpected margin in bottom sheet after using SearchView - android

I am facing a strange issue working with SearchView and BottomSheet.
BottomSheet gets an unexpected margin when used after using SearchView. They video explains the problem.
Video
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
<xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundcolor"
android:fitsSystemWindows="true">
<android.support.v7.widget.RecyclerView
android:id="#+id/trip_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.widget.NestedScrollView
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundcolor"
android:clipToPadding="false"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="32dp"
android:orientation="vertical"
android:padding="12dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:text="Sort & Filter"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/sheet_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:drawablePadding="8dp"
android:drawableStart="#drawable/ic_done"
android:text="Done"
android:textSize="18sp" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Sort By" />
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Spinner
android:id="#+id/sheet_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/sort_by"
android:padding="8dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Price Range" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:id="#+id/sheet_price_min"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/sheet_price_max"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:gravity="end" />
</LinearLayout>
<SeekBar
android:id="#+id/sheet_bar_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:thumb="?android:attr/textSelectHandle" />
<TextView
android:id="#+id/sheet_price_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Rs 1,115" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Ratings" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_weight="1"
android:gravity="start"
android:text="2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:layout_weight="1"
android:gravity="end"
android:text="4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:text="5" />
</LinearLayout>
<SeekBar
android:id="#+id/sheet_bar_rating"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="4"
android:thumb="?android:attr/textSelectHandle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Packages" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/sheet_package_gold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="1"
android:text="Gold" />
<CheckBox
android:id="#+id/sheet_package_diamond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="1"
android:text="Diamond" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/sheet_package_silver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="1"
android:text="Silver" />
<CheckBox
android:id="#+id/sheet_package_platinum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="1"
android:text="Platinum" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Activity
public class TripsActivity extends AppCompatActivity implements
SearchView.OnQueryTextListener, SeekBar.OnSeekBarChangeListener, View.OnClickListener {
public String MAX = "150000";
public String MIN = "0";
#BindView(R.id.trip_recyclerView)
RecyclerView recyclerView;
#BindView(R.id.bottom_sheet)
View view;
#BindView(R.id.sheet_spinner)
Spinner spinner;
#BindView(R.id.sheet_price_min)
TextView minPrice;
#BindView(R.id.sheet_price_max)
TextView maxPrice;
#BindView(R.id.sheet_price_text)
TextView currentPrice;
#BindView(R.id.sheet_bar_price)
SeekBar priceBar;
#BindView(R.id.sheet_bar_rating)
SeekBar ratingBar;
#BindView(R.id.sheet_package_gold)
CheckBox packageGold;
#BindView(R.id.sheet_package_silver)
CheckBox packageSilver;
#BindView(R.id.sheet_package_platinum)
CheckBox packagePlatinum;
#BindView(R.id.sheet_package_diamond)
CheckBox packageDiamond;
BottomSheetBehavior.BottomSheetCallback callback = new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
if (slideOffset < 0.4)
getSupportActionBar().show();
else
getSupportActionBar().hide();
}
};
private SearchView searchView;
private TripsAdapter adapter;
private BottomSheetBehavior mBottomSheetBehavior;
#OnClick(R.id.sheet_done)
public void onClick(View view) {
switch (view.getId()) {
case R.id.sheet_done:
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
break;
}
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_trips_activity);
ButterKnife.bind(this);
mBottomSheetBehavior = BottomSheetBehavior.from(view);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
mBottomSheetBehavior.setPeekHeight(0);
mBottomSheetBehavior.setBottomSheetCallback(callback);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED)
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
else
this.finish();
} else if (item.getItemId() == R.id.filterOptions)
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.seach_meanu, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
adapter.filter(query);
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.filter(newText);
return true;
}
#Override
public void onBackPressed() {
if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED)
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
else
super.onBackPressed();
}
}
Someone can help me identify the problem. Thanks

Add this to your manifest.This issue trigered by the softkeyboard.Sadly I cant find the real source of the problem.
<activity>
android:name=..
android:windowSoftInputMode="adjustPan">
I get this from : https://stackoverflow.com/a/12956520/7202257

Related

NullPointerException on setContentView

I am having two layouts in same xml file and I have kept one layout as hidden and want to show this hidden layout on button click. But when I try to load this activity, it gives me exception on setContentView.
Here is my layout:
<androidx.drawerlayout.widget.DrawerLayout 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:id="#+id/drawer1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f0f5f9"
tools:context="com.techsof.demoapp.Auftrag">
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/round_corners"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="serif"
android:gravity="center"
android:text="#string/heading"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="serif"
android:gravity="center"
android:text="#string/heading_text"
android:textColor="#fff"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:id="#+id/form_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:fontFamily="serif"
android:gravity="center"
android:text="#string/form_heading"
android:textColor="#4C4C4C"
android:textSize="20sp" />
<LinearLayout
android:id="#+id/form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/form_heading"
android:layout_margin="10dp"
android:orientation="vertical">
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/input_fields"
android:fontFamily="serif"
android:hint="#string/name"
android:padding="10dp" />
<EditText
android:id="#+id/mail"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#drawable/input_fields"
android:fontFamily="serif"
android:hint="#string/mail_"
android:padding="10dp" />
<EditText
android:id="#+id/tel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#drawable/input_fields"
android:fontFamily="serif"
android:hint="Tel/Mobilnummer *"
android:padding="10dp" />
<EditText
android:id="#+id/aus"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#drawable/input_fields"
android:fontFamily="serif"
android:hint="Ausgangssprache *"
android:padding="10dp" />
<EditText
android:id="#+id/ziel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#drawable/input_fields"
android:fontFamily="serif"
android:hint="Zielsprache *"
android:padding="10dp" />
<EditText
android:id="#+id/nach"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="10dp"
android:background="#drawable/input_fields"
android:fontFamily="serif"
android:gravity="start"
android:hint="Nachricht *"
android:padding="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/doc1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#drawable/odd_corners"
android:fontFamily="serif"
android:gravity="start"
android:padding="10dp" />
<Button
android:id="#+id/attach_btn1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1.5"
android:background="#drawable/odd_corners_btn"
android:drawableStart="#drawable/ic_file_upload_black_24dp"
android:drawableLeft="#drawable/ic_file_upload_black_24dp"
android:text="#string/doc1"
android:textAllCaps="false"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/doc2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#drawable/odd_corners"
android:fontFamily="serif"
android:gravity="start"
android:padding="10dp" />
<Button
android:id="#+id/attach_btn2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1.5"
android:background="#drawable/odd_corners_btn"
android:drawableStart="#drawable/ic_file_upload_black_24dp"
android:drawableLeft="#drawable/ic_file_upload_black_24dp"
android:text="#string/doc2"
android:textAllCaps="false"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/doc3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#drawable/odd_corners"
android:fontFamily="serif"
android:gravity="start"
android:padding="10dp" />
<Button
android:id="#+id/attach_btn3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1.5"
android:background="#drawable/odd_corners_btn"
android:drawableStart="#drawable/ic_file_upload_black_24dp"
android:drawableLeft="#drawable/ic_file_upload_black_24dp"
android:text="#string/doc3"
android:textAllCaps="false"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/footer_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/form"
android:layout_margin="10dp"
android:fontFamily="serif"
android:gravity="center"
android:text="#string/footer_text"
android:textColor="#4C4C4C"
android:textSize="12sp"
android:textStyle="bold" />
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/footer_text"
android:layout_marginLeft="10dp"
android:buttonTint="#0F84AA"
android:fontFamily="serif"
android:text="#string/checkbox_text"
android:textColor="#4c4c4c"
android:textStyle="bold" />
<Button
android:id="#+id/sendBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/checkbox"
android:layout_margin="10dp"
android:background="#drawable/round_corners"
android:text="#string/button_text"
android:textAllCaps="false"
android:textColor="#fff" />
</RelativeLayout>
</ScrollView>
<LinearLayout
android:id="#+id/dialogBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4c4c4c"
android:padding="10dp"
android:text="#string/complete_action_using"
android:textColor="#fff"
android:textSize="20sp" />
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fff" />
<LinearLayout
android:id="#+id/cameraOption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CDCDCD"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:src="#drawable/ic_camera_alt_black_24dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/camera"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fff" />
<LinearLayout
android:id="#+id/phoneStorageOption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CDCDCD"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:src="#drawable/ic_folder_open_black_24dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/phone_storage"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fff" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CDCDCD"
android:gravity="end"
android:padding="10dp">
<Button
android:id="#+id/cancelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:backgroundTint="#0F84AA"
android:text="#string/cancel"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f0f5f9"
app:headerLayout="#layout/nav_header"
app:itemIconTint="#000"
app:itemTextColor="#000"
app:menu="#menu/menu_main" />
</androidx.drawerlayout.widget.DrawerLayout>
and here is my Java Code:
public class Auftrag extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout eDrawerlayout;
private ActionBarDrawerToggle eToggle;
NavigationView nav;
private LinearLayout camera, phone, dialog;
private Button attachBtn1, attachBtn2, attachBtn3, submitButton, cancel;
private TextView file1, file2, file3;
private EditText name, mail, telephone, ausgang, ziels, nachricht;
private ArrayList<Uri> attachments = new ArrayList<>();
private CheckBox agreement;
private String textfield;
public static final int REQUEST_SELECT_FILE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
initUI();
eToggle = new ActionBarDrawerToggle(this, eDrawerlayout, R.string.Open, R.string.Close);
eDrawerlayout.addDrawerListener(eToggle);
eToggle.syncState();
nav.setNavigationItemSelectedListener(this);
androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.parseColor("#f0f5f9")));
eDrawerlayout.setDrawerListener(eToggle);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
}
private void initUI() {
nav = findViewById(R.id.navigation1);
eDrawerlayout = findViewById(R.id.drawer1);
name = findViewById(R.id.name);
mail = findViewById(R.id.mail);
telephone = findViewById(R.id.tel);
ausgang = findViewById(R.id.aus);
ziels = findViewById(R.id.ziel);
nachricht = findViewById(R.id.nach);
submitButton = findViewById(R.id.sendBtn);
attachBtn1 = findViewById(R.id.attach_btn1);
attachBtn2 = findViewById(R.id.attach_btn2);
attachBtn3 = findViewById(R.id.attach_btn3);
/*dialog = findViewById(R.id.dialogBox);
camera = findViewById(R.id.cameraOption);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openCamera();
}
});
phone = findViewById(R.id.phoneStorageOption);
phone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openFileChooser();
}
});
cancel = findViewById(R.id.cancelBtn);
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.setVisibility(View.GONE);
}
});*/
file1 = findViewById(R.id.doc1);
file2 = findViewById(R.id.doc2);
file3 = findViewById(R.id.doc3);
attachBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textfield = "btn1";
chooseOption();
/*dialog.setVisibility(View.VISIBLE);*/
}
});
attachBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textfield = "btn2";
/*dialog.setVisibility(View.VISIBLE);*/
}
});
attachBtn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textfield = "btn3";
/*dialog.setVisibility(View.VISIBLE);*/
}
});
agreement = findViewById(R.id.checkbox);
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendData();
}
});
}
private void chooseOption() {
final CharSequence[] options = {"Camera", "Phone", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add file using: ");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Camera")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
} else if (options[item].equals("Phone")) {
openFileChooser();
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
private void openFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, REQUEST_SELECT_FILE);
}
private void sendData() {
if (TextUtils.isEmpty(name.getText()) || TextUtils.isEmpty(mail.getText()) ||
TextUtils.isEmpty(telephone.getText()) || TextUtils.isEmpty(ausgang.getText())
|| TextUtils.isEmpty(ziels.getText()) || TextUtils.isEmpty(nachricht.getText())
|| TextUtils.isEmpty(file1.getText()) || TextUtils.isEmpty(file3.getText()) ||
TextUtils.isEmpty(file3.getText())) {
Toast.makeText(this, "Please fill out all fields.", Toast.LENGTH_SHORT).show();
} else {
if (agreement.isChecked()) {
if (isConnected()) {
ArrayList<String> data = new ArrayList<>();
data.add(name.getText().toString());
data.add(mail.getText().toString());
data.add(telephone.getText().toString());
data.add(ausgang.getText().toString());
data.add(ziels.getText().toString());
data.add(nachricht.getText().toString());
new SendMailTask(this).execute("metaphrase.online#gmail.com", "Admintogether-2000222",
"salman.footy.k7#gmail.com", "User Information", data.get(0), data.get(1),
data.get(2), data.get(3), data.get(4), data.get(5), getFullPath(attachments.get(0)),
getFullPath(attachments.get(1)), getFullPath(attachments.get(2)));
} else {
Toast.makeText(this, "Please check your internet connection and try again.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Please select the checkbox.", Toast.LENGTH_SHORT).show();
}
}
}
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (eToggle.onOptionsItemSelected(item)) {
}
return true;
}
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.home:
startActivity(new Intent(Auftrag.this, MainActivity.class));
break;
case R.id.web:
Intent browser = new Intent(Intent.ACTION_VIEW, Uri.parse("https://metaphrase.online"));
startActivity(browser);
break;
case R.id.help:
startActivity(new Intent(Auftrag.this, Feedback.class));
break;
case R.id.upload:
if (isConnected()) {
startActivity(new Intent(Auftrag.this, Auftrag.class));
} else {
Toast.makeText(this, "Bitte überprüfen Sie Ihre Internetverbindung!", Toast.LENGTH_SHORT)
.show();
}
break;
}
eDrawerlayout.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode != REQUEST_SELECT_FILE || resultCode != RESULT_OK) {
Toast.makeText(this, "No file selected.", Toast.LENGTH_SHORT).show();
dialog.setVisibility(View.GONE);
} else {
importFile(intent);
}
}
private void importFile(Intent intent) {
if (intent != null) {
String fileName = getFileName(intent.getData());
if (textfield != null) {
if (textfield.equals("btn1")) {
dialog.setVisibility(View.GONE);
file1.setText(fileName);
attachments.add(intent.getData());
} else if (textfield.equals("btn2")) {
dialog.setVisibility(View.GONE);
file2.setText(fileName);
attachments.add(intent.getData());
} else {
dialog.setVisibility(View.GONE);
file3.setText(fileName);
attachments.add(intent.getData());
}
} else {
Toast.makeText(this, "Try again later.", Toast.LENGTH_SHORT).show();
}
}
}
private String getFullPath(Uri fileUri) {
String filePath = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
try {
filePath = PathUtil.getPath(this, fileUri);
} catch (URISyntaxException e) {
e.printStackTrace();
}
} else {
File file = new File(fileUri.getPath());
final String[] split = file.getPath().split(":");
filePath = split[1];
}
return filePath;
}
private String getFileName(Uri data) {
Cursor cursor = getContentResolver().query(data, null, null, null, null);
if (cursor.getCount() <= 0) {
cursor.close();
throw new IllegalArgumentException("Can't obtain file name, cursor is empty");
}
cursor.moveToFirst();
String fileName = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
cursor.close();
return fileName;
}
public boolean isConnected() {
boolean connected = false;
try {
ConnectivityManager cm = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nInfo = cm.getActiveNetworkInfo();
connected = nInfo != null && nInfo.isAvailable() && nInfo.isConnected();
return connected;
} catch (Exception e) {
Log.e("Connectivity Exception", e.getMessage());
}
return connected;
}
}
So the problem should be in the <view ../> tag, the correct form is <View ... />. You should change your XML file with the following:
<androidx.drawerlayout.widget.DrawerLayout 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:id="#+id/drawer1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f0f5f9"
tools:context="com.techsof.demoapp.Auftrag">
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/round_corners"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="serif"
android:gravity="center"
android:text="#string/heading"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="serif"
android:gravity="center"
android:text="#string/heading_text"
android:textColor="#fff"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:id="#+id/form_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:fontFamily="serif"
android:gravity="center"
android:text="#string/form_heading"
android:textColor="#4C4C4C"
android:textSize="20sp" />
<LinearLayout
android:id="#+id/form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/form_heading"
android:layout_margin="10dp"
android:orientation="vertical">
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/input_fields"
android:fontFamily="serif"
android:hint="#string/name"
android:padding="10dp" />
<EditText
android:id="#+id/mail"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#drawable/input_fields"
android:fontFamily="serif"
android:hint="#string/mail_"
android:padding="10dp" />
<EditText
android:id="#+id/tel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#drawable/input_fields"
android:fontFamily="serif"
android:hint="Tel/Mobilnummer *"
android:padding="10dp" />
<EditText
android:id="#+id/aus"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#drawable/input_fields"
android:fontFamily="serif"
android:hint="Ausgangssprache *"
android:padding="10dp" />
<EditText
android:id="#+id/ziel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#drawable/input_fields"
android:fontFamily="serif"
android:hint="Zielsprache *"
android:padding="10dp" />
<EditText
android:id="#+id/nach"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="10dp"
android:background="#drawable/input_fields"
android:fontFamily="serif"
android:gravity="start"
android:hint="Nachricht *"
android:padding="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/doc1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#drawable/odd_corners"
android:fontFamily="serif"
android:gravity="start"
android:padding="10dp" />
<Button
android:id="#+id/attach_btn1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1.5"
android:background="#drawable/odd_corners_btn"
android:drawableStart="#drawable/ic_file_upload_black_24dp"
android:drawableLeft="#drawable/ic_file_upload_black_24dp"
android:text="#string/doc1"
android:textAllCaps="false"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/doc2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#drawable/odd_corners"
android:fontFamily="serif"
android:gravity="start"
android:padding="10dp" />
<Button
android:id="#+id/attach_btn2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1.5"
android:background="#drawable/odd_corners_btn"
android:drawableStart="#drawable/ic_file_upload_black_24dp"
android:drawableLeft="#drawable/ic_file_upload_black_24dp"
android:text="#string/doc2"
android:textAllCaps="false"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/doc3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#drawable/odd_corners"
android:fontFamily="serif"
android:gravity="start"
android:padding="10dp" />
<Button
android:id="#+id/attach_btn3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1.5"
android:background="#drawable/odd_corners_btn"
android:drawableStart="#drawable/ic_file_upload_black_24dp"
android:drawableLeft="#drawable/ic_file_upload_black_24dp"
android:text="#string/doc3"
android:textAllCaps="false"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/footer_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/form"
android:layout_margin="10dp"
android:fontFamily="serif"
android:gravity="center"
android:text="#string/footer_text"
android:textColor="#4C4C4C"
android:textSize="12sp"
android:textStyle="bold" />
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/footer_text"
android:layout_marginLeft="10dp"
android:buttonTint="#0F84AA"
android:fontFamily="serif"
android:text="#string/checkbox_text"
android:textColor="#4c4c4c"
android:textStyle="bold" />
<Button
android:id="#+id/sendBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/checkbox"
android:layout_margin="10dp"
android:background="#drawable/round_corners"
android:text="#string/button_text"
android:textAllCaps="false"
android:textColor="#fff" />
</RelativeLayout>
</ScrollView>
<LinearLayout
android:id="#+id/dialogBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4c4c4c"
android:padding="10dp"
android:text="#string/complete_action_using"
android:textColor="#fff"
android:textSize="20sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fff" />
<LinearLayout
android:id="#+id/cameraOption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CDCDCD"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:src="#drawable/ic_camera_alt_black_24dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/camera"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fff" />
<LinearLayout
android:id="#+id/phoneStorageOption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CDCDCD"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:src="#drawable/ic_folder_open_black_24dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/phone_storage"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fff" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CDCDCD"
android:gravity="end"
android:padding="10dp">
<Button
android:id="#+id/cancelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:backgroundTint="#0F84AA"
android:text="#string/cancel"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f0f5f9"
app:headerLayout="#layout/nav_header"
app:itemIconTint="#000"
app:itemTextColor="#000"
app:menu="#menu/menu_main" />
</androidx.drawerlayout.widget.DrawerLayout>

Recyclerview doesn't show or scroll in fragment

I have found some questions regarding this issue here but none of them helped to solve my problem. Recycler view works fine but there is issue in lollipop version. It works perfectly in an activity but doesn't show or scroll when used inside fragment. My fragment screen appears blank but the tap listeners in recycler view's adapter is working. When I migrated my fragment code to an activity, it worked fine. I am using 28.0.0 Here is my xml code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipeToRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/initial_layout_empty_view" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_order"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:nestedScrollingEnabled="false" />
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
My item xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<LinearLayout
android:id="#+id/wholeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtVendorName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorTextListTitle"
android:textSize="16sp"
android:textStyle="bold"
tools:text="Redsun Family Restaurant" />
<TextView
android:id="#+id/txtVendorAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorTextNormal"
android:textSize="14sp"
android:textStyle="bold"
tools:text="KumariPati,Lalitpur,Nepal" />
</LinearLayout>
<ImageView
android:id="#+id/imageCompleted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_check_green"
android:visibility="gone" />
<Button
android:id="#+id/btnDeliveryStatus"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#drawable/circular_btn_background_dark_green"
android:backgroundTint="#color/colorPrimary"
android:text="Accept"
android:textColor="#color/white"
android:textSize="12sp"
android:visibility="gone" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<include layout="#layout/layout_seperator" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:orientation="horizontal">
<include
android:id="#+id/included"
layout="#layout/layout_vendor_logo" />
<View
android:id="#+id/catLogoDivider"
android:layout_width="0.7dp"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#e8e8e8" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relOrderNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/txtLblOrderNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:singleLine="true"
android:text="#string/txtOrderNo"
android:textColor="#color/colorTextNormal" />
<TextView
android:id="#+id/txtOrderNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/txtLblOrderNo"
android:gravity="end"
android:maxLines="2"
android:textColor="#color/black"
tools:text="#16-0131-102716" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relDeliveryTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/txtLblDeliveryTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:singleLine="true"
android:text="#string/txtDeliveryTime"
android:textColor="#color/colorTextNormal" />
<TextView
android:id="#+id/txtDeliveryTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/txtLblDeliveryTime"
android:gravity="end"
android:maxLines="2"
android:textColor="#color/black"
tools:text="Jul 5, 2017, 7:15pm" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/txtLblStatus"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:gravity="center"
android:text="#string/txtStatus"
android:textColor="#color/colorTextNormal" />
<TextView
android:id="#+id/txtStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:gravity="end"
android:maxLines="2"
android:textSize="14sp"
android:textColor="#color/black"
tools:text="Processing" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/txtTotalAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:text="#string/txtTotalAmount"
android:textColor="#color/colorTextNormal" />
<TextView
android:id="#+id/txtTotalAmountValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:textColor="#color/black"
tools:text="Rs. 197.50" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
And the long fragment code
public class OrderFragment extends MvpFragment<OrderView, OrderListPresenter> implements OrderView, OnItemClickListener {
private View view;
private Toolbar mToolbar;
private RecyclerView recyclerView;
private SwipeRefreshLayout swipeRefreshLayout;
private List<Order> orderList = new ArrayList<>();
private OrderAdaptor adaptor;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_order, container, false);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
findViews();
setActionBar();
initRecyclerView();
initSwipeToRefresh();
if (getActivity() != null)
getActivity().setTitle(getString(R.string.titleOrders));
presenter.getOrderData();
}
#Override
public OrderListPresenter createPresenter() {
return new OrderListPresenter(getContext());
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home)
if (getActivity() != null) {
((MainActivity) getActivity()).openDrawerLayout();
}
return super.onOptionsItemSelected(item);
}
#Override
public void setAdaptor(List<Order> orderList) {
hideLoading();
this.orderList.clear();
this.orderList.addAll(orderList);
adaptor.notifyDataSetChanged();
}
#Override
public void showLoading() {
swipeRefreshLayout.setRefreshing(false);
if (getActivity() != null)
ProgressDialogFactory.getInstance(getActivity()).show();
}
#Override
public void initSwipeToRefresh() {
swipeRefreshLayout.setOnRefreshListener(() -> {
showLoading();
presenter.getOrderData();
});
}
#Override
public void onItemClicked(int position) {
// Don't navigate if order is completed
if (!orderList.get(position).getStatusId().equals(Constants.ORDER_STATUS_DELIVERED))
GlobalUtils.navigateActivityWithMultipleData(getContext(), false, OrderDetailActivity.class, R.anim.slide_in_left, R.anim.no_change, orderList.get(position).getOrderId(), orderList.get(position).getStatusDisplay());
}
private void findViews() {
mToolbar = view.findViewById(R.id.mToolbar);
recyclerView = view.findViewById(R.id.recyclerView_order);
swipeRefreshLayout = view.findViewById(R.id.swipeToRefresh);
}
private void initRecyclerView() {
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setHasFixedSize(true);
recyclerView.setVisibility(View.VISIBLE);
adaptor = new OrderAdaptor(orderList, this);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(adaptor);
recyclerView.setNestedScrollingEnabled(false);
}
private void setActionBar() {
if (getActivity() == null)
return;
setHasOptionsMenu(true);
((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
}
}
}
God Damn! After being stuck in this issue for 2 whole days, I finally found the solution. It was because I was using FragmentTransaction.TRANSIT_FRAGMENT_FADE when adding fragment inside activity. Removing the transition solved my problem

EditText OnFocusChangeListener() doesn't hide cursor and view

I made an EditText OnFocusChangeListener that supposedly hides the cursor and another view when it's not in focus (i.e. clicking anywhere on the screen aside from the EditText will hide those items). Clicking on the EditText is the only time that will display them.
This is my OnFocusChangeListener for my EditText pageTitle:
pageTitle.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b) {
pageTitle.setCursorVisible(true);
saveCancelBar.setVisibility(View.VISIBLE);
} else {
pageTitle.setCursorVisible(false);
saveCancelBar.setVisibility(View.GONE);
}
}
});
I also made an OnclickListener:
pageTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pageTitle.setCursorVisible(true);
saveCancelBar.setVisibility(View.VISIBLE);
}
});
The OnClickListener works but not the OnFocusListener. Ideally, pageTitle's cursor and saveCancelBar are initially hidden and will only appear when pageTitle is clicked.
This my XML code:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:ignore="missingPrefix"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context="app.wanderast.activity.AddPhotoActivity">
<LinearLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal">
<TextView
android:id="#+id/back_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/arrow_left"
android:textSize="20sp"
android:clickable="true"
android:textColor="#color/black" />
</LinearLayout>
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_below="#id/toolbar"
android:orientation="vertical">
<EditText
android:id="#+id/title"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="36dp"
android:background="#color/transparent"
android:maxLength="64"
android:privateImeOptions="nm"
android:inputType="textNoSuggestions|textMultiLine"
android:cursorVisible="false"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="16sp"
app:autoSizeMaxTextSize="24sp"
app:autoSizeStepGranularity="4sp"
android:textColor="#color/grey700"/>
<View
android:layout_width="100dp"
android:layout_height="2dp"
android:layout_marginTop="3dp"
android:background="#color/grey700"
/>
<LinearLayout
android:id="#+id/sort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:layout_marginTop="20dp"
android:clickable="true"
android:gravity="bottom"
android:orientation="horizontal"
android:visibility="gone">
<TextView
android:id="#+id/sort_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
android:text="List View"
android:textColor="#color/grey700"
android:textSize="14sp"/>
<TextView
android:id="#+id/sort_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/chevron_circle_down"
android:textColor="#color/grey700"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/add_photo_layout"
android:layout_marginTop="150dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_centerInParent="true">
<TextView
android:id="#+id/placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="30dp"
android:background="#null"
android:minLines="0"
android:text="Add your first travel moment to your story"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:id="#+id/capture_photo_button"
android:layout_marginBottom="15dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center|center_vertical"
android:background="#drawable/green_pill_thick">
<TextView
android:id="#+id/capture_photo_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/camera"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/green500"
android:layout_marginEnd="5dp"/>
<TextView
android:id="#+id/capture_photo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Moment"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/green500"
android:textAlignment="center"
android:textSize="14sp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/add_photo_button"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center|center_vertical"
android:background="#drawable/blue_pill_thick">
<TextView
android:id="#+id/add_photo_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/photo"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/blue500"
android:layout_marginEnd="5dp"/>
<TextView
android:id="#+id/add_photo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add from Gallery"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/blue500"
android:textAlignment="center"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/list_view_layout"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="150dp"
android:paddingBottom="50dp">
<ListView
android:id="#+id/list_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll">
</ListView>
</RelativeLayout>
</LinearLayout>
<!--Navbar-->
<LinearLayout
android:id="#+id/photo_story_navbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#drawable/navbar"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:visibility="gone">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingStart="15dp"
android:orientation="horizontal"
android:layout_marginEnd="10dp">
<!-- Take Photo Button -->
<LinearLayout
android:id="#+id/nav_capture_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:orientation="horizontal"
android:background="#drawable/green_pill_button"
android:layout_gravity="center_vertical"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="#+id/nav_capture_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/camera"
android:textColor="#color/green500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingEnd="5dp"/>
<TextView
android:id="#+id/nav_capture_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/green500"
android:textAlignment="center"
android:textSize="12sp"/>
</LinearLayout>
<!-- Add from Gallery Button -->
<LinearLayout
android:id="#+id/nav_gallery_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:layout_marginLeft="5dp"
android:orientation="horizontal"
android:background="#drawable/blue_pill"
android:layout_gravity="center_vertical"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="#+id/nav_gallery_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/photo"
android:textColor="#color/blue500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingEnd="5dp"/>
<TextView
android:id="#+id/nav_gallery_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gallery"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/blue500"
android:textAlignment="center"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingEnd="15dp"
android:orientation="horizontal">
<!-- Review button -->
<LinearLayout
android:id="#+id/nav_review_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:orientation="horizontal"
android:background="#drawable/red_500_pill"
android:layout_gravity="right"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="#+id/nav_review_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Review"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/red500"
android:textAlignment="center"
android:textSize="12sp"/>
<TextView
android:id="#+id/nav_review_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/angle_double_right"
android:textColor="#color/red500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingStart="5dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/save_cancel_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/linearLayout"
android:layout_alignParentStart="true"
android:background="#drawable/black_border_top"
android:visibility="gone">
<RelativeLayout
android:id="#+id/save_title_btn"
android:clickable="true"
android:layout_width="84dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:background="#drawable/blue_pill"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="fill"
android:text="Save Title"
android:textSize="12sp"
android:textColor="#2196F3"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/cancel_title_btn"
android:layout_width="84dp"
android:layout_height="32dp"
android:clickable="true"
android:layout_alignParentRight="true"
android:background="#drawable/grey_700_pill"
android:layout_marginRight="105dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Cancel"
android:layout_gravity="fill"
android:textSize="12sp"
android:textColor="#616161" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
If you check the definition of onFocusChange(View v, boolean hasFocus) you will notice that your code must change like this:
pageTitle.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(!hasFocus) {
pageTitle.setCursorVisible(true);
saveCancelBar.setVisibility(View.VISIBLE);
} else {
pageTitle.setCursorVisible(false);
saveCancelBar.setVisibility(View.GONE);
}
}
});
This is the code for checking the soft keyboard visibility in Android
public class MainActivity extends AppCompatActivity
{
public static String TAG = MainActivity.class.getSimpleName();
private TextView saveCancelBar;
private EditText pageTitle;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListenerToRootView();
saveCancelBar = (TextView) findViewById(R.id.cancel_bar);
pageTitle = (EditText) findViewById(R.id.title);
pageTitle.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View view, boolean hasFocus)
{
...
}
});
}
public void setListenerToRootView()
{
final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int screenHeight = activityRootView.getRootView().getHeight();
// r.bottom is the position above soft keypad or device button.
// if keypad is shown, the r.bottom is smaller than that before.
int keypadHeight = screenHeight - r.bottom;
Log.e(TAG, "keypadHeight = " + keypadHeight);
if (keypadHeight > screenHeight * 0.15) {
// 0.15 ratio is perhaps enough to determine keypad height.
Toast.makeText(getApplicationContext(), "Gotcha!!! softKeyboard open", Toast.LENGTH_SHORT).show();
} else {
// keyboard is closed
Toast.makeText(getApplicationContext(), "Gotcha!!! softKeyboard closed", Toast.LENGTH_SHORT).show();
saveCancelBar.setVisibility(View.GONE);
}
}
});
}
}
I think you forgot to add focusableInTouchMode. Add focusableInTouchMode to your editText to get focused on touching the view.
android:focusableInTouchMode="true"

How to get Item Clicked in Fragment's Recycler View to activity?

i have an activity which is having about 4 fragment and fragment have a recyclerview and in each recyclerview item there is a button.. i have a textfield in activity and i want to show the no. of button clicked.
thats my activity.
<?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:weightSum="3"
tools:context="com.example.khaalijeb.newlistview_module.PromoCodeActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.4"
android:background="#f9f9f9"
android:elevation="5dp">
<ImageView
android:id="#+id/back"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:src="#drawable/greyback" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/back"
android:text="Fancy Some Deals ?"
android:textSize="16dp"
android:textStyle="bold" />
<Button
android:id="#+id/skip"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/roundedcorneredittext"
android:text="SKIP"
android:textColor="#0277bd"
android:textSize="12dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.55"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabIndicatorColor="#E12728"
app:tabMode="scrollable"
app:tabSelectedTextColor="#E12728"
app:tabTextAppearance="#style/TextAppearance.Design.Tab" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.05"
android:background="#f9f9f9"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.2"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#727272"
/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/TextLabel">
<EditText
android:id="#+id/spass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:hint="Promo Code"
android:inputType="textPassword"
android:paddingLeft="10dp"
android:textColor="#color/material_blue_grey_800"
android:textSize="14dp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#727272" />
<Button
android:id="#+id/apply"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/roundedcorneredittext"
android:text="APPLY"
android:textColor="#0277bd"
android:textSize="12dp" />
<Button
android:id="#+id/applied"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/roundedcorneredittext"
android:text="APPLIED"
android:textColor="#0277bd"
android:textSize="12dp"
android:visibility="invisible"
/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8">
<TextView
android:id="#+id/credittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textSize="12dp"
android:text="Credited To Jeb No." />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/credittext"
android:layout_toRightOf="#+id/credittext"
android:text="C100"
android:textSize="12dp"
/>
<TextView
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/credittext"
android:layout_marginRight="16dp"
android:text="Rs.100"
android:textSize="12dp"
/>
<TextView
android:id="#+id/pickdeal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/credittext"
android:layout_margin="5dp"
android:text="Haven't Picked Any Deals Yet"
android:textColor="#78909C"
android:textSize="12dp" />
<TextView
android:id="#+id/dealamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/amount"
android:text="Rs.50"
android:layout_marginRight="16dp"
android:layout_alignTop="#+id/pickdeal"
android:textSize="12dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/dealamount"
android:text="+"
android:layout_alignTop="#+id/dealamount"
android:layout_marginRight="2dp"
android:textSize="12dp"
/>
<TextView
android:id="#+id/discount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pickdeal"
android:text="Discount"
android:layout_alignLeft="#+id/pickdeal"
android:textColor="#F44336"
android:textSize="12dp"
android:layout_marginTop="5dp"
/>
<TextView
android:id="#+id/discountedamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Rs.50"
android:textColor="#F44336"
android:layout_alignTop="#+id/discount"
android:layout_marginRight="16dp"
android:textSize="12dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/discountedamount"
android:layout_alignTop="#+id/discountedamount"
android:text="-"
android:textColor="#F44336"
android:layout_marginRight="2dp"
/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0277bd"
android:text="PROCEED TO PAY" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
recyelrview item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/llContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:padding="5dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
card_view:cardBackgroundColor="#f9f9f9"
card_view:cardCornerRadius="1dp"
card_view:cardElevation="5dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:orientation="vertical"
android:weightSum="2"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.7">
<ImageView
android:id="#+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/nike" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:orientation="horizontal"
android:weightSum="2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5">
<TextView
android:id="#+id/brandname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="hello"
android:textAllCaps="true"
android:textColor="#000000"
android:textSize="14dp"
android:typeface="sans" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/brandname"
android:layout_below="#+id/brandname"
android:layout_marginTop="10dp"
android:text="Get 50% off on Apparels"
android:textSize="12dp" />
<TextView
android:id="#+id/seemore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/brandname"
android:layout_below="#+id/title"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="See More"
android:textSize="12dp" />
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignTop="#+id/seemore"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/seemore"
android:src="#drawable/sendgrey" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5">
<Button
android:id="#+id/free"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:layout_marginBottom="16dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="16dp"
android:background="#drawable/roundedcorneredittext"
android:text="Free"
android:textColor="#0277bd"
android:textSize="12dp"
/>
<Button
android:id="#+id/taken"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:layout_marginBottom="16dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="16dp"
android:background="#drawable/roundedcorner1"
android:text="Great!"
android:textColor="#ffffff"
android:textSize="12dp"
android:visibility="invisible"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Adapter.java
public class PromoCodeRecyclerViewAdapter extends RecyclerView.Adapter<PromoCodeRecyclerViewAdapter.MyViewholder> {
private ClickListener clickListener;
private LayoutInflater inflator;
private Context mcontext;
Typeface font;
public Button b1;
public ImageButton b2;
List<promocodedata> data = Collections.emptyList();
public PromoCodeRecyclerViewAdapter(Context context, List<promocodedata> y, Typeface font) {
inflator = LayoutInflater.from(context);
this.data = y;
this.mcontext = context;
this.font = font;
}
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
#Override
public MyViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflator.inflate(R.layout.promocode_item_layout, parent, false);
MyViewholder holder = new MyViewholder(view);
return holder;
}
#Override
public void onBindViewHolder(final MyViewholder holder, int position) {
promocodedata current = data.get(position);
holder.brandicon.setImageResource(current.brandicon);
holder.brandname.setText(current.brandname);
holder.title.setText(current.title);
holder.free.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.taken.setVisibility(View.VISIBLE);
holder.free.setVisibility(View.INVISIBLE);
}
});
holder.taken.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.taken.setVisibility(View.INVISIBLE);
holder.free.setVisibility(View.VISIBLE);
}
});
}
#Override
public int getItemCount() {
return data.size();
}
public interface ClickListener {
void itemClicked(View v, int position);
}
class MyViewholder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView brandicon;
TextView brandname;
TextView title;
Button free;
Button taken;
public MyViewholder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
brandicon = (ImageView) itemView.findViewById(R.id.icon);
brandname = (TextView) itemView.findViewById(R.id.brandname);
title = (TextView) itemView.findViewById(R.id.title);
free = (Button)itemView.findViewById(R.id.free);
taken = (Button)itemView.findViewById(R.id.taken);
}
#Override
public void onClick(View v) {
if (clickListener != null) {
clickListener.itemClicked(v, getPosition());
}
}
}
}
You need to create your onitemclick because RecyclerView does not have a OnItemClick like Listview, i have an example in Github check it.

What's the difference between listview item and listview footer in rendering life cycle?

I have a listview contains items and footer views. Items layout works as it should, but footer's layout doesn't work like item views...
I could't understand where my fault is.
This is my item's layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/lstCardItem"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#drawable/pm_dashboard_cardshadow" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5.33dp"
android:layout_marginLeft="11.67dp"
android:layout_marginRight="11.67dp"
android:layout_marginTop="6dp" >
<ImageView
android:id="#+id/ivCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:animateLayoutChanges="true"
android:scaleType="fitXY" />
<com.controls.DynamicResizeImageView
android:id="#+id/ivCardLock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#drawable/pm_dashboard_cardmask"
android:scaleType="center"
android:src="#drawable/selector_btn_dashboard_lock" />
<LinearLayout
android:id="#+id/llInfo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:gravity="top|right"
android:orientation="vertical"
android:paddingLeft="18dp"
android:paddingRight="18dp"
android:visibility="visible" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|right"
android:gravity="center|right"
android:orientation="horizontal"
android:paddingBottom="4dp"
android:paddingTop="4dp" >
<TextView
android:id="#+id/tvCardBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/tvCurrency"
android:gravity="top"
android:text="10,344"
android:textColor="#color/white"
android:textSize="18dp"
android:typeface="serif" />
<TextView
android:id="#+id/tvCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="2dp"
android:gravity="top"
android:text="#string/global_currency"
android:textColor="#color/white"
android:textSize="10dp"
android:typeface="sans" />
<TextView
android:id="#+id/tvCardBalanceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="#string/pgDashboard_lblBalanceBanner"
android:textColor="#color/white"
android:textSize="11.33dp"
android:typeface="monospace" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/pm_dashboard_seam" />
</LinearLayout>
For my businness side lifecycle:
private void refreshCards() {
if (applyButtons != null)
DashboardFragment.this.lvCards.removeFooterView(applyButtons);
applyButtons = new LinearLayout(getActivity());
applyButtons.setTag("FOOTER");
applyButtons.setOrientation(LinearLayout.VERTICAL);
AbsListView.LayoutParams LLParams = new AbsListView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
applyButtons.setLayoutParams(LLParams);
if (ininalCount < 32) {
View createNewCardButton = getApplyButtonView(R.string.pgDashboard_lblCreateIninalCard, R.drawable.pm_dashboard_card_yenikartolustur, R.drawable.pm_dashboard_arrow_icon, new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), CreateNewCardActivity.class);
startActivity(intent);
}
});
View AddCard = getApplyButtonView(R.string.pgDashboard_lblAddCard, R.drawable.pm_dashboard_card_yenikartekle, R.drawable.pm_dashboard_plus_icon, new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), AddCardActivity.class);
startActivity(intent);
}
});
applyButtons.addView(createNewCardButton);
applyButtons.addView(AddCard);
}
final View applyIngCard = getApplyButtonView(R.string.pgDashboard_lblApplyToIngCard, R.drawable.pm_dashboard_card_orange, R.drawable.pm_dashboard_arrow_icon, new View.OnClickListener() {
#Override
public void onClick(View v) {
CardApplicationActivity.Synchronizer.synchronize((BaseActivity) getActivity(), new OnSyncCompletedListener() {
#Override
public void onSyncCompleted() {
Intent intent = new Intent(getActivity(), CardApplicationActivity.class);
startActivity(intent);
}
});
}
});
applyButtons.addView(applyIngCard);
View view = new View(getActivity());
view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics())));
view.setBackgroundResource(getResources().getColor(R.color.transparent));
applyButtons.addView(view);
bindAdapter();
}
private View getApplyButtonView(int stringId, int backgroundId, int iconId, View.OnClickListener onClickListener) {
final View view = inflater.inflate(R.layout.dashboard_list_card_item, null);
return view;
}
public void bindAdapter() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
DashboardFragment.this.lvCards.setLayoutTransition(new LayoutTransition());
DashboardFragment.this.lvCards.addFooterView(applyButtons, null, false);
if (adapter == null) {
adapter = new CardListAdapter(getActivity(), R.layout.dashboard_list_card_item, productModelContainerList);
controller = new CardListController(lvCards);
lvCards.setDropListener(this);
controller.setDragInitMode(CardListController.ON_LONG_PRESS);
lvCards.setFloatViewManager(controller);
lvCards.setOnTouchListener(controller);
lvCards.setAdapter(adapter);
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
DashboardFragment.this.lvCards.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
DashboardFragment.this.lvCards.setLayoutTransition(null);
}
}
Here i made some changes,, use these and let me know... otherwise i need your drawable resources to set this.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/lstCardItem"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/background_holo_dark" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5.33dp"
android:layout_marginLeft="11.67dp"
android:layout_marginRight="11.67dp"
android:layout_marginTop="6dp" >
<ImageView
android:id="#+id/ivCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:animateLayoutChanges="true"
android:scaleType="fitXY" />
<ImageView
android:id="#+id/ivCardLock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#color/background_holo_light"
android:scaleType="center"
android:src="#color/bgColor" />
<LinearLayout
android:id="#+id/llInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/cropBG"
android:gravity="top|right"
android:orientation="vertical"
android:paddingLeft="18dp"
android:paddingRight="18dp"
android:visibility="visible" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right|center_horizontal"
android:gravity="center|right"
android:orientation="horizontal"
android:paddingBottom="4dp"
android:paddingTop="4dp" >
<TextView
android:id="#+id/tvCardBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/tvCurrency"
android:gravity="top"
android:text="10,344"
android:textColor="#android:color/white"
android:textSize="18dp"
android:typeface="serif" />
<TextView
android:id="#+id/tvCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="2dp"
android:gravity="top"
android:text="Currency"
android:textColor="#android:color/white"
android:textSize="10dp"
android:typeface="sans" />
<TextView
android:id="#+id/tvCardBalanceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="pgDashboard_lblBalanceBanner"
android:textColor="#android:color/white"
android:textSize="11.33dp"
android:typeface="monospace" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bottom_btn_bg_color" />
</LinearLayout>

Categories

Resources