Also I was trying to set background of my a card View used in recycler view in my app for that i did the following
card xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
android:layout_height="120dp"
android:layout_width="200dp"
android:layout_margin="5dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="5dp">
<LinearLayout
android:id="#+id/home_card_subject"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:textColor="#ffffff"
android:id="#+id/home_card_subject_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="#string/subject_text"
android:textSize="30sp"
android:gravity="bottom|center_horizontal"
android:padding="5dp"
android:layout_weight="70"/>
<TextView
android:textColor="#ffffff"
android:id="#+id/home_card_credits"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4 credits"
android:gravity="center_horizontal"
android:layout_weight="30"
/>
</LinearLayout
>
</androidx.cardview.widget.CardView>
dashboard fragment:
package com.example.app100.ui.home;
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
LinearLayout cardView;
private ArrayList<Subject> subject = new ArrayList<>();
private DatabaseHelper databaseHelper;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
databaseHelper = new DatabaseHelper(getContext());
populateArrayList();
//homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
cardView = root.findViewById(R.id.home_card_subject);
/*final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});*/
Random rand = new Random();
int rand_int1 = rand.nextInt(3);
switch (rand_int1){
case 0:
cardView.setBackgroundResource(R.drawable.gradient_color1);
break;
case 1:
cardView.setBackgroundResource(R.drawable.gradient_color2);
break;
case 2:
cardView.setBackgroundResource(R.drawable.gradient_color3);
break;
}
RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.home_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext() , LinearLayoutManager.HORIZONTAL , false));
recyclerView.setAdapter(new RecyclerAdapter(subject));
// recycle.setLayoutManager(new GridLayoutManager( getActivity(),2));
// recycle.setAdapter(new RecyclerAdapter(subList));
return root;
}
private void populateArrayList(){
Log.d(TAG , "populating array list");
Cursor data = databaseHelper.getdata();
while(data.moveToNext()){
Subject sub = new Subject(data.getString(0) , data.getInt(1), data.getInt(2),
data.getInt(3), data.getInt(4),data.getInt(5) , data.getInt(6));
subject.add(sub);
}
}
}
but doing this gave the following errors:
2020-04-09 12:18:18.142 15575-15575/com.example.app100 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app100, PID: 15575
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setBackgroundResource(int)' on a null object reference
at com.example.app100.ui.home.HomeFragment.onCreateView(HomeFragment.java:65)
I wanted a scroll effect where the bottom part scrolls over the the top part like in the image bellowlike the image below the whiete part would scroll over the purple part and not scroll with it.
You are did not defined any id to cardview, and give linear layout reference to the cardview so cardview become null and occur Null pointer Exception. Please set id to cardView and give that id reference to the cardView.
Related
I am using a recycler view to show some data. When the app is launched it looks correct as follows with wrap content for height. After I scroll past the last item, I am able to keep scrolling and the data is no longer wrapped, looking like match parent instead for height. Scrolling back up, everything has changed to match parent for the height.
Using past references here, I have tried with ConstraintLayouts and switched height wrapping between the parent layout and the recyclerview itself. Both doesn't help. I am guessing this has to do more with the xml. Please advice.
This is what I expect to always get. This is what I current get when app launches, but changes after I scroll to last item.
When I scroll to last item this happens.
Now if I scroll back up, the height is no longer wrapped. Everything seems to have changed to match parent.
This is xml for the custom view I am using to inflate.
<?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:id="#+id/feed_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/feed_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingRight="10dp"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp"
tools:text="Test Title" />
<TextView
android:id="#+id/feed_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="12sp"
app:layout_constraintTop_toBottomOf="#+id/feed_title"
tools:layout_editor_absoluteX="0dp"
android:layout_below="#+id/feed_title"
tools:text="This is some random description for testing purposes. Other wise just typing on to create more stuff..." />
</RelativeLayout>
This is layout for the Recycler View which is placed on a Fragment activity.
<?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="wrap_content"
tools:context=".fragment.CurrentFeedFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/current_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
This is over at my FragmentActivity where I am loading the data for the RecyclerView.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_current_feed, container, false);
initiateTestData();
loadDataToRecyclerView(view);
return view;
}
private void initiateTestData(){
testTitles = new ArrayList<>();
testDescriptions = new ArrayList<>();
for (int i = 5; i < 25; i++) {
testTitles.add("title " + i);
testDescriptions.add("This is some random description for testing purposes. Other wise just typing on to create more stuff... " + i);
Log.d(TAG, "initiateTestData: " + "title " + i);
}
}
private void loadDataToRecyclerView(View v){
Log.d(TAG, "loadDataToRecyclerView: " + testTitles.size());
RecyclerView recyclerView = v.findViewById(R.id.current_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
FeedAdapter adapter = new FeedAdapter(testTitles, testDescriptions, getActivity());
recyclerView.setAdapter(adapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
Don't think this is relevant. But for reference, this is my adapter class.
public class FeedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private static final String TAG = "FeedAdapter";
private List<String> titles;
private List<String> descriptions;
private Context context;
public FeedAdapter(List<String> titles, List<String> descriptions, Context context) {
this.titles = titles;
this.descriptions = descriptions;
this.context = context;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.custom_current_feed, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, int i) {
final ViewHolder holder = (ViewHolder) viewHolder;
holder.feedTitle.setText(titles.get(i));
holder.feedDescription.setText(descriptions.get(i));
holder.layout.setOnClickListener(v -> {
Log.d(TAG, "Clicked: " + titles.get(i));
Toast.makeText(context, titles.get(i), Toast.LENGTH_SHORT).show();
});
}
#Override
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView feedTitle;
TextView feedDescription;
RelativeLayout layout;
public ViewHolder(#NonNull View itemView) {
super(itemView);
feedTitle = itemView.findViewById(R.id.feed_title);
feedDescription = itemView.findViewById(R.id.feed_description);
layout = itemView.findViewById(R.id.feed_layout);
}
}
}
Your recycler view height is wrap_content
And recycler view item height match_parent
I would think you'd want them the other way around.
The RV's height = match_parent, i.e. the recycler view occupies all available height.
The RV item's height = wrap_content, i.e. each item only as tall as it needs to be, so that multiple items can fit.
I'm trying to add and show a multiple image views existing in a fragment to another in the same activity by OnItemClickListener on GridView.
I was able to add the first image view but when I add the second one the app crashes.
By the way, I'm adding the image views to a linear layout inside Horizontal Scroll View.
Here is the layout of the first fragment:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_fragment_id"
>
<GridView
android:id="#+id/items_id_gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:columnWidth="100dp"
android:minHeight="40dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:numColumns="auto_fit"
android:horizontalSpacing="10.0dip"
android:verticalSpacing="10.0dip"
android:cacheColorHint="#00000000"
android:gravity="center_horizontal"
android:requiresFadingEdge="vertical"
/>
</RelativeLayout>
The layout of the second fragment:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageButton
android:id="#+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/play_36dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:contentDescription="#string/play_button" />
<HorizontalScrollView
android:id="#+id/sentenceBarScrollView"
android:layout_width="fill_parent"
android:layout_height="104dp"
android:layout_toLeftOf="#id/play_button"
android:layout_toStartOf="#id/play_button"
android:padding="1dp"
>
<LinearLayout
android:id="#+id/sentence_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="2dp"
android:orientation="horizontal"
>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
The layout of the main activity:
<android.support.constraint.ConstraintLayout
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=".Home"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="#+id/sentence_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<fragment
android:id="#+id/sentenceBarFragment"
android:name="ye.com.ebra.contentfragments.SentenceBarFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<RelativeLayout
android:id="#+id/content_Layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/sentence_layout"
android:padding="5dp"
>
<!-- put contents fragments here :) -->
</RelativeLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
the class of the first fragment.
sendItemToSentenceBar connector;
//for retrieving the values of the image stored in database
private Item item;
static ArrayList<Integer> back;
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
back=new ArrayList<>();
DBConnection dbConnection= new DBConnection(getActivity());
GridView gridView=getActivity().findViewById(R.id.items_id_gridView);
final ArrayList<Item> items2=new ArrayList<>(dbConnection.getAll(Cat_id));
ContentAdapter dataAdapter=new ContentAdapter(getActivity(),items2);
gridView.setAdapter(dataAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
item=items2.get(position);
if(item.getType().equals("Category"))
{
back.add(Cat_id);
changeFragment(item.getID());
AddNewDialogFragment.Cat_id=item.getID();
}else {
speak(item.getName());
connector.sendData(item);
}
}
});
}
and I'm using an interface to pass the data from OnItemClickListener to the other fragment by "connector.sendData(item);"
Here is the interface:
public interface sendItemToSentenceBar {
// send item to the sentence bar fragment :)
void sendData(Item item);
}
Then in the class of the other fragment
There is the method AddItem(Item item) that should add the image view to the second fragment, the first image view can be added be on the second one the app just crashes :
private ArrayList<Item> items;
private LinearLayout sentenceLayout;
private View itemView;
private ArrayList<View> itemsViews;
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View sentenceBarFragment=inflater.inflate(R.layout.activity_sentence_bar_fragment,container,false);
sentenceLayout = sentenceBarFragment.findViewById(R.id.sentence_bar);
itemView = inflater.inflate(R.layout.activity_item,sentenceLayout,false);
return sentenceBarFragment;
}
public void AddItem(Item item){
TextView name_id = itemView.findViewById(R.id.name_id);
name_id.setText(item.getName());
ImageView image_id = itemView.findViewById(R.id.image_id);
image_id.setImageBitmap(convertByteToBitmap(item.getImage()));
itemsViews.add(itemView);
sentenceLayout.addView(itemsViews.get(0));
items.add(item);
}
Finally the method AddItem(Item item) is used in the class of the main activity that contain both of the fragments:
#Override
public void sendData(Item item) {
FragmentManager fragmentManager=getFragmentManager();
SentenceBarFragment s= (SentenceBarFragment) fragmentManager.findFragmentById(R.id.sentenceBarFragment);
s.AddItem(item);
}
Here the Logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
at android.view.ViewGroup.addView(ViewGroup.java:3249)
at android.view.ViewGroup.addView(ViewGroup.java:3194)
at android.view.ViewGroup.addView(ViewGroup.java:3170)
at ye.com.ebra.contentfragments.SentenceBarFragment.AddItem(SentenceBarFragment.java:95)
at ye.com.ebra.contentfragments.Home.sendData(Home.java:129)
at ye.com.ebra.contentfragments.ContentFragment$2.onItemClick(ContentFragment.java:83)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
at android.widget.AbsListView$1.run(AbsListView.java:3529)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
of the Logcat, I could identify those errors:
at ye.com.ebra.contentfragments.SentenceBarFragment.AddItem(SentenceBarFragment.java:95)
which is at the class of the second fragment, in the method AddItem(Item item)
sentenceLayout.addView(itemsViews.get(0));
.
at ye.com.ebra.contentfragments.Home.sendData(Home.java:129)
which is at the class of the main activity, in the used method
sendData(Item item)
s.AddItem(item);
.
at ye.com.ebra.contentfragments.ContentFragment$2.onItemClick(ContentFragment.java:83)
which is in the class of the first fragment, in OnItemClickListener:
connector.sendData(item);
Sorry for the long post, but I'm really stuck here
You appear to be repeatedly modifying and adding the same item_view object inflated in onCreateView(...) method. It strikes me that you need to inflate a new object each time you call "AddItem".
There is also a possible issue with appending views to the itemViews list but repeatedly adding the first view in the list (ie. not the one you just added) .
So SentenceBarFragment might look instead like this:
private ArrayList<Item> items;
private LinearLayout sentenceLayout;
private ArrayList<View> itemsViews;
#Nullable
#Override public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View sentenceBarFragment=inflater.inflate(R.layout.activity_sentence_bar_fragment,container,false);
sentenceLayout = sentenceBarFragment.findViewById(R.id.sentence_bar);
return sentenceBarFragment;
}
public void AddItem(Item item){
// inflate a new view to be added
View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.activity_item,sentenceLayout,false);
TextView name_id = itemView.findViewById(R.id.name_id);
name_id.setText(item.getName());
ImageView image_id = itemView.findViewById(R.id.image_id);
image_id.setImageBitmap(convertByteToBitmap(item.getImage()));
// add the view you just inflated
itemsViews.add(itemView);
sentenceLayout.addView(itemView); // or addView(itemViews.get(itemViews.size()-1))
items.add(item);
}
I am trying to set an onClickListener to my CheckBox but my checkBox instance is returning null on the onClickListener giving me a nullPointerException. I have looked at other questions similar to mine and haven't found anything that worked. I'm not too familiar with RecyclerView so I would not be surprised if it has something to do with how I'm initializing the view.
Here is the code in question-
Months0Through6.java
public class Months0Through6 extends android.support.v4.app.Fragment {
public Months0Through6() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
CheckBox checkBox;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
//This is returning null
checkBox = (CheckBox)rootView.findViewById(R.id.checkBox_ID);
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
rv.setHasFixedSize(true);
MilestonesAdapter adapter = new MilestonesAdapter(new String[]{"Month 0 stuff", "Example Two", "Example Three", "Example Four", "Example Five" , "Example Six" , "Example Seven"});
rv.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(),
"Your Message", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
card_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="68dp">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="62dp"
card_view:cardCornerRadius="4dp"
card_view:elevation="14dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="#+id/checkBox_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/tv_text"
android:layout_toRightOf ="#+id/checkBox_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
</TextView>
<TextView
android:id="#+id/tv_blah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Another Recyclerview Fragment"
android:layout_below="#+id/tv_text"
android:layout_toRightOf="#+id/checkBox_ID"
android:layout_toEndOf="#+id/checkBox_ID">
</TextView>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Your checkbox is inside card_item layout. But you are inflating fragment_blank and trying to map checkbox to that layout.
You could map checkbox inside view holder and set onclick listener inside onBindViewHolder of adapter class
You should findViewById in your MilestonesAdapter, MilestonesAdapter is responsible for creating items for the RecyclerView.
See the guide from Android Developers site.
Check the stuff about onBindViewHolder.
I have created a fragment and want to start a dialogue on a button click on which i would display n numbers of texts distributed vertically using Linear layout vertical orientation. To begin with i have created a textview which i am populating dynamically. But i get a null pointer exception saying the any of the layout i referenced here points to NULL.
View view,tempView;
TextView myName, myPhNo, myEmail, myDob;
ImageButton selectRingTone;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_profile, container, false);
tempView = inflater.inflate(R.layout.fragment_selectsong, container, false);
myName = (TextView)view.findViewById(R.id.username);
myPhNo = (TextView)view.findViewById(R.id.userPhNo);
myEmail = (TextView)view.findViewById(R.id.useremailId);
myDob = (TextView)view.findViewById(R.id.userdob);
selectRingTone = (ImageButton)view.findViewById(R.id.selectRingTone);
setUserProfileData();
//setUserTextStatus();
//setUserAudioStatus();
selectRingTone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chooseAndSetRingTone();
}
});
return view;
}
private void chooseAndSetRingTone(){
final Dialog fbDialogue = new Dialog(view.getContext(), android.R.style.Theme_Black);
fbDialogue.getWindow().setTitle("Select your audio status song");
fbDialogue.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(100, 0, 0, 0)));
fbDialogue.setContentView(R.layout.fragment_selectsong);
getSongsAndFillDialogue();
fbDialogue.setCancelable(true);
fbDialogue.show();
}
private void getSongsAndFillDialogue(){
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout container = (LinearLayout) tempView.findViewById(R.id.eachRingToneSong);
TextView tv = new TextView(tempView.getContext());
tv.setText("Hi hello");
Button b = new Button(tempView.getContext());
b.setText("abcde");
container.addView(tv);
container.addView(b);
}
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLyt"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollCotainerForRingToneSongs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/eachRingToneSong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--<TextView
android:id="#+id/song1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abcde"
/>-->
</LinearLayout>
</ScrollView>
</RelativeLayout>
you are creating View for your Dialog in onCreateView method:
tempView = inflater.inflate(R.layout.fragment_selectsong, container, false);
and fulfilling it using getSongsAndFillDialogue method, but you are setting for your Dialog another new instance passing only resource id:
fbDialogue.setContentView(R.layout.fragment_selectsong);
when you are passing just id then Dialog is inflating this layout by itself (just like you). So there are two layouts created, one in Activity - fulfilled, and one created "automatically" by setContentView method, but not touched at all by your methods (so it stays empty)
instead of passing resource id pass already prepared tempView, like below:
fbDialogue.setContentView(tempView);
I am developing app for Android TV. In app there is a fragment layout that has four ImageView in LinearLayout horizontal direction and a RecyclerView that is below on LinearLayout. When I launch app and navigate through DPAD control then RecyclerView navigate up and down. Requirement is that when fragment launch by default first ImageView of LinearLayout would be selected. After when we press right button of DPAD then next ImageView would be select. When we press down key then focus will move on RecyclerView. When focus will on RecyclerView first child and we press Up key then focus move on first Imageview of LinearLayout.
I have also use imageview.requestFocus() but it is not working. Please help me.
Below is my layout file
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/home_fragment_menu_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="10dp">
<ImageView
android:id="#+id/img_playall"
style="#style/home_fragment_menu"
android:src="#drawable/play_all"
android:focusable="true"
android:focusableInTouchMode="true"/>
<ImageView
android:id="#+id/img_trending"
style="#style/home_fragment_menu"
android:src="#drawable/trending"/>
<ImageView
android:id="#+id/img_search"
style="#style/home_fragment_menu"
android:src="#drawable/search"/>
<ImageView
android:id="#+id/img_setting"
style="#style/home_fragment_menu"
android:src="#drawable/settings"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/home_fragment_menu_container"
android:layout_marginLeft="45dp"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/vertical_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</RelativeLayout>
Below is fragment.java file
public class HomeFragment extends Fragment {
private static final String TAG = "HomeFragment";
ImageView playAllImage, trendingImage, searchImage, settingImage;
Context context;
RecyclerView verticalList;
ArrayList<ArrayList<VideoContentModel>> categoriesVideoList;
ArrayList<CategoryModel> categoryDataList;
public static HomeFragment newInstance(Context context, ArrayList<ArrayList<VideoContentModel>> categoriesVideoList,
ArrayList<CategoryModel> categoryDataList){
HomeFragment fragment = new HomeFragment();
fragment.context = context;
fragment.categoriesVideoList = categoriesVideoList;
fragment.categoryDataList = categoryDataList;
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment_layout, container, false);
playAllImage = (ImageView)view.findViewById(R.id.img_playall);
trendingImage = (ImageView)view.findViewById(R.id.img_trending);
searchImage = (ImageView)view.findViewById(R.id.img_search);
settingImage = (ImageView)view.findViewById(R.id.img_setting);
playAllImage.requestFocus();
verticalList = (RecyclerView)view.findViewById(R.id.vertical_recyclerview);
verticalList.setHasFixedSize(true);
verticalList.setLayoutManager(new LinearLayoutManager(context));
CatListVerticalAdapter adapter = new CatListVerticalAdapter(context,categoriesVideoList,categoryDataList);
verticalList.setAdapter(adapter);
verticalList.clearFocus();
//
if(playAllImage.hasFocus())
Log.i(TAG,"play all has focus");
else
Log.i(TAG,"===playall has not focus");
return view;
}
How we navigate through DPAD above requirement? Please help me..