OnCreate ImageButton Onclick - android

I'm trying to get an ImageButton to create another ImageButton in a different layout from an OnClick. My logcat takes me to an error on this line, but provides no further feedback:
ImageButton adab = (ImageButton)findViewById(R.id.abbutton);
My code:
package org.iimed.www;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
public class Penicillins extends Activity implements OnClickListener{
ImageButton back,addmed;
Context abbuttonplain;
public void onCreate(Bundle SavedInstanceState){
super.onCreate(SavedInstanceState);
setContentView(R.layout.penicillin);
back = (ImageButton) findViewById(R.id.back);
addmed = (ImageButton) findViewById(R.id.addmed);
back.setOnClickListener(this);
addmed.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.back:
startActivity(new Intent(
Penicillins.this, ImageTextListViewActivity.class));
break;
case R.id.addmed:
RelativeLayout ll=(RelativeLayout)findViewById(R.layout.sundayopen);
LayoutParams param = new
RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageButton adab = (ImageButton)findViewById(R.id.abbutton);
ll.addView(adab,param);
}
}
}
Thanks for you patience.
Edit: the .xml the button is to create on
<?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"
android:background="#drawable/miimedback" >
<ImageView
android:id="#+id/pillboxm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="63dp"
android:contentDescription="#string/pillbox"
android:src="#drawable/pillbox" />
<ImageButton
android:id="#+id/satlid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/pillboxm"
android:layout_alignRight="#+id/pillboxm"
android:background="#android:color/transparent"
android:contentDescription="#string/satlid"
android:src="#drawable/slid" />
<ImageButton
android:id="#+id/frilid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thulid"
android:layout_toLeftOf="#+id/satlid"
android:background="#android:color/transparent"
android:contentDescription="#string/frilid"
android:src="#drawable/flid" />
<ImageButton
android:id="#+id/thulid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/pillboxm"
android:layout_toLeftOf="#+id/frilid"
android:background="#android:color/transparent"
android:contentDescription="#string/thulid"
android:src="#drawable/tlid" />
<ImageButton
android:id="#+id/homebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="11dp"
android:background="#android:color/transparent"
android:contentDescription="#string/homebutton"
android:src="#drawable/homebut1" />
<ImageButton
android:id="#+id/wedlid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thulid"
android:layout_toLeftOf="#+id/thulid"
android:background="#android:color/transparent"
android:contentDescription="#string/wedlid"
android:src="#drawable/wlid" />
<ImageButton
android:id="#+id/tuelid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/pillboxm"
android:layout_toLeftOf="#+id/wedlid"
android:background="#android:color/transparent"
android:contentDescription="#string/tuelid"
android:src="#drawable/tlid" />
<ImageButton
android:id="#+id/monlid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/pillboxm"
android:layout_toLeftOf="#+id/tuelid"
android:background="#android:color/transparent"
android:contentDescription="#string/monlid"
android:src="#drawable/mlid" />
<ImageButton
android:id="#+id/adbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/abbutton"
android:background="#android:color/transparent"
android:contentDescription="#string/adbutton"
android:src="#drawable/adbutton" />
<ImageButton
android:id="#+id/abbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cobutton"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:contentDescription="#string/abbutton"
android:src="#drawable/antibiotic_buton" />
<ImageButton
android:id="#+id/bppressure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/monlid"
android:layout_alignTop="#+id/adbutton"
android:background="#android:color/transparent"
android:contentDescription="#string/bpbutton"
android:src="#drawable/bpbutton" />
<ImageButton
android:id="#+id/satlidopen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/monlid"
android:layout_toLeftOf="#+id/monlid"
android:background="#android:color/transparent"
android:contentDescription="#string/satlidopen"
android:src="#drawable/satlidopen" />
<ImageButton
android:id="#+id/cobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/satlidopen"
android:layout_marginBottom="133dp"
android:layout_toRightOf="#+id/wedlid"
android:background="#android:color/transparent"
android:contentDescription="#string/cobutton"
android:src="#drawable/cobutton" />
</RelativeLayout>
the logcat:
01-06 23:35:23.725: E/AndroidRuntime(29654): at `org.iimed.www.Penicillins.onClick(Penicillins.java:50)`

