How to set edittext editable dynamically by click an imagebutton? - android

I list dynamically string values from an ArrayList into EditTexts with a for loop, and ImageButtons are next to every EditText. I want this: when I click an ImageButton, the corresponsive EditText be editable.
Here is my code from the activity:
LinearLayout container = (LinearLayout) findViewById(R.id.container);
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View showProduct = layoutInflater.inflate(R.layout.row_show_product, null);
for (int i = 0; i < product.size(); ++i) {
final EditText edt_row_show_product = (EditText) showProduct.findViewById(R.id.edt_row_show_product);
edt_row_show_product.setText(product.get(i));
ImageButton ib_row_show_product_edit_icon = (ImageButton)showProduct.findViewById(R.id.ib_row_show_product_edit_icon);
ib_row_show_product_edit_icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//container.getParent()...;
}
});
container.addView(showProduct);
}

I made a quick app to make your requirement work. Please take a look.
I had the code working in a Fragment so if you are working in an Activity please make the required changes.
Fragment Code:
package viewtest.myapplication;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
public FragVisibilityInterface mInterface = null;
private LinearLayout showProduct, editTextsLL;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mInterface = new FragVisibilityInterface() {
#Override
public void toggleFragVisibility() {
}
};
}
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//Button testBtn = (Button) rootView.findViewById(R.id.testbutton);
/*testBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
hideFrag();
}
}); */
String [] myArray = getResources().getStringArray(R.array.populate_array);
ArrayList<String> product = new ArrayList<>(Arrays.asList(myArray));
for (int i = 0; i < product.size(); ++i){
View showProd = createNewTextView(product.get(i));
LinearLayout editTextLL = (LinearLayout) rootView.findViewById(R.id.editTextsLL);
editTextLL.addView(showProd);
showProd = null;
}
return rootView;
}
/*private void hideFrag() {
SecondActivityFragment secFrag = new SecondActivityFragment();
getFragmentManager().beginTransaction().add(R.id.fragment, secFrag, "SecFrag").commit();
getFragmentManager().beginTransaction().hide(this).commit();
}*/
private View createNewTextView(String text) {
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View showProduct = layoutInflater.inflate(R.layout.edit_text_layout, null);
final EditText edt_row_show_product = (EditText) showProduct.findViewById(R.id.editText);
edt_row_show_product.setText(text);
Button ib_row_show_product_edit_icon =(Button) showProduct.findViewById(R.id.button);
ib_row_show_product_edit_icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edt_row_show_product.requestFocusFromTouch();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt_row_show_product, InputMethodManager.SHOW_IMPLICIT);
}
});
return showProduct;
}
public interface FragVisibilityInterface{
public void toggleFragVisibility();
}
}
EditText and Button Layout
<?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:id="#+id/linearLayout">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
/>
</LinearLayout>
Main Fragment Layout
<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=".MainActivityFragment">
<TextView
android:id="#+id/textview"
android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/testbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textview"
android:text="Button"/>
<LinearLayout
android:id="#+id/editTextsLL"
android:layout_below="#id/testbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
</RelativeLayout>
The code may be a little rough around the edges as it was made real quick. Please make relevant changes. Hope this helps! :)

You can set set the tag of the imageview with the corresponding EditText and get the tag in the onClick
final EditText edt_row_show_product = (EditText) showProduct.findViewById(R.id.edt_row_show_product);
edt_row_show_product.setText(product.get(i));
ImageButton ib_row_show_product_edit_icon =(ImageButton)showProduct.findViewById(R.id.ib_row_show_product_edit_icon);
ib_row_show_product_edit_icon.setTag(edt_row_show_product); //set the Edittext object here above
ib_row_show_product_edit_icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText et = (EditText)v.getTag()
//do what ever you want with the EditText
}
});

Related

Button not causing the onClick event

