i try make spinner and button with clickHandler
this is my nyoba.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class nyoba extends Activity {
String[] nama_hari = {"Senin", "Selasa", "Rabu","Kamis","Jum'at","Sabtu","Minggu"};
/** Called when the activity is first created. */
Spinner spinner1;
Button btnSubmit;
TextView textView1;
String tes;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSubmit = (Button)findViewById(R.id.btnSubmit);
textView1 = (TextView)findViewById(R.id.textView1);
spinner1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, nama_hari);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(aa);
}
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
tes = nama_hari[arg2];
}
public void clickHandler(View view){
switch (view.getId()){
case R.id.btnSubmit:
textView1.setText(tes);
break;
}
}
}
and this is my main.xml file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#ffffff">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
>
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickHandler"
android:text="Submit" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
but when i run it to emulator,and i choose 1 option in spinner and click the button , the TextView doesn't show up the word from spinner which i choose
(for example, i choose "senin" at spinner, and click the button, but the text "senin" doesn't appear in the TextView )
i check DDMS and there are no error.....
is the code wrong?
can you help me please which one is wrong?
Thank anyway....
I think you don't set the selection listener on your spinner.
But actually you can just retrieve the spinner's selected position with getSelectedItemPosition in the click handler of your button:
Try this:
public void clickHandler(View view) {
switch (view.getId()) {
case R.id.btnSubmit:
textView1.setText(nama_hari[spinner1.getSelectedItemPosition()]);
break;
}
}
Related
Through out the whole android application I want to capture button click, radio button clicks, link click etc... basically a user interaction in whole android application. Is there any common method to detect which element user click and its values.?
Try using onCickListener() on the buttons.
In Kotlin:
Add id to button in .xml files with android:id="#+id/nameOfButton". Every button needs an unique name.
In .kt file, use setOnClickListener with the id to set up the action when user click the button.
If there are several buttons, just follow step 1 and 2.
Example:
step 1
<Button
android:id="#+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
step 2
buttonSave.setOnclickListenter {
//TODO: your code goes here
}
Its very simple you just need to get the text and id of button from the onclick method. Here is java and xml code for it:
XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:onclick="getid"
android:text="Save" />
JAVA:
public void getid(View v){
int id = v.getId();
String text = v.getText();
}
As #Muthukumar Lenin asked here is listview in xml and java
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/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textColor="#5f65ff"
android:padding="5dp"
android:text="Choose is the best football player?"/>
<ListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textview" />
</RelativeLayout>
JAVA:
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.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
final TextView textView = (TextView) findViewById(R.id.textview);
String[] players = new String[] {"CR7", "Messi", "Hazard", "Neymar"};
List<String> Players_list = new ArrayList<String>(Arrays.asList(players));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Players_list);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = (String) parent.getItemAtPosition(position);
textView.setText("The best football player is : " + selectedItem);
}
});
}
}
Some of these code are from tutorialspoint and some are edited by me.
hello friends today my question is about string_array.On my activity i created two buttons.Now on (strings.xml) i created string array name with two items and i have inserted some text as shown below.On each button click i would like to access each item individually.for example on button1 click show me first item and on button 2 click give me the 2nd item on.Can you please give me the code for button click as i have little idea on how to access item.I have created a textview activity to display my item with every click.my code is working fine till now but i need help with extra code that i have to add.Please help me .
// Button activity page
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:text="#string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
//strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
//main
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
Button btnOne;
Button btnTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
String[] chapters=getResources().getStringArray(R.array.chapters);
btnOne = findViewById(R.id.btn1);
btnTwo = findViewById(R.id.button1);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.btn1 : {
text = getResources().getStringArray(R.array.chapters)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.chapters)[1];
break;
}
}
Intent intent = new Intent(Lipok.this,lipokchapters.class);
intent.putExtra("DATA", text);
startActivity(intent);
}
}
//textview activity page
package com.Aolai.temeshilai;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class lipokchapters extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipokchapters);
textView=findViewById(R.id.textv);
String text= getIntent().getStringExtra("Data");
textView.setText(text);
}
}
Try this :
xml file
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="#+id/OKILA"
android:layout_width="wrap_content"
android:text="#string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
Main Class
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
}
#Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.OKILA : {
text = getResources().getStringArray(R.array.testArray)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.testArray)[1];
break;
}
}
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, YOUR_ACTIVITY_NAME.class);
intent.putExtra("DATA", text);
startActivity(intent);
// Or else you can do whatever you want to do here with that text
}
}
Then in your other activity
String data = getIntent().getStringExtra("DATA");
YOUR_TEXTVIEW_NAME.setText(data);
This is basics of Android so before answering I suggest you to go
through basic tutorials before posting on Stackoberflow.
Now,
Here is how you can access the items from your string_array.
String[] chapters= getResources().getStringArray(R.array.chapters);
Now,here is pseudo code that might help you.
btn1.setOnClickListener(
your_text_view.setText(chapters[0])
)
//Here 0 means your first item, likewise you can access items by their index in
array
I am new to Android Studio/Development, but not programming itself. I am struggling with the syntax (as I do think it is my problem) yet I cannot find a solution anywhere on here or on Google. I have this 2nd activity named FilterActivity. FilterActivity currently has 2 TextViews. Both create dynamic CheckBoxes. Right now, I only have one doing this so I can get one right before I go onto another. Here's the issue, I click on the TextView to get the dynamically created CheckBox and it shows perfectly fine. However, clicking on it again just adds the same values until it appears to completely fill the parent via xml.
No matter what I search, what I do, it all does the same exact thing. I know, I have nothing for onCheckedChanged, but I have previously before and it did not work either. So question(s), should I not be using LinearLayout and do like a container of sorts? There will be a decently big database for the checkboxes (500+) so I was thinking I would have to implement ScrollView at some point. Also, if LinearLayout is the correct way to go, what am I doing wrong? I am just completely spinning and I know it should not be this hard.
Thank you to anyone that gives feedback!
Here's 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"
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"
tools:context="xxx.AppEx.FilterActivity"
tools:layout_editor_absoluteY="81dp"
tools:showIn="#layout/activity_filter">
<View
android:id="#+id/firstView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<View
android:id="#+id/secondView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/AllTextView"
android:clickable="true"
android:text="All"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:layout_toLeftOf="#id/firstView"
android:layout_alignParentTop="true"
android:gravity="center"
android:textStyle="bold"
android:textAllCaps="true"/>
<TextView
android:id="#+id/DisTextView"
android:clickable="true"
android:text="Dis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:layout_toRightOf="#+id/firstView"
android:layout_alignParentTop="true"
android:gravity="center"
android:textStyle="bold"
android:textAllCaps="true"/>
<LinearLayout
android:id="#+id/CheckBoxLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/secondView">
</LinearLayout>
</RelativeLayout>
Here's the main code:
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.content.Intent;
public class FilterActivity extends AppCompatActivity implements
CompoundButton.OnCheckedChangeListener {
LinearLayout CheckBoxLayout;
CheckBox checkBox;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter);
CheckBoxLayout = (LinearLayout) findViewById(R.id.CheckBoxLayout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView AllTextView = (TextView) findViewById(R.id.AllTextView);
AllTextView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
AllTextView.setBackgroundColor(Color.BLUE);
onAllClick();
}
});
TextView DisTextView = (TextView) findViewById(R.id.DisTextView);
DisTextView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//onAllClick();
}
});
}
public void onAllClick()
{
Intent intent = getIntent();
dummy();
}
public void dummy()
{
String[] array = new String[]
{
"Rice", "Beans"
};
LinearLayout CheckBoxLayout = (LinearLayout)findViewById(R.id.CheckBoxLayout);
for (int i = 0; i < array.length; i++)
{
CheckBox checkBox = new CheckBox(this);
checkBox.setText(array[i]);
CheckBoxLayout.addView(checkBox);
}
}
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
}
}
This question already has answers here:
how to implement a long click listener on a listview
(10 answers)
Closed 8 years ago.
I searched on various forums for the right answer but nothing works for me. For the moment I have this code in private class MyDiary:
ListView list = new ListView(Monday.this);
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
new EditListItemDialog(view.getContext()).show();
return true;
}
});
It doesn't return errors but the EditListItemDialog does not open. What am I missing here?
EDIT
code for EditListItemDialog:
package com.example.classorganizer;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
class EditListItemDialog extends Dialog implements View.OnClickListener {
private View editText;
public EditListItemDialog(Context context) {
super(context);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons
View btnOk = findViewById(R.id.button_ok);
editText = findViewById(R.id.edit_text);
btnOk.setOnClickListener(this);
}
#Override
public void onClick(View v) {
((TextView) editText).getText().toString();//here is your updated(or not updated) text
dismiss();
}
}
XML files:
<ListView
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="fill_parent"
android:id="#android:id/list"
android:longClickable="true"
>
</ListView>
And to create row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignLeft="#+id/name"
android:layout_below="#+id/name"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="0dip">
<TextView android:textSize="16dip"
android:layout_width="wrap_content"
android:layout_height="29dp"
android:layout_marginTop="0dp"
android:id="#+id/name"
android:layout_marginRight="4dp"
android:text="Diary Title"
android:textStyle="bold"
android:longClickable="true" />
</RelativeLayout>
Assuming you are using a ListActivity you should change the following line (from your first code block)...
ListView list = new ListView(Monday.this);
...to be...
ListView list = getListView();
You can then set the listener for it.
I can not get the correct screen open.
I have the following code, and I also added the corresponding class in the manifest.
Please, if you can give me some idea.
Thank you.
listV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
ListAdapter adp = (ListAdapter)parent.getAdapter();
//lista.get(position);
Seccion s = lista.get(Integer.parseInt(adp.getItem(position).toString()));
if (s.getTitle().contains("Estudiar")) {
//sel.setText("Seleccionado el del if"+ s.getTitle());
Intent i = new Intent(LiverpoolguideActivity.this,tipoListado.class);
startActivity(i);
}
}
});
The code tipoListado.class :
package jorgechu.com.liverpoolguide;
import java.util.ArrayList;
import jorgechu.com.liverpoolguide.Secciones.Seccion;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class tipoListado extends Activity {
public class LiverpoolguideActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listadotipo);
}
}
}
And the code screen xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/textViewListadoTipo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tipo:"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<ListView
android:id="#+id/listViewType"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Focusable views inside a ListView item will disable the ability to select ListView items. Applying android:focusable="false" to the TextView will allow OnItemClick to work again.