You should create a new ImageButton in code, and not try to add the same ImageButton reference (R.id.abbutton). You can't add the same View (you get a reference to same inflated object using findViewbyId). Try something like:
RelativeLayout ll=(RelativeLayout)findViewById(R.layout.sundayopen);
LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageButton adab = new ImageButton(getApplicationContext());
// Initialise ImageButton properties like the Image here...
ll.addView(adab,param);
Also, as a good practice you should keep the reference to your RelativeLayout ll in a member variable, assign in it once (look it up using findViewbyId) in your onCreate and just use it wherever you need it, that way you save the unnecessary lookups every time you click the R.id.addmed ImageButton.

back = (ImageButton) findViewById(R.id.back);
addmed = (ImageButton) findViewById(R.id.addmed);
you are using this id's to click and getting error for this is
adab = (ImageButton)findViewById(R.id.abbutton);
check id and layout proper as you have no id of abbuttonin your layout

Related

Can't make calculations using a mix of TextView and EditText

I'm currently making a very simple math game. In this math game I want the player to solve easy equations. It looks like this: 9 * __ = 45, the player then fill in the correct number to solve the equation and then presses a Correct-button. If correct scores are added to the player.
The empty space is an EditText and the others are TextViews. Because I use a mix of TextView & EditText I need somehow to make a convertion for the program to be able to read and calculate. I've been reading like crazy and tried all kinds of different methods without success. How should I do to get around this?
This is how my data looks like:
activity_play.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:id="#+id/activity_play"
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.android.laboration2.PlayActivity">
<TextView
android:id="#+id/textPlayMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textPlayNumber1"
android:layout_centerHorizontal="true"
android:text="#string/multiply"
android:textAlignment="textStart"
android:layout_gravity = "start"
android:textColor="#android:color/black"
android:textSize="40sp" />
<TextView
android:id="#+id/textPlayNumber1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/textPlayScore"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/textPlayScore"
android:layout_marginTop="48dp"
android:text="#string/number_1"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="50sp" />
<TextView
android:id="#+id/textPlayEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#string/equal"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="50sp"
android:layout_below="#+id/editTextPlayNumber2"
android:layout_alignLeft="#+id/textPlayMultiply"
android:layout_alignStart="#+id/textPlayMultiply" />
<TextView
android:id="#+id/textPlayResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:text="#string/result_number3"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="50sp"
android:layout_below="#+id/textPlayEqual"
android:layout_alignLeft="#+id/textPlayEqual"
android:layout_alignStart="#+id/textPlayEqual" />
<TextView
android:id="#+id/textPlayScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="31dp"
android:layout_toStartOf="#+id/answerButton"
android:text="#string/score_0"
android:textAlignment="textStart"
android:layout_gravity="start"
android:textColor="#android:color/black"
android:textSize="24sp"
android:layout_toLeftOf="#+id/answerButton" />
<Button
android:id="#+id/answerButton"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:background="#android:color/holo_orange_dark"
android:text="#string/button_result"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="18sp"
android:textStyle="bold"
android:layout_below="#+id/textPlayResult"
android:layout_centerHorizontal="true" />
<EditText
android:id="#+id/editTextPlayNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textPlayMultiply"
android:layout_alignBottom="#+id/textPlayMultiply"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_toEndOf="#+id/answerButton"
android:layout_toRightOf="#+id/answerButton"
android:hint=" "
android:inputType="number"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="50sp" />
<TextView
android:id="#+id/textPlayLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textPlayScore"
android:layout_alignBottom="#+id/textPlayScore"
android:layout_toEndOf="#+id/answerButton"
android:layout_toRightOf="#+id/answerButton"
android:text="#string/level_0"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="24sp" />
</RelativeLayout>
.
PlayActivity.java
package com.example.android.laboration2;
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 PlayActivity extends AppCompatActivity implements View.OnClickListener {
TextView textPlayNumber1;
EditText editTextPlayNumber2;
TextView textPlayResult;
TextView textPlayScore;
TextView textPlayLevel;
Button answerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
textPlayNumber1 = (TextView) findViewById(R.id.textPlayNumber1);
editTextPlayNumber2 = (EditText) findViewById(R.id.editTextPlayNumber2);
textPlayResult = (TextView) findViewById(R.id.textPlayResult);
textPlayScore = (TextView) findViewById(R.id.textPlayScore);
textPlayLevel = (TextView) findViewById(R.id.textPlayLevel);
answerButton = (Button) findViewById(R.id.answerButton);
answerButton.setOnClickListener(this);
}//onCreate ends here
#Override
public void onClick(View v) {
}//onClick ends here
}//PlayActivity ends here
You can do
int playNumberValue = Integer.getInteger(textPlayNumber1.getText().toString());
int userInputValue = Integer.getInteger(editTextPlayNumber2.getText().toString());
int result = Integer.getInteger(textPlayResult.getText().toString());
if(result == userInputValue+playNumberValue)
//win game
Your TextView and EditText all have String type values. You have to parse those value to Integer then calculate and show the result.
Here is the action onClick answer button-
int score = 0;
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.answerButton:
int playNum1 = Integer.parseInt(textPlayNumber1.getText().toString());
int playNum2 = Integer.parseInt(editTextPlayNumber2.getText().toString());
int playResult = Integer.parseInt(textPlayResult.getText().toString());
if(playNum1*playNum2 == playResult){
score++;
textPlayScore.setText(""+score);
}
break;
}
}
Hope this helps.