I'm working on an app (part of a project in school), and I have an activity, that consists of an ImageView, a SearchView, a ListView, and a Button.
I'm trying to put the button in front of the ListView, which was working gucci until I got to the actual making the button do something part.
After setting the onClick method, I noticed that the button didn't do anything. I tried just printing something using the button, but nothing worked. Also tried creating a new button, outside the ListView, but that also failed (also didn't cause the onClick event).
I am kinda clueless, and cannot think of anything that could've caused this problem...
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:background="#color/main_color"
android:orientation="vertical"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
android:layout_gravity="center"
tools:context=".SetupActivity">
<ImageView
android:layout_width="300dp"
android:layout_gravity="center"
android:layout_height="130dp"
android:layout_marginTop="13dp"
android:background="#drawable/setup_text"
android:id="#+id/setupText"
></ImageView>
<androidx.appcompat.widget.SearchView
android:layout_width="match_parent"
android:layout_height="65dp"
android:id="#+id/searchViewSetup"
android:theme="#style/AppTheme.Toolbar"
app:iconifiedByDefault="false"
></androidx.appcompat.widget.SearchView>
<RelativeLayout
android:layout_width="wrap_content"
android:paddingHorizontal="8dp"
android:layout_height="wrap_content">
<ListView
android:id="#+id/list_item"
android:layout_gravity="center"
android:layout_width="395dp"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
android:layout_height="wrap_content"
tools:listitem="#layout/activity_ingredients_layout"
android:paddingBottom="10.0sp"
android:clipToPadding="false"
android:scrollbars="none"
android:paddingTop="10.0sp">
</ListView>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/setup_btn_shadow"
android:layout_marginLeft="287dp"
android:layout_marginTop="450dp"
android:alpha="0.5"
></ImageView>
<android.widget.Button
android:layout_width="73dp"
android:id="#+id/setup_floating_btn"
android:layout_height="73dp"
android:layout_marginLeft="300dp"
android:layout_marginTop="460dp"
android:foregroundGravity="bottom"
android:src="#drawable/ic_baseline_arrow_forward_ios_24"
android:background="#drawable/setup_btn"
></android.widget.Button>
</RelativeLayout>
</LinearLayout>
java:
package com.example.yummilyproject;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import com.example.yummilyproject.databinding.ActivitySetupFridgeBinding;
import java.util.ArrayList;
public class SetupActivity extends AppCompatActivity implements View.OnClickListener {
ActivitySetupFridgeBinding binding;
SearchView searchView;
ListView listView;
Button btn;
String[] ingredients = {"Avocado", "Tomato", "Pasta", "Cauliflower", "Egg", "Salmon", "Chicken", "Beef", "Broccoli", "Cheese", "Zucchini", "Lemon", "Sweet Potato", "Kale", "Carrot", "Black Beans", "Asparagus",
"Spinach", "Rice", "Potato", "Yoyo Beans"};
Boolean[] checkboxes = {false, false, false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
int[] images = {R.drawable.ic_launcher_foreground, R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_setup_fridge);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
listView = findViewById(R.id.list_item);
searchView = findViewById(R.id.searchViewSetup);
btn = (Button) findViewById(R.id.setup_floating_btn);
btn.bringToFront();
btn.setOnClickListener(this);
binding = ActivitySetupFridgeBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
ArrayList<Ingredient> ingredientArrayList = new ArrayList<>();
for (int i = 0; i < images.length; i++) {
Ingredient ingredient = new Ingredient(ingredients[i], images[i], checkboxes[i]);
ingredientArrayList.add(ingredient);
}
ListAdapter listAdapter = new ListAdapter(SetupActivity.this, ingredientArrayList);
binding.listItem.setTextFilterEnabled(true);
binding.listItem.setAdapter(listAdapter);
binding.listItem.setClickable(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
listAdapter.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
listAdapter.getFilter().filter(newText);
return false;
}
});
binding.listItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Ingredient ig = (Ingredient) parent.getItemAtPosition(position);
ig.setIngredientCheckBox(!ig.isIngredientCheckBox());
CheckBox cb = view.findViewById(R.id.ingredientChekcBox);
cb.setChecked(!cb.isChecked());
//cb.setChecked(!cb.isChecked());
}
});
}
#Override
public void onClick(View v) {
v.startAnimation(buttonClick);
if (v == findViewById(R.id.setup_floating_btn))
{
System.out.println("press btn");
String[] ingredientsList = new String[ingredients.length];
int cnt = 0;
for (int i = 0; i<ingredients.length; i++)
{
if (checkboxes[i] == true)
{
ingredientsList[cnt] = ingredients[i];
cnt++;
}
}
System.out.println(ingredientsList);
Intent intent = new Intent (this, MainActivity.class);
intent.putExtra("ingredientsList", ingredientsList);
}
}
private AlphaAnimation buttonClick = new AlphaAnimation(1F, 0.8F);
public class Ingredient
{
private String ingredientTitle;
private int ingredientPhoto;
private boolean ingredientCheckBox;
public Ingredient(String ingredientTitle, int ingredientPhoto, boolean ingredientCheckBox) {
this.ingredientTitle = ingredientTitle;
this.ingredientPhoto = ingredientPhoto;
this.ingredientCheckBox = ingredientCheckBox;
}
public String getIngredientTitle() {
return ingredientTitle;
}
public void setIngredientTitle(String ingredientTitle) {
this.ingredientTitle = ingredientTitle;
}
public int getIngredientPhoto() {
return ingredientPhoto;
}
public void setIngredientPhoto(int ingredientPhoto) {
this.ingredientPhoto = ingredientPhoto;
}
public boolean isIngredientCheckBox() {
return ingredientCheckBox;
}
public void setIngredientCheckBox(boolean ingredientCheckBox) {
this.ingredientCheckBox = ingredientCheckBox;
}
}
public class ListAdapter extends ArrayAdapter<Ingredient>
{
public ListAdapter (Context context, ArrayList<Ingredient> ingredientList)
{
super(context, R.layout.activity_ingredients_layout, ingredientList);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
Ingredient ingredient = getItem(position);
if (convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_ingredients_layout, parent, false);
}
ImageView imageView = convertView.findViewById(R.id.ingredientPhoto);
TextView title = convertView.findViewById(R.id.ingredientTitle);
CheckBox cb = convertView.findViewById(R.id.ingredientChekcBox);
imageView.setImageResource(ingredient.ingredientPhoto);
title.setText(ingredient.ingredientTitle);
cb.setActivated(ingredient.ingredientCheckBox);
return convertView;
}
}
public void finish() {
super.finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
Your way of using equality is wrong. You cannot use == to see if two objects are equal to each other.
If you really want to use equals to verify it's the same object, you should do this instead:
if (v.equals(findViewById(R.id.setup_floating_btn)) {
// Do whatever you need to do when the button is clicked here.
}
While this can work, it's not the recommended way of verifying whether this was the View clicked. Instead you should check the clicked View's ID like this:
if (v.getId() == R.id.setup_floating_btn) {
// Do whatever you need to do when the button is clicked.
}
Have a look at the difference between equals and == here.
On a side-note: you're mixing things together here - ViewBindings with normal findViewById which is really bad and makes things very difficult to read. ViewBindings are meant to make it easier to inflate all views in a screen as well as making all references to views type-safe and making sure no views are null. You should probably read up on this too.
Also in your XML you use sp as units for dividerHeight and padding, which is completely wrong. sp units are used only for text and not for heights or paddings of views.
Also take a look at what bringToFront() does on a View and see if this really should apply/be used on your Button.

EditText inside ListView

I have an EditText inside ListView and I want to enter some integer values in all the EditTexts. I have one Button outside the ListView onClicking. From this button I want to get the data from all the EditText from ListView and save in array to show in toast. Also my EditText id is same for all the EditText. Can any buddy give so sample code so I can proceed to next step. Thanks in advance.
main.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="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listGejalaPilih"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp" >
</ListView>
<Button
android:id="#+id/btnDiagnosis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Diagnosis Penyakit" >
</Button>
</LinearLayout>
Item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textGejalaPilih"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_alignParentLeft="true" />
<EditText
android:id="#+id/editPersenYakin"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="3"
android:layout_alignParentRight="true" >
</EditText>
</RelativeLayout>
Main.java
package id.app.pakarsawit;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class DetailDiagnosis extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] pilihan = {"pilihan 1","pilihan 2","pilihan 3", "pilihan 4"};
ListView listView = (ListView)findViewById(R.id.listGejalaPilih);
Button btn = (Button)findViewById(R.id.btnDiagnosis);
ArrayAdapter<String> adapterPilih = new LVAdapterDetailDiagnosis(this, pilihan);
listView.setAdapter(adapterPilih);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
}
}
LVAdapterDetailDiagnosis.java
package id.app.pakarsawit;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class LVAdapterDetailDiagnosis extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public LVAdapterDetailDiagnosis(Context context, String[] values) {
super(context, R.layout.item);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item, parent, false);
TextView textView = (TextView)rowView.findViewById(R.id.textGejalaPilih);
textView.setText(values[position]);
return rowView;
}
}
try this:
for (int a = 0; a < listGejalaPilih.getCount(); a++) {
EditText et = (EditText) listGejalaPilih.getChildAt(a).findViewById(R.id.editPersenYakin);
arraylist.add(et.getText().toString());
}
Or if you want Array
String string[] = new String[listGejalaPilih.getCount()];
for (int a = 0; a < listGejalaPilih.getCount(); a++) {
EditText et = (EditText) listGejalaPilih.getChildAt(a).findViewById(R.id.editPersenYakin);
string[a] = et.getText().toString();
}

