list view show only 10 items - android

I would like to implement a method that allows me to implement a listview with 2 different String-array from from strings.xml.
My code works but my phone can only show the first 10 items. where is my problem? I f I try to run the code on a virtual device it works correctly.
Here is my code with only one textview. (this one shows only the first 10 items)
Button btt_backHome;
ListView lV_titoli;
EditText eT_search;
private ArrayAdapter<CharSequence> adapter , adapter_testi;
ImageView img_titoli;
List<RowData> rowData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titoli);
img_titoli = (ImageView) findViewById(R.id.img_titoli); // Create an icon
Picasso.get().load(R.drawable.sfondo).resize(2048, 1356).centerInside().into(img_titoli);
btt_backHome = (Button) findViewById(R.id.btt_backHome);
lV_titoli = (ListView) findViewById(R.id.lV_titoli);
eT_search = (EditText) findViewById(R.id.eT_search);
//serve per nascondere la tastiera
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
adapter = ArrayAdapter.createFromResource(this, R.array.titoli, android.R.layout.simple_list_item_1);
lV_titoli.setAdapter(adapter);
adapter.getFilter().filter("");
final Intent refresh = new Intent(this, activity_titoli.class);
final Intent to_Home = new Intent (this , Activity_Main.class);
final Intent to_Canzone_from_titoli = new Intent (this , activity_canzone.class);
eT_search.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
(activity_titoli.this).adapter.getFilter().filter(arg0);
}
});
btt_backHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(to_Home);
}
});
lV_titoli.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Resources res = getResources();
List<String> list = Arrays.asList(res.getStringArray(R.array.titoli));
to_Canzone_from_titoli.putExtra("riga", list.indexOf(adapter.getItem(i)));
startActivity(to_Canzone_from_titoli);
}
});
}
And this is my xml code:
<ImageView
android:id="#+id/img_titoli"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="1"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btt_backHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:text="indietro"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
<EditText
android:id="#+id/eT_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:ems="10"
android:hint="Ricerca..."
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="128dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/lV_titoli"
android:layout_width="match_parent"
android:layout_height="500dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</ScrollView>

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="128dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/lV_titoli"
android:layout_width="match_parent"
android:layout_height="500dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</ScrollView>
this is the problem. you have a listview nested inside a scroll view. you dont need to do that as a listview itsself scrolls. try this in place of that whole thing:
<ListView
android:id="#+id/lV_titoli"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="128dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</ListView>

Your problem is so straightforwardly simple: You don't have to put ListView in ScrollView to make it able to scroll, because ListView has its own class implementation of scrolling.
Delete this tag:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="128dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</ScrollView>
And keep it like that:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/lV_titoli"
android:layout_width="match_parent"
android:layout_height="500dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
If you wish to dig deeper, please check this question:
ListView inside ScrollView is not scrolling on Android
Good Luck!

Related

Constraintlayout does not behave well as recyclerview 's item

