I created three buttons and each button shows text when it is clicked. Now I need to add to that so the buttons show text + an image. all the texts and images will only appear if the buttons are clicked. For example, when the user clicks on first button, text1 + image1 appears. When the user clicks on second button, text2 + image2 appears in the same places of text1 and image1. Any help please? here are my codes:
XML (I couldn't post the first code part here but below is the most important parts)
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recipe 1"
android:id="#+id/click_btn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/response"
android:layout_below="#+id/click_btn"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/button15"
android:layout_alignEnd="#+id/button15" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recipe 2"
android:id="#+id/button15"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recipe 3"
android:id="#+id/button16"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/response"
android:layout_toEndOf="#+id/response" />
</RelativeLayout>
Java:
package com.example.android.ch;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import static com.example.android.R.*;
public class Recipes extends ActionBarActivity implements View.OnClickListener {
TextView resp; ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_recipes);
resp = (TextView)this.findViewById(id.response);
Button b = (Button)this.findViewById(id.click_btn);
Button c = (Button)this.findViewById(id.button15);
Button d = (Button)this.findViewById(id.button16);
ImageView imageView = (ImageView) findViewById(id.imageView);
b.setOnClickListener(this);
c.setOnClickListener(this);
d.setOnClickListener(this);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
}
}
#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_recipes, 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);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.click_btn: /** on click_btn button click */
resp.setText("2 medium sweet potatoes (about 1 pound total) \n1/2 teaspoon salt\n1/2 teaspoon ground cumin\n1/2 teaspoon chili powder\n1/2 teaspoon paprika\n1/4 teaspoon ground black pepper");
break;
case R.id.button15: /** on button15 button click */
resp.setText("1 large egg yolk, at room temperature\n1/2 cup extra-virgin olive oil; more for grilling\n2 teaspoons minced fresh flat-leaf parsley\n1 teaspoon minced fresh tarragon\n1 1/2 teaspoons fresh lemon juice; more to taste Kosher salt");
break;
case R.id.button16: /** on button16 button click */
resp.setText("1/2 cup butter, softened\n1 cup packed light brown sugar\n1 1/2 cups all-purpose flour\n3 cups rolled oats\n1 teaspoon ground cinnamon");
break;
}
}
}
yes you can add image or icon to a button with text.
use below code for this -
<Button
android:drawableRight="#drawable/ic_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recipe 3"
android:id="#+id/button16"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
here i have added one more property android:drawableRight you can change it to android:drawableLeft, android:drawableTop, or android:drawableBottom and set image on button.
Use drawableLeft (or any direction) attribute to display image with text,
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text"
android:drawableLeft="#drawable/button_icon"
... />
Above code will display image like
include the imageview and textview in a layout and keep it's visibility as invisible/gone.Then on clicking a button(for example button1) inside the onclick listener method you need to try like
imageview.setimage("your image");
textview.setText("your text");
change this code inside each buttonclick listener with various value.
I got right answer with many of you and thank you all but I meant to create three buttons and show text+image Not inside the icon. To do that I created three fragments xml files so when the user click on a button it will bring the fragment and place it.
Related
hi programmers my big respect to all of you.
im new here in android programming.
im having a problem in working my radio button with a function of gravity in my textview by aligning them from center, left and right.
My API is 23
and im following this tutorial youtube tutorial but mine is not working
package com.example.testavd;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnCheckedChangeListener {
TextView txtV;
RadioGroup rg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtV = (TextView) findViewById(R.id.textOut);
rg = (RadioGroup) findViewById(R.id.rdG);
rg.setOnCheckedChangeListener(this);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txtV.setText("boom");
}
});
}
#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.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.rbCenter:
txtV.setGravity(Gravity.CENTER);
break;
case R.id.rbLeft:
txtV.setGravity(Gravity.LEFT);
break;
case R.id.rbRight:
txtV.setGravity(Gravity.RIGHT);
break;
}
}
}
My 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="com.example.testavd.MainActivity" >
<RadioGroup
android:id="#+id/rdG"
android:layout_width="fill_parent"
android:layout_height="match_parent"
>
<RadioButton
android:id="#+id/rbRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/rbLeft"
android:layout_below="#+id/rbLeft"
android:layout_marginTop="36dp"
android:text="Right" />
<RadioButton
android:id="#+id/rbLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="17dp"
android:layout_toLeftOf="#+id/button1"
android:text="Left" />
<RadioButton
android:id="#+id/rbCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_alignLeft="#+id/rbRight"
android:layout_marginBottom="16dp"
android:text="Center" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
<TextView
android:id="#+id/textOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="32dp"
android:text="Output"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Use android:gravity="center_horizontal" on the RadioGroup to center the radiobuttons.Also gravity doesn't work in RelativeLayout You have to align according to parent or other widgets. If want to use gravity or layout_gravity use linearlayout.
Your TextView parent is a RelativeLayout, you cannot use gravity with RelativeLayout.
Try using in xml layout file
android:layout_centerHorizontal="true"
or in Activity
case R.id.rbCenter:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
txtV.setLayoutParams(params);
break;
Solution 1 :
android:gravity sets the gravity of the content of the View its used on.
you have used gravity in TextView having wrap_content width, so it will not make any diferrence as view is having width as much as content.
try it with match_parent width and you will see difference.
android:layout_width="macth_parent"
Solution 2 :
use android:layout_gravity, it sets the gravity of the View or Layout in its parent.
no matter how much content your TextView is having, it will set whole TextView in center/right/left aligned.
To make it more affective make sure following things :
When you are using android:gravity use width of view as match_parent.
When you are using android:layout_gravity use width of view as wrap_content.
refer this to programmatically set layout_gravity : How to set layout_gravity PROGRAMATICALLY?
I need to create three buttons in Android Studio and each button shows different text. When the user clicks on first button it shows "Welcome" at the right side of the button. When the user click on the second button the "Welcome" message will disappear, and in the same place a"Hello" message will appear. Third button is the same with difference message"Bye". Here my code and thanks in advance.
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="com.example.android.Recipes">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R1"
android:id="#+id/click_btn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/response"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/click_btn"
android:layout_toEndOf="#+id/click_btn" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R2"
android:id="#+id/button15"
android:layout_below="#+id/click_btn"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="34dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R3"
android:id="#+id/button16"
android:layout_centerVertical="true"
android:layout_alignRight="#+id/button15"
android:layout_alignEnd="#+id/button15" />
Java Code:
package com.example.android;
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.TextView;
import static com.example.android.R.*;
public class Recipes extends ActionBarActivity implements View.OnClickListener {
TextView resp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_recipes);
resp = (TextView)this.findViewById(id.response);
Button b = (Button)this.findViewById(id.click_btn);
b.setOnClickListener(this);
resp.setText("Welcome ");
}
#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_recipes, 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);
}
#Override
public void onClick(View v) {
resp.setText("Welcome ");
}
}
Set setOnClickListener for all Button's in same way as currently doing for click_btn Button .
To change text of TextView clicked use switch-case inside onClick method like:
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.click_btn: /** on click_btn button click */
resp.setText("Welcome ");
break;
case R.id.button15: /** on button15 button click */
resp.setText("Hello ");
break;
case R.id.button16: /** on button16 button click */
resp.setText("Bye ");
break;
}
}
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.)
does anybody has any idea to increase or decrease the word spacing in the text set on the textview in android. I want to do the word spacing and line spacing by providing any integer value...
May be this could help
By setting this property to your xml you can get it done
android:lineSpacingExtra
hi i had solved the problem of word spacing by replacing the single space by multiple spaces according to requirement. I am giving an example in which there are one textview and two button one is for increasing the space and one is for decreasing the space. the code is given below and used android:lineSpacingExtra for line spacing as suggested above.
package com.example.wordspacingexample;
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.TextView;
public class MainActivity extends Activity implements OnClickListener {
private TextView mTextView;
private Button mIncrease;
private Button mDecrease;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView1);
mIncrease = (Button) findViewById(R.id.btn_increase);
mDecrease = (Button) findViewById(R.id.btn_decrease);
mTextView.setTag(" ");
mIncrease.setOnClickListener(this);
mDecrease.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btn_increase) {
String space = (String) mTextView.getTag();
String text = mTextView.getText().toString();
mTextView.setText(text.replace(space, (space += " ")));
mTextView.setTag(space);
} else if (v.getId() == R.id.btn_decrease) {
String space = (String) mTextView.getTag();
String text = mTextView.getText().toString();
if (space.length() > 2) {
mTextView.setText(text.replace(space,
space = space.substring(0, space.length() - 2)));
mTextView.setTag(space);
} else if (space.length() == 2) {
mTextView.setText(text.replace(space, " "));
mTextView.setTag(" ");
}
}
}
#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;
}
}
This the code of the activity with the implementation.
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/btn_increase"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="Click to increase word space" />
<Button
android:id="#+id/btn_decrease"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_increase"
android:layout_centerHorizontal="true"
android:text="Click to decrease word space" />
Code of the xml file used. ScreenShots are given as:
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.