How to impliment the Click events for multiple buttons in a ListView

I have an interface designed as a ListView (but I can change if needed). Each row has 2 buttons: MS and DROP.
Clicking on the buttons should affect only the corresponding row. For example, on the last row (where int "3" is displayed), clicking on MS should call function1(rowPosition) and clicking on DROP should call function2(rowPosition).
I have 2 problems:
Even without any click listener, clicking on any row activate both corresponding buttons. Maybe ListView is not the correct choice for this kind of interface.
I can't figure out how to implement the Click listener.
Here is the main code:
package com.thomas.calculation.app;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class Memory_listview extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memory_listview);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new MyAdapter(this));
}
}
class MyAdapter extends BaseAdapter {
Context context;
ArrayList<String> singleRow_list;
MyAdapter(Context c) {
context = c;
singleRow_list = new ArrayList<String>();
for (int i=1; i<10; i++)
singleRow_list.add(i, Integer.toString(i) );
}
#Override
public int getCount() {
return singleRow_list.size();
}
#Override
public Object getItem(int i) {
return singleRow_list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
class MyViewHolder
{
TextView myTextView;
MyViewHolder(View view) {
myTextView = (TextView) view.findViewById(R.id.textView);
}
}
#Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View view_single_row = view;
MyViewHolder myViewHolder = null;
if (view_single_row==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view_single_row = inflater.inflate(R.layout.memory_listview_row, viewGroup, false);
myViewHolder = new MyViewHolder(view_single_row);
view_single_row.setTag(myViewHolder);
}
else
{
myViewHolder = (MyViewHolder) view_single_row.getTag();
}
String newData = singleRow_list.get(i);
//This is not working:
//Button button_MS = (Button) findViewById(R.id.button_MS); //ERROR: findViewById undefined
//Button button_MS = (Button) this.findViewById(R.id.button_MS); //ERROR: findViewById undefined
//Button button_MS = (Button) view.findViewById(R.id.button_MS); //APP CRASH
//Button button_MS = (Button) view_single_row.findViewById(R.id.button_MS); //APP CRASH
//Button button_MS = (Button) viewGroup.findViewById(R.id.button_MS); //APP CRASH
myViewHolder.myTextView.setText(newData);
return view_single_row;
}
}
Here is the row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/bgStack"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:text=""
style="#style/memory_monitor"
android:weightSum="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/linearView" />
<LinearLayout
android:id="#+id/linearView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<TextView
android:id="#+id/button_MS"
style="#style/memory_tiny"
android:text="#string/calc_MS" />
<TextView
android:id="#+id/button_del"
style="#style/memory_tiny"
android:text="#string/calc_del" />
</LinearLayout>
</RelativeLayout>
Inside your getView.
MyViewHolder.button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// do for the other button too..
// yout methods here.
}
});
And change the TextView to Button in your xml