Remove and add layout dynamically by click button in Android

I'm working on a big project and I can not solve the problem of a dynamically adding of components.I want to add layout into other layout by click on a button ADD. After this I want to remove it by click a button REMOVE.
Specially for stackoverflow I build a small example of what I want to do.
To ADD it's not a problem but remove it's a problem.When I click a "remove" button this remove not what I need (I want remove parent of "remove" button).
After this I want to ask something more important.I will need save all this data to the DB.So I don't know how to get data from each Text Fields and put it into list (or something else) because all this Text Fields have same ID.
So I see two way of solution:
1)Change there ID dynamically
2)Something else))
Thank you very much!!!
This is
sub_fields.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:id="#+id/detailsLayout"
android:focusableInTouchMode="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/txtName"
android:hint="name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/txtPhone"
android:layout_gravity="center_horizontal"
android:hint="phone" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:id="#+id/btnAddd"
android:onClick="onClickAddd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REMOVE"
android:id="#+id/btnRemove"
android:onClick="onClickAddd" />
</LinearLayout>
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:id="#+id/generalLayout">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:gravity="center"
android:background="#7d65258a">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="FILL FIELDS"
android:id="#+id/textView" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/subLayoutFields">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/txtName"
android:hint="name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/txtPhone"
android:layout_gravity="center_horizontal"
android:hint="phone" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:id="#+id/btnAdd"
android:onClick="onClickAdd" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package andrey.adddinamicallycontrolsapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClickAdd(View view) {
LayoutInflater ltInflater = getLayoutInflater();
final LinearLayout subLayoutFields = (LinearLayout) findViewById(R.id.subLayoutFields);
final View view1 = ltInflater.inflate(R.layout.sub_fields, subLayoutFields, true);
Button buttonRemove = (Button)view1.findViewById(R.id.btnRemove);
buttonRemove.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
subLayoutFields.removeView((LinearLayout)(v.getParent().getParent()));
}});
}
Before adding some view by addView(childView) you should save reference to that. In that case removeView(childView) will work. Getting that view by traveling in view tree by getParent() is not a good idea.
By the way, where is your addView call?
maybe try subLayoutFields.removeView(view1) - view1 is final
if you want to somehow differ your widgets (for storing in db, iterating or whatever you want) you may set for them dynamic ids using View.generateViewId() (API17) or using custom method from most voted answer here
Thank you guys!!!
The answer is :
public void onClickAddPanel(View view) {
LayoutInflater ltInflater = getLayoutInflater();
LinearLayout subLayoutFieldsForBtnAdd = (LinearLayout) findViewById(R.id.productDetails);
View view1 = ltInflater.inflate(R.layout.product_details, subLayoutFieldsForBtnAdd, true);
}
public void onClickRemovePanel(View v) {
View v1 = (View) v.getParent();
LinearLayout subLayoutFieldsForBtnRemove = (LinearLayout) findViewById(R.id.productDetails);
subLayoutFieldsForBtnRemove.removeView(v1);
}

how to display value of textbox on same screen in android application just like whatsaap

