android widget within a widget not working as intended - java

I use a FrameLayout as a custom list item. I have an onItemClickListener set up to the listView but my items each have a button within themselves, and while the button in them is clickable, the item itself is not and they're supposed to do different things. How do I make the item as well as the button within it both clickable?
Here's the code:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class ProjectCreateScreen extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondary_layout1);
final TextView noProject = (TextView) findViewById(R.id.NOPROJECT);
Button btn = (Button) findViewById(R.id.addBtn);
final ArrayList<String> listItems=new ArrayList<String>();
final ListAdapter addAdapter = new ArrayAdapter<String>(this,
R.layout.list_item, R.id.listFrame, listItems);
final ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(addAdapter);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
noProject.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
listItems.add("New Project");
((ArrayAdapter) addAdapter).notifyDataSetChanged();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent switchToEdit = new Intent(ProjectCreateScreen.this,
teamCreateScreen.class);
startActivity(switchToEdit);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The custom listView item xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/listFrame"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="20sp"
/>
<Button android:id="#+id/frameBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="top|right"
/>
</FrameLayout>
The xml of the activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rl">
<Button
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="#string/AddProject"
android:id="#+id/addBtn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/noProjectsNotice"
android:id="#+id/NOPROJECT"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="16sp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/addBtn"
android:id="#+id/lv"
android:visibility="invisible">
</ListView>
</RelativeLayout>

Add the following two attributes for any view you want to be clickable inside the listview:
android:focusable="false"
android:focusableInTouchMode="false"
That should solve most cases. If that is not enough, also add the following attribute to the root layout of your list view layout
android:descendantFocusability="blocksDescendants"

Related

ListView isn't working, it is not displaying the listView on the main page

my ListView isn't working, it is not displaying the listView on the main page. Any solutions?
Does it have to do with my layout file too? I will attach the layout file below too!
I was trying to make an adapter for the listview
It is just a simple one page application which I try to learn how to use listview. Did I do any wrong about the code? I was mainly following a youtube tutorial for this but still an error occur
MainActivity.java
package com.example.myapplication;
import android.content.res.Resources;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
FloatingActionButton fab;
ListView myListView;
String [] items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Resources res = getResources();
myListView = (ListView) findViewById(R.id.myListView);
items = res.getStringArray(R.array.items);
ItemAdapter itemAdapter = new ItemAdapter(this, items);
myListView.setAdapter(itemAdapter);
// myListView.setAdapter(new ArrayAdapter<String>(this,R.layout.));
fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ItemAdapter.java
package com.example.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
//A class that are BaseAdapter but with another name call 'ItemAdapter'
//To pass values/data to ListView
public class ItemAdapter extends BaseAdapter {
LayoutInflater mInflater;
String[] items;
public ItemAdapter(Context c, String[] i){
items = i;
mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //To initialize inflater
}
#Override
public int getCount() {
return items.length;
}
#Override
public Object getItem(int i) {
return items[i];
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View convertView, ViewGroup parent) {
View v = mInflater.inflate(R.layout.my_listview_details,null);
TextView nameTextView = (TextView) v.findViewById(R.id.nameTextView);
String name = items[i];
nameTextView.setText(name);
return v;
}
}
activity_main.xml
<?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"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="Your Library"
app:titleTextColor="#fff" />
<include
android:id="#+id/include"
layout="#layout/content_main"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginBottom="24dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/round_add_white_24" />
</androidx.constraintlayout.widget.ConstraintLayout>
content_main.xml
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginTop="?attr/actionBarSize"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<ListView
android:id="#+id/myListView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
my_listview_details.xml
<?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">
<TextView
android:id="#+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="TextView" />
</RelativeLayout>
Any solutions?
used this
Change Height and width 0dp to as below
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
Instead of using ItemAdapter a custom class. If you have very simple list then just use the built-in ArrayAdapter class to register the listview.
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, items);
listView.setAdapter(adapter);
Resources res = getResources();
myListView = (ListView) findViewById(R.id.myListView);
items = res.getStringArray(R.array.items);
-- > Try to check items.length in toast or console. Check if it is showing data or not.