Soft key not appear while tapping on EditText which is inside listView and listView is in AleartBox

In my application customize Alear Box is there. In this alert box there is a customize ListView in which one TextView and one EditText is there. Every thing work fine and Alert Box coming on the screen perfectly.
But when I tap on Edit text to fill the characters into the Edit Text, Android Softkey on appearing. Therefore I am not able to insert and data into Edit Text.
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"
android:gravity="center"
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=".MainActivity" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Population"
android:onClick="getPopulation" />
</RelativeLayout>
alert_box.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >
<ListView
android:id="#+id/lView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
tools:listitem="#layout/list_view_resourse" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
<Button
android:id="#+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
list_view_resourse.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/district"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textColor="#000000"
android:textStyle="bold" />
<EditText
android:id="#+id/population"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity
package com.exmp;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity implements OnClickListener{
private ArrayAdapter<Disrict> adapter;
private ArrayList<Disrict> disricts;
private ListView lView;
private Button add, cancel;
private AlertDialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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);
return true;
}
private void getPopulation() {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.alert_box, null);
lView = (ListView) view.findViewById(R.id.lView);
add = (Button) view.findViewById(R.id.add);
cancel = (Button) view.findViewById(R.id.cancel);
disricts = new ArrayList<Disrict>();
disricts.add(new Disrict(1, "Bangalore", 45.22));
disricts.add(new Disrict(1, "Gulbarga", 22.22));
adapter = new DistrictAdapter(MainActivity.this, R.layout.list_view_resourse, disricts);
lView.setAdapter(adapter);
cancel.setOnClickListener(this);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Population");
alert.setView(view);
alert.setCancelable(false);
dialog = alert.create();
dialog.show();
}
public void getPopulation(View v){
getPopulation();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
break;
case R.id.cancel:
dialog.dismiss();
break;
}
}
}
Disrict
package com.exmp;
public class Disrict {
private int id;
private String strDistrict;
private double population;
public Disrict(int id, String strDistrict, double population) {
super();
this.id = id;
this.strDistrict = strDistrict;
this.population = population;
}
public int getId() {
return id;
}
public String getStrDistrict() {
return strDistrict;
}
public double getPopulation() {
return population;
}
#Override
public String toString() {
return strDistrict;
}
}
DistrictAdapter
package com.exmp;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class DistrictAdapter extends ArrayAdapter<Disrict> {
private final Context context;
private final int rowResourceId;
private final ArrayList<Disrict> disricts;
public DistrictAdapter(Context context, int resource,
ArrayList<Disrict> disricts) {
super(context, resource, disricts);
this.context = context;
this.rowResourceId = resource;
this.disricts = disricts;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Spinner spinnerPaymentType = null;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(rowResourceId, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.district);
textView.setText(disricts.get(position).getStrDistrict());
return rowView;
}
}
try this code in your listview
<ListView
android:id="#android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:descendantFocusability="beforeDescendants"
/>
There are some problems which help me.
It better to use dynamical views rather than ListView when you have e Focasable view inside of Cell
related links:
EditText inside ListView lost focus,
EditText inside ListView will not stay focused,
Android :EditText loses content on scroll in ListView?
ettext.requestFocus();
ettext.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(ettext, 0);
}
},200);