This is my main file where i am calling the click event on button to pass the value.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setTitle("aakash");
SetContentView(R.id.lst);
Button b = (Button) findViewById(R.id.sendbtn);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(android.view.View v) {
startActivity(new Intent(View.this,View.class));
}
});
}
**xml**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:textColor="#000000"
android:text="aakash"
android:background="#c0c0c0"
/>
<ScrollView
android:id="#+id/lst"
android:background="#fefefe"
android:layout_below="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_holder"
>
</ScrollView>
<EditText
android:id="#+id/chattxt"
android:layout_width="230dp"
android:layout_height="45dp"
android:hint="type here"
android:inputType="textMultiLine" />
<Button
android:id="#+id/sendbtn"
android:layout_width="75dp"
android:layout_height="45dp"
android:text="Send" />
</LinearLayout>
I want the value of textbox to display on the same screen(scrollview)box and also i want to pass a httppost request to pass a value..plz help me out
Thnks in advance
I think, you can use a ListView insted of scroll view. and add list items from the chat text value dynamically.
call notifydatasetchanged() every time when you add a new item to it.
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:textColor="#000000"
android:text="aakash"
android:background="#c0c0c0"
/>
<ListView
android:id="#+id/listView_chats"
android:layout_width="match_parent"
android:layout_height="100dp"
>
</ListView>
<EditText
android:id="#+id/chattxt"
android:layout_width="230dp"
android:layout_height="45dp"
android:hint="type here"
android:inputType="textMultiLine" />
<Button
android:id="#+id/sendbtn"
android:layout_width="75dp"
android:layout_height="45dp"
android:text="Send" />
</LinearLayout>
Write a custom adapter for the listView and add the code to manage the list data.
i got the answer to my question
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ButtonActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
}
public void setText(){
Button myButton = (Button)findViewById(R.id.button1);
final TextView myText = (TextView)findViewById(R.id.text1);
final EditText myInput = (EditText)findViewById(R.id.edit);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
myText.setText(myInput.getText());
}
});
}
}

how to clear entries from several EditTexts?