How to add row to listView on button click in android studio?

I am using a custom adapter for my ListView which I already don't understand as much as I would like to. I can find information online on how to add a row to ListView but it doesn't integrate nicely into my code, I'm guessing because I'm using a custom adapter. I have my setOnClickListener() method for my button in my Main Activity and have been experimenting there but just cant figure it out I'm also not sure if my method is in the right place?
this is the mainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import static java.util.logging.Logger.global;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] Chores = {""};
ListAdapter MyAdapter = new CustomAdapter(this, Chores);
ListView listViewObject = (ListView)findViewById(R.id.customListView_ID);
listViewObject.setAdapter(MyAdapter);
listViewObject.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String ChoreString = String.valueOf(parent.getItemAtPosition(position));
}
}
);
// this is where I am trying to have button click add another row to my listView
final Button button = (Button) findViewById(R.id.button_ID);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//this is where I am stuck
}
});
}
}
here is my CustomAdapter class
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
class CustomAdapter extends ArrayAdapter{
public CustomAdapter(Context context, String[] choreText) {
super(context, R.layout.custon_listview_row, choreText);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater myInflater = LayoutInflater.from(getContext());
View customView = myInflater.inflate(R.layout.custon_listview_row, parent, false);
String singleListItem = (String) getItem(position);
EditText choreText = (EditText) customView.findViewById(R.id.editText_ID);
ImageButton imageButton = (ImageButton) customView.findViewById(R.id.imageButton_ID);
choreText.setText(singleListItem, TextView.BufferType.EDITABLE);
imageButton.setImageResource(R.drawable.clock);
return customView;
}
}
activity_main.xml
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.emilythacker.chorelist.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/customListView_ID"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp" />
<Button
android:text="Add Chore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/button_ID" />
</RelativeLayout>
and custom_listview_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="60dp">
<ImageButton
android:layout_width="60dp"
android:scaleType="fitCenter"
app:srcCompat="#drawable/clock"
android:id="#+id/imageButton_ID"
android:layout_height="60dp"
android:background="#null"
android:layout_alignParentRight="true"
android:padding="5dp"
android:layout_weight="1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/editText_ID"
android:layout_alignParentLeft="true"
android:alpha = ".5"
android:hint="Enter chore"
android:maxLines="1"
android:inputType="text"
/>
</RelativeLayout>
First make your list into an ArrayList instead of String array.
private ArrayList<String> arrayList;
In the buttons onClick
arrayList.add("New Item");
((ArrayAdapter)MyAdapter).notifyDataSetChanged();
public void onClick(View v) {
String stringToAdd = ... //
MyAdapter.add(stringToAdd);
}
You will need to add final to the adapter declaration for this to work:
final ListAdapter MyAdapter = new CustomAdapter(this, Chores);
EDIT
To add more than one row at a time:
public void onClick(View v) {
String[] rowsToAdd = ... //
MyAdapter.addAll(rowsToAdd);
}

About Spinners and button interface

