i`m trying to do android viewflipper. i have 3 views i would to be changed on button click (next & previous). i wrote these code and the log said there is button widget error. How to solve this?
my xml layout
<?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"
android:background="#drawable/radial_background">
<RelativeLayout
android:id="#+id/relative_buy1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/buy_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true">
</Button>
<Button
android:id="#+id/buy_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/buy_previous">
</Button>
<Button
android:id="#+id/buy_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/buy_next">
</Button>
</RelativeLayout>
<RelativeLayout
android:layout_alignTop="#id/relative_buy1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<ViewFlipper
android:id="#+id/ViewFlipper01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/buy_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
</TextView>
<EditText
android:id="#+id/buy_item_name_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#id/buy_item_name">
</EditText>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/buy_ques_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
</TextView>
<Button
android:id="#+id/buy_food_cat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#id/buy_ques_category">
</Button>
<Button
android:id="#+id/buy_education_cat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#id/buy_food_cat_button">
</Button>
<Button
android:id="#+id/buy_personal_cat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#id/buy_education_cat_button">
</Button>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/buy_ques_cost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
</TextView>
<EditText
android:id="#+id/buy_ques_cost_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#id/buy_ques_cost">
</EditText>
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
my activity
package apps.questions;
import android.app.Activity;
import android.content.ContentValues;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ViewFlipper;
import apps.project.R;
public class BuyActivity extends Activity implements OnClickListener {
ViewFlipper vf;
private static final String FOOD = "Food/Drinks";
private static final String EDUCATION = "Education";
private static final String PERSONAL = "Personal";
Button food_cat;
Button education_cat;
Button personal_cat;
Button next;
Button previous;
Button submit;
TextView item_name;
TextView item_cost;
TextView item_cat;
EditText name;
EditText cost;
ContentValues cv;
String iName;
double iPrice;
String iCategory;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buy_advice_layout);
inflateView();
/*food_cat.setOnClickListener(this);
education_cat.setOnClickListener(this);
personal_cat.setOnClickListener(this);*/
}
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
private void inflateView() {
// TODO Auto-generated method stub
cv = new ContentValues();
vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);
next = (Button)findViewById(R.id.buy_next);
previous = (Button)findViewById(R.id.buy_previous);
submit = (Button)findViewById(R.id.buy_submit);
/*food_cat = (Button)findViewById(R.id.buy_food_cat_button);
education_cat = (Button)findViewById(R.id.buy_education_cat_button);
personal_cat = (Button)findViewById(R.id.buy_personal_cat_button);*/
item_name = (TextView)findViewById(R.id.buy_item_name);
item_name.setText("Please enter the name of the item you wish to buy");
item_cat = (TextView)findViewById(R.id.buy_ques_category);
item_cat.setText("Please select the category of the item you wish to buy by clicking on the category button");
item_cost = (TextView)findViewById(R.id.buy_ques_cost);
item_cost.setText("Please enter the price of the item you wish to buy");
name = (EditText)findViewById(R.id.buy_item_name_et);
cost = (EditText)findViewById(R.id.buy_ques_cost_et);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == next) {
vf.setAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.push_left_in));
vf.showNext();
}
if (v == previous) {
vf.setAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.push_left_out));
vf.showPrevious();
}
if (v == submit) {
//vf.showPrevious();
}
if (v == food_cat) {
iCategory = FOOD;
}
if (v == education_cat) {
iCategory = EDUCATION;
}
if (v == personal_cat) {
iCategory = PERSONAL;
}
}
}
It looks like you are missing the assignment of your click listeners to your buttons. Use the following code just below your find view by id calls for the buttons.
next = (Button)findViewById(R.id.buy_next);
previous = (Button)findViewById(R.id.buy_previous);
submit = (Button)findViewById(R.id.buy_submit);
//New code
next.setOnClickListener(this);
previous.setOnClickListener(this);
submit.setOnClickListener(this);
Related
I've got few problems in using animation in android studios:
I'm trying to translate certain images at an angle. Like in the image below I want to move that attachment pin like icons to all the ends of the corner. How can I do this?
I was trying to scale up the image of the gear image in the center every time its tapped and scale down when tapped again. However, the image starts losing its quality. How can I prevent the quality from getting deteriorated?
Here's the code:
MainActivity.java
package com.rootonelabs.vicky.pulse;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import com.gigamole.library.PulseView;
public class MainActivity extends AppCompatActivity {
PulseView pulseView;
Button btnStart, btnStop;
ImageView gear;
ImageView icon1,icon2,icon3,icon4,icon5,icon6;
int chkClick = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pulseView = (PulseView)findViewById(R.id.pv);
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pulseView.startPulse();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pulseView.finishPulse();
}
});
gear = (ImageView)findViewById(R.id.gear);
gear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
gear.animate().scaleX(1.5f).scaleY(1.5f).setDuration(500);
animateDiagonalPan();
icon1 = findViewById(R.id.icon1);
icon1.animate().translationXBy(120).translationYBy(120).setDuration(500);
if(chkClick==0)
{
pulseView.animate().alpha(0).setDuration(500);
gear.animate().scaleX(1.5f).scaleY(1.5f).setDuration(500);
pulseView.finishPulse();
chkClick = 1;
}else{
pulseView.animate().alpha(1f).setDuration(500);
gear.animate().scaleX(1f).scaleY(1f).setDuration(500);
pulseView.startPulse();
chkClick = 0;
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f39c12"
tools:context="com.rootonelabs.vicky.pulse.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnStart"
android:layout_weight="1"
android:text="START"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btnStop"
android:layout_weight="1"
android:text="STOP"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:id="#+id/gear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/men3"/>
<ImageView
android:id="#+id/icon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_video"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_archive"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_attachment_black_24dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_video"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_archive"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_attachment_black_24dp"/>
<com.gigamole.library.PulseView
android:id="#+id/pv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:pv_alpha="70"
app:pv_color="#FFF"
app:pv_icon="#drawable/men2"
app:pv_icon_width="100dp"
app:pv_icon_height="100dp"
app:pv_interpolator="#android:anim/linear_interpolator"
app:pv_measure="height"
app:pv_spawn_period="1000" />
</RelativeLayout>
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 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));
}
}
The app executed just fine, and the interface loads just fine as well, but when I try to press any of the buttons, nothing that should happen happens. The point is fairly simple: press the '+' button, the quantity increases, and the price immediately updates to match said quantity, vice versa for the '-' button. I don't know what I've done wrong, but any button interaction with the app crashes it.
Here's my Java:
package com.t99sdevelopment.mobile.eyy;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public int quantityValue = 1;
public void increaseQuantity (View view) {
quantityValue = quantityValue + 1;
updateQuantityValue(view);
}
public void decreaseQuantity (View view) {
quantityValue = quantityValue - 1;
updateQuantityValue(view);
}
public void updateQuantityValue(View view) {
updateQuantity(quantityValue);
updatePrice(quantityValue);
}
private void updateQuantity(int number) {
TextView quantity = (TextView) findViewById(
R.id.quantityValue);
quantity.setText(number);
}
private void updatePrice(int number) {
TextView price = (TextView) findViewById(R.id.priceValue);
price.setText(NumberFormat.getCurrencyInstance().format(number * 5));
}
}
Here's my XML:
<TextView
android:text="Quantity"
android:id="#+id/quantityText"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:autoText="false"
android:paddingBottom="20dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal">
<TextView
android:text="0"
android:id="#+id/quantityValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/quantityValue"
android:layout_toStartOf="#+id/quantityValue"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:onClick="decreaseQuantity" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/quantityValue"
android:layout_toEndOf="#+id/quantityValue"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:onClick="increaseQuantity" />
</RelativeLayout>
<TextView
android:text="Price"
android:id="#+id/priceText"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:autoText="false"
android:paddingTop="20dp" />
<TextView
android:text="$0"
android:id="#+id/priceValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:paddingBottom="20dp" />
<Button
android:text="Order"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The formatting of the actual code is just fine, but it got screwy when I pasted in into stack, so I know that's not the problem.
Change you updateQuantity and updatePrice method like :
private void updateQuantity(int number) {
TextView quantity = (TextView) findViewById(
R.id.quantityValue);
quantity.setText(String.valueOf(number));
}
private void updatePrice(int number) {
TextView price = (TextView) findViewById(R.id.priceValue);
price.setText(String.valueOf(NumberFormat.getCurrencyInstance().format(number * 5)));
}
}
This is the xml code:
<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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="eBay Search"
android:textSize="13pt"
android:textStyle="bold"
android:textColor="#000099"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</TextView>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:id="#+id/tableLayout">
<TableRow
android:layout_marginTop="15dp">
<TextView
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="Keyword:"
android:textSize="10pt"
android:layout_weight="2">
</TextView>
<EditText
android:id="#+id/keyword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
>
</EditText>
</TableRow>
<TableRow
android:layout_marginTop="15dp">
<TextView
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="Price From:"
android:textSize="10pt"
android:layout_weight="2">
</TextView>
<EditText
android:id="#+id/from"
android:layout_width="0dp"
android:inputType="numberDecimal"
android:layout_height="wrap_content"
android:layout_weight="3"
>
</EditText>
</TableRow>
<TableRow
android:layout_marginTop="15dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Price To:"
android:textSize="10pt"
android:layout_weight="2">
</TextView>
<EditText
android:id="#+id/to"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="3"
android:inputType="numberDecimal"
android:layout_width="0dp"
>
</EditText>
</TableRow>
<TableRow
android:layout_marginTop="15dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Sort By:"
android:textSize="10pt"
android:layout_weight="2">
</TextView>
<Spinner
android:id="#+id/sortby"
android:layout_height="wrap_content"
android:prompt="#string/op1"
android:layout_width="fill_parent"
android:entries="#array/sortlist">
</Spinner>
</TableRow>
<TableRow
android:layout_marginTop="15dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="#+id/Clear"
android:onClick="clearForm"
android:layout_marginLeft="50dp"
/>
<Button
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="Search"
android:textColor="#color/black"
android:id="#+id/search"
android:layout_marginLeft="50dp"
/>
</TableRow>
</TableLayout>
<TextView
android:id="#+id/keyerror"
android:layout_marginTop="420dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="7pt"
android:textColor="#color/red"
>
</TextView>
<TextView
android:id="#+id/priceerror"
android:layout_marginTop="445dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="7pt"
android:textColor="#color/red"
>
</TextView>
This is the mainactivity code which has onclicklistener method for the search button:
package com.example.hrishikesh.hw9;
import android.app.AlertDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.net.URLEncoder;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button search = (Button)findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int error=0;
EditText text1 = (EditText) findViewById(R.id.keyword);
EditText text2 = (EditText) findViewById(R.id.from);
EditText text3 = (EditText) findViewById(R.id.to);
TextView keyerror = (TextView) findViewById(R.id.keyerror);
TextView priceerror = (TextView) findViewById(R.id.priceerror);
String keyword = text1.getText().toString();
String from = text2.getText().toString();
String to = text3.getText().toString();
String reg = "^[0-9]+$";
if (keyword == null || keyword.length() < 1 || keyword.trim().length() < 1) {
keyerror.setText("Enter a valid keyword");
error=1;
}
else
{
keyerror.setText("");
error=0;
}
if(to.matches(reg) || to.length()==0)
{
}
else
{
priceerror.setText("Enter a valid positive number for maximum price");
error=1;
}
if(from.matches(reg) || from.length()==0)
{
}
else
{
priceerror.setText("Enter a valid positive number for minimum price");
error=1;
}
if(from.length()>0 && to.length()>0)
if(Integer.parseInt(from) - Integer.parseInt(to)>=0){
priceerror.setText("Minimum price has to be less than maximum price");
error=1;
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void clearForm(View v){
EditText text1 = (EditText)this.findViewById(R.id.keyword);
EditText text2 = (EditText)this.findViewById(R.id.from);
EditText text3 = (EditText)this.findViewById(R.id.to);
if (text1 != null) text1.setText("");
if (text2 != null) text2.setText("");
if (text3 != null) text3.setText("");
Spinner spin1 = (Spinner)this.findViewById(R.id.sortby);
spin1.setSelection(0);
}
public void validate(View v){
}
public void search(String keywords,int from,int to,String sortorder){
String url;
int i=0;
url = "http://hrishisaraf-env.elasticbeanstalk.com/";
}
}
Whenever the onclick method gets executed and an error is detected like the keyword being blank. The error text is set and the Search button becomes blank.
No idea why but Setting the width to wrap_content solved the problem.