Dictionary app with listview, Button and EditText.
EditText show keyboard in real device on the first click but it is not showing words and
then it doesn't appear again.
I tried many times to find a solution in stack overflow but none of them worked for me.
Any one can help?
Thanks in advance.
public class MainActivity extends AppCompatActivity {
private ListView listView;
List<WordTranslation> quotes;
Button searchBtn;
EditText editText;
DatabaseAccess databaseAccess;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
protected void onStart() {
super.onStart();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
AlertDialog alert = alertDialog.show();
alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchBtn = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.EditText);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
searchBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String word= editText.getText().toString();
databaseAccess.getwordtranslation(word);
}
});
this.listView = (ListView) findViewById(R.id.ListView);
final DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
databaseAccess.getQuotes();
quotes = databaseAccess.ShowDictionaryDetails();
databaseAccess.close();
ArrayAdapter<String> adapter = new Adapter(this, R.layout.activity_list_view_item, quotes);
listView.setAdapter(adapter);
}
xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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="com.example.future.DictionaryEdition.MainActivity"
android:focusableInTouchMode="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/line2"
android:layout_below="#+id/line1">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ListView">
</ListView>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="True"
android:focusableInTouchMode="true"
android:id="#+id/line1">
<!--android:layout_alignRight="#+id/line3">-->
<!--<SearchView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:layout_weight="1"-->
<!--android:id="#+id/search_view"/>-->
<EditText
android:hint="#string/search_word_here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/EditText"
android:focusable="True"
android:focusableInTouchMode="true"
android:layout_weight="3"
android:textColor="#color/colorAccent"
android:inputType="text" />
<Button
android:text="#string/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_weight="1" />
</LinearLayout> </RelativeLayout>
Because as you can see in the xml file you write nothing in the android:text field. so it give you nothing written. So change it.
<EditText
android:text="balabalbla"
android:hint="#string/search_word_here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/EditText"
android:focusable="True"
android:focusableInTouchMode="true"
android:layout_weight="3"
android:inputType="text" />
and write something on android:text="baablaabla"
Related
I want to have a spinner which will contain a list of items.
The design I want is as follow:
I tried to put background for the spinner so now it only shows a background. I want white layout inside and with separator.
I tried to create a layout for the item and apply to the spinner but it gives error :
Process: com.kiranaapp, PID: 16697
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
My design now looks like this:
And onclick of spinner I get layout like this:
But I want the list to be shown onClick of TextInputLayout.
Here is Xml file:
<?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"
android:background="#drawable/bg"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.kiranacustomerapp.Activities.SearchActivity"
tools:showIn="#layout/activity_search">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp"
android:paddingTop="05dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/bg">
<Spinner
android:id="#+id/items_spinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" />
</LinearLayout>
<android.support.design.widget.TextInputEditText
android:layout_width="240dp"
android:layout_height="45dp"
android:id="#+id/editTextItemName"
android:layout_gravity="center_horizontal"
android:hint="#string/item_name"
android:textSize="15sp"
android:padding="10dp" >
</android.support.design.widget.TextInputEditText>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_unit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="05dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<android.support.design.widget.TextInputEditText
android:layout_width="240dp"
android:layout_height="45dp"
android:id="#+id/editTextItemUnit"
android:layout_gravity="center_horizontal"
android:hint="#string/unit"
android:textSize="15sp"
android:padding="10dp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="05dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<android.support.design.widget.TextInputEditText
android:layout_width="240dp"
android:layout_height="45dp"
android:id="#+id/editTextItemQuantity"
android:layout_gravity="center_horizontal"
android:hint="#string/quantity"
android:textSize="14sp"
android:padding="10dp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button
android:layout_width="100dp"
android:layout_height="30dp"
android:text="Select"
style="?android:attr/borderlessButtonStyle"
android:id="#+id/buttonSelect"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"
android:layout_marginBottom="50dp"
android:background="#drawable/btn_hlf_blue"
android:textColor="#android:color/white"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
Activity:
public class SearchActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private TextInputEditText edt_Item_Name,edt_Item_Unit,edt_Item_quantity;
private TextInputLayout textInput_Item_name,textInput_Item_Unit,textInput_Item_quantity;
private Spinner spinner;
private Button btnSelect;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_back);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
setUpUI();
}
public void setUpUI(){
edt_Item_Name = (TextInputEditText) findViewById(R.id.editTextItemName);
edt_Item_quantity = (TextInputEditText)findViewById(R.id.editTextItemQuantity);
edt_Item_Unit = (TextInputEditText)findViewById(R.id.editTextItemUnit);
textInput_Item_name = (TextInputLayout)findViewById(R.id.input_layout_item_name);
textInput_Item_quantity = (TextInputLayout)findViewById(R.id.input_layout_item_quantity);
textInput_Item_Unit = (TextInputLayout)findViewById(R.id.input_layout_item_unit);
spinner = (Spinner)findViewById(R.id.items_spinner);
btnSelect = (Button)findViewById(R.id.buttonSelect);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.items_array, R.layout.order_item_layout);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
textInput_Item_name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
spinner.setVisibility(View.VISIBLE);
edt_Item_Unit.setVisibility(View.GONE);
edt_Item_quantity.setVisibility(View.GONE);
btnSelect.setVisibility(View.GONE);
textInput_Item_name.setBackgroundResource(R.drawable.purple_bg);
}
});
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
How can I achieve this design? Can anyone help with this please? Thank you..
Use ListView or RecyclerView and put a spinner in a layout that will be use in your viewholder.class associated with getView method in adapter class, after-that set your data using ArrayList/Hashmap with the help of constructor of Adapter class.
Follow this
package com.example.spinner;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
class AndroidSpinnerExampleActivity extends Activity implements OnItemSelectedListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Automobile");
categories.add("Business Services");
categories.add("Computers");
categories.add("Education");
categories.add("Personal");
categories.add("Travel");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
Use this layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Category:"
android:layout_marginBottom="5dp"/>
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/spinner_title"/>
</LinearLayout>
Draw your layout somthing like this:
<RelativeLayout
android:id="#+id/spinner_layout"
android:layout_width="match_parent"
android:layout_height="#dimen/dp_30"
android:layout_margin="#dimen/dp_20"
android:background="#color/fragment_bg"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="#dimen/dp_20"
android:hint="#string/select_city"
android:singleLine="tru
android:textSize="#dimen/sp_15"/>
<Spinner
android:id="#+id/spinner_city"
android:layout_width="match_parent"
android:layout_height="#dimen/dp_30"
android:layout_centerVertical="true"
android:entries="#array/StateName"
android:gravity="center"
android:padding="#dimen/sp_15"
app:binding="#{location.locations}"/>
</RelativeLayout>
and use your textinput layout instead of textview
Don't use Edittext use Textview to set clicked item text. "if you use edittext, then you have to tap two times to get call in onClickListener method because on first time of click it set to get focus and open soft keyboard"
I am having a layout with a single editText in it,below the editText I added a add button. Now my question is when I click the add button I need to get another editText below the editText and has to repeat the same.Please anyone help, Thank you.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ll_edit_texts_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD" />
</LinearLayout>
and put following code in your Activity
final LinearLayout llEditTextsContainer = (LinearLayout) view.findViewById(R.id.ll_edit_texts_container);
Button buttonAdd = (Button) view.findViewById(R.id.buttonAdd);
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = new EditText(getActivity());
//editText.setId(); you should set id smartly if you wanted to use data from this edittext
llEditTextsContainer.addView(editText);
}
});
First of all create a container view in your main layout which is going to hold the new Edittexts.
<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.example.vuclip.dynamictextedit.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/addTextView"
android:text="Add EditText"
android:onClick="onClick"
/>
<TableLayout
android:id="#+id/containerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
Here, on pressing the button, new Edittexts will be created and added into the TableLayout.
Now create a new Layout which will hold your edittext(I called this file new_layout.xml).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/newEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Now add this layout into your main activity.
public class MainActivity extends AppCompatActivity {
TableLayout container;
static int rowIndex = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = (TableLayout) findViewById(R.id.containerLayout);
}
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newRowView = inflater.inflate(R.layout.new_layout,null);
container.addView(newRowView,rowIndex);
rowIndex++;
}}
I am trying to simply hide a linear layout if a checkbox is clicked. I have defined function and onClick attribute calls it which is also tested with toast but when I try hiding the layout, it doesn't do anything.
The layout.xml:
<CheckBox
android:id="#+id/cbguest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.9"
android:onClick="itemClicked"
/>
In MainActivity.java
int[] i = {0};
public void itemClicked(View v) {
LinearLayout layoutguest1 = (LinearLayout) findViewById(R.id.guestlayout);
if (lns[0]%2 == 0) {
Toast.makeText(getApplicationContext(),"Visible", Toast.LENGTH_SHORT).show();
layoutguest1.setVisibility(View.VISIBLE);
lns[0]+=1;
}
if (lns[0]%2 != 0) {
layoutguest1.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),"Gone", Toast.LENGTH_SHORT).show();
lns[0]+=1;
}
}
**activity_main.xml
put this code in activity_main.xml file**
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.demo.MainActivity" >
<LinearLayout
android:id="#+id/layoutLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UserName" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<CheckBox
android:id="#+id/chkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hide" />
</RelativeLayout>
**MainActivity.java
put this code in MainActivity.java file**
public class MainActivity extends Activity {
private LinearLayout layoutLogin;
private CheckBox chkBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chkBox = (CheckBox) findViewById(R.id.chkBox);
layoutLogin = (LinearLayout) findViewById(R.id.layoutLogin);
chkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
// TODO Auto-generated method stub
if(isChecked)
{
layoutLogin.setVisibility(View.GONE);
chkBox.setText("UnHide");
}
else
{
layoutLogin.setVisibility(View.VISIBLE);
chkBox.setText("Hide");
}
}
});
}
}
You should be using onCheckedChangeListener as I don't think it registers as an onClick- but an onCheck.
Android: checkbox listener
I'm new to Android, and am currently using a ViewFlipper. I'd like to know if it's possible to use only one ImageView? That way I can just create an Arraylist of my images. I think using multiple ImageViews is not a good practice since I have 50+ images.
public void initContent() {
imageArrayList = new ArrayList<Integer>();
imageArrayList.add(R.drawable.pig2);
imageArrayList.add(R.drawable.pig3);
imageArrayList.add(R.drawable.pig4);
imageArrayList.add(R.drawable.pig5);
imageArrayList.add(R.drawable.pig6);
imageArrayList.add(R.drawable.pig7);
imageArrayList.add(R.drawable.pig8);
imageArrayList.add(R.drawable.pig9);
imageArrayList.add(R.drawable.pig10);
imageArrayList.add(R.drawable.pig11);
imageArrayList.add(R.drawable.pig12);
imageArrayList.add(R.drawable.pig13);
imageArrayList.add(R.drawable.pig14);
imageArrayList.add(R.drawable.pig24);
imageArrayList.add(R.drawable.pig25);
imageArrayList.add(R.drawable.theend);
MainActivity.java
public class MainActivity extends Activity {
ViewFlipper viewFlipper;
Button Next;
private Integer images[] = {R.drawable.ic_launcher,
R.drawable.ic_no_image, R.drawable.calendar52};
ImageView imageView1;
private int currImage = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper) findViewById(R.id.ViewFlipper01);
Next = (Button) findViewById(R.id.Next);
imageView1 = (ImageView) findViewById(R.id.imageView1);
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
currImage++;
if (currImage == 3) {
currImage = 0;
}
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(images[currImage]);
viewFlipper.showNext();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/RelativeLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ViewFlipper
android:id="#+id/ViewFlipper01"
android:layout_width="fill_parent"
android:layout_height="400dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4B0082" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout03"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center" >
<Button
android:id="#+id/Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:text="Next" />
</RelativeLayout>
</LinearLayout>
Yes. Use the frame animation, delete your arraylist. Here is some sample code: Link
Good day everyone,
I'm learning Android development and I try to build some dynamic lists for my application. And I'm stuck... like for 4 hours already.
I have two layouts:
1. main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/action_buttons"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="center"
>
<Button
android:id="#+id/button_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_calculate" />
<Button
android:id="#+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/button_count"
android:text="#string/button_add" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/action_buttons"
>
<LinearLayout
android:id="#+id/action_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
action_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_list_item_root"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/action_list_item_edittext_drinkName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/action_list_item_button_remove"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/action_list_item_title"
android:padding="10dp" />
<EditText
android:id="#+id/action_list_item_edittext_drinkPercent"
android:text="40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/action_list_item_edittext_drinkAmount"
android:inputType="number"
android:padding="10dp"/>
<EditText
android:id="#+id/action_list_item_edittext_drinkAmount"
android:text="0.0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/action_list_item_button_remove"
android:layout_alignBottom="#+id/action_list_item_button_remove"
android:inputType="numberDecimal"
android:width="50dp"
android:padding="10dp" />
<Button
android:id="#+id/action_list_item_button_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/action_list_item_button_remove" />
And code that creates dynamic list:
public class MainActivity extends Activity {
LinearLayout actionList = null;
private Button btnAdd;
private Button btnCalculate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initActivityElements();
}
private void initActivityElements() {
initAddButton();
initCalculateButton();
}
private void initCalculateButton() {
btnCalculate = (Button) findViewById(R.id.button_count);
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView amount = (TextView) findViewById(R.id.action_list_item_edittext_drinkAmount);
//this retrives TextView value from first list
Toast.makeText(getApplicationContext(), amount.getText().toString(),
Toast.LENGTH_SHORT)
.show();
}
});
}
private void initAddButton() {
actionList = (LinearLayout) findViewById(R.id.action_list);
btnAdd = (Button) findViewById(R.id.button_add);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout listItem = (RelativeLayout) View.inflate(
MainActivity.this, R.layout.action_list_item, null);
TextView name = (TextView) listItem
.findViewById(R.id.action_list_item_edittext_drinkName);
name.setText("Here is the Title " + actionList.getChildCount());
listItem.findViewById(R.id.action_list_item_button_remove)
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
actionList.removeView((View) v.getParent());
}
});
actionList.addView(listItem);
}
});
}
My problem is that I need to get all data from TextView boxes (Amount, Percent), but I can retrive that data only from first item of a list (look at onClickListener for btnCalculate) and can't figure out how to do it, but tried to add 'unique ids' for view (but that brakes layout, badly), tried setting Tags but again with no luck.
Maybe anyone can give a tip? I bet there is some easy way to do it, but I'm failing to find it and google is no help here.
Thanks
I'm not really sure what exactly you are trying to do but it sounds like you are trying to take in information and populate it into a view. I would recommend having a static text box that takes in your information and builds that into a listview. A good example of how to do that is located here http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html.