I have created a layout for a simple app which has one question and 4 answers in 4 radio buttons in a grid. I have designed the layout using 2 Radio groups. Please help me to design the same using one radio group. Thanks in advanceThis is what my layout looks like, but I have accomplished this using two radio groups one after another using two radio buttons in each
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.educationalapp.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/Q1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="#string/Q1" />
<RadioGroup
android:id="#+id/R1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/Q1"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/A1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="#string/A1" />
<RadioButton
android:id="#+id/B1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="#string/B1" />
</RadioGroup>
<RadioGroup
android:id="#+id/R2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/R1"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/C1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="#string/C1" />
<RadioButton
android:id="#+id/D1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="#string/D1" />
</RadioGroup>
<Button
android:id="#+id/S1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/R2"
android:layout_marginTop="4dp"
android:layout_marginRight="4dp"
android:text="Check" />
<View
android:id="#+id/V1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/S1"
android:layout_marginTop="8dp"
android:background="#color/colorPrimaryDark">
</View>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/Q2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="#string/Q2" />
<RadioGroup
android:id="#+id/R3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/Q2"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/A2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="#string/A2" />
<RadioButton
android:id="#+id/B2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="#string/B2" />
</RadioGroup>
<RadioGroup
android:id="#+id/R4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/R3"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/C2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="#string/C2" />
<RadioButton
android:id="#+id/D2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="#string/D2" />
</RadioGroup>
<Button
android:id="#+id/S2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/R4"
android:layout_marginTop="4dp"
android:text="Check"
android:layout_marginRight="4dp"
android:onClick="onCheckButtonClicked"/>
<View
android:id="#+id/V2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/S2"
android:layout_marginTop="8dp"
android:background="#color/colorPrimaryDark">
</View>
</RelativeLayout>
</LinearLayout>
This is how my XML code looks like. All I want is that only one radio button should be selected at a time for each question. I know the code using one radio groups, but for two radio groups I'm unable to figure out. Please advice.
The following approach works for me. It is based on the Udacity MOOC Material Design for Android Developers which contains a demo application named ImmersiveImages that has a group of nine radio buttons arranged in two columns. The code below is a simplified version of this app, generating a grid 1 row x 2 columns. It can easily be extended to a larger grid.
activity_main.xml
<FrameLayout 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=".MainActivity">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"
android:paddingRight="16dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CENTER"
android:id="#+id/centerBtn"
android:layout_column="0"
android:layout_row="0" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIT_END"
android:id="#+id/fitEndBtn"
android:layout_row="0"
android:layout_column="2" />
</GridLayout>
</FrameLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
RadioButton centerBtn;
RadioButton fitEndBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
centerBtn = (RadioButton) findViewById(R.id.centerBtn);
fitEndBtn = (RadioButton) findViewById(R.id.fitEndBtn);
centerBtn.setOnClickListener(this);
fitEndBtn.setOnClickListener(this);
centerBtn.setChecked(true);
}
/**
* Obtain reference to selected button
* #param view The most recently clicked radio button.
* #return The clicked radio button if different to parameter view.
*/
public RadioButton getSelectedRadio(View view) {
RadioButton[] btns = {centerBtn, fitEndBtn};
for (RadioButton radioButton : btns) {
if (radioButton.isChecked() && radioButton != view) {
return radioButton;
}
}
return null;
}
#Override
public void onClick(View view) {
// Determine if a radio button clicked and implement handler
RadioButton checkedRadio = view instanceof RadioButton ? getSelectedRadio(view) : null;
if (checkedRadio != null) {
// Untick all buttons except currently selected.
if (checkedRadio != view) {
checkedRadio.setChecked(false);
}
String radioId = ((RadioButton) view).getText().toString();
switch (radioId) {
case "CENTER":
Toast.makeText(this, "Button CENTER selected", Toast.LENGTH_SHORT).show();
break;
case "FIT_END":
Toast.makeText(this, "Button FIT_END selected", Toast.LENGTH_SHORT).show();
break;
}
}
else {
// Other view handlers
}
}
}
For reference, the app is available to download from here: https://github.com/usplitu/android_grid_radio_buttons
Related
Hello I am bulding a project which use fragments.
There is a fragment which should has list of items using scroll view and linear layout and there is a button to add new item which navigate to another fragment to get the details of a the new item the problem is that after adding the details there is a button called save to add a view (card).
I used the function add view but when I click on the button nothing happens.
I used view model and live data but I don't know what is the problem.
This is the card xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/Linear_card"
>
<androidx.cardview.widget.CardView
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/round_border"
app:cardCornerRadius="10dp"
app:cardElevation="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:fontFamily="#font/glamor"
android:text="Name"
android:textColor="#color/colorPrimary"
android:textFontWeight="1000"
android:textSize="40dp" />
<Space
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"></Space>
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/glamor"
android:lineSpacingExtra="40dp"
android:text="Price"
android:textFontWeight="1000"
android:textSize="20dp" />
</LinearLayout>
<TextView
android:id="#+id/company"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:fontFamily="#font/glamor"
android:lineSpacingExtra="30dp"
android:text="Company"
android:textColor="#color/colorPrimary"
android:textFontWeight="1000"
android:textSize="20dp" />
</androidx.cardview.widget.CardView>
and this is the detail fragment xml
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="newItem"
type="com.example.skincareapp.NewItem" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/FragmentDetail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DetailFragment"
>
<View
android:id="#+id/view"
android:layout_width="0dp"
android:layout_height="188dp"
android:background="#drawable/instruction_wave1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/view2"
android:layout_width="0dp"
android:layout_height="145dp"
android:background="#drawable/instruction_wave2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:layout_width="0dp"
android:layout_height="99dp"
android:background="#drawable/instruction_view3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#drawable/ic_wave4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/name_edit"
android:layout_width="#dimen/input_width"
android:layout_height="#dimen/input_height"
android:layout_marginTop="92dp"
android:background="#drawable/round_border"
android:drawablePadding="#dimen/padding"
android:ems="10"
android:hint="Enter the product name"
android:inputType="textPersonName"
android:padding="#dimen/padding"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.489"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view"
android:text="#{newItem.item_name}"
/>
<EditText
android:id="#+id/company_edit"
android:layout_width="#dimen/input_width"
android:layout_height="#dimen/input_height"
android:layout_marginTop="16dp"
android:background="#drawable/round_border"
android:drawablePadding="#dimen/padding"
android:ems="10"
android:hint="Enter the product company"
android:inputType="textPersonName"
android:padding="#dimen/padding"
app:layout_constraintBottom_toTopOf="#+id/cancel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.489"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/price_edit"
app:layout_constraintVertical_bias="0.0"
android:text="#{newItem.item_company}"
/>
<EditText
android:id="#+id/price_edit"
android:layout_width="#dimen/input_width"
android:layout_height="#dimen/input_height"
android:layout_marginTop="16dp"
android:background="#drawable/round_border"
android:drawablePadding="#dimen/padding"
android:ems="10"
android:hint="Enter the price"
android:inputType="textPersonName"
android:padding="#dimen/padding"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.489"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/name_edit"
android:text="#{newItem.item_price}"
/>
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="64dp"
android:layout_marginBottom="144dp"
android:background="#drawable/round_bg"
android:text="cancel"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.956"
app:layout_constraintStart_toEndOf="#+id/save" />
<Button
android:id="#+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="144dp"
android:background="#drawable/round_bg"
android:text="save"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.247"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/add_product_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:fontFamily="#font/my_font"
android:text="Add Product"
android:textColor="#color/colorPrimary"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
and this is the fragment xml which I should add the item to it
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ShopFragmnet"
>
<ScrollView
android:id="#+id/scroll_view_layout"
android:layout_width="match_parent"
android:layout_height="658dp"
app:layout_constraintBottom_toTopOf="#+id/add"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="SpeakableTextPresentCheck">
<LinearLayout
android:id="#+id/container_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="#drawable/round_bg"
android:text="Add"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
and finally this is the code of the detail fragment
class DetailFragment : Fragment() {
private val newItem:NewItem by activityViewModels()
//private lateinit var newItem:NewItem
private lateinit var cont_layout:LinearLayout
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding:FragmentDetailBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_detail,container,false)
val binding2:FragmentShopBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_shop,container,false)
cont_layout = binding2.containerLayout
//newItem = ViewModelProvider(this).get(newItem::class.java)
binding.save.setOnClickListener {
newItem.saveName(binding.nameEdit.text.toString())
newItem.saveCompany(binding.companyEdit.text.toString())
newItem.savePrice(binding.priceEdit.text.toString())
addCard(newItem)
findNavController().navigate(R.id.action_detailFragment_to_shopFragment)
}
binding.cancel.setOnClickListener { view:View ->
view.findNavController().navigate(DetailFragmentDirections.actionDetailFragmentToShopFragment())
}
return binding.root
}
private fun addCard(newItem: NewItem) {
val view:View = layoutInflater.inflate(R.layout.card,null,false)
val name_t:TextView = view.findViewById(R.id.name)
val company_t:TextView = view.findViewById(R.id.company)
val price_t:TextView = view.findViewById(R.id.price)
name_t.setText(newItem.itemName.value.toString())
company_t.setText(newItem.itemCompany.value.toString())
price_t.setText(newItem.itemPrice.value.toString())
cont_layout.addView(view)
} }
note : this also is the view model class which I used:
class NewItem: ViewModel() {
var item_name = MutableLiveData("")
var item_price = MutableLiveData("")
var item_company = MutableLiveData("")
val itemName:LiveData<String> = item_name
val itemPrice:LiveData<String> = item_price
val itemCompany:LiveData<String> = item_company
fun saveName(newname:String){
item_name.value = newname
}
fun savePrice(newPrice:String){
item_price.value = newPrice
}
fun saveCompany(newCompany:String){
item_company.value = newCompany
}
}
I am having a CollapsingToolbar and a bottom navbar as shown:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:fitsSystemWindows="true"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintTop_toTopOf="parent">
<!-- Scrollable view here -->
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="#dimen/height335"
android:background="#drawable/ic_appbar_bg"
android:fitsSystemWindows="true"
app:elevation="0dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="#color/colorPrimaryDark"
app:toolbarId="#+id/toolbar"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/transparent"
android:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
android:gravity="center"
app:contentInsetEnd="0dp"
app:contentInsetEndWithActions="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="2dp"
app:layout_collapseMode="pin"
app:theme="#style/ToolbarTheme">
<include
android:id="#+id/toolbar_header_view"
layout="#layout/widget_header_view_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
bind:cardDetails="#{viewModel}" />
</androidx.appcompat.widget.Toolbar>
<include
layout="#layout/widget_header"
bind:cardDetails="#{viewModel}" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<!-- just a nestedscrollview
override fun onTouchEvent(ev: MotionEvent): Boolean {
return scrollable && super.onTouchEvent(ev)
}
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
return scrollable && super.onInterceptTouchEvent(ev)
}
fun setScrollingEnabled(enabled: Boolean) {
scrollable = enabled
} -->
<com.android.utils.LockableNestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:clipChildren="false"
android:clipToPadding="false"
android:fillViewport="true">
<LinearLayout>
<Some views...
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_height="match_parent" />
</LinearLayout>
</com.android.utils.LockableNestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="#{()->viewModel.fabClicked()}"
android:src="#drawable/ic_plus"
app:fabCustomSize="#dimen/fab70"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
android:elevation="#dimen/elevation3"
android:paddingTop="#dimen/padding10"
app:itemTextAppearanceActive="#style/BottomNavigationViewTextStyle"
app:itemTextAppearanceInactive="#style/BottomNavigationViewTextStyle"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/nav_view" />
Fragment container will be replaced with fragments when nav bar items are clicked. The issue is that in one of the fragment I have a recyclerview. The onBindView of all the items in the recyclerview is getting invoked all at once. I need to avoid this mainly because I am trying to implement Pagination like this:
override fun onBindViewHolder(holder: TopUpViewHolder, position: Int) {
val item = myDataset[position]
holder.bind(item)
if (position == this.itemCount - 1){
// do your load more task here
viewModel.fetchTopUpData()
}
}
(I tried attaching scroll listener and implement pagination like this. But the canScrollVertically(1) is getting invoked even when the user has not reached the end, probably because of nestedscrollview itself.)
Here is my fragment with recyclerview:
<?xml version="1.0" encoding="utf-8"?>
<data>
<import type="android.view.View" />
<variable
name="viewmodel"
type="com.android.ui.topup.viewmodel.TopUpViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:padding="#dimen/padding15">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/status_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/padding20"
android:visibility="#{viewmodel.headerVisibility}">
<TextView
android:id="#+id/recentLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin20"
android:text="#string/recent_top_ups"
android:textAllCaps="true"
android:textColor="#color/topup_label_text_color"
android:textSize="#dimen/text12"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/viewAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin20"
android:onClick="#{()->viewmodel.viewAllClicked()}"
android:text="#string/view_all"
android:textColor="#color/topup_label_color_orenge"
android:textSize="#dimen/text14"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/topUpRCView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:clipChildren="false"
android:clipToPadding="false"
app:scrollListener="#{viewmodel.scrollListener}" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="#{viewmodel.fetchingFlag?View.VISIBLE:View.GONE}" />
</LinearLayout>
<LinearLayout
android:id="#+id/empty_list"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="#{viewmodel.listEmpty}"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_illustration_receipt"/>
<TextView
android:id="#+id/empty_list_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/you_have_no_topup_requests"
android:fontFamily="#font/colfaxregular"
android:textColor="#333547"
android:layout_marginTop="36dp"
android:textSize="#dimen/text16"
android:alpha=".3"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Things that I have tried:
Added android:nestedScrollingEnabled="false" in the recyclerview
Added app:layout_behavior="#string/appbar_scrolling_view_behavior"
Changed the height of recyclerview to match_parent wrap_content also 0 and set layout weight as 1(RCV is inside a linearlayout)
https://stackoverflow.com/a/44470106/6341943 => adding a header viewholder for some reason
https://stackoverflow.com/a/37558761/6341943 => cant do this because, I have a bottomnavbar
Now for the Weird part
If I keep my fragment inside a viewpager, for some reason the issue is not happening. (Even this hack is not usable is because it's messing up my collpsingToolbarLayout)
I have wasted at least a week behind this and the only solution that works is to remove the nestedscrollview. To make the recyclerview to actually recycle the view, you HAVE TO REMOVE the nestedscrollview. If you happen to have multiple views inside instead of just the recyclerview, You have to add them as items of the recyclerview by setting different viewholders, and then adding
app:layout_behavior="#string/appbar_scrolling_view_behavior"
to the recyclerview or the parent of recyclerview(the parent cannot have any other view other than Recyclerview for this to work).
In my case, the requirement was as follows:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:fitsSystemWindows="true"
app:layout_constraintTop_toTopOf="parent">
<!-- Scrollable view here -->
<com.google.android.material.appbar.AppBarLayout
.... >
<com.google.android.material.appbar.CollapsingToolbarLayout
....>
<androidx.appcompat.widget.Toolbar
....>
<include
android:id="#+id/toolbar_header_view"
.... />
</androidx.appcompat.widget.Toolbar>
<include layout="#layout/widget_header" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
....>
<LinearLayout orientation="vertical" ....>
<View .../> <!-- I needed to have these two views along with recyclerview and i wanted them to do nestedscroll along with recyclerview -->
<View .../>
<androidx.recyclerview.widget.RecyclerView
.... />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I needed to have two views before recyclerview and I wanted them to do nestedscroll along with recyclerview. The only thing that worked was by removing these to views and adding them as the first two items of recyclerview, and then adding
app:layout_behavior="#string/appbar_scrolling_view_behavior"
to the swipetorefresh layout
Hope this helps someone. I will add more info If required.
I have to add tetxview and two buttons inside cardview in such way that,
the two buttons should be on single line and below textview.
also please check below code for reference
here is my code
<FrameLayout xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#333333"
android:text="Caption"
android:id="#+id/textQuestion"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="4dp" />
<Button
android:id="#+id/btnAnswer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Expalantion"
/>
<Button
android:id="#+id/btnVideo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="video"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
I want create custom ui notification and show it full screen. How can I do it.
<LinearLayout>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="#drawable/icon_image" />
<TextView
android:id="#+id/alarm_text"
style="#style/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:text="#string/in_progress" />
<RelativeLayout
android:id="#+id/sticky_header"
android:layout_width="match_parent"
android:layout_height="#dimen/eader_height"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/sticky_text"
style="#style/normal_white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="#string/message" />
<TextView
android:id="#+id/button"
style="#style/white"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="#drawable/msg_selector"
android:gravity="center"
android:text="#string/msg" />
</RelativeLayout>
</LinearLayout>
I know there are RemoteViews we could use, but I am not sure how to set it up to NotificationCompat.Builder
Also I need to take action(launch activity) when the user presses the button in id -> button
Below is an ImageView that can't 'wrap_content' on the height and width as it is displaying the image using Picasso's fit() and centerInside() attributes.
Due to this, I have to either set a height in dp or state 'match_parent'. If I set a specific height in dp my Button will display but this isn't ideal for different sizes screens of course. If I set 'match_parent' the ImageView fills the rest of the screen and I can't display my Button underneath the ImageView.
Does anyone know of a way to nest my ImageView in some way so I can display my final Button?
Also, I can't work out how to align my picture of the top of the ImageView, please help with this as well if you can.
main_activity.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackground">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
style="#style/Toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:minHeight="?attr/actionBarSize" />
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar"
android:padding="10dp">
<Button
android:id="#+id/button_kitty"
style="#style/Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/button_kitty" />
<Space
android:id="#+id/space"
android:layout_width="10dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button_cat"
style="#style/Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/button_cat" />
</LinearLayout>
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/button_layout"
android:layout_marginBottom="17dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="4dp"
android:adjustViewBounds="true" />
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/button_layout"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:indeterminate="true"
android:visibility="invisible" />
<Button
android:id="#+id/button_facebook"
style="#style/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/image"
android:layout_centerHorizontal="true"
android:gravity="center" />
ActivityMain.java
#Override
public void onResponse(Call<KittyCatModel> call, Response<KittyCatModel> response) {
KittyCatModel model = response.body();
String url = model.getSource();
Picasso.with(this)
.load(url)
.fit()
.centerInside()
.into(imageView, this);
}
On Picasso's github, there's a thread regarding fit() not working when an imageView has adjustViewBounds set to true. Remove either one and see how it goes (I'm doing this from mobile, so can't test it before answering, sorry).
Here's a link to that thread:
https://github.com/square/picasso/issues/425
That thread links to another thread on Picasso's git regarding creating a custom imageView to make this work the way you want
Here's a link to that thread:
https://github.com/square/picasso/issues/457
Had some offline assistance with this issue, answer is:
This is nothing to do with Picasso, it is just an XML issue.
The ImageView and ProgressBar needed to be in a RelativeLayout within a
LinearLayout so I could apply layout_weight=“1” to that RelativeLayout.
This allowed me to “match_parent” on my ImageView width and height so
the image fills its layout but doesn’t hide the bottom button.
Hope that helps somebody else in the future.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_background"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
style="#style/Toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:minHeight="?attr/actionBarSize" />
<LinearLayout
android:id="#+id/button_layout_generate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar"
android:orientation="horizontal"
android:padding="12dp">
<Button
android:id="#+id/button_kitty"
style="#style/Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/button_kitty" />
<Space
android:id="#+id/space"
android:layout_width="10dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button_cat"
style="#style/Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/button_cat" />
</LinearLayout>
<RelativeLayout
android:id="#+id/image_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/button_layout_generate"
android:layout_weight="1"
android:paddingRight="16dp"
android:paddingLeft="16dp">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="true"
android:visibility="invisible" />
</RelativeLayout>
<LinearLayout
android:id="#+id/button_layout_facebook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/image"
android:orientation="horizontal"
android:padding="12dp">
<Space
android:id="#+id/space_left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
<Button
android:id="#+id/button_facebook"
style="#style/Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#id/image"
android:layout_weight="1"
android:enabled="false"
android:gravity="center"
android:text="#string/button_facebook" />
<Space
android:id="#+id/space_right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
</LinearLayout>
</LinearLayout>