I have a recyclerview whose items look like this:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_food_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<LinearLayout
android:id="#+id/linear_food_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"
android:orientation="horizontal"
android:padding="4dp">
<com.mikhaellopez.circleview.CircleView
android:id="#+id/food_image"
android:layout_width="80dp"
android:layout_height="80dp"
app:cv_color="#color/colorAccent" />
<LinearLayout
android:id="#+id/food_contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:orientation="vertical">
<TextView
android:id="#+id/food_title_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:fontFamily="#font/sans_regular"
android:text="عنوان غذا"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:id="#+id/ingredients_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:fontFamily="#font/sans_regular"
android:text="مواد اولیه"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle1" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
I'm trying to convert this into a flat hierarchy using Constraintlayout.
Here's how it looks:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_food_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/linear_food_item"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/food_title_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:text="عنوان غذا"
android:textAppearance="#style/TextAppearance.AppCompat.Title"
app:layout_constraintStart_toEndOf="#+id/food_image"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/ingredients_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="مواد اولیه"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle1"
app:layout_constraintStart_toEndOf="#+id/food_image"
app:layout_constraintTop_toBottomOf="#+id/food_title_homepage" />
<com.mikhaellopez.circleview.CircleView
android:id="#+id/food_image"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:cv_color="#color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Here's where my Recyclerview is inserted:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.homepage.HomepageFragment">
<ProgressBar
android:id="#+id/homepage_progressbar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recipes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/homepage_toolbar"
tools:listitem="#layout/fragment_homepage_food_item" />
<include
android:id="#+id/homepage_toolbar"
layout="#layout/fragment_homepage_toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here's how each item should be: https://i.stack.imgur.com/AcfAm.png
But I don't get the same results in the Recyclerview: https://i.stack.imgur.com/jpUG4.png
I didn't have the problem before I converted the view into Constraintlayout!! Everything was working just fine until now that I converted them. They all become left-aligned in the Recyclerview!!
Does anyone know what the problem is?? Thanks in advance
-- Updated --
How it looks like on an android emulator or any other devices: https://i.stack.imgur.com/tvGz4.png
When I scroll down to the bottom (I update the list after each scroll) some items become right-aligned (they somehow get fixed) but the rest stays the same (still left aligned)
-- Java Code --
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
viewModel = new ViewModelProvider(this).get(HomepageViewModel.class);
getData(0);
setRecyclerView(linearLayoutManager);
EndlessRecyclerViewScrollListener scrollListener = new EndlessRecyclerViewScrollListener(linearLayoutManager) {
#Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
getData(page);
}
};
recyclerView.addOnScrollListener(scrollListener);
private void getData(int offset) {
viewModel.init(this, offset);
viewModel.getFoodsLiveData().observe(getViewLifecycleOwner(), foodList -> {
if (foodList != null) {
foods.addAll(foodList);
foodList.clear();
foodAdapter.notifyDataSetChanged();
}
});
}
private void setRecyclerView(LinearLayoutManager linearLayoutManager) {
foodAdapter = new FoodAdapter(foods);
recyclerView.setAdapter(foodAdapter);
recyclerView.setLayoutManager(linearLayoutManager);
}
My adapter:
private void getData(int offset) {
viewModel.init(this, offset);
viewModel.getFoodsLiveData().observe(getViewLifecycleOwner(), foodList -> {
if (foodList != null) {
foods.addAll(foodList);
foodList.clear();
foodAdapter.notifyDataSetChanged();
}
});
}
private void setRecyclerView(LinearLayoutManager linearLayoutManager) {
foodAdapter = new FoodAdapter(foods);
recyclerView.setAdapter(foodAdapter);
recyclerView.setLayoutManager(linearLayoutManager);
}
My adapter:
private List<Food> foods;
FoodAdapter(List<Food> foods) {
this.foods = foods;
}
static class FoodViewHolder extends RecyclerView.ViewHolder {
CircleView foodImage;
TextView foodTitle, ingredients;
CardView foodItem;
FoodViewHolder(View itemView) {
super(itemView);
foodImage = itemView.findViewById(R.id.food_image);
foodTitle = itemView.findViewById(R.id.food_title_homepage);
ingredients = itemView.findViewById(R.id.ingredients_homepage);
foodItem = itemView.findViewById(R.id.card_food_item);
}
}
#NonNull
#Override
public FoodViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_homepage_food_item, parent, false);
return new FoodViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull FoodViewHolder holder, int position) {
final Food currentFood = foods.get(position);
StringBuilder food_ingredient_str = new StringBuilder();
int ingredient_position = 0;
int ingredients_size = currentFood.getFood_ingredients().size();
for (Food.FoodIngredient foodIngredient : currentFood.getFood_ingredients()) {
food_ingredient_str.append(foodIngredient.getIngredient());
if (ingredient_position != ingredients_size / 3)
food_ingredient_str.append(", ");
if (ingredient_position == ingredients_size / 3) {
food_ingredient_str.append(" و ...");
break;
}
ingredient_position += 1;
}
holder.foodTitle.setText(currentFood.getName());
holder.ingredients.setText(food_ingredient_str);
holder.foodItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putSerializable("current_food", currentFood);
Navigation.findNavController(v).navigate(R.id.action_navigation_home_to_navigation_food, bundle);
}
});
}
#Override
public int getItemCount() {
return foods.size();
}
If you want to create a list using recyclerview and each item to be right aligned as in Arabic.. then try these approaches
replace your "recycle_view_item_layout.xml" with this
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_food_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<LinearLayout
android:id="#+id/linear_food_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="4dp">
<LinearLayout
android:id="#+id/food_contents"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/food_title_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:ellipsize="start"
android:fontFamily="#font/sans_regular"
android:singleLine="true"
android:text="عنوان غذا"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:id="#+id/ingredients_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:ellipsize="start"
android:fontFamily="#font/sans_regular"
android:singleLine="true"
android:text="مواد اولیه"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle1" />
</LinearLayout>
<com.mikhaellopez.circleview.CircleView
android:id="#+id/food_image"
android:layout_width="80dp"
android:layout_height="80dp"
app:cv_color="#color/colorAccent" />
</LinearLayout>
</androidx.cardview.widget.CardView>
replace your "recycle view" with this
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recipes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/homepage_toolbar"
tools:listitem="#layout/fragment_homepage_food_item" />
and attach a layout manager like this (important)
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
setRecyclerView(layoutManager);
and rest of your code seems okay

