CardView doesn't work properly - java

I have a work with the RecyclerView, and I use the CardView as well.I have made all these standards procedures (created the ViewHolder, implemented necessary methods) but the problem is that CardView's corners aren't rounded.I get only rectangular corners of the CardView.
Here is the XML code of Adapter's "main layout":
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_margin="15dp"
card_view:cardElevation="10dp"
card_view:cardCornerRadius="30dp">
<RelativeLayout
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/adapterText"
android:text="#string/textCaption"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/adapterDate"
android:text="#string/date"
android:layout_marginTop="10dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/adapterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>
And another layout with RecyclerView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/contactList"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
And initialization of the RecyclerView:
#Override
public void onViewCreated(View root, Bundle savedInstanceState) {
if (root != null) {
RecyclerView mRecyclerContactList=(RecyclerView)(root.findViewById(R.id.contactList));
mContactAdapter=new ContactAdapter(getContext(),mContextualMultiMode,mContactList);
mRecyclerContactList.setAdapter(mContactAdapter);
RecyclerView.ItemAnimator animator=new DefaultItemAnimator();
mRecycle
rContactList.setItemAnimator(animator);
RecyclerView.ItemDecoration mVerticalDecoration=new DividerItemDecoration
(getContext(), LinearLayoutManager.VERTICAL);
mRecyclerContactList.addItemDecoration(mVerticalDecoration);
mRecyclerContactList.setLayoutManager(new LinearLayoutManager(getContext()));
}
}
Does anybody know how to solve this issue?

Cardview as root causes strange problems to me. Try to use Relativelayout as root.

Hi try using this code
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="250dp"
android:baselineAligned="false"
android:elevation="6dp"
cardview:cardCornerRadius="6dp"
cardview:cardElevation="6dp"
cardview:cardPreventCornerOverlap="false"
cardview:cardUseCompatPadding="true"
cardview:contentPadding="0dp">
</android.support.v7.widget.CardView>

Use this code when you are using cardview
<android.support.v7.widget.CardView
android:id="#+id/cardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:elevation="100dp"
card_view:cardBackgroundColor="#color/cardview_initial_background"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="#dimen/margin_large"
android:layout_marginRight="#dimen/margin_large"
>

Related

How to Refresh Fragments with swipe to refresh widget?

I wanted to refresh my fragment with SwipeToRefresh widget.
This is my XML Code
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#drawable/gradient_drawable"
android:padding="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragment_1">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progress"
android:layout_width="76dp"
android:layout_height="64dp"
android:elevation="10dp"
android:foregroundGravity="center"
android:indeterminateTint="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.84" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:padding="5dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</FrameLayout>
and this is my JAVA code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_1, container, false);
newconfig= getResources().getConfiguration();
db=new DBHandler(getActivity());
swipeRefreshLayout=view.findViewById(R.id.swipe);
recyclerView=view.findViewById(R.id.listview);
progressBar=view.findViewById(R.id.progress);
if(book.size()==0) {
book = db.readCourses();
}
progressBar.setVisibility(View.GONE);
BookFragAdapter adapter = new BookFragAdapter(Fragment_land1.this,book);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),RecyclerView.VERTICAL,false));
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
this.onRefresh();
getFragmentManager().beginTransaction().detach(Fragment_land1.this).attach(Fragment_land1.this).commit();
swipeRefreshLayout.setRefreshing(false);
}
});
return view;
}
When I try to refresh it doesn't refresh and the widget doesn't stops after 250ms. I assume that the code inside onRefreshListener is not actually executing therefore the next line is ignored. I don't know how change it with. I surfed in Stackoverflow and Developer.android none of the answers solved my problem. Can anyone help me?
first check that you have added dependencies in build.gradle file
i.e implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
Secondly swipe to refresh layout tag should start from start instead of fragment
for eg :
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_drawable"
android:padding="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragment_1">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progress"
android:layout_width="76dp"
android:layout_height="64dp"
android:elevation="10dp"
android:foregroundGravity="center"
android:indeterminateTint="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.84" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:padding="5dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
third then write swipeRefreshLayout.setOnRefreshListener code in java file

Android/Java: How to use ConstraintLayout in a ScrollView, in a Fragment that is in a navigation bar?

