I have some problem for understanding spinner in android
strings.xml
<string-array name="pickformula">
<item>add</item>
<item>multiply</item>
</string-array>
xml file
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner1"
android:entries="#array/pickformula"
android:layout_marginLeft="10dp"/>
<Spinner
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="calc" />
<EditText
android:id="#+id/input1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="number"/>
<EditText
android:id="#+id/input2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="number"/>
Java
package com.aururatech.dropvoltage3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
}
}
This is like simple calculator.
If I pick add in spinner, the formula when I press the button is = input1+input2
If I pick multiply in spinner, the formula when I press the button is = input1*input2
I need the java code for that example..
Thanks
Please follow these code to solve your problem. In this i create a calculator according to you. All operations perform by spinner change.
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Spinner operation;
Button calculate;
EditText input1;
EditText input2;
TextView result;
String val;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
operation = findViewById(R.id.spinner1);
calculate = findViewById(R.id.button1);
input1 = findViewById(R.id.input1);
input2 = findViewById(R.id.input2);
result = findViewById(R.id.textView);
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String val1 = input1.getText().toString();
String val2 = input2.getText().toString();
if (val.equals("add")) {
result.setText(String.valueOf(Integer.parseInt(val1) + Integer.parseInt(val2)));
}
if (val.equals("sub")) {
result.setText(String.valueOf(Integer.parseInt(val1) - Integer.parseInt(val2)));
}
if (val.equals("multiply")) {
result.setText(String.valueOf(Integer.parseInt(val1) * Integer.parseInt(val2)));
}
if (val.equals("divide")) {
result.setText(String.valueOf(Integer.parseInt(val1) / Integer.parseInt(val2)));
}
}
});
operation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
val = operation.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
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:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner1"
android:entries="#array/pickformula" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="calc" />
<EditText
android:id="#+id/input1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner1"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:inputType="number" />
<EditText
android:id="#+id/input2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/input1"
android:layout_marginTop="28dp"
android:inputType="number" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp" />
</RelativeLayout>
string.xml
<resources>
<string-array name="pickformula">
<item>add</item>
<item>sub</item>
<item>multiply</item>
<item>divide</item>
</string-array>
</resources>
Related
Working in Android studio and as part of a bigger app. I will have an ArrayList of Integers and want to send this to a new instance/screen where each integer in the list displays a string description from which the user can pick whatever entry they want. Once they click it, I want the new instance to send back the integer corresponding to the position in the array of their choice so that I can use it for the next operation.
I made this program as a stand-alone just to get this operation down, but it fails to work (ListChooser has stopped working on load)
my MainActivity.java:
package com.example.kenn.listchooser;
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.hardware.camera2.TotalCaptureResult;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.app.ListActivity;
public class MainActivity extends ListActivity {
private static final String TOTAL_COUNT = "total_count";
public ArrayList<Integer> listValues;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listValues= new ArrayList<Integer>();
listValues.add(1);
listValues.add(2);
listValues.add(4);
listValues.add(5);
}
public int randomMe(View view) {
Intent randomIntent = new Intent(this, Chooser.class);
randomIntent.putExtra(TOTAL_COUNT, listValues);
startActivity(randomIntent);
int pick=getIntent().getIntExtra (TOTAL_COUNT, 2);
return pick;
}
public void action(View view) {
TextView showCountTextView = (TextView) findViewById(R.id.textViewA);
showCountTextView.setText(Integer.toString(this.randomMe(view)));
} }
my Chooser.java:
package com.example.kenn.listchooser;
import ...
public class Chooser extends ListActivity{
private String TOTAL_COUNT = "total_count";
private TextView text;
private ArrayList<Integer> listHere;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chooser);
List<Integer> def = new ArrayList<Integer>();
listHere=getIntent().getIntegerArrayListExtra(TOTAL_COUNT);
text = (TextView) findViewById(R.id.mainText);
// initiate the listadapter
ArrayAdapter<Integer> myAdapter = new ArrayAdapter <Integer>(this,
R.layout.row_layout, R.id.listText, listHere);
// assign the list adapter
setListAdapter(myAdapter);
}
// when an item of the list is clicked
#Override
protected void onListItemClick(ListView list, View view, int position, long id) {
super.onListItemClick(list, view, position, id);
String selectedItem = (String) getListView().getItemAtPosition(position);
Intent randomIntent = new Intent(this, com.example.kenn.listchooser.MainActivity.class);
randomIntent.putExtra(TOTAL_COUNT, selectedItem);
} }
my activity main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="#color/colorPrimary"
tools:context=".MainActivity">
<Button
android:id="#+id/random_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="24dp"
android:layout_marginTop="8dp"
android:background="#color/colorPrimaryDark"
android:onClick="action"
android:text="Choose"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/textViewA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:text="Alert Dialog"
android:textSize="35dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
my chooser.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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainText"
android:text="My list" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mainText"
android:id="#android:id/list"
android:background="#aaaaaa" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mainText"
android:id="#android:id/empty"
android:text="There is no data"
android:textStyle="bold" />
</RelativeLayout>
my row_layout.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" >
<TextView
android:id="#+id/listText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#ff00ff" />
</LinearLayout>
**I am making some app and cant see a button that I have created (this is when running the app on my phone) ,but if I run it on the emulator I can see everything - the button that I can't see on my phone is the clicked button (its id is "startBtn").
**
Here is my 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"
android:id="#+id/activity_main"
android:background="#798"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="unleashed.myprefs.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="90dp"
android:text="hello"
android:background="#ff14"
/>
<Button
android:id="#+id/startBtn"
android:text="start"
android:layout_below="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addOne"
/>
<Button
android:id="#+id/resetBtn"
android:text="restart"
android:layout_below="#+id/textView"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="restart"
/>
</RelativeLayout>
And this is my main activity
package unleashed.myprefs;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button restartNums;
SharedPreferences prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("main", MODE_PRIVATE);
}
private int getNum(){
return prefs.getInt("num",1);
}
private void setNum(int num){ // put the number into my shared preference
prefs.edit().putInt("num", num).apply();
}
public void addOne(View v) { //adding number to the starting button
int num = getNum();
((Button)v).setText("Clicked " + num);
setNum(num + 1);
}
public void restart(View v){
restartNums = (Button) findViewById(R.id.startBtn);
prefs.edit().remove("num").apply();
restartNums.callOnClick();
}
}
I will appreciate any help.
AlignParentEnd should be used in ResetButton.( Not sure )
(Alternative) If you trying to have two buttons in the same row, you can try this layout..
`
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="90dp"
android:text="hello"
android:background="#ff14"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation = "horizontal"
android:weightsum="2"
android:layout_below="#+id/textView">
<Button
android:id="#+id/startBtn"
android:text="start"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="addOne"
/>
<Button
android:id="#+id/resetBtn"
android:text="restart"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="restart"/>
`
My problem is that once my editText fields are created, I don't know how to remove them one by one with the other button in my app. I don't really understand the android:id element as it relates to setID() in my java code. I guess my main question is, how do I figure out how to target dynamically created editText views with their id's if I don't know their id's. Here is my code, any help is really appreciated:
Java:
package com.zdowning.decisionmaker;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ScrollView;
import android.widget.Toast;
import java.lang.*;
import static com.zdowning.decisionmaker.R.layout.activity_main;
public class MainActivity extends AppCompatActivity {
public int numberOfLines = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getAnswer();
}
});
final Button add_button = (Button) findViewById(R.id.add_button);
add_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Add_Line();
}
});
final Button remove_button = (Button) findViewById(R.id.remove_button);
remove_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Remove_Line();
}
});
}
public void Add_Line() {
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setId(numberOfLines++);
et.setText("Enter Answer #" + numberOfLines++);
ll.addView(et);
numberOfLines++;
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void Remove_Line() {
}
public void getAnswer() {
String[] options = new String[numberOfLines];
EditText text = (EditText)findViewById(R.id.editText2);
options[0] = text.getText().toString();
text = (EditText)findViewById(R.id.editText3);
options[1] = text.getText().toString();
text = (EditText)findViewById(R.id.editText4);
options[2] = text.getText().toString();
int number = (int)(Math.random() * 3);
String answer = options[number];
TextView answerBox = (TextView)findViewById(R.id.textView7);
answerBox.setText(answer);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="#d4cfcd">
tools:context="com.zdowning.decisionmaker.MainActivity">
<LinearLayout
android:id="#+id/linearLayoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Your Question Here"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/>
<LinearLayout
android:id="#+id/linearLayoutDecisions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #2" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #1" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="#+id/add_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="+"
android:textColor="#android:color/background_light"
android:textSize="18sp"/>
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:id="#+id/remove_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="-"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:layout_margin="10dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="DECIDE!"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:layout_centerInParent="true" />
</RelativeLayout>
<TextView
android:id="#+id/textView7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="20dp"
android:textColor="#android:color/black"
android:textSize="36sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
There is no relationship between android:id and your setID.
Check this answer to unsderstand more about that.
Anyway, one approach to achieve to remove a generated line could be based on the children index of your LinearLayout like this:
private LinearLayout mEditTextContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
mEditTextContainer = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
...
}
public void addLine() {
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Enter Answer #" + (mEditTextContainer.getChildCount()+1));
mEditTextContainer.addView(et);
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void removeLine() {
mEditTextContainer.removeViewAt(mEditTextContainer.getChildCount()-1);
}
I have one textView and two EditText when i am clicking on the plus button the textView and the two EditText should be added ,and i am able to do that .My problem is the when i am adding the TextView the text should be set as 1 then when the TextView is added again then the text should be set as 2 and so on .i am not able set the text by using t1.setText(i).Here is my code-
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
Button btnDisplay;
ImageButton btnAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = (ImageButton) findViewById(R.id.btnAdd);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
MyLayoutOperation.add(this, btnAdd);
MyLayoutOperation.display(this, btnDisplay);
}
#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;
}
}
MyLayoutOperation.java
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MyLayoutOperation {
public static void display(final Activity activity, Button btn)
{
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout scrollViewlinerLayout = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
java.util.ArrayList<String> msg = new ArrayList<String>();
for (int i = 0; i < scrollViewlinerLayout.getChildCount(); i++)
{
LinearLayout innerLayout = (LinearLayout) scrollViewlinerLayout.getChildAt(i);
TextView t2=(TextView)innerLayout.findViewById(R.id.t1);
t2.setText(i);
EditText e1=(EditText)innerLayout.findViewById(R.id.e1);
EditText edit = (EditText) innerLayout.findViewById(R.id.editDes);
msg.add(t2.getText().toString());
msg.add(e1.getText().toString());
msg.add(edit.getText().toString());
}
Toast t = Toast.makeText(activity.getApplicationContext(), msg.toString(), Toast.LENGTH_SHORT);
t.show();
}
});
}
public static void add(final Activity activity, ImageButton btn)
{
final LinearLayout linearLayoutForm = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final LinearLayout newView = (LinearLayout)activity.getLayoutInflater().inflate(R.layout.rowdetail, null);
newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
btnRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayoutForm.removeView(newView);
}
});
linearLayoutForm.addView(newView);
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutTeste"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/textView1"
android:layout_width="95dp"
android:layout_height="fill_parent"
android:layout_alignBottom="#+id/btnAdd"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/btnAdd"
android:gravity="center_vertical|center_horizontal"
android:text="#string/titleTecnologies"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageButton android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/btnAdd"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
<ScrollView android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="275dp">
<LinearLayout android:id="#+id/linearLayoutForm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<Button android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnDisplay" />
</LinearLayout>
rowdetails.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowdetail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_marginLeft="10dp"
android:id="#+id/t1"/> <requestFocus />
<EditText android:id="#+id/e1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.62"
android:ems="10"
android:inputType="text"> <requestFocus />
</EditText>
<EditText android:id="#+id/editDes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.62"
android:ems="10"
android:inputType="text"> <requestFocus />
</EditText>
<ImageButton android:id="#+id/btnRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/btnRemove"
android:src="#android:drawable/ic_delete" />
</LinearLayout>
Got the answer just do
t2.setText(i+".");
I have tried many answers already given to this, but nothing seems to work properly. And the answers are only for changing state of the button, not colour and text. What am I missing?
I am a very VERY new learner to both Android and programming. And this is my first question on Stack Overflow. Hope it's as per guidelines.
I have a login page which looks like below (img1) [disabled][1]. If textfield has any value, the button should get enabled (img2) [enabled][2]. The Java and XML files are given below.
package io.kaapi.kaapimobileassistant.Activities;
import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.TextView;
import io.kaapi.kaapimobileassistant.Misc.StorageManager;
import io.kaapi.kaapimobileassistant.R;
public class LoginActivity extends AppCompatActivity {
private final static String TAG = "LoginActivity";
private Button login_button;
private TextInputLayout login_activation_layout;
private EditText login_activation_code;
private LinearLayout login_signup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_button = (Button) findViewById(R.id.login_button);
login_activation_layout = (TextInputLayout) findViewById(R.id.login_acitivation_layout);
login_activation_code = (EditText) findViewById(R.id.login_activation_code);
login_signup = (LinearLayout) findViewById(R.id.login_signup);
login_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG, "Login pressed");
String activation_code_layout = login_activation_layout.getEditText().getText().toString();
Log.v(TAG, "Layout "+activation_code_layout);
String activation_code = login_activation_code.getText().toString();
Log.v(TAG, "Code "+activation_code);
if(activation_code.equalsIgnoreCase("")){
Log.v(TAG, "It's blank");
login_activation_layout.setError("Please enter an activation code");
} else {
Log.v(TAG, "Call login API, validate and show errors or login");
//StorageManager.write(LoginActivity.this, null, "client_domain", "http://ankit50.kaapi.io");
//StorageManager.write(LoginActivity.this, null, "client_logo", "http://cdn.kaapi.io/static");
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
}
}
});
login_activation_code.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
enableSubmitIfReady();
}
#Override
public void afterTextChanged(Editable s) {
}
public void enableSubmitIfReady() {
Button login_button = (Button) findViewById(R.id.login_button);
if(login_activation_code.toString().trim().length()==0){
login_button.setEnabled(false);
} else {
login_button.setEnabled(true);
}
}
});
login_signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri = Uri.parse("https://business.kaapi.io");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
The XML file is below
<?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"
android:background="#android:color/white"
android:gravity="center_vertical|center_horizontal"
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="io.kaapi.kaapimobileassistant.Activities.LoginActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="71dp"
android:layout_marginRight="71dp"
android:layout_marginTop="66dp"
android:src="#drawable/kaapi_logo_login"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="28dp"
android:textStyle="bold"
android:gravity="center"
android:textColor="#color/colorTitle"
android:text="Activate Mobile Assistant"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="36dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorText"
android:src="#drawable/ic_info_outline_black_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorText"
android:textSize="14sp"
android:textStyle="italic"
android:layout_marginLeft="5dp"
android:text="The code was sent to you in sign up email and your web dashboard." />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/login_acitivation_layout"
android:layout_width="match_parent"
android:layout_marginTop="29dp"
android:layout_height="wrap_content">
<EditText
android:id="#+id/login_activation_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:maxLength="20"
android:hint="Activation code"
android:maxLines="1"
android:textSize="14sp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
style="#style/Widget.AppCompat.Button"
android:text="Activate"/>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="New to Kaapi? " />
<LinearLayout
android:id="#+id/login_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|top"
android:text="Sign up first"
android:textColor="#color/colorPrimary"
android:textSize="12sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
try this
public void enableSubmitIfReady() {
Button login_button = (Button) findViewById(R.id.login_button);
if(login_activation_code.toString().trim().length()==0){
login_button.setClickable(false);
login_button.setBackgroundColor(getResources().getColor(R.color.holo_light_green));// change color here so it's look like button disable
} else {
login_button.setClickable(true);
login_button.setBackgroundColor(getResources().getColor(R.color.holo_dark_green));
}
}