Show tooltip arrow at top of icon which is align end of text in TextView

I have a TextView in which i am setting text dynamically(text length is not fixed ) and end of the text there is a icon,So My question is I want to show tooltip like tooltip arrow should be at top of info icon , as shown in attached image
You can follow this way
XML File
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="#+id/ivDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:src="#drawable/ic_done_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/tvShipInBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Ship in Box($10 Extra per box)"
android:textColor="#E83535"
app:layout_constraintBottom_toBottomOf="#id/ivDone"
app:layout_constraintStart_toEndOf="#id/ivDone" />
<ImageView
android:id="#+id/ivInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:src="#drawable/ic_info_black_24dp"
app:layout_constraintBottom_toBottomOf="#id/ivDone"
app:layout_constraintStart_toEndOf="#id/tvShipInBox" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clToolTip"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:background="#drawable/ic_tooltip_bg"
app:layout_constraintBottom_toTopOf="#id/clMain"
app:layout_constraintHeight_percent="0.075"
app:layout_constraintStart_toStartOf="#id/clMain"
app:layout_constraintWidth_percent="0.65">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginHorizontal="8dp"
android:alpha="0.87"
android:text="A box increases an items volumetric weight and costs more."
android:textColor="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java File
public class MainActivity extends AppCompatActivity {
ImageView ivInfo;
ConstraintLayout clToolTip;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivInfo = findViewById(R.id.ivInfo);
clToolTip = findViewById(R.id.clToolTip);
ivInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ivInfo.setVisibility(View.INVISIBLE);
ivInfo.postDelayed(new Runnable() {
public void run() {
clToolTip.setVisibility(View.VISIBLE);
}
}, 7000);
}
});
}
}

How to not repeat recycle view inside nested layout?