I have a problem to include a ScrollView with a ConstraintLayout. How Can I mix the two? I would like to get a good resizing of the screen, but with the capability by scrolling the screen if the number of the buttons will increase.
I tried this, but without success. I used that ticket
here is the xml file:
<ScrollView
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:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/homeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/imagealliance"
android:backgroundTint="#80FFFFFF"
android:backgroundTintMode="src_over"
android:gravity="center"
android:theme="#style/AppTheme"
tools:context=".LoginActivity">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:alpha="0.6"
android:scaleType="fitXY"
android:src="#drawable/logoalliance"
app:layout_constraintBottom_toTopOf="#+id/txt_login"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Here is the code for the buttons:
public class HomeFragment extends Fragment {
...
layout.addView(eventBtn);
if (i == 0)
{
test1 = eventBtn.getId();
Log.d("test1", String.valueOf(eventBtn.getId()));
}
if (i == countUser.length() - 1)
{
test2 = eventBtn.getId();
Log.d("test2", String.valueOf(eventBtn.getId()));
}
Here is the code for the layout:
// Create ConstraintSet
ConstraintSet constraintSet = new ConstraintSet();
// Make sure all previous Constraints from ConstraintLayout are not lost
constraintSet.clone(layout2);
// Create Rule that states that the START of btn_contact_us1 will be positioned at the END of btn_contact_us2
constraintSet.connect(imageView1.getId(), ConstraintSet.START, test1, ConstraintSet.END);
constraintSet.applyTo(layout2);
Thanks in advance
Try updating your xml by following code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivityStack">
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:alpha="0.6"
android:scaleType="fitXY"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

Height MapFragment Layout not work

I'm build an app that contain two tabhost (top and botton) and in one tab, I want to put a Google Map through the Google API. All works well but the map does not display correctly. You can see better in this picture:
My code is:
fragment_mapa.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
MapaFragment.java
public class MapaFragment extends Fragment {
public MapaFragment(){}
public GoogleMap mapa;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mapa, container, false);
if (container != null) {
container.removeAllViews();
}
GoogleMap map = ((SupportMapFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
((MainActivity) getActivity()).setBar();
//Asigno el nombre de la vista e integro tabhost-sliding
((MainActivity) getActivity()).setTitle("Mapa");
((MainActivity) getActivity()).integrationMenu();
return rootView;
}
public void onDestroyView() {
super.onDestroyView();
Log.d("DESTROY", "onDestroyView");
Fragment f = getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map);
if (f != null) {
getActivity().getSupportFragmentManager()
.beginTransaction().remove(f).commit();
}
}
}
And my activity_main.xml that contain the two tabhost and a framelayout that call the fragments:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Estructura de los dos tabhost -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TABHOST SUPERIOR -->
<RelativeLayout
android:id="#+id/l1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="right"
android:layout_above="#+id/l2"
android:layout_alignParentTop="true">
<android.support.v4.app.FragmentTabHost
android:id="#+id/tabhost_sup"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_weight="0"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0"/>
</ScrollView>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</RelativeLayout>
<!-- TABHOST INFERIOR -->
<RelativeLayout
android:id="#+id/l2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_weight="1"
android:gravity="right"
android:layout_alignParentBottom="true"
android:background="#android:color/background_light" >
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/RelativeLayout1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginBottom="0dp"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</RelativeLayout>
</RelativeLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
<ListView
android:id="#+id/right_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
</android.support.v4.widget.DrawerLayout>
Removing scrollview is not convinient solution.Instead of it try android:FillViewPort="True" for Scrollview in your layout solve your problem for all screen where you may require scroll for your activity content.Hope this helps you!!
SOLUTION (by Hardik):
Remove the scrollview in my activity_main.xml and it works well.
Use hierarchiviewer of android tools to check layout values.
For the future:
If you have a ScrollView then elements must specify their height (width for horizontal scroll view) otherwise what height should they have, infinite?
I also had an issue where the exact thing happened when placing the map fragment inside a Relative Layout that had height = Wrap_Content, with minimum height.
Setting a constant height resolved the issue, same idea.
came across this same issue but my map fragment is inside a scrollview and it needs to have a scrollview since there are also other views aside from the map. if it only contains the map, then yes scrollview is not needed.

static header with dynamic list view troubles

Similar to the contacts view on the androids contacts section, which has the search bar on top with a dynamic list view below it, example here, I am trying to have a spinner instead of a text field.
My problem is that in the layout, the Spinner repeats per list view item.
I have the following list.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sc" />
<TextView
android:id="#+id/rowData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
and the follow Java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
application = ((MyAppApplication) getApplication());
populateOffers();
}
private void populateOffers()
{
MyAppRequestor MyApp = new MyAppRequestor();
ArrayList<String> list = MyApp.items(application.getMyAppId());
adapter = new ArrayAdapter<String>(this,
R.layout.list, R.id.rowData, list);
setListAdapter(adapter);
}
Since you are using list.xml as your layout it will be repeated.. If you want spinner only once, then you have to put in the main layout (Layout where you have defined the list view as a component.
try this list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/bgd" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sc" />
<TextView
android:id="#+id/rowData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

Android how to add a Linearlayout to another LinearLayout from code

I have a LinearLayout in main.xml:
<?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="fill_parent"
android:orientation="vertical"
android:id="#+id/mainLayout" >
</LinearLayout>
I have made another XML file, called item_box.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/item_bg"
android:gravity="right" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="20dp"
android:text="#string/item1"
android:textSize="30dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="20dp"
android:gravity="right"
android:text="#string/number1"
android:textColor="#color/number_bg"
android:textSize="30dp" />
</LinearLayout>
Basically, what I want to do from the code (programmatically), is add a couple item_box.xml into the main.xml. How can I accomplish this?
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);
mainLayout.addView(itemBox);
try this linearlayout inside another linearlayout add id to linearlayout layout1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/firstlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
in oncreate() call
LinearLayout firstlayout=(LinearLayout)findviewbyId(R.id.firstlayout);
LinearLayout secondlayoout=(LinearLayout)this.getLayoutInflater().inflate(R.layout.layout2,null); //inflating view from xml
TextView btn1=(TextView)secondlayoout.findviewbyId(R.id.button1);
btn1.settext("TEST");
firstlayout.addview(secondlayoout);
You are getting a Nullpointer because linearLayout1 is not in mainLayout. You need to inflate your view first and then add it. You should read this question I think it will help you.
Use LayoutInflater:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);

Categories

Resources