Creating multiple selection list Android - java

I would like to add a form of input in my app where a list of options appear and multiple choices can be selected. I would use a spinner, but that only allows one option to be selected. I would then need for the options selected to be sensed for me to perform further operations with this. I think I would need something involving a checkedlistview but I am not sure. What would be the best way to go about doing this? I hope my situation is understood.

Use checkedlistview it Help you
ListMain.Java
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
#SuppressLint("ShowToast")
public class ListMain extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView mylist = (ListView) findViewById(R.id.companionsearch_listView1);
String[] list={"one","two","three"};
ArrayAdapter adapter = new ArrayAdapter<String>(ListMain.this,android.R.layout.simple_list_item_multiple_choice,list);
mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mylist.setAdapter(adapter);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/bottombarbuttonlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:baselineAligned="false"
android:weightSum="3" >
<ListView
android:id="#+id/companionsearch_listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#ffffff" >
</ListView>
</LinearLayout>
</RelativeLayout>
Output:

Related

how to prevent edit text from shrinking when keyboard opens?

I am a beginner in Android, I am trying to make a simple app where text in the edit text gets added to recycler view, but as soon as the Keyboard opens, edit text shrinks and content inside it is not visible
This is activty_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" >
<EditText
android:hint="ADD ITEMS"
android:layout_margin="10dp"
android:id="#+id/etItems"
android:textSize="24sp"
android:textColor="#android:color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
<Button
android:layout_margin="10dp"
android:id="#+id/btnAdd"
android:text="ADD"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvItems"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6" />
</LinearLayout>
This is MainActivity.Java
package com.example.todolist_09082020;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> items;
Button btnAdd;
EditText etItems;
RecyclerView rvItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = findViewById(R.id.btnAdd);
etItems = findViewById(R.id.etItems);
rvItems = findViewById(R.id.rvItems);
items = new ArrayList<>();
final ItemAdapter adapter = new ItemAdapter(items);
rvItems.setLayoutManager(new LinearLayoutManager(this));
rvItems.setAdapter(adapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = etItems.getText().toString();
if(!text.equals("")){
items.add(text);
adapter.notifyDataSetChanged();
}
etItems.setText("");
}
});
}
}
I tried solutions from other posts related to this, but this could not be solved.
edit text shrinked
Change layout_height of EditText and its parent LinearLayout to wrap_content. It should solve your issue
Your problem is the linear layout holding the text and button height. There's several things you can do, but a minimal way to keep it from collapsing is to change
android:layout_height="0dp"
to
android:layout_height="wrap_content"
You'll probably also see nicer results if you additionally change the text and button heights to wrap_content and choose a fixed font size (24sp looked nice) for your button text.

Android Studio add EditText input to a ListView with button onClick

I am trying to make a simple Planner app so I have an XML file with a ListView on it, and another XML file with an EditText and a sumbit button on it, and of course my Java files. I have done a ton of research but I can't find a working method to make it when I press the button, it adds the EditText stuff to the ListView as an item. Here is my code:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:layout_width="300dp"
android:layout_height="fill_parent"
android:id="#+id/event_list"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/datePicker" />
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/datePicker"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<CalendarView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calendarView"
android:layout_alignBottom="#+id/datePicker"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Event"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignEnd="#+id/datePicker"
android:layout_toEndOf="#+id/event_list"
android:background="#android:color/holo_blue_light"
android:onClick="newEvent"
android:textSize="20dp"
android:textColor="#android:color/white" />
</RelativeLayout>
new_plan.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/event_name"
android:hint="Event Name"
android:inputType="textCapWords"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/event_date"
android:hint="Event Date" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="#+id/button2"
android:layout_gravity="center_horizontal"
android:background="#android:color/holo_blue_light"
android:textSize="20dp"
android:textColor="#android:color/white" />
</LinearLayout>
MakePlan.java
package com.kass.planner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MakePlan extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_plan);
}
}
MainActivity.java
package com.kass.planner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void newEvent(View v){
Intent i = new Intent(this, MakePlan.class);
startActivity(i);}}
Please help, thank you.
Take a look on this google developers project. It's for animations but i think it also have the solution to your question.
https://developer.android.com/training/animation/layout.html
You should append data to the collection of items linked to your ListView via your adapter and notify your adapter that your data has changed. This illustration assumes that you have a List of String objects.
EditText editText = ...
List<String> list = ...
YourAdapter adapter = ...
ListView listView = ...
You must have done something like this to setup your listView.
listView.setAdapter(new YourAdapter(list, this));
Now on button click you should get the String in EditText, add it to your List and finally notify the adapter that your data has changed in order to update your ListView for e.g.
void OnButtonClick(View view){
String strToAdd = editText.getText().toString();
list.add(strToAdd);
adapter.notifyDataSetChanged();
}

Android list with ListAdapter layout Android Studio