I am trying to create a nested recycle view. So far i have a parent recycle view and under this i have a child recycle view. The problem that i am having is my child recycle view continues to be recycled with my parent recycle view. I do not want this i would like the child recycle view to only be recycled once. Does any one know how to go about fixing this. I understand this might sound a little confusing, but an example of what i want is instagram news feed. The top of the app have circle image views with your followers stories which can be swiped and the bottom have the news feed itself, in my case my app loads the user stories continuously under the news feed as i continue to scroll users stories are being continuously recycled under the news feed post.
Main UI with parent recycleView
<?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"
android:background="#FFFFFF">
<androidx.appcompat.widget.Toolbar
android:id="#+id/newsFeedToolBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="12dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/backButton"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="left"
android:background="#drawable/ic_arrow_back_black_24dp"
android:onClick="backImageView"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="15dp" />
</androidx.appcompat.widget.Toolbar>
<SearchView
android:id="#+id/searchView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="6dp"
android:background="#FFFFFF"
android:iconifiedByDefault="false"
android:queryHint="Search"
app:layout_constraintBottom_toBottomOf="#+id/newsFeedToolBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/newsFeedToolBar"
app:layout_constraintTop_toTopOf="#+id/newsFeedToolBar"
app:searchIcon="#null" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/newsRecycleVIew"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/News"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/News"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="News"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.045"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/searchView" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Row Item for parent recycleview added child recycle view*
<?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:id="#+id/constraintlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/newsThumbNail"
android:layout_width="0dp"
android:layout_height="200dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.448"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/newsTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Dangerous fire is out of control at 24 and park"
android:textSize="17sp"
app:layout_constraintEnd_toEndOf="#+id/newsThumbNail"
app:layout_constraintStart_toStartOf="#+id/newsThumbNail"
app:layout_constraintTop_toBottomOf="#+id/newsThumbNail" />
<TextView
android:id="#+id/newsBody"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Some text is here"
app:layout_constraintEnd_toEndOf="#+id/newsTitle"
app:layout_constraintStart_toStartOf="#+id/newsTitle"
app:layout_constraintTop_toBottomOf="#+id/newsTitle" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/earningsRecycleView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.448"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Upcoming Earnings"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.045"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView" />
</androidx.constraintlayout.widget.ConstraintLayout>
**Parent RecycleView adapter **
public class SearchStockAdapter extends RecyclerView.Adapter<SearchStockAdapter.SearchStocksViewHolder> {
private Context context;
private ArrayList<News_Model> stockInformaiton;
private RequestQueue requestQueue;
private SearchStockSubAdapter searchStockSubAdapter;
private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
public SearchStockAdapter(Context context, ArrayList<News_Model> stockInformaiton) {
this.context = context;
this.stockInformaiton = stockInformaiton;
}
#NonNull
#Override
public SearchStocksViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_stock_row, viewGroup, false);
requestQueue = Volley.newRequestQueue(context);
return new SearchStocksViewHolder(view);
}
//Loads data into views
#Override
public void onBindViewHolder(#NonNull SearchStocksViewHolder viewHolder, int i) {
viewHolder.newsTitle.setText(stockInformaiton.get(i).getTitle());
viewHolder.newsBody.setText(stockInformaiton.get(i).getDescription());
Glide.with(this.context).asBitmap().load(stockInformaiton.get(i).getUrlToImage()).into(viewHolder.newsThumbImage);
//Second recycle view...
LinearLayoutManager layoutManager = new LinearLayoutManager(
viewHolder.upcomingEarningsRecycleView.getContext(),
LinearLayoutManager.HORIZONTAL,
false
);
layoutManager.setInitialPrefetchItemCount(stockInformaiton.size());
//second recycle view..
SearchStockSubAdapter stockSubAdapter = new SearchStockSubAdapter(context, stockInformaiton);
viewHolder.upcomingEarningsRecycleView.setLayoutManager(layoutManager);
viewHolder.upcomingEarningsRecycleView.setAdapter(stockSubAdapter);
viewHolder.upcomingEarningsRecycleView.setHasFixedSize(true);
viewHolder.upcomingEarningsRecycleView.setRecycledViewPool(viewPool);
}
#Override
public int getItemCount() {
return stockInformaiton.size();
}
static class SearchStocksViewHolder extends RecyclerView.ViewHolder {
private TextView newsTitle, newsBody;
private ImageView newsThumbImage;
private RecyclerView upcomingEarningsRecycleView;
public SearchStocksViewHolder(#NonNull View itemView) {
super(itemView);
//newsThumbnail = itemView.findViewById(R.id.newsThumbNail);
newsTitle = itemView.findViewById(R.id.newsTitle);
newsBody = itemView.findViewById(R.id.newsBody);
newsThumbImage = itemView.findViewById(R.id.newsThumbNail);
upcomingEarningsRecycleView = itemView.findViewById(R.id.earningsRecycleView);
}
}
}

