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?
Related
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.
I am trying to create a radio group with two button on and off in Android Studio. I already created the radio group with two button. I want when the user clicks on an picture will appear beneath them and if the user clicks off the picture will disappear. Thank you.
Here is my Java code:
package com.example.android.testapp;
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.ImageView;
import android.widget.RadioButton;
import android.widget.ToggleButton;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static com.example.android.testapp.R.id.imageView;
import static com.example.android.testapp.R.id.radioButton;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioButton rb = (RadioButton) findViewById(R.id.radioButton);
ImageView image = (ImageView) findViewById(R.id.imageView);
if(rb.ischecked()){ image.setVisibility(View.VISIBLE);
} else {
image.setVisibility(View.GONE);
}}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is 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: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">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="200dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On"
android:id="#+id/radioButton"
android:checked="true"
android:enabled="true"
android:clickable="true"
android:onClick="SetImage"
android:nestedScrollingEnabled="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Off"
android:id="#+id/radioButton2"
android:checked="false" />
</RadioGroup>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/ic_launcher"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="90dp"
android:layout_marginStart="90dp"
android:layout_marginBottom="130dp"
/>
There is no need to have 2 radio buttons if you only want a on/off state. One is enough and when it will be checked then you can make your picture appear and if it's unchecked, you can make it disappear. How to do this, in your onCreate method, you can write:
RadioButton rb = (RadioButton) findViewById(R.id.radiobutton);
ImageView image = (ImageView) findViewById(R.id.image);
if(rb.isChecked()){
image.setVisibility(View.VISIBLE);
} else {
image.setVisibility(View.GONE);
}
Set setOnCheckedChangeListener for RadioGroup to change image visibility according to RadioButton click.
1. Add id to RadioGroup in xml :
android:id="#+id/radioGroup"
2. Add setOnCheckedChangeListener to RadioGroup in onCreate method after setContentView :
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.radioButton) {
image.setVisibility(View.VISIBLE);
} else if(checkedId == R.id.radioButton2) {
image.setVisibility(View.GONE);
}
}});
I have created an application where users can send and share text that they have input, if they click the send button, the text that they input will be displayed, if they click the share button, the application opens up a list of sharing methods (GMail, Messaging etc..), what I want is though, to allow the users to view there text and then share it, hwoever, when the user clicks send and it goes to the file activity_display, the text shows, but the button does not. Any ideas why? Could you fix this? Here's the code;
activity_display_message.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=".DisplayMessageActivity" >
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="shareMessage"
android:layout_below="#+id/text_view"
android:text="test" />
</RelativeLayout>
MAIN PROBLEM: Button Share does not show.
EDIT: I suspect I may need to include something to this file, here's the code, any help?
DisplayMessageActivity.java:
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
public class DisplayMessageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Since you are using RelativeLayout you need to specify where to layout your views. Try this for example:
<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=".DisplayMessageActivity" >
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="shareMessage"
android:layout_below="#+id/text_view"
android:text="test" />
you are using relative layout. use linear layout instead. this will solve your problem.
You are using a relative layout. The objects are overlapping because you have not set them up to be LeftOf or Below or something like that.
Just replace your button with:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:onClick="shareMessage"
android:text="#string/button_share" />
And TextView with this:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
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.)
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!