I am trying to create a FrameLayout that contains other FrameLayouts. I have created an EditorView class that extends FrameLayout, and I want it to display FileView, another class that extends FrameLayout. But in Android Studio preview I get an blank view. What am I doing wrong.
This is the ediorview_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<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">
<com.itsvaske.webio.Views.LineView
android:id="#+id/html1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.itsvaske.webio.Views.LineView
android:id="#+id/html2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/html1" />
<com.itsvaske.webio.Views.LineView
android:id="#+id/html3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/html2" />
<com.itsvaske.webio.Views.LineView
android:id="#+id/html4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/html3" />
<com.itsvaske.webio.Views.LineView
android:id="#+id/html5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/html4" />
<com.itsvaske.webio.Views.LineView
android:id="#+id/html6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/html5" />
<com.itsvaske.webio.Views.LineView
android:id="#+id/html7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/html6" />
</RelativeLayout>
This is lineview_layout.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="match_parent">
<TextView
android:id="#+id/lineNumber"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_weight="0"
android:gravity="center"
android:text="1"
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:id="#+id/line"
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_weight="3"
android:background="#null"
android:ems="10"
android:hint="#string/typeyourline"
android:inputType="textPersonName"
android:minHeight="32dp"
android:textColorHint="#546E7A" />
</LinearLayout>
This is EditorView class:
public class EditorView extends FrameLayout {
private Context mContext;
private AttributeSet mAttributeSet;
public EditorView(#NonNull Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
this.mAttributeSet = attrs;
initView();
}
private void initView() {
View view = View.inflate(mContext, R.layout.editor_start_html_layout, this);
LineView line1 = view.findViewById(R.id.html1);
LineView line2 = view.findViewById(R.id.html2);
LineView line3 = view.findViewById(R.id.html3);
LineView line4 = view.findViewById(R.id.html4);
LineView line5 = view.findViewById(R.id.html5);
LineView line6 = view.findViewById(R.id.html6);
LineView line7 = view.findViewById(R.id.html7);
line1.setLineNumber(1);
line1.setLineContents("<html>");
line2.setLineNumber(2);
line2.setLineContents("<head>");
line3.setLineNumber(3);
line3.setLineContents(" <title></title>");
line4.setLineNumber(4);
line4.setLineContents("</head>");
line5.setLineNumber(5);
line5.setLineContents("<body>");
line6.setLineNumber(6);
line6.setLineContents("</body>");
line7.setLineNumber(7);
line7.setLineContents("</html>");
}
}
And In preview I get this:
Related
I want to be able to perform onClickListner on root RelativeLayout called "rl". Not it does not work I suspect it can be because of MotionLayout as a children. How to solve that?
Here is what I have done so far, my code.
ShowNoteActivity.java
(...)
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
(...)
RelativeLayout rl = findViewById(R.id.rl);
rl.setOnClickListener(view -> {
Toast.makeText(this, "abc", Toast.LENGTH_SHORT).show();
});
}
show_note_activity.xml
<?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"
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true">
<pl.jawegiel.simplenotepad.CustomMotionLayout
android:id="#+id/motion_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
app:layoutDescription="#xml/show_note_activity_scene">
<TextView
android:id="#+id/note_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:clickable="false"
android:duplicateParentState="true"
android:focusable="false"
android:gravity="center"
android:text="#string/note_title"
android:textColor="?attr/myTextColor"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/note_title_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView3"
android:clickable="false"
android:duplicateParentState="true"
android:focusable="false"
android:gravity="top|left"
android:hint="#string/enter_title"
android:lines="2"
android:maxLines="10"
android:minHeight="48dp"
android:textColor="?attr/myTextColor"
android:textColorHint="?attr/myHintTextColor"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#id/note_title" />
<TextView
android:id="#+id/note_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/note_title_value"
android:layout_marginTop="10dp"
android:clickable="false"
android:duplicateParentState="true"
android:focusable="false"
android:gravity="center"
android:text="#string/note_desc"
android:textColor="?attr/myTextColor"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/note_title_value" />
<androidx.core.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
app:layout_constraintTop_toBottomOf="#id/note_desc">
<RelativeLayout
android:id="#+id/rl2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false">
<TextView
android:id="#+id/note_desc_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:textColor="?attr/myTextColor"
android:textColorHint="?attr/myHintTextColor"
android:textSize="18sp" />
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
</pl.jawegiel.simplenotepad.CustomMotionLayout>
</RelativeLayout>
CustomMotionLayout.java
public class CustomMotionLayout extends MotionLayout {
public CustomMotionLayout(Context context) {
super(context);
}
public CustomMotionLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomMotionLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return super.onInterceptTouchEvent(event);
}
return false;
}
}
show_note_activity_scene.xml
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ConstraintSet android:id="#+id/start">
<Constraint
android:id="#id/note_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Constraint
android:id="#id/note_title_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#id/note_title"/>
<Constraint
android:id="#id/note_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/note_title_value"/>
</ConstraintSet>
<ConstraintSet android:id="#+id/end">
<Constraint
android:id="#id/note_title"
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha="0.0"/>
<Constraint
android:id="#id/note_title_value"
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha="0.0"/>
<Constraint
android:id="#id/note_desc"
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</ConstraintSet>
<Transition
app:constraintSetEnd="#id/end"
app:constraintSetStart="#id/start"
app:duration="1000">
<OnSwipe
app:touchAnchorSide="top"
app:dragDirection="dragUp"
app:moveWhenScrollAtTop="true"
app:touchAnchorId="#id/nestedScrollView"/>
<KeyFrameSet>
</KeyFrameSet>
</Transition>
</MotionScene>
If you want to click on parent you should disable motionlayout transition and do your transition programmatically with one button or ...
Disable motion layout:
motionLayout.getTransition(R.id.yourTransition).setEnable(false)
If you must to use onSwipe in your project you can disable transition after collapsing motionlayout and return layout to top programmatically and when expand enable motionlayout transition.
This question already has answers here:
RecyclerView items don't fill width
(3 answers)
Recycler view showing single item
(8 answers)
Closed 2 years ago.
I need to create a Recyclerview. For that I have created an cardview model to be displayed in a recyclerview. Even though i have given match parent to the layouts, the width of cardview is not fitting the parent. Its width is showing approximately up to half of the screen(not matching parent) Do anyone can support?.please advise better way to do this.
Attached the xml code below
<?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">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
app:cardCornerRadius="15dp"
android:layout_height="wrap_content"
android:foreground="#drawable/border_creatives"
android:layout_marginHorizontal="6dp"
android:layout_marginVertical="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/dficon"
app:layout_constraintBottom_toTopOf="#id/guideline19"
app:layout_constraintStart_toStartOf="#id/guideline23"
app:layout_constraintTop_toBottomOf="#id/guideline18"
app:layout_constraintEnd_toStartOf="#id/guideline50"/>
<TextView
android:id="#+id/dname"
android:layout_width="0dp"
android:layout_height="0dp"
android:fontFamily="#font/amiko_semibold"
android:textColor="#color/black"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/guideline24"
app:layout_constraintEnd_toStartOf="#+id/ddownbutton"
app:layout_constraintStart_toStartOf="#+id/guideline50"
app:layout_constraintTop_toTopOf="#+id/guideline18" />
<TextView
android:id="#+id/ddate"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:fontFamily="#font/amiko_semibold"
android:textColor="#color/black"
android:textSize="13sp"
app:layout_constraintBottom_toTopOf="#+id/guideline19"
app:layout_constraintEnd_toStartOf="#id/barrier7"
app:layout_constraintStart_toStartOf="#id/guideline50"
app:layout_constraintTop_toTopOf="#+id/guideline24" />
<TextView
android:id="#+id/dfileextension"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="30dp"
android:fontFamily="#font/amiko_semibold"
android:textColor="#color/black"
android:textSize="13sp"
app:layout_constraintBottom_toTopOf="#+id/guideline19"
app:layout_constraintStart_toEndOf="#id/barrier7"
app:layout_constraintTop_toTopOf="#+id/guideline24" />
<ImageButton
android:id="#+id/ddownbutton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/mins_customselector"
android:clickable="true"
android:src="#drawable/filedownload_icon"
app:layout_constraintBottom_toTopOf="#+id/guideline19"
app:layout_constraintEnd_toStartOf="#+id/guideline22"
app:layout_constraintTop_toTopOf="#+id/guideline18"
android:focusable="true" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.10" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.90" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.8275" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.97" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.045" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.57" />
<androidx.constraintlayout.widget.Barrier
android:id="#+id/barrier7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
app:constraint_referenced_ids="ddate" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.20" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
main layout having recyclerview
<?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=".Download_Activity">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/toolbar"
app:layout_constraintStart_toStartOf="#+id/guideline49"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="#id/toolbar">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="start"
android:text="#string/downloads"
android:textColor="#FCFFFD"
android:textSize="30sp"
app:fontFamily="#font/carter_one"/>
</com.google.android.material.textfield.TextInputLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/drecyclerdownloads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/toolbar" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline49"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.20" />
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter class
public class DownloadsAdapter extends RecyclerView.Adapter<Downloadsviewholder> {
Download_Activity download_activity;
ArrayList<Downloadsmodel> downloadsmodels;
public DownloadsAdapter(Download_Activity download_activity, ArrayList<Downloadsmodel> downloadsmodels) {
this.download_activity = download_activity;
this.downloadsmodels = downloadsmodels;
}
#NonNull
#Override
public Downloadsviewholder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(download_activity.getBaseContext());
View view = layoutInflater.inflate(R.layout.downloads_elements,null,false);
return new Downloadsviewholder(view);
}
#Override
public void onBindViewHolder(#NonNull final Downloadsviewholder downloadsviewholder, final int i) {
downloadsviewholder.dName.setText(downloadsmodels.get(i).getName());
downloadsviewholder.dUploaddate.setText(downloadsmodels.get(i).getUploaddate());
downloadsviewholder.dExtension.setText(downloadsmodels.get(i).getFileextension());
Picasso.get().load(downloadsmodels.get(i).getIcon()).into(downloadsviewholder.dIcon);
downloadsviewholder.dButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (ConnectionCheck.checkconnetion(download_activity.getBaseContext())) {
dowloadFile(downloadsviewholder.dName.getContext(), downloadsmodels.get(i).getName(), downloadsmodels.get(i).getFileextension()
, DIRECTORY_DOWNLOADS, downloadsmodels.get(i).getLink());
} else {
Toast.makeText(download_activity.getBaseContext(),"Check your internet connection", Toast.LENGTH_SHORT).show();
}
}
});
}
public void dowloadFile (Context context,String filename, String fileextension, String destinationdirectory, String url){
DownloadManager downloadManager=(DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
Uri uri=Uri.parse(url);
DownloadManager.Request request=new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(context, destinationdirectory, filename+"."+fileextension);
downloadManager.enqueue(request);
}
#Override
public int getItemCount() {
return downloadsmodels.size();
}
}
Viewholder
public class Downloadsviewholder extends RecyclerView.ViewHolder {
TextView dName;TextView dLink;TextView dUploaddate; TextView dExtension;
ImageView dIcon;
ImageButton dButton;
public Downloadsviewholder(#NonNull View itemView) {
super(itemView);
dName = itemView.findViewById(R.id.dname);
dUploaddate=itemView.findViewById(R.id.ddate);
dExtension=itemView.findViewById(R.id.dfileextension);
dButton=itemView.findViewById(R.id.ddownbutton);
dIcon=itemView.findViewById(R.id.dficon);
}
}
I am creating a simple tasklist app, and I want to programmatically add a TextView to an embedded LinearLayout. However, it is not showing up for some reason.
I try to add the TextView in MainActivity (irrelevant code removed).
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
processExtraData();
}
private void processExtraData(){
Integer hours = 4;
Integer minutes = 35;
Integer seconds = 0;
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linlayout);
TextView txt1 = new TextView(this);
final float scale = this.getResources().getDisplayMetrics().density;
int pixels = (int) (64 * scale + 0.5f);
ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(0, pixels, 1f);
txt1.setLayoutParams(params); txt1.setText(hours.toString()+":"+minutes.toString()+":"+seconds.toString());
linearLayout.addView(txt1);
}
The embedded LinearLayout is declared in activity_main:
<?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"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Super Reminder App!"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:orientation="horizontal">
<EditText
android:id="#+id/taskAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.5"
android:hint="New Task" />
<Button
android:id="#+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="Add Task" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="12.5"
android:orientation="horizontal">
<ListView
android:id="#+id/tasks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.5"/>
<LinearLayout
android:id="#+id/linlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"/>
</LinearLayout>
<Button
android:id="#+id/clearButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Clear All" />
</LinearLayout>
I have already tested and made sure that onNewIntent and processExtraData are called. No errors are thrown either. Could this be a GUI issue? Any help would be greatly appreciated.
Recycler View is only occupying 1st half of the screen no matter what I do.
Can anyone suggest how to make Recyclerview occupy full width of the screen?
This is what it displays me in Android Studio
But this is the actual output on a device. It's all wrapped to the left side of the device.
activity.java
public class RecordLogs extends AppCompatActivity {
private RecyclerView recyclerView;
private RecordsAdapter adapter;
private List<Record> recordList;
private ProgressBar progressBar;
private FirebaseFirestore db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record_logs);
progressBar = findViewById(R.id.progressbar);
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager((this)));
recordList = new ArrayList<>();
adapter = new RecordsAdapter(this, recordList);
recyclerView.setAdapter(adapter);
db = FirebaseFirestore.getInstance();
activity.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RecordLogs">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
row.xml
<?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="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="15sp"
tools:text="26/7/2018" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Date"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/qty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="15sp"
tools:text="5" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Qty. L"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/rate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="15sp"
tools:text="25" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rate ₹"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/total"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="15sp"
tools:text="125" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Total ₹"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
adapter.java
public class RecordsAdapter extends RecyclerView.Adapter<RecordsAdapter.RecordViewHolder> {
private Context mCtx;
private List<Record> recordList;
public RecordsAdapter(Context mCtx, List<Record> recordList) {
this.mCtx = mCtx;
this.recordList = recordList;
}
#NonNull
#Override
public RecordViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.record_logs_card, null);
return new RecordViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RecordViewHolder holder, int position) {
Record record = recordList.get(position);
holder.date.setText(record.getDate());
holder.qty.setText(String.valueOf(record.getQty()));
holder.rate.setText(String.valueOf(record.getRate()));
holder.total.setText(String.valueOf(record.getTotal()));
}
#Override
public int getItemCount() {
return recordList.size();
}
class RecordViewHolder extends RecyclerView.ViewHolder {
TextView date, qty, rate, total;
public RecordViewHolder(View itemView) {
super(itemView);
date = itemView.findViewById(R.id.date);
qty = itemView.findViewById(R.id.qty);
rate = itemView.findViewById(R.id.rate);
total = itemView.findViewById(R.id.total);
}
}
}
I found it:
All I had to change the following line of code in my custom adapter as follows:
View view = inflater.inflate(R.layout.record_logs_card, null);
to
View view = inflater.inflate(R.layout.record_logs_card, parent, false);`
I am trying to make a reusable header. Here is my XML.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg" >
<ImageButton
android:id="#+id/imagebuttonforheader"
android:layout_height="50dp"
android:layout_width="50dp"
android:layout_alignParentLeft="true"
android:background="#drawable/back_button"
/>
<ImageButton
android:id="#+id/imagebuttonforheader"
android:layout_height="50dp"
android:layout_width="50dp"
android:layout_alignParentRight="true"
android:background="#drawable/forward_button"
/>
</RelativeLayout>
I made a class for it:
public class Header extends RelativeLayout {
Context context;
Activity activity;
public Header(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
li.inflate(R.layout.header, null, false);
}
}
And then added this layout into my main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<my.testy.view.Header
android:id="#+id/headerOnMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</my.testy.view.Header>
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/headerOnMain"
android:text="#string/hello" />
</RelativeLayout>
But it's not showing up on the application.
What am I doing wrong here?
You have to attached your inflated layout to your custom class:
li.inflate(R.layout.header, this, true);