CardView-TextView marginRight - java

I am learning android. I am trying to set marginRight in my textview, but its cutting my text and showing empty space in layout. My XML is like below.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="#+id/quoteCard"
android:layout_marginTop="4dp"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginBottom="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_width="match_parent"
android:elevation="3dp"
app:cardCornerRadius="6dp"
android:layout_height="match_parent">
<ImageView
android:id="#+id/cardBackground"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:padding="8dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:id="#+id/authorImage"
android:layout_width="128dp"
android:layout_height="128dp"
app:civ_border_width="2dp"
app:civ_border_color="#99333333"/>
<TextView
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="6dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="6dp"
android:lineSpacingExtra="15dp"
android:id="#+id/textDetailQuote"
android:text="The price of success is hard work, dedication to the job at hand, and the determination that whether we win or lose, we have applied the best of ourselves to the task at hand.Read more at: "
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_marginBottom="10dp"
android:layout_marginRight="8dp"
android:id="#+id/textAuthorSign"
android:layout_gravity="right"
android:text="- ABJ Abdul Kalam"
android:textStyle="bold"
android:textSize="16sp"
android:visibility="invisible"
android:typeface="serif"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
This is the image of my issue.... Please check and let me know.
Please check and let me know what I have wrong in this .
Thanks

instead of maringLeft and marginRight use paddingLeft and paddingRight in your textView
<TextView
android:gravity="center"
android:layout_marginTop="6dp"
android:paddingRight="16dp"
android:paddingLeft="16dp"
android:layout_marginBottom="6dp"
android:lineSpacingExtra="15dp"
android:id="#+id/textDetailQuote"
android:text="The price of success is hard work, dedication to the job at hand, and the determination that whether we win or lose, we have applied the best of ourselves to the task at hand.Read more at: "
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Related

How can I convert an XML to make a programmatically view in Android Java

I made a design for a comment section in XML, but I want to create it programmatically when I get some data from database. How can I convert all of these to Java?
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:cardCornerRadius="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/complete"
android:scaleType="centerCrop"
android:layout_gravity="center">
</ImageView>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.75" >
<TextView
android:id="#+id/commentFullname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calin Onaca:"
android:textColor="#000"
android:layout_gravity="center"/>
<TextView
android:id="#+id/commentReply"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:textColor="#000"
android:text="So Bleesed i choosed him no matter what you say i will always choose him"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/postReply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reply"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="Reply: "/>
<TextView
android:id="#+id/replyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="Thank you very much my dear" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
All the texts will be displayed from database. I know it sounds stupid, but I'm a begginer and I really don't know how to do this. :) Thank you in advance!
Use one of the LayoutInflater inflate methods that best satisfies your use case. The below example will create View containing your layout.
View commentView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.comment_layout,null);

Android/XML: RelativeLayout design

basically, I don't understand how to use RelativeLayout. I don't know how to design it. I always use LinearLayout with Table inside of it. Now I use RelativeLayout for my design. I want to know, how to add text "Sorry, no list available" Below is my current code of XML
<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:id="#+id/haha"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyHistoryList">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:gravity="center"
android:longClickable="false"
android:maxLines="1"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<TextView
android:id="#+id/tvSuggestion"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".80"
android:gravity="start"
android:text="Suggestion"
android:textAllCaps="true"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/colorAccent"
android:textStyle="bold"
app:fontFamily="#font/anaheim" />
<TextView
android:id="#+id/tvStatus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:gravity="start"
android:text="Status"
android:textAllCaps="true"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/colorAccent"
android:textStyle="bold"
app:fontFamily="#font/anaheim" />
</TableRow>
</LinearLayout>
<RelativeLayout
android:id="#+id/huhu"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recylcerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:visibility="gone" />
<ImageView
android:id="#+id/imgEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#drawable/empty"/>
</RelativeLayout>
</LinearLayout>
This is how it looks like
(source: fbcdn.net)
But, I want to add text "Sorry, no list available" below of the image (marginTop = 20dp). Can anyone help?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_below="#id/imgEmpty"
android:text="Sorry, no list available"/>
Insert it below your ImageView of RelativeLayout!
Replace your Relative layout with this.
<RelativeLayout
android:id="#+id/huhu"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recylcerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:visibility="gone" />
<ImageView
android:id="#+id/imgEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#drawable/empty"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_below="#id/imgEmpty"
android:text="Sorry, no list available"/>
Add this below imgEmpty. Use android:layout_below to place the text below image,android:layout_centerInParent="true" to move text to center.
<TextView
android:layout_marginTop="20dp"
android:layout_centerInParent="true"
android:text = "Sorry, no list available"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imgEmpty"/>
Here the output
in relative layout you can use below/above to put view below/above its sibling
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imgEmpty"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="TextView"/>

Android : code not functioning as intended

