I have not been able to find an answer for this anywhere. I have a menu that holds 24 items. I want the user to be able to choose which 24 items they want to display on a menu using Settings. I can't arrive at a strategy for this. Does anyone have any ideas? I have no code because I don't know where to start, aside from the basics of using Setiings.
Asked
Active
Viewed 79 times
-3
-
This isn't confusing but here, You can only get help once you've shown your own efforts. And as this question needs mainly advice which varies individually that's the reason for those downvotes. – Lalit Fauzdar Jan 14 '18 at 17:34
-
Thank you for the explanation. Honestly, I wish I had something to show for the vast amount of time I've put into it. It would be less frustrating. I learned how to use Settings and populate it with an array, but none of those things gave me a start to my problem. – F. Moss Jan 14 '18 at 18:07
-
Just filter the arraylist as per the user's needs... – Lalit Fauzdar Jan 15 '18 at 07:59
1 Answers
-1
This is what I finally arrived at after I learned what ListView could do:
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"
tools:context="com.triunedev.custommenucode.MainActivity">
<ListView
android:id="@+id/outOfMenuList"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:choiceMode="multipleChoice"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<LinearLayout
android:id="@+id/moveArrows"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/insideMenuList"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/outOfMenuList">
<ImageView
android:id="@+id/addArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_weight="2"
android:contentDescription="@string/in_arrow"
android:onClick="moveItem"
android:visibility="visible"
app:srcCompat="@drawable/arrow_down" />
<ImageView
android:id="@+id/removeArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="100dp"
android:layout_weight="2"
android:contentDescription="@string/out_arrow"
android:onClick="moveItem"
android:visibility="visible"
app:srcCompat="@drawable/arrow_up" />
</LinearLayout>
<ListView
android:id="@+id/insideMenuList"
android:layout_width="0dp"
android:layout_height="200dp"
android:choiceMode="multipleChoice"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/moveArrows"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
JAVA
package com.triunedev.custommenucode;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import static java.util.Arrays.asList;
public class MainActivity extends AppCompatActivity {
ListView insideMenuList;
ListView outOfMenuList;
ArrayList<String> insideMenuArray;
ArrayList<String> outOfMenuArray;
ArrayAdapter<String> outOfMenuAdapter;
ArrayAdapter<String> insideMenuAdapter;
int count;
String listInFocus;
String[] toMoveToMenuList;
String[] toMoveOutOfMenuList = new String[24];
public void buildListArrays() {
insideMenuList = (ListView) findViewById(R.id.insideMenuList);
outOfMenuList = (ListView) findViewById(R.id.outOfMenuList);
insideMenuArray = new ArrayList<String>(asList(
"Chick-fil-A",
"In-N-Out Burger",
"Wendys",
"Dairy Queen",
"Kentucky Fried Chicken",
"Carl's Jr.",
"McDonald's",
"Sonic",
"Jack-in-the-Box",
"Taco Bell",
"Burger King",
"Arby's",
"El Pollo Loco",
"Popeye's",
"Starbucks",
"Panda Express",
"Steak 'n Shake",
"Jersey Mike's",
"A&W",
"Whitecastle",
"Hardee's",
"Del Taco",
"Subway",
"Long John Silver's"));
outOfMenuArray = new ArrayList<String>(asList(
"Auntie Anne's",
"Cinnabon",
"Dunkin' Donuts",
"Pizza Hut",
"Panera Bread",
"Domino's",
"Chipotle",
"Little Caesars",
"Papa John's",
"Jimmy John's",
"Zaxby's",
"Culver's",
"Bojangles'",
"Church's Chicken",
"Papa Murphy's",
"Baskin-Robbins",
"Jamba Juice",
"Coldstone Creamery",
"Five Guys",
"Jollibee",
"Krispy Kreme",
"Hook Burger",
"Quizno's",
"Coffee Bean & Tea Leaf",
"TCBY",
"Tim Hortons",
"Wingstop",
"WingStreet"));
populateListViews();
insideMenuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
if(listInFocus.equals("outOfMenuList")) {
outOfMenuList.setAdapter(outOfMenuAdapter);
count = 0;
}
parent.getChildAt(position).setBackgroundColor(Color.argb(100, 255, 242, 99));
toMoveOutOfMenuList[position] = insideMenuArray.get(position);
count++;
listInFocus = parent.getResources().getResourceEntryName(parent.getId());
}
});
outOfMenuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
if(listInFocus.equals("insideMenuList")) {
insideMenuList.setAdapter(insideMenuAdapter);
count = 0;
}
parent.getChildAt(position).setBackgroundColor(Color.argb(100, 255, 242, 99));
toMoveToMenuList[position] = outOfMenuArray.get(position);
count++;
listInFocus = parent.getResources().getResourceEntryName(parent.getId());
}
});
}
public void populateListViews() {
toMoveToMenuList = new String[outOfMenuArray.size()];
for(int i=0; i<outOfMenuArray.size(); i++) {
toMoveToMenuList[i] = "empty";
}
for(int i=0; i<24; i++) {
toMoveOutOfMenuList[i] = "empty";
}
insideMenuAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, insideMenuArray);
insideMenuList.setAdapter(insideMenuAdapter);
outOfMenuAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, outOfMenuArray);
outOfMenuList.setAdapter(outOfMenuAdapter);
listInFocus = "";
count = 0;
}
public void moveItem(View view) {
String id = view.getResources().getResourceEntryName(view.getId());
if((id.equals("removeArrow") || id.equals("addArrow")) && listInFocus.equals("")) {
Toast.makeText(getApplicationContext(), "Please choose an item to move.", Toast.LENGTH_LONG).show();
}
else {
if (id.equals("addArrow") && listInFocus.equals("outOfMenuList")) {
if (insideMenuArray.size() <= 24 - count) {
for(int i=0, j=0; i<outOfMenuArray.size(); i++) {
if(!toMoveToMenuList[i].equals("empty")) {
insideMenuArray.add(toMoveToMenuList[i]);
outOfMenuArray.remove(i - j);
j++;
}
}
}
else Toast.makeText(getApplicationContext(), "List already contains 24 items.\nYou will need to remove 1 or more items\nbefore adding any", Toast.LENGTH_LONG).show();
}
else if (id.equals("removeArrow") && listInFocus.equals("insideMenuList")) {
for(int i=0, j=0; i<24; i++) {
if(!toMoveOutOfMenuList[i].equals("empty")) {
outOfMenuArray.add(toMoveOutOfMenuList[i]);
insideMenuArray.remove(i - j);
j++;
}
}
}
else Toast.makeText(getApplicationContext(), "An item cannot be moved to its own list.", Toast.LENGTH_LONG).show();
populateListViews();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buildListArrays();
}
}
Now I will be working on how to integrate it into the app I am building.

F. Moss
- 55
- 1
- 7