My dialog is not being correctly displayed

I have created a custom dialog layout and I am trying to display the dialog as a Activity instead of inflating the view inside the alert dialog. But the dialog is not being displayed as desired. Please have a look.
Custom Dialog Layout
The dialog activity is being shown like below
You can take a look at the code below
MainActivity
public class MainActivity extends AppCompatActivity {
private static final int PROFILE_PHOTO = 1;
private ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = findViewById(R.id.image);
mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showImageOptionsDialog();
}
});
}
private void showImageOptionsDialog() {
Intent imageDialogIntent = new Intent(this, ImageDialogActivity.class);
startActivityForResult(imageDialogIntent, PROFILE_PHOTO);
}
}
ImageDialogActivity
public class ImageDialogActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_dialog);
}
}
activity_image_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:padding="#dimen/sixteen"
android:clipToPadding="false"
tools:context=".ImageDialogActivity">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="#dimen/eight"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label_profile_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="#string/label_profile_photo"
android:textColor="#color/primary_text"
android:textSize="#dimen/dialog_label"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:layout_marginTop="40dp"
android:background="#color/secondary_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/option_gallery" />
<Button
android:id="#+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/cancel_button_bg"
android:text="#string/action_cancel"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/divider" />
<TextView
android:id="#+id/option_gallery"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:drawableTop="#mipmap/ic_pick_img"
android:drawablePadding="#dimen/eight"
android:gravity="center"
android:text="#string/option_gallery"
android:textColor="#color/primary_text"
app:layout_constraintEnd_toStartOf="#+id/option_remove_pic"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/label_profile_pic" />
<TextView
android:id="#+id/option_remove_pic"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:drawableTop="#mipmap/ic_remove_pic"
android:drawablePadding="#dimen/eight"
android:gravity="center"
android:text="#string/option_remove_photo"
android:textColor="#color/primary_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/option_gallery"
app:layout_constraintTop_toTopOf="#+id/option_gallery"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
Manifest
<activity
android:name=".ImageDialogActivity"
android:theme="#style/Theme.AppCompat.Light.Dialog" />
I'm not able to figure out the issue. Please help. Regards!
It is worked for me
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
dialog.getWindow().setLayout((6 * width) / 7, LinearLayout.LayoutParams.WRAP_CONTENT);

Android spinner implementation not showing selected item

