Android: On changing the image on ImageView the Edittext disappeared - android

I am new to android app development on eclipse IDE.
I am trying to make a basic app in which, on clicking the button both the image in ImageView as well as the text in EditText replaces with another image and text respectively.
But on doing so, primarily when the app starts, a button is displayed on the top, then the ImageView and then the EditText, they work fine, but on clicking the button, only image is replaced successfully while my EditText disappeared. Can you help me out why this happened and how to rectify it?
xml file:
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/click_it" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:src="#drawable/prac" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/imageView1"
android:layout_below="#+id/imageView1"
android:layout_marginTop="30dp"
android:ems="10"
android:text="hello!" >
<requestFocus />
</EditText>
</RelativeLayout>
java file:
package com.example.practicetwo;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity {
Button b1;
ImageView iv1;
EditText et1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(new R.id().button1);
iv1 = (ImageView)findViewById(new R.id().imageView1);
et1 = (EditText)findViewById(new R.id().editText1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv1.setImageResource(R.drawable.prac1);
et1.setText("how r u?");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Your image prac1 might be greater than the size of the screen, and as you replace prac with prac1 the EditText is pushed down. Try using LinearLayout instead of RelativeLayout.

Related

How to change click event in android

Its been few days over to me, learning Android App development :
I have created an APK which is having two buttons (largeButton & smallButton) if you click on these button test will large and small accordingly.
What i am trying to do :
When i click second time button should change in previous mode i.e Vice Versa should happen.
Here is my code :
mainActivity:
package com.firstapk.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button largButton;
Button smallButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
largButton = (Button) findViewById(R.id.largbutton);
smallButton = (Button) findViewById(R.id.smallbutton);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//public void getDisplay(View view){
public void largebuttonclick(View view){
try{
largButton.setTextSize(40);
}catch(Exception e){
Log.d("TestApp", e.getMessage());
}
}
//}
public void smallButtonClick(View view){
try{
smallButton.setTextSize(5);
}catch(Exception e){
Log.d("TestApp",e.getMessage());
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/largbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="96dp"
android:onClick="largebuttonclick"
android:text="#string/button_large" />
<Button
android:id="#+id/smallbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="320dp"
android:onClick="smallButtonClick"
android:text="#string/button_small" />
</RelativeLayout>
As you did now it's right, if he click "Max size" he will see max size;
So maybe you want to use only one button which works like (+ and -)? If yes use one onClick and a boolean variable to save the current status (max size, small size).
private boolean maxSize; // false if small, true if max
public void onChangeSize(View view)
{
if (maxSize)//max->small
{
smallButton.setTextSize(5);
}
else//small->max
{
largButton.setTextSize(40);
}
maxSize = !maxSize;
}
Then android:onClick="onChangeSize" in the two buttons xml.
You could use the same approch for your current code, but it will work in the same way.

how to set visibility options of android.widget.textview

had FC issues with this
private TextView msg, NameOut, DateOut;
msg = (TextView) findViewById(R.id.txtviewOut) ;
down the line... (problem lies here)
msg.setVisibility(View.INVISIBLE);//set visibility to false on create
how do I set the visibility of this TextView (msg) to false when the app starts?
UPDATE AFTER #1 Answer EDIT
okay here is all the code:
fragment_main.xml
<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$PlaceholderFragment">
<EditText
android:id="#+id/textenter"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="#+id/lbledt1"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Name"
android:id="#+id/lbledt1"
android:layout_marginTop="26dp"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter your date of birth (e.g. xx July 19xx)"
android:id="#+id/textView"
android:layout_below="#+id/textenter"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp" />
<EditText
android:id="#+id/editText"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press this button"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="You have been awesome since"
android:id="#+id/txtviewOut"
android:layout_marginTop="86dp"
android:textIsSelectable="false"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/txtoutName"
android:textIsSelectable="false"
android:layout_alignBottom="#+id/txtviewOut"
android:layout_centerHorizontal="true"
android:layout_marginBottom="41dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/txtOutDate"
android:layout_marginTop="28dp"
android:textIsSelectable="false"
android:layout_below="#+id/txtviewOut"
android:layout_centerHorizontal="true" />
MainActivity.java:
package com.example.helloandroidstudio;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private Button btnClick;
private EditText Name, Date;
private TextView msg, NameOut, DateOut;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnClick = (Button) findViewById(R.id.button) ;
Name = (EditText) findViewById(R.id.textenter) ;
Date = (EditText) findViewById(R.id.editText) ;
msg = (TextView) findViewById(R.id.txtviewOut) ;
NameOut = (TextView) findViewById(R.id.txtoutName) ;
DateOut = (TextView) findViewById(R.id.txtOutDate) ;
// msg.setVisibility(View.GONE);//set visibility to false on create
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public void onClick(View v)
{
if (v == btnClick)
{
if (Name.equals("") == false && Date.equals("") == false)
{
NameOut = Name;
DateOut = Date;
msg.setVisibility(View.VISIBLE);
}
else
{
msg.setText("Please complete both fields");
msg.setVisibility(View.VISIBLE);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
I tried it with setInvisibility(View.GONE), but still FC
I check witht the id values, there isa aid value assisgned and I did do the typecast of the object...
please keep in mind, I have done c#, but this is my first java/android app so please bear with me
well i have some doubt what exactly are you trying to accomplish , but i would try to clear some concept
if you want to hide your view completely then use Visibility.Gone or if you want to hide it but want to keep the place then use Visibility.Invisible
msg.setVisibility(View.INVISIBLE); // for hiding but keeping the place of your view
msg.setVisibility(View.GONE);// for hiding it completely , view will not take any place
Set that as the default in your layout.
<TextView
android:id="#+id/txtviewOut"
...
android:visibility="invisible" />
UPDATE:
If you always need it to start off invisible, this is the way to go. Note that "invisible" means that it's not displayed but it will still take up blank space when rendering. If you want it to be collapsed (does not take up any space, does not display), then use View.GONE in code or android:visibility="gone" in your layouts.
If calling setVisibility on a View causes a FC, that's most likely going to be a null pointer exception (check in LogCat though to be sure). If that's the case, you either haven't called findViewById yet or your layout does not contain the view by that id.
UPDATE #2:
First of all, you should always look at LogCat and get the exact exception that is thrown and the exact line it is thrown on. I'm willing to bet it happens on this line:
NameOut = Name;
DateOut = Date;
You're attempting to set a variable of type TextView to a value of type EditText. What I assume you're trying to do is set the text of your TextView to the value of the text in your EditText. This is done as follows:
NameOut.setText(Name.getText().toString());
DateOut.setText(Date.getText().toString());
You have implemented an
onClick(View v)
method but did not wire up with your Button in the layout.
Set the property android:onClick="onClick" in your layout xml file.
Right now, your button click is not getting registered and the method is not getting executed
For Doing this in Kotlin:
<Text android:visibility="gone"/>
If you always need it to start off invisible, this is the way to go. Note that "invisible" means that it's not displayed but it will still take up blank space when rendering. If you want it to be collapsed (does not take up any space, does not display), then use View.GONE in code.
For Visibility:
text.Visibility = View.Visible
For Gone :
text.Visibility = View.Gone
(As Gone does not uses your space in main_activity.xml.
It Consumes Space Only When it is Set To Visible.)

Android - Show image from res/drawable onClick

First of all, I wanna say I've been seeking for an answer on the Forum and I found didn't match for what I wanted. Basically, what I want is: when the user clicks on one of the images previously "specified" on the .xml file, a new image is displayed on the center of the screen that is not "specified" on the .xml file. I wrote "specified" cause idk if it's the correct way to refer to this.
EDIT: there was no need to not specify the image previously, all I needed was to set "gone" for visibiity. This code is working exactly how I wanted (ty guys):
Main.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
public class Principal extends Activity {
ImageView cuia1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
cuia1 = (ImageView) findViewById(R.id.cuia1);
cuia1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ImageView cuia1grande = (ImageView) findViewById(R.id.cuia1grande);
cuia1grande.setVisibility(1);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.principal, menu);
return true;
}
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*">
<TableRow
android:id="#+id/tabelaCuias"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center">
<TextView
android:id="#+id/selecionaCuia"
android:text="Selecione a cuia"
android:textStyle="bold">
</TextView>
<ImageView
android:id="#+id/cuia1"
android:src="#drawable/cuia1">
</ImageView>
<ImageView
android:id="#+id/cuia2"
android:src="#drawable/cuia2">
</ImageView>
<ImageView
android:id="#+id/cuia3"
android:src="#drawable/cuia3">
</ImageView>
<ImageView
android:id="#+id/cuia4"
android:src="#drawable/cuia4">
</ImageView>
</TableRow>
</TableLayout>
<ImageView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:visibility="gone"
android:id="#+id/cuia1grande"
android:src="#drawable/cuia1grande">
</ImageView>
Is there any reason you don't want to "specify" the image in your layout file? You could place it there and not display it (visibilty="gone"), and then show/hide it when you deem fit.
Here's what I'd do:
Make your layout a RelativeLayout instead of a TableLayout (this will make things easier for showing the image in the center)
Place your TableLayout within the wrapping RelativeLayout
Define an ImageView as the last child within the wrapping RelativeLayout, set centerInParent="true", visibilty="gone"
In your onClick method, simply set its visibility as visible.
If you really don't want to define the ImageView in the layout, then you can create it programmatically:
Follow the same steps 1-2 as before
Capture the reference to the wrapping RelativeLayout in the code
In the onClick method, create the ImageView programatically, specifying the centerInParent="true" via the code (let me know if you want an example on how to do this & I'll edit the answer with a code sample).
Add the new view to the RelativeLayout via myRelativeLayout.addView(myImageView);
Hope this helps :)
public class Principal extends Activity {
ImageView cuia1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
cuia1 = (ImageView) findViewById(R.id.cuia1);
//set invisible
cuia1 .setVisibility(View.INVISIBLE);
cuia1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//show image on the center of screen
//set image
cuia1.setImageResource(R.drawable.cuia1);
// set visible
cuia1 .setVisibility(View.VISIBLE);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.principal, menu);
return true;
}
}
import import android.view.View;
Cheerz!

Respond to send button

I installed the Android SDK bundle today and I am following the "My First App" tutorial and I am stuck, it states:
Open the MainActivity class (located in the project's src/ directory) and add the corresponding method:
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
Where do I put this in the file? and is this the "MainActivity.java" file?
I have tried and I keep getting errors so I am obviously going wrong somewhere.
activity_main.xml:
<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="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
MainActivity.java:
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Hope I have made my problem clear, I looked on the forum for an answer but I couldn't find anything.
If you have a button in your(say activity_main.xml) xml layout and you have the below attribute for button
android:onClick="sendMessage"
and you have the below in MainActiivty.java
setContentView(R.layout.activity_main);
You should have the below in MainActivity.java
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
Example:
MainActivity.java
// Your imports
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //setting the layout to activity
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
// other widgets
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="146dp"
android:onClick="sendMessage"
android:text="Button" />
</RelativeLayout>
If you are a new android developer and doing your first so start from basic like launch new activity it contain hello world or any text view, button then you will clear idea about application.
create your android application
in XML layout drag the button and text view
run your first app.
you will get your output.
Put it in MainActivity.java at the top right after
public class MainActivity extends ActionBarActivity {
After you do this, you may have to import. Do this by pressing control / shift / O (not zero)

Button OnClickListener not working

I am new to Android. I am trying to get input from the EditText and displaying it in TextView with the help of button. When I click the button nothing happens. Please help, here is my MainActivity.java code -
package com.example.addname;
import android.app.Activity;
import android.os.Bundle;
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;
public class MainActivity extends Activity {
Button b1;
TextView v1;
EditText t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
t1 = (EditText) findViewById(R.id.editText1);
v1 = (TextView) findViewById(R.id.textView1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(t1.getText().toString()=="")
{
v1.setText(t1.getText().toString());
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and here is my activity_main.xml
<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:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="25dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/editText1"
android:layout_marginTop="48dp"
android:text="Button" />
</RelativeLayout>
Instead of
if(t1.getText().toString()=="")
{
v1.setText(t1.getText().toString());
}
You can use
if(t1.getText().isEmpty()) // isEmpty() is only available from API 9 and Above
{
v1.setText(t1.getText().toString());
}
or this
if(t1.getText().trim().length == 0)
{
v1.setText(t1.getText().toString());
}
Don't compare strings using ==. You're actually comparing reference of an object. To compare strings, use equals() method.
I'd recommend comparing strings with .equals() since Objects compared with == usually fails (== compares references, not Object content).
if(t1.getText().toString().equals("")))
in your case though,since you're comparing an empty String, you can also use
if (t1.getText().toString().length == 0)
Then note that once you set the text, you're setting v1's text to an empty string since your logic indicates that only upon an empty string you set v1's text. So I'd recommend getting rid of the if altogether:
#Override
public void onClick(View arg0) {
v1.setText(t1.getText().toString());
}

Categories

Resources