Screenshot of Code
I'm trying to display information about a business.
However the phone number is being displyed in wrong place ( ie top right)
According to me, It should come right of call image.
What is wrong with code phonenumber_text_view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rafi.ansarenglishschool.MainActivity">
<ImageView
android:id="#+id/ansar_image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="#drawable/ansar"
android:layout_alignParentTop="true"
/>
<TextView
android:id="#+id/ansar_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ansar_image_view"
android:text="Ansar English School"
android:textSize="30sp"
android:background="#2962ff"
android:padding="20dp"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/call_image_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="#drawable/call1"
android:layout_below="#id/ansar_text_view"
/>
<TextView
android:id="#+id/phonenumber_text_view"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_toRightOf="#id/call_image_view"
android:layout_alignParentRight="true"
android:text="123456789"
android:textSize="30sp"
android:padding="20dp"
android:textColor="#android:color/black" />*
<ImageView
android:id="#+id/location_image_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="#drawable/location1"
android:layout_below="#id/call_image_view"
/>
<TextView
android:id="#+id/disc_text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/location_image_view"
android:text="Ansar English is a private school run by Ansari Charitable trust located in Perumpilavu."
android:textSize="15sp"
android:padding="20dp"
android:textColor="#android:color/black" />
Just use the android:layout_below attribute to align it properly like following:
<TextView
android:id="#+id/phonenumber_text_view"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_toRightOf="#id/call_image_view"
android:layout_below="#id/ansar_text_view"
android:text="123456789"
android:textSize="30sp"
android:padding="20dp"
android:textColor="#android:color/black" />
PS: There is a * right after above TextView in your xml file

Gray line inside cardview :)?

How do I remove the gray line (you can see on the screen) at the bottom of CardView?
Archive with source-code
I reviewed everything. I don't understand what the problem is.
<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:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="contact det"
android:gravity="center_vertical"
android:textColor="#android:color/background_dark"
android:textSize="14dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<View
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#color/accent"
android:layout_below="#+id/linearLayout"/>
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="#+id/linearLayout"
android:layout_alignLeft="#+id/txtSurname"
android:layout_alignStart="#+id/txtSurname"/>
<TextView
android:id="#+id/txtSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="#+id/txtName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:id="#+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="10dp"
android:layout_above="#+id/txtAdd"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<TextView
android:id="#+id/txtAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textSize="10dp"
android:layout_alignTop="#+id/txtSurname"
android:layout_alignLeft="#+id/txtEmail"
android:layout_alignStart="#+id/txtEmail"/>
</RelativeLayout>
Its my item CardView.
when
card_view:cardCornerRadius="2dp"
all is good, but when I write another value and line appears!
Just use “com.google.android.material.card.MaterialCardView” it will avoid issues connected to CardView
Try this -
android:divider="#null"
Or from your code -
getListView().setDivider(null);
getListView().setDividerHeight(0);

Can't scroll 3 TableViews in a parent ScrollView, even though I used the nestedScrollView

I've got 3 TableViews(third party library I found on GitHub) on my activity. It's in a scrollable parent view ... I used the nestedScrollView to enable scrolling in my TableViews but it doesn't work... it turns dark and data doesn't get displayed... please help
Reply
the library I used :
//TableView library
compile 'de.codecrafters.tableview:tableview:2.8.0'
Code snippet - I removed the ScrollView and the multiples nestedScrollViews due to the fact that I had a deadline to release my app and was forced to do without :
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="com.example">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2979ff"
android:id="#+id/driver_money_double"
android:gravity="center"
android:textColor="#fafafa"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tableOneBackColor"
android:id="#+id/yyy"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:textColor="#color/white"
android:text="FARE 1"
android:textSize="12sp" />
<de.codecrafters.tableview.TableView xmlns:table="http://schemas.android.com/apk/res-auto"
android:id="#+id/summary_table_one"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</de.codecrafters.tableview.TableView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tableTwoBackColor"
android:id="#+id/xxx"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:textColor="#color/white"
android:text="FARE 2"
android:textSize="12sp"/>
<de.codecrafters.tableview.TableView
xmlns:table="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/summary_table_two"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tableThreeBackColor"
android:id="#+id/zzz"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:textColor="#color/white"
android:text="Combination"
android:textSize="12sp"/>
<de.codecrafters.tableview.TableView
xmlns:table="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/summary_table_three" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="#d84315"
android:id="#+id/Money_hand"
android:gravity="center"
android:text="Change in Hand R"
android:textColor="#fafafa"
android:textSize="25sp"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#d84315"
android:id="#+id/change_money_double"
android:gravity="center"
android:textColor="#fafafa"
android:textSize="25sp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
</LinearLayout>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ama_ads"
android:layout_gravity="center"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub4444444">
</com.google.android.gms.ads.AdView>
</LinearLayout>
</RelativeLayout>

Categories

Resources