I am new to java and android so please forgive me if i am asking to simple question.
I have an application which requires user input in two EditTexts. Those inputs are multipied and result is displayed in TextView. I would like to use "clear entries" button which would clear the content of user entries and displayed result. Is there any way to do it?
Here is an application code.
package c.example.rectangle;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
EditText l;
EditText w;
TextView a;
Button b;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (EditText) findViewById(R.id.length);
w = (EditText) findViewById(R.id.width);
a = (TextView) findViewById(R.id.lblarea);
b = (Button) findViewById(R.id.calculate);
b.setOnClickListener(this);
}
public void onClick(View v) {
calculateRectangle(l.getText().toString(), w.getText().toString());
}
private void calculateRectangle(String clength, String cwidth){
double area = Double.parseDouble(clength)*Double.parseDouble(cwidth);
a.setText(String.valueOf(area));
}}
And here is my XML Code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#8B4513"
android:orientation="vertical" >
<TextView
android:id="#+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:gravity="center"
android:text="#string/rect"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:background="#2F4F4F"
android:gravity="center"
android:text="#string/cm"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/length"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="50dp"
android:background="#2F4F4F"
android:ems="10"
android:gravity="center"
android:hint="#string/help"
android:inputType="text" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#2F4F4F"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="#string/breadth"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/width"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="33dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:inputType="text"
android:hint="#string/help"
android:ems="10"
android:gravity="center"
>
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:id="#+id/calculate"
android:layout_width="fill_parent"
android:layout_marginLeft="100dip"
android:layout_marginRight="100dip"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:layout_marginTop="20dp" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:text="#string/area"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblarea"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:gravity="center"/>
</LinearLayout>
<Button
android:id="#+id/clear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:layout_marginRight="100dip"
android:layout_marginTop="20dp"
android:text="#string/clear" />
</LinearLayout>
I would be very appreciate for the answer.
Why not just set both editTexts to empty when you no longer need the data in them to be displayed?
EditText.setText("");
Same thing with the TextView;
TextView.setText("");
If you are wanting to iterate through them, you can put them in a list then use a for loop to set the text to ""
List<EditText> myList = new List<EditText>();
myList.add(editText1);
Then in your clear method
for (int x = 0; x < myList.size(); x++
{
myList.get(x).setText("");
}
Why nobody seems to use the really useful android:onClick ?
<Button
...
android:text="#string/calculate"
android:onClick="calculate" />
<Button
...
android:text="#string/clear"
android:onClick="clearForm" />
With the following activity :
class MyActivity extends Activity
{
...
/**
* Calculate
* android:onClick="calculate"
*/
public void calculate(View view)
{
// Handle click on your 'Calculate' button
}
/**
* Clear form
* android:onClick="clearForm"
*/
public void clearForm(View view)
{
int[] ids = new int[]{R.id.length, R.id.width};
for(int id : ids)
{
((EditText) this.findViewById(id).)setText("");
}
}
}
This way, you do not have to care about ids and your code will be more clean than clean.
Ids should not be overused! They are great on views that can be 'changed' by the user to handle those changes (and to enjoy the onSaveInstanceState() natural behavior) but that's it!
IMO.
If you want to create a clear button, do the following.
Create a Button in your xml:
<Button
android:id="#+id/clear_button"
... you own layout prefs ...
/>
Create a listener for the button in your code:
OnClickListener clearButtonListener = new OnClickListener(){
#Override
public void onClick(View view) {
((EditText)findViewById(R.id.id_for_text_box_a)).setText("");
//...do this for all your edit texts that you want to clear
}
};
Connect the listener to the button
Button clearButton = (Button) findViewById(R.id.clear_button);
clearButton.setOnClickListener(clearButtonListener);
Alternatively, instead of finding the edit texts by id in the listener, they could be instance variables that get initialized in onCreate or wherever. I would also recommend not using one letter variable names.
Personally, I would set OnClickListeners instead of the onClick attribute in the XML. Although using the XML onClick attribute may amount to fewer lines of code, it unfortunately creates a very tight coupling of layout and functionality. I prefer to have XML for layout, and Java for functionality. Additionally, being forced to use ids amounts to requiring XML elements to have variable names, which makes for more readable layout code (what is this button? what is this checkbox for?). Another problem I see with using the onClick attribute is that it forces your methods called to be public, which doesn't really make sense for many of these methods. I prefer to understand what an Activity does functionally through reading the Java, and I would rather not have unreferenced public methods floating around in my Activities.

RelativeLayout Gravity not applied?

I have an activity with the following layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutOverlays"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/testlayout_bottom"
android:layout_width="fill_parent"
android:layout_height="62dp"
android:background="#122334" >
<ImageView
android:id="#+id/testbtnBlock"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_gravity="left|center_vertical"
android:contentDescription="Test1"
android:padding="#dimen/padding_medium"
android:src="#drawable/btnblock" />
<TextView
android:id="#+id/testtxtZoomPan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/testbtnX"
android:layout_toRightOf="#+id/testbtnBlock"
android:gravity="center_horizontal"
android:text="#string/txtZoomPan"
android:textColor="#FFFFFF" />
<ImageView
android:id="#+id/testbtnX"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_gravity="right|center_vertical"
android:contentDescription="Test2"
android:padding="#dimen/padding_medium"
android:src="#drawable/btnx" />
</RelativeLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutPuzzleInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutChronoErrors"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Chronometer
android:id="#+id/testchronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:format="#string/chronometer_initial_format"
android:gravity="center" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutErrors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="1px"
android:layout_height="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
and the following code :
package minmaxdev.android.picrossanywhere;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.support.v4.app.NavUtils;
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
// Capture our button from layout
ImageView button = (ImageView) findViewById(R.id.testbtnBlock);
// Register the onClick listener with the implementation above
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
RelativeLayout rv = (RelativeLayout) findViewById(R.id.testlayoutOverlays);
rv.setGravity(Gravity.CENTER);
Button btnRetry = new Button(TestActivity.this);
btnRetry.setId(R.id.btnRetry);
btnRetry.setBackgroundResource(R.drawable.btnselector);
RelativeLayout.LayoutParams prmBtn = new RelativeLayout.LayoutParams(Util.DPsToPixels(200, getResources()), Util.DPsToPixels(40, getResources()));
prmBtn.setMargins(0, 120, 0, 0);
btnRetry.setLayoutParams(prmBtn);
// btnRetry.setGravity(Gravity.CENTER_HORIZONTAL);
btnRetry.setText("Retry");
btnRetry.setOnClickListener(new ImageView.OnClickListener() {
public void onClick(View v) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
rv.addView(btnRetry);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_test, menu);
return true;
}
}
I would like to know :
Why is my dynamically created Button (named btnRetry) not appearing in the center of the screen, since I set the parent RelativeLayout gravity to center with rv.setGravity(Gravity.CENTER) ?
Thank you very much for your time
The answer is easy. Gravity works for the content, layout_gravity for the view that uses that.
Source basically: https://stackoverflow.com/a/3482757/180538
Try to use LayoutParams with addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
To answer your gravity understanding: You are right but I guess that this information in the documentation is important:
Note that since RelativeLayout considers the positioning of each child relative to one another to be significant, setting gravity will affect the positioning of all children as a single unit within the parent. This happens after children have been relatively positioned.
I can't test your layout at the moment but my guess is that the time where gravity is applied doesn't create the expected result.
Personally I would use gravity only in LinearLayouts and the centerInParent for RelativeLayouts.

Categories

Resources