I am trying to write my first app- a simple application where the user enters a hex color code (EditText), hits enter (Button), and the background color of a ImageView is changed to the user's entered hex code. How I see it, I will have to gettext from the edittext, write the gettext to a file, then edit the file to append 0xAA before the hex code so that it can be entered in ImageView.setBackgroundColor(0xAA"HEXHEX"). Please let me know how I can do this, or if that is even the correct way to do this.
Here is my java so far (on button click, bg color changes to white, clear reverts it to black)
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
public class ChkhexActivity extends Activity {
private EditText hex;
private Button chk;
private Button clear;
private ImageView view;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
hex = (EditText)findViewById(R.id.hex);
chk = (Button)findViewById(R.id.chk);
clear = (Button)findViewById(R.id.clear);
view = (ImageView)findViewById(R.id.view);
chk.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ Chk(); }});
clear.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ Clear(); }});
}
private void Chk()
{
view.setBackgroundColor(0xFFffffff);
}
private void Clear()
{
hex.setText("");
view.setBackgroundColor(0xFF000000);
}
}
Very good exercise for a beginner.
Use Color.parseColor(). Don't forget to validate the Input first!
view.setBackgroundColor(Color.parseColor(edt.getText().toString()));
every time u want to change the color of imageview based on text entered in edittext.so u cant fix it like this
view.setBackgroundColor(0xFFffffff);
u have to get the text from edittext.some example like this..
public class test extends Activity{
private EditText ed;
private Button btn;
private ImageView iv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ed=(EditText)findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button1);
iv=(ImageView)findViewById(R.id.imageView1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String value=ed.getText().toString();
iv.setBackgroundColor(Color.parseColor(value));
}
});
}
}
the edittext text u entered can be like hex format example like #B0171F,#fafad2,..
Related
I am new to coding.I want to code
in order to go to second activity using a button,
an editText and a pasdword in case it maches.
But it does n`t work.Please help me.Following is my code:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.content.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText eText= (EditText)findViewById(R.id.mainEditText1);
String myCode=eText.getText().toString();
Button nextPage= (Button)findViewById(R.id.mainButton1);
if(myCode.equals("Titan")){
nextPage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v){
startActivity(new Intent(MainActivity.
this,actor.class));
}
});
}
}
}
Move your condition and getText() call in listener
nextPage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String myCode=eText.getText().toString();
if(myCode.equals("Titan")){
startActivity(new Intent(MainActivity.
this,actor.class));
}
}
}
So, here's what's happening:
You have an EditText field in your app, where the user types their password.
You capture that EditText in your code using EditText eText = ...
You then check to see if eText.equals("Titan");
If it matches Titan, you see the onClick listener.
But, when this code is ran, the EditText is always empty, so you never set the onClick listener!
The fix is fairly straightforward:
nextPage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
String myCode=eText.getText().toString();
if(myCode.equals("Titan")) {
startActivity(new Intent(MainActivity.this,actor.class));
}
}
});
Now, when the button is clicked:
You capture the value of eText as myCode.
You check if myCode is equal to Titan
If true (ie it matches), you then run startActivity(new Intent(MainActivity.this,actor.class));
You have written class name actor.class starting with a smaller case. Make it starts with uppercase Actor.class. Bellow is the answer to your questions.
Button nextPage = (Button)findViewById(R.id.mainButton1);
nextPage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v){
String myCode=eText.getText().toString();
if(myCode.equals("Titan"))
{
startActivity(new Intent(MainActivity.
this,Actor.class));
}else{
//password not matching
}
});
I'm just a beginner in coding using Android studio and I have a couple of questions in coding a calculator (The calculator is used to only compute carbon emissions so it varies a little bit compared to an actual calculator):
How do I have a clear all button?
How do I add all of the
products that the user has calculated and show its sum? (Kind of like
how those tracking expenses kind of app)
This is my code so far:
package com.carschoolingpromo.scires.carschooling;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Calculator extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
Button caremissionscheck = findViewById(R.id.checkcar);
caremissionscheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent tocarbonemissions = new Intent(Calculator.this, CarbonEmissions.class);
startActivity(tocarbonemissions);
}
});
Button distancecheck = findViewById(R.id.checkdestination);
distancecheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent checkdistance = new Intent(Calculator.this, DistancesList.class);
startActivity(checkdistance);
}
});
final Button multiply =(Button)findViewById(R.id.multiply);
final EditText num1 =(EditText)findViewById(R.id.carboninput);
final EditText num2 =(EditText)findViewById(R.id.distanceinput);
final TextView ans =(TextView)findViewById(R.id.answer);
final TextView carbontotal=(TextView)findViewById(R.id.sumofcarbon);
multiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
double n1 = Double.parseDouble(num1.getText().toString());
double n2 = Double.parseDouble(num2.getText().toString());
ans.setText(String.valueOf(n1*n2));
}
});
}
}
How do I have a clear all button?
You need to create a new button in the layout file, and get a reference of it in your code, then set a click listener on that reference. Then in the onClick() implementation you will clear your 2 edit texts and 2 text views, something like this:
final Button clearAll =(Button)findViewById(R.id.clear_all);
clearAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num1.setText("");
num2.setText("");
ans.setText("");
carbontotal.setText("");
}
});
How do I add all of the products that the user has calculated and show its sum? (Kind of like how those tracking expenses kind of app)
For this you need to use some sort of a way to show a list on your UI, I suggest you look into this tutorial by google to creating lists
I want to build a questionnaire type app with questions that show up individually and when a user submits an answer and clicks the button the data is stored in a textView above and the question goes to the next question where that answer is stored in a new textView. I am having difficulty getting the question to change to the next question.
Below I have attempted using the array method but have gotten errors both here
myTexts[0]=findViewById(R.id.question1);
myTexts[1]=findViewById(R.id.question2);
-- and
myTexts[questionNumber].setText(mEdit.getText().toString());
questionNumber++;
Below is complete source code:
package com.example.greg;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class menu extends Activity {
Button mButton;
EditText mEdit;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.userAnswereditText);
TextView [] myTexts = new TextView[2];
myTexts[0]=findViewById(R.id.question1);
myTexts[1]=findViewById(R.id.question2);
int questionNumber = 0;
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
myTexts[questionNumber].setText(mEdit.getText().toString());
questionNumber++;
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
To change question you should have array of String or List of String that can hold all the question and as soon as button is clicked and the answer is stored in the TextView you will load the next question from that array or List of String that is holding the questions.
String [] questions;
int numberOfQuestions = 2;
and then in onCreate() method
questions=new String[numberOfQuestions];//numberOfQuestion refers to integer that holds total number of questions
questions[0]="This is first question?";
questions[1]="This is second question?";
and now in your view.onClickListener
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
myTexts[questionNumber].setText(mEdit.getText().toString());
questionNumber++;
//here you will have to load the next question
if(questionNumber < numberOfQuestions)
questionTextViewHolder.setText(questions[questionNumber]);
else
Toast.makeText(menu.this,"No more questions!",Toast.LENGTHLONG).show();
//Note one thing that your questionNumber variable should not increase myTexts length or questions length because if it does you will get exception
}
});
questionTextViewHolder is the TextView in which you are displaying the question that is to be answered, you will have to replace "questionTextViewHolder " with your textview in which you are displaying the questions !
Class name menu starting letter should be capital if you follow the coding conventions.
Shouldn't this...
myTexts[0]=findViewById(R.id.question1);
be a cast to TextView instead?...
myTexts[0]=(TextView)findViewById(R.id.question1);
That will definitely eliminate a ClassCastException
I'm just starting out with the Android/Eclipse SDK and I have no previous Java experience.
I've seen lots of tutorials for running Toasts onclick but I'm trying to make it so that when a button is clicked a text field is populated with the text of that button.
In other words if I press a button which is labelled as 'Hello' then the contents of a textfield will become 'Hello'.
Any help is greatly appreciated.
TextView mText=(TextView)findViewById(R.id.textview1);
Button mbutton=(Button)findViewById(R.id.button1);
mbutton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
mText.setText("Hello");
}
});
Hope i may works..
Wrapping it up. You must have a "button1" and a "textview1" defined in your main.xml.
package my.dummy.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
public class HelloActivity extends Activity implements OnClickListener {
Button b=null;
TextView tv=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.button1); // button1 set in main.xml
b.setOnClickListener(this);
tv=(TextView)findViewById(R.id.textview1); // textview1 set in main.xml
}
public void onClick( View v ) {
if (v == b) {
tv.setText( b.getText() );
}
}
}
write code in onClick()
String text = view.getText();
// use toast here to display text..
Suppose your button's id is button1, and textview's id is textview1
Button My_Button=(Button)findViewById(R.id.button1);
TextView textView = (TextView)findViewById(R.id.textview1);
My_Button.setOnClickListener(new OnClickListener(){
public void onclick(View v){
textView.setText("hello");
}
);
Trying to create a game that kids chase the marble. I want to have the buttons background change only if another buttons background equal something. But can't get the onclick with the a if statement to work. Here is the code I currently have.
package test.tablet.design;
import java.text.DateFormat;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MarbleGame extends Activity {
private Button btnA1mc; private Button btnA2mc; private Button btnA3mc; private Button btnA4mc; private Button btnB1mc; private Button btnB2mc; private Button btnB3mc; private Button btnB4mc;
private Button btnC1mc; private Button btnC2mc; private Button btnC3mc; private Button btnC4mc; private Button btnD1mc; private Button btnD2mc; private Button btnD3mc; private Button btnD4mc;
private Button btnE1mc; private Button btnE2mc; private Button btnE3mc; private Button btnE4mc; private Button btnF1mc; private Button btnF2mc; private Button btnF3mc; private Button btnF4mc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.marble_chase);
TextView tv_time = (TextView) findViewById(R.id.tv_time);
String currenttime = DateFormat.getDateTimeInstance().format(new Date());
tv_time.setText(currenttime);
btnA1mc = (Button)findViewById(R.id.btnA1mc); btnA2mc = (Button)findViewById(R.id.btnA2mc); btnA3mc = (Button)findViewById(R.id.btnA3mc); btnA4mc = (Button)findViewById(R.id.btnA4mc);
btnB1mc = (Button)findViewById(R.id.btnB1mc); btnB2mc = (Button)findViewById(R.id.btnB2mc); btnB3mc = (Button)findViewById(R.id.btnB3mc); btnB4mc = (Button)findViewById(R.id.btnB4mc);
btnC1mc = (Button)findViewById(R.id.btnC1mc); btnC2mc = (Button)findViewById(R.id.btnC2mc); btnC3mc = (Button)findViewById(R.id.btnC3mc); btnC4mc = (Button)findViewById(R.id.btnC4mc);
btnD1mc = (Button)findViewById(R.id.btnD1mc); btnD2mc = (Button)findViewById(R.id.btnD2mc); btnD3mc = (Button)findViewById(R.id.btnD3mc); btnD4mc = (Button)findViewById(R.id.btnD4mc);
btnE1mc = (Button)findViewById(R.id.btnE1mc); btnE2mc = (Button)findViewById(R.id.btnE2mc); btnE3mc = (Button)findViewById(R.id.btnE3mc); btnE4mc = (Button)findViewById(R.id.btnE4mc);
btnF1mc = (Button)findViewById(R.id.btnF1mc); btnF2mc = (Button)findViewById(R.id.btnF2mc); btnF3mc = (Button)findViewById(R.id.btnF3mc); btnF4mc = (Button)findViewById(R.id.btnF4mc);
btnE2mc.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
btnE2mc.setBackgroundResource(R.drawable.marble_x);
btnB4mc.setBackgroundResource(R.drawable.marble);
}
});
btnB4mc.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if (btnE2mc.equals(getResources().getDrawable(R.drawable.marble_x)))
{
btnB4mc.setBackgroundResource(R.drawable.marble_x);
btnC1mc.setBackgroundResource(R.drawable.marble);
}
else{}
}
});
btnC1mc.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if (btnB4mc.equals(getResources().getDrawable(R.drawable.marble_x)))
{
btnC1mc.setBackgroundResource(R.drawable.marble_x);
btnF1mc.setBackgroundResource(R.drawable.marble);
}
else{}
}
});
}
}
I would have an internal boolean set to true whenever the button is clicked and the background is changed.
boolean isBackgroundChanged = false;
Whenever the button is clicked, you change the background and set isBackgroundChanged to true.
And then, instead of doing:
btnB4mc.equals(getResources().getDrawable(R.drawable.marble_x)
you would have
if(isBackgroundChanged)...
You're trying to compare the Button object with a Drawable object, which will always return false.
You might instead want to use View.setTag() to track the state for each individual button. With a tag (named or unnamed) you can associate arbitrary data with an individual View. In the example below, auto-boxing is used to convert booleans into Booleans.
if ((Boolean)btn1.getTag()) {
btn1.setTag(false);
btn2.setTag(true);
// TODO actual background swapping
}
For readability and reducing duplication, you might want to use a helper function that can do the swapping for you, which will take two Buttons as arguments.