I have 3 Spinners and I am selecting values from each of them. But when I declare the setOnItemSelected() method outside of the onClickListener() of the next button, The selected value doesn't show up in the Toast. When I declare the setOnItemSelected() method inside the onClickListener of the button, it works but then I can't hide my edittext when I select "Set Limit" from the last spinner.
Please help.
Below is my .java file.
public class AvailabilityActivity extends AppCompatActivity {
private Button next;
private Spinner advanceNotice, shortestTrip, longestDist;
private Bundle bundle;
private EditText setLimit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_availability);
bundle = getIntent().getExtras();
intializeSpinners();
setLimit = (EditText) findViewById(R.id.setlimit);
ArrayAdapter<CharSequence> adapteradvNotice = ArrayAdapter.createFromResource(this, R.array.advNoticeArray, R.layout.spinner_layout);
adapteradvNotice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
advanceNotice.setAdapter(adapteradvNotice);
ArrayAdapter<CharSequence> adaptershortestTrip = ArrayAdapter.createFromResource(this, R.array.shortestTripArray, R.layout.spinner_layout);
adaptershortestTrip.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
shortestTrip.setAdapter(adaptershortestTrip);
ArrayAdapter<CharSequence> adapterlongestDist = ArrayAdapter.createFromResource(this, R.array.longestDistanceArray, R.layout.spinner_layout);
adapterlongestDist.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
longestDist.setAdapter(adapterlongestDist);
onNextPressed();
}
private void intializeSpinners() {
advanceNotice = (Spinner) findViewById(R.id.acceptAdvanceNotice);
shortestTrip = (Spinner) findViewById(R.id.acceptShortestTrip);
longestDist = (Spinner) findViewById(R.id.acceptLongestTrip);
}
private void onNextPressed() {
next = (Button) findViewById(R.id.nextButton1);
final String[] setLimitText = {""};
final String[] selectedlongestDist = new String[1];
longestDist.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedlongestDist[0] = parent.getItemAtPosition(position).toString();
if (selectedlongestDist[0].equals("Set Limit")){
setLimit.setVisibility(View.VISIBLE);
setLimitText[0] = setLimit.getText().toString();
}
if (selectedlongestDist[0].equals("No Limit")) {
setLimit.setVisibility(View.INVISIBLE);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
if (setLimitText[0] == "")
setLimitText[0] = selectedlongestDist[0];
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String[] selectedNotice = new String[1];
advanceNotice.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedNotice[0] = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
final String[] selectedshortestTrip = new String[1];
shortestTrip.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedshortestTrip[0] = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Toast.makeText(getApplicationContext(), setLimitText[0], Toast.LENGTH_LONG).show();
bundle.putString("advancenotice", selectedNotice[0]);
bundle.putString("shortesttrip", selectedshortestTrip[0]);
bundle.putString("longesttrip", setLimitText[0]);
Intent intent = new Intent(getBaseContext(),ImageActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
Below is my layout file.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.csci567.dailyrentals.AvailabilityActivity">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="How much advance notice do you need to confirm a trip request?"
android:textColor="#000000"
android:textSize="18dp"
android:layout_marginLeft="15dp"
android:id="#+id/advanceNoticeRequestText"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.035"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Advance notice"
android:textSize="14dp"
android:id="#+id/advanceNoticeText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.13"
android:layout_marginStart="15dp" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/acceptAdvanceNotice"
android:hint="Advance Notice"
style="#style/Widget.AppCompat.Spinner.Underlined"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.18"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Block trips that don't give you enough advance notice."
android:textSize="16dp"
android:id="#+id/blockNoticeText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.26"
android:layout_marginStart="15dp" />
<View android:background="#a8a8a6"
android:layout_width = "0dp"
android:layout_height="1dp"
android:id="#+id/separatorLine1"
android:layout_marginTop="25dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.3"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="How long would you like trips to last?"
android:textSize="18dp"
android:textColor="#000000"
android:id="#+id/tripDurationText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.4"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Shortest possible trip"
android:textSize="14dp"
android:id="#+id/shortestTripText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.46"
android:layout_marginStart="15dp" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/acceptShortestTrip"
android:hint="Enter Shortest Trip"
style="#style/Widget.AppCompat.Spinner.Underlined"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.52"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Longest Possible trip"
android:textSize="14dp"
android:id="#+id/longestTripText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.6"
android:layout_marginStart="15dp" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/acceptLongestTrip"
android:hint="Enter Longest Trip"
style="#style/Widget.AppCompat.Spinner.Underlined"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.66"
android:layout_marginStart="15dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/setlimit"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.76"
android:hint="Enter the value of longest possible trip"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:text="Next"
android:textSize="18dp"
android:textColor="#ffffff"
android:background="#8b36bc"
android:id="#+id/nextButton1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="1.0"/>
</android.support.constraint.ConstraintLayout>
The way you're coding is the problem.
First thing you must change is the place you set listeners. You should avoid setting listeners inside other listeners.
Secondly, the code will execute all the code out of those listeners and then execute the code inside those listeners.
Thirdly, if you wanna show a Toast with a message generated inside a Spinner, you should put it inside the spinner's listener.
Try to reorganize you code thinking of those tips and see if you'll have the problem again.

Categories

Resources