I have a fragment class which uses Recycleview to show list of Text and Image.
How can I access Recycleview row items and set an custom text to any Textview or Hide the Desired Textview from fragment class.
This is my layout
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:paddingBottom="4dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="4dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
<include
android:id="#+id/include_tag"
layout="#layout/row_item"/>
</FrameLayout>
and My row item layout is
<?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:layout_width="match_parent">
<TextView
android:id="#+id/flower_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:maxLines="1"
android:layout_centerInParent="true"
android:gravity="center_vertical|center_horizontal"
android:textAlignment="center"
android:textColor="#E91E63"
android:fontFamily="sans-serif-condensed"
android:text="Sagar Rawal"/>
<TextView
android:layout_marginTop="8dp"
android:id="#+id/flower_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="bold"
android:maxLines="3"
android:textColor="#673AB7"
android:text="This Text need to Be Change "/>
</RelativeLayout>
and my Fragment class i s
public View onCreateView(final LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.activity_main, container, false);
View test1View = view.findViewById(R.id.include_tag);
mhideText = (TextView) test1View.findViewById(R.id.flower_details);
mhideText.setVisibility(View.INVISIBLE);
return view;
But it doesn't work. and Textview is still Visible.
Please help. Thanks in advance.
You must have a custom ViewHolder which should extends RecyclerView.ViewHolder. In your ViewHolder you can link row item and you can access all views from your row layout.
Your custom ViewHolder looks like:
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView textView;
public MyViewHolder(TextView v) {
super(v);
textView = v;
//Note: To set custom text
textView.setText("My custom text");
//Note: To hide TextView remove comment from below line
//textView.setVisibility(View.GONE);
}
}
Above code shows sample ViewHolder as MyViewHolder where you can access your views.
If you are passing parent Layout to MyViewHolder instead of passing TextView then you can find child views using v.findViewById(R.id.xxx)
Your adapter should contain similar code as below:
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
...
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
In above code snippet, creating instance of MyViewHolder by passing the view TextView. You can pass parent layout in your case its RelativeLayout from your row layout.
Use this ViewHolder to take any actions on views like set custom text to TextView or you can hide TextView as per your requirements.
To refer full example you can follow the android documentation or click here.
To achieve communication between ViewHolder and fragment you can use Interface.
Thank you!
Try doing this :
rowview = inflater.inflate(R.layout.row_item, container, false);
mhideText = (TextView)rowview.findViewById(R.id.flower_details);
mhideText.setVisibility(View.INVISIBLE);
Related
I want to create Fragment with ListView, so, I have a ListView with listitem in my Fragment activity, but my listitem is not displayed when i open my fragment, i need help with it, how display items in fragment? When i make this ListView in Activity, all is working well, how fix this? My code here:
FollowFragment.java
public class FollowFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
getActivity().setTitle("Подписки");
return inflater.inflate(R.layout.fragment_follow,container,false);
}
}
follow_fragment.xml
<?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:padding="10dp">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/item_follow"/>
</FrameLayout>
item_follow.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/favourite_match_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/team_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="#font/roboto_medium"
android:text="FC Chelsea"
android:textColor="#color/black"
tools:ignore="MissingConstraints" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/team_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_medium"
android:text="Manchester City"
app:layout_constraintTop_toBottomOf="#+id/team_1"
android:textColor="#color/black"
tools:ignore="MissingConstraints" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/match_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="#font/roboto"
android:text="25 сентября 2021 года"
android:textColor="#color/colorGrey"
android:textSize="12sp"
app:layout_constraintTop_toBottomOf="#id/team_2"
tools:ignore="MissingConstraints" />
<ImageButton
android:layout_width="48dp"
android:layout_height="50dp"
android:layout_marginEnd="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="#drawable/ic_love"
android:background="#00000000"
app:layout_constraintLeft_toRightOf="#id/team_2"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
well that's because you didn't assign an adapter with the correct data to it yet.
your adapter should extend ArrayAdapter and override getView in which you should set your view with the correct content based on your data.
what an adapter might look like :
public class MyAdapter extends ArrayAdapter<YourDataCustomClassHere> {
ArrayList<YourDataClassHere> data;
Context context ;
public MyAdapter(#NonNull Context context, int resource, #NonNull ArrayList<YourDataCustomClassHere> objects) {
super(context, resource, objects);
this.data = objects;
this.context = context;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
convertView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_follow, parent, false);
}
//example on setting one of your fields
((AppCompatTextView) convertView.findViewById(android.R.id.team_1))
.setText(data.get(position).getText());
//set Other Fields here
return convertView;
}
}
where your data class should be a custom class that holds the correct information for each item in the list and getText in data.get(position).getText() is an arbitary method I assumed that will be in your custom class to get some sort of text but you're free to replace it with what you want.
then in your onCreateView you should set the adapter to your ListView like that :
public class FollowFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
getActivity().setTitle("Подписки");
View rootView= inflater.inflate(R.layout.fragment_follow,container,false);
ListView list = rootView.findViewById(R.id.listview);
MyAdapter myadapter = new MyAdapter(getContext(),R.layout.item_follow,YourDataArrayListHere);
list.setAdapter(myadapter);
return rootView;
}
}
I trust that you'll find your way and replace YourDataArrayListHere with the correct ArrayList of your data custom class.
anyway I hope you reconsider your choice of ListView as it consume much Ram (Memory) during runtime and use a RecyclerView instead
I have Fragment that contain Linear layout with orientation "horizontal" (ItemViewContainer).
I'm created another layout that container imageView and textView (ItemView).
I want to add the ItemView to the ItemViewContainer programmatically a few times.
How can i do that instead of adding the ItemView with from ItemViewContainer.
ItemView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/LLTopServiceRootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="#+id/CVServiceIMGHolder"
android:layout_width="50dp"
android:layout_height="50dp"
android:translationZ="5dp"
card_view:cardBackgroundColor="#color/fragment_background_color"
card_view:cardCornerRadius="100dp"
card_view:elevation="5dp">
<ImageView
android:id="#+id/IVServiceIMG"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:tint="#color/text_color"
card_view:srcCompat="#drawable/lights_icon" />
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/TVServiceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/text_color"
android:layout_gravity="center_horizontal"
android:text="Service\nName"
android:textAlignment="center" />
</LinearLayout>
ItemViewContainer
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:orientation="vertical"
android:translationZ="5dp"
app:cardBackgroundColor="#color/fragment_background_color"
app:cardCornerRadius="5dp"
card_view:elevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:id="#+id/LLTopServiceContainer"
</LinearLayout>
</androidx.cardview.widget.CardView>
Fragment that contain layout with the ItemViewContainer:
public class HomeFragment extends Fragment {
private View view;
private TextView mTVPageTitle, mTVHomeNavigationTXT;
private LottieAnimationView mLAVHome;
private ButtonsManager bm;
private LinearLayout mLLTopServiceContainer;
private final String TAG = "HomeLifeCycle";
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_home, container, false);
bm = new ButtonsManager(getContext());
initView(view);
Log.d(TAG, "onCreateView: ");
return view;
}
private void initView(View view) {
if (getActivity() != null) {
mLAVHome = getActivity().findViewById(R.id.LAVHome);
mTVPageTitle = getActivity().findViewById(R.id.TVPageTitle);
mTVHomeNavigationTXT = getActivity().findViewById(R.id.TVHome);
mLLTopServiceContainer = view.findViewById(R.id.LLTopServiceContainer);
addSelectedIconStyle();
}
}
}
I want to add the itemView 4th times
You could do it dynamically like this way : Simple Android RecyclerView example
Or adding manually:
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout= (LinearLayout) findViewById(R.id.LLTopServiceContainer);
View view = (View) inflater.inflate(R.layout.item:view_name_file, linearLayout, true);
I want to change a text color in text spinner in dropView. I tried to override the method getDropDownView and change a text color but it doesn't work.
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_spinner_item,
extendedCursor, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER) {
#Override
public View getDropDownView(int position, View convertView,android.view.ViewGroup parent){
View v = convertView;
if (v == null) {
Context mContext = AddEditLoadActivity.this;
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Androids orginal spinner view item
v = vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
}
// The text view of the spinner list view
TextView tv = (TextView) v.findViewById(android.R.id.text1);
boolean disabled = !isEnabled(position);
if(disabled){tv.setTextColor(Color.WHITE);}
else{tv.setTextColor(Color.WHITE);}
return v;
}
#Override
public long getItemId(int position) {
extendedCursor.moveToPosition(position);
return extendedCursor.getLong(extendedCursor.getColumnIndex(DatabaseContract.DictionaryTable.ITEM_ID));
}
};
android.R.layout.simple_spinner_item this line in your spinner adapter use default behavior, try using custom layout to change text color.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="#color/your_color" />
make sure its id will be #android:id/text1 and don't be changed.
use own custom layout
Create a layout named spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#316FA2"
android:textSize="12sp"
android:gravity="left"
android:singleLine="true"
android:padding="6dip"
android:textColor="#color/white" />///you can add your color
/>
replace android.R.layout with R.layout.spinner_row.xml
Alright so I have a fragment and I'd like to include, within its' xml file another layout which is programmatically inflated
Fragment:
public class zGoal_Fragment extends Fragment{
private LinearLayout todayView;
private View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.main_goal_view, container, false);
todayView = (LinearLayout)view.findViewById(R.id.todayView);
return view;
}
}
xml's file for fragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/todayView"
>
</LinearLayout>
and xml layout I want included within the above xml programmatically:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/goalEditLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/test_color_two"
>
</LinearLayout>
I've tried a couple different methods all of which led to " on a null object reference" errors...
plz help :))
I suppose the solution which you are looking for are Android ViewStubs. These are dynamically inflated layouts.
For more information you could refer these :
How to use View Stub in android
https://developer.android.com/reference/android/view/ViewStub.html
However, if you don't wish to inflate one layout inside another during runtime, you could try this the include tag:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/todayView">
<include layout="#layout/your_layout_file_name"/>
</LinearLayout>
Have you tried something like this:
LinearLayout child = getLayoutInflater().inflate(R.layout.goalEditLayout, null);
todayView.addView(child);
Edit:
Inside onCreateView:
LinearLayout inflatedLayout = (LinearLayout) inflater.inflate(R.layout.goalEditLayout, todayView, true);
My app is for reading news from one site. The news parse from RSS feed and display as list of elements that include title and date. The main layout is ListView, and the layout post_entry for messages (news) looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="#+id/post_title">
</TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:paddingLeft="5dp"
android:paddingBottom="5dp"
android:id="#+id/post_pubDate">
</TextView>
</LinearLayout>
One TextView for the title, and another is for date.
Adapter for this view looks like this:
public class PostAdapter extends ArrayAdapter<PostItem> {
public ArrayList<PostItem> messages;
public LayoutInflater inflater;
public PostAdapter(Activity context, int resource,
ArrayList<PostItem> objects) {
super(context, resource, objects);
messages = objects;
inflater = LayoutInflater.from(context);
}
static class ViewHolder {
public TextView titleView;
public TextView pubDateView;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.post_entry, null, true);
holder = new ViewHolder();
holder.titleView = (TextView) convertView
.findViewById(R.id.post_title);
holder.pubDateView = (TextView) convertView
.findViewById(R.id.post_pubDate);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.titleView.setText(messages.get(position).title);
holder.pubDateView.setText(messages.get(position).date);
return convertView;
}
}
I want to add the button for refresh in the main activity.
After in the main activity looks like that:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.8" />
<Button
android:id="#+id/refresh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="Refresh"
android:onClick="Update"/>
</LinearLayout>
In graphical mode in Eclipse I see the list of items and the button below it.
Everything seems ok, but when I run my app there is no button on the screen. I see just list of the news.
Do you know why can this be? And how to add the button below the list?