Android App Fragment Keeps Crashing

I am working on a simple app to convert the temperature. I am trying to figure out how to process a number that a user inputs and then process it and give out the conversion. I searched different places and found part of the following code but my app crashes when I actually click on the button.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
import org.w3c.dom.Text;
public class CelsiusFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.celsiusfragment, container, false);
Button celsiusbutton = (Button) view.findViewById(R.id.button_celsius);
celsiusbutton.setOnClickListener(this);
return view;
}
Button mButton;
EditText mEdit;
TextView mText;
int output;
#Override
public void onClick(View view) {
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int number = 0;
int number1 = number;
output = number1 + 2;
TextView result = (TextView) view.findViewById(R.id.result);
result.setText(String.valueOf(output));
}
});
}
}
Is there a tutorial that you would recommend to accomplish this?
Thanks!
Try to change your code like this:
fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_gravity="center_horizontal" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/calculate_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_gravity="center_horizontal" />
</LinearLayout>
CelsiusFragment.java
public class CelsiusFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
Button calculateButton = (Button) view.findViewById(R.id.calculate_button);
calculateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView result = (TextView) view.findViewById(R.id.result);
EditText value = (EditText) view.findViewById(R.id.value);
int output = Integer.parseInt(value.getText().toString()) + 2;
result.setText(String.valueOf(output));
}
});
return view;
}
}

Categories

Resources