how do I combine the
slected item of 3 spinners i have created and link it to a button??
here is my code...
package com.example.nikhiljoshi.time_table;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
char branch1;
Spinner spbranch,spsem,spdiv;
ArrayAdapter<CharSequence>adapter;
ArrayAdapter<CharSequence>adapter1;
ArrayAdapter<CharSequence>adapter2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Submit = (Button) findViewById(R.id.button);
spbranch = (Spinner)findViewById(R.id.spinner);
spsem = (Spinner)findViewById(R.id.spinner2);
spdiv = (Spinner)findViewById(R.id.spinner3);
adapter = ArrayAdapter.createFromResource(this,R.array.branch,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spbranch.setAdapter(adapter);
spbranch.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int i;
// branch1=spbranch.getItemAtPosition(i).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
adapter1 = ArrayAdapter.createFromResource(this,R.array.sem,android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spsem.setAdapter(adapter1);
adapter2 = ArrayAdapter.createFromResource(this,R.array.division,android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spdiv.setAdapter(adapter2);
Submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(view, "this is my Toast message!!! =)",
// Toast.LENGTH_LONG).show();
// Intent intent = new Intent(v.getContext(), nik.class);
// startActivityForResult(intent, 0);
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
layout...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Branch"
android:id="#+id/textView"
android:layout_marginTop="105dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp"
android:textSize="20dp" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_marginLeft="34dp"
android:layout_marginStart="34dp"
android:layout_alignTop="#+id/textView"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sem"
android:id="#+id/textView2"
android:layout_below="#+id/spinner"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:layout_marginTop="30dp"
android:textSize="20dp" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner2"
android:layout_alignTop="#+id/textView2"
android:layout_alignLeft="#+id/spinner"
android:layout_alignStart="#+id/spinner" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Division"
android:id="#+id/textView3"
android:layout_below="#+id/spinner2"
android:layout_alignLeft="#+id/textView2"
android:layout_alignStart="#+id/textView2"
android:layout_marginTop="30dp"
android:textSize="20dp" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner3"
android:layout_alignTop="#+id/textView3"
android:layout_alignLeft="#+id/spinner2"
android:layout_alignStart="#+id/spinner2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/button"
android:layout_below="#+id/spinner3"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter the details"
android:id="#+id/textView4"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
when i press the submit button, the following things should happen
for eg
if(branch=='cs'&& sem=='5' && div=='A')
then new activity
how do i do that?
You can use Spinner.getSelectedItem() to get the currently selected values from your three spinners, then use those values to determine if you should launch your Activity.

Populate a Spinner class using by entering the first letter of elements in an array

I am supposed to create an app to generate a drop down list of states, that starts with a letter entered by user. The app should have one text input field and one button ("search"). It should also have an empty drop-down list below.
The list of all US states should be stored in an array.
Once a button clicked:
if there is a single letter entered a drop down list is populated with the states names from the array that starts with an entered
letter. If none or more than one letter is entered, nothing should be done.
I'm so lost and confused on what to do at this point. I tried using a searchView but I'm not sure if that is even the right way to go, as it's not doing any type of filtering what so ever, if i click the button the array just shows up in the drop down, all 50 states no matter what is typed in the searchView. I'm almost wondering if I should switch to just having it be a textView... but either way, help please! (end of semester is near and my teacher though he says "ask for help" is not even bothering to respond to email... sigh) Here is my code:
Content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_below="#+id/button"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Look for States below"
android:id="#+id/textView"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search States"
android:id="#+id/button"
android:layout_below="#+id/searchView"
android:layout_centerHorizontal="true"
android:onClick="buttonOnClick"/>
<SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchView"
android:layout_below="#+id/textView"
android:layout_alignStart="#+id/button"
android:queryHint="Search" />
</RelativeLayout>
MainActivity.Java:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
SearchView searchView;
Button stateLook;
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
searchView = (SearchView)findViewById(R.id.searchView);
stateLook = (Button) findViewById(R.id.button);
};
public void buttonOnClick(View v) {
Button button = (Button) v;
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayList states = new ArrayList();
states.add("Alabama");
states.add("Alaska");
states.add("Arizona");
states.add("Arkansas");
states.add("California");
states.add("Colorado");
states.add("Connecticut");
states.add("Delaware");
states.add("Florida");
states.add("Georgia");
states.add("Hawaii");
states.add("Idaho");
states.add("Illinois");
states.add("India");
states.add("Iowa");
states.add("Kansas");
states.add("Kentucky");
states.add("Louisiana");
states.add("Maine");
states.add("Maryland");
states.add("Massachusetts");
states.add("Michigan");
states.add("Minnesota");
states.add("Mississippi");
states.add("Missouri");
states.add("Montana");
states.add("Nebraska");
states.add("Nevada");
states.add("New Hampshire");
states.add("New Jersey");
states.add("New Mexico");
states.add("New York");
states.add("North Carolina");
states.add("North Dakota");
states.add("Ohio");
states.add("Oklahoma");
states.add("Oregon");
states.add("Pennsylvania");
states.add("Rhode Island");
states.add("South Carolina");
states.add("South Dakota");
states.add("Tennessee");
states.add("Texas");
states.add("Utah");
states.add("Vermont");
states.add("Virgina");
states.add("Washington");
states.add("West Virgina");
states.add("Wisconsin");
states.add("Wyoming");
final
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, states);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView myText = (TextView) view;
Toast.makeText(this, "You Selected "+ myText.getText(), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Ok...than do something like have an edittext and a button. on clicking button fill your adapter with filtered data.
For Ex.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Edittext
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Look for States below"
android:id="#+id/search"
android:maxLength="1"
android:textSize="30dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search States"
android:id="#+id/button"
android:layout_below="#+id/searchView"
android:layout_centerHorizontal="true"
android:onClick="buttonOnClick"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_below="#+id/button"
/>
</LinearLayout>
In Java Code
public void buttonOnClick(View v) {
Button button = (Button) v;
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayList states = new ArrayList();
//fill your list with only matching criteria by any query
}

All three buttons lead to same activity?

I'm having an issue that I can't quite figure out. I've got all three of my buttons to take me to another activity, but the problem is that they all lead me to the activity that I only want my first (Easy) button to do.
fragment_main_menu.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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.app.whosesloganisthat.MainMenu$PlaceholderFragment" >
<requestFocus />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="#string/welcometowhosesloganisthat"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/buttonHard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonHard"
android:layout_alignParentBottom="true"
android:layout_marginBottom="46dp"
android:onClick="sendMessage"
android:text="#string/hard" />
<Button
android:id="#+id/buttonEasy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="48dp"
android:onClick="sendMessage"
android:text="#string/easy" />
<Button
android:id="#+id/buttonIntermediate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonEasy"
android:layout_centerVertical="true"
android:onClick="sendMessage"
android:text="#string/intermediate" />
</RelativeLayout>
MainMenu.java:
package com.app.whosesloganisthat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.os.Build;
public class MainMenu extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.app.whosesloganisthat.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main_menu); //xml file name which contains button
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, EasyLevelInfo.class);
startActivity(intent);
// Do something in response to button
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_menu,
container, false);
return rootView;
}
}
EasyLevelInfo.java:
package com.app.whosesloganisthat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class EasyLevelInfo extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainMenu.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.easy_level_info, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_easy_level_info,
container, false);
return rootView;
}
}
fragment_easy_level_info.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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.app.whosesloganisthat.EasyLevelInfo$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:text="#string/youhaveselectedeasy"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Thanks everyone!
Look in your fragment_main_menu.xml
layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="#string/welcometowhosesloganisthat"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/buttonHard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonHard"
android:layout_alignParentBottom="true"
android:layout_marginBottom="46dp"
android:onClick="sendMessage"
android:text="#string/hard" />
<Button
android:id="#+id/buttonEasy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="48dp"
android:onClick="sendMessage"
android:text="#string/easy" />
<Button
android:id="#+id/buttonIntermediate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonEasy"
android:layout_centerVertical="true"
android:onClick="sendMessage"
android:text="#string/intermediate" />
You have three buttons buttonHard, buttonEasy , buttonIntermediate . And these three buttons have attribute android:onClick = "sendMessage" This means if one of these buttons is clicked do sendMessage method . Lets look on that method.
public void sendMessage(View view) {
Intent intent = new Intent(this, EasyLevelInfo.class);
startActivity(intent);
// Do something in response to button
}
This method starts Activity EasyLevelInfo . So if one of three buttons is clicked it will call this sendMessage method and open EasyLevelInfo Activity .

Categories

Resources