I'm working with android studio and I have this list and I can't get the layout to work.(You'll notice I have a listener that opens another activity when you click on a single item, and that works just fine).
Here's my activity_estoque.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
</LinearLayout>
and this is the estoque.java class, that uses the .xml file above:
package com.example.asus.mingausfashionmoda;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class estoque extends ListActivity {
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//string que pega do query
String[] adobe_products = {"SANSUMG", "LG", "MOTOROLA", "Apple"};
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_estoque, R.id.label, adobe_products));
ListView lv = getListView();
//listening to single item in list view
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//selected item
LinearLayout ll = (LinearLayout) view;
TextView tv = (TextView) ll.findViewById(R.id.label);
final String product = tv.getText().toString();
//String product = ((TextView) view).getText().toString();
//lauching new activity to single item
Intent i = new Intent(getApplicationContext(), estoqueSingle.class);
//sending data to new activity
i.putExtra("product", product);
startActivity(i);
}
});
}
}
I added some pictures to help you understand what I want and what I'm getting(sorry for bad paiting):
1 - The screen on the left is what I should see, according to the Android Studio activity_estoque.xml Design session, and the right one is what I actually get(Notice the "Mingau's fashion moda" thing missing, I really want this blue thing): (What I should get vs what I'm getting)
2 - Now, the left screen is what I want, a top single button and the list bellow. The right one is what I'm receiving after adding a button on the activity_estoque.xml file(code bellow): (What I want vs what I get)
this is the activity_estoque.xml file after I add the button:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnttt"
android:text="This is a button"
/>
</LinearLayout>
First I thought that I should add a setContentView(R.layout.activity_estoque); on the onCreat method, like this:
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_estoque);
//string que pega do query
String[] adobe_products = {"SANSUMG", "LG", "MOTOROLA", "Apple"};
but if I do that I get the following error, and I'm not sure if it makes any sense to add this setContentView...:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asus.mingausfashionmoda/com.example.asus.mingausfashionmoda.estoque}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
and this:
at com.example.asus.mingausfashionmoda.estoque.onCreate(estoque.java:20)
this is what I have on estoque.java.20:
setContentView(R.layout.activity_estoque);
Anyone can help? Thanks.
What you want to achieve is explained here as example here. And the blue bar above you see is actually achievable using Action Bar example here. This action bar is configurable for color, icon, title, sub title and the menus(show/not to show).
You are extending a ListActivity, so when you start the activity it will use the default layout. If you try to do setContentView(R.layout.activity_estoque); in the code then the activity will expect your layout to hava list view with the id "list".
Edit--
Change your layout to
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnttt"
android:text="This is a button"
/>
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
and
and do setContentView(R.layout.activity_estoque) in onCreate
You will need to create a new layout for your list item view, which you need to pass to the ArrayAdapter
For eg:
activity_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
then change this this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_estoque, R.id.label, adobe_products)); to this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_test, R.id.label1, adobe_products));

My text is half visible when i want to add a name to my list (SQLite) Android

Hello im making an assignment list with database to add assignments. When i push on the add button i can type my assignment. But i only see half of it. When i click add, it correctly adds the new assignment to the list and i can see the whole text.
How can i solve this ?
This is my XML layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="be.ehb.android.arnojansens.DetailGroupsActivity"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listDetails"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4.60"></ListView>
<EditText
android:id="#+id/editDetails"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="Details Assignment"/>
<Button
android:id="#+id/btnAddDetailsOpdracht"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Add Details Assignment"/>
</LinearLayout>
</RelativeLayout>
And this is the java of the class
package be.ehb.arnojansens.fragmentexampleii;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class DetailGroupsActivity extends Activity implements View.OnClickListener {
static Dbhelper opdrDbHelper;
static Button btnAddDetailsOpdracht;
static EditText editDetails;
static ListView listDetails;
static ArrayList<Detail> details;
static long detailId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_groups);
btnAddDetailsOpdracht = (Button)findViewById(R.id.btnAddDetailsOpdracht);
editDetails = (EditText)findViewById(R.id.editDetails);
listDetails = (ListView)findViewById(R.id.listDetails);
btnAddDetailsOpdracht.setOnClickListener(this);
opdrDbHelper = new Dbhelper(this);
detailId = (Long)getIntent().getSerializableExtra("detailId");
String opdrachtName = (String)getIntent().getStringExtra("opdrachtName");
this.setTitle(opdrachtName);
if(detailId != -1)
details = opdrDbHelper.findDetailsForOpdracht(detailId);
else
details = new ArrayList<Detail>();
loadDetails();
}
public void loadDetails(){
ArrayAdapter<Detail> adapter =
new ArrayAdapter<Detail>(
this,
android.R.layout.simple_list_item_1,
details);
listDetails.setAdapter(adapter);
}
#Override
public void onClick(View v) {
String detailString = editDetails.getText().toString();
editDetails.setText("");
Detail alb = opdrDbHelper.addAlbum(detailId,detailString);
details.add(alb);
loadDetails();
}
}
Sound to me like the text doesn't fit in the EditText container. Make it wrap_content instead of a fixed size:
<EditText
android:id="#+id/editDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Details Assignment"/>
Also keep in mind that fill_parent was deprecated, use match_parent instead.

ListView Inside Layout

I need some help creating a listview inside a layout i have. im having torubles creating it properly.
Here is what i got so far.
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/gradient_bg"
android:src="#drawable/gs3ultimate" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/thelistarray"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#555555" />
<TextView
android:id="#+id/summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/thelistarry_details"
android:textColor="#555555"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Now its just a image on the left, then 2 textview, Now i believe im gonna need 2 string arrys, one for the titles and the 2nd for the details. and i think a 3rd one for the images. Is that right?
Here some java for my list view
AndroidListViewActivity.java
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class AndroidListViewActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] list_array = getResources().getStringArray(R.array.list_array);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.item, R.id.label, list_array));
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
}
I know that my java is missing adding the details list_array_details. and i dont know how to add it so it populates. Im not yet using the onitemclick yet for my intent's. However would i determine which one is click and what activity to launch? example. i click list item b and want to like activity "b" but when i click a it launches activity "a"
Any help would be greately appreciated, Thanks in advance.
You need to have a custom adapter for this purpose to populate someother view other than a textview or multiple textviews.
Take a look on customadapter here and here

Categories

Resources