how to set visibility options of android.widget.textview - android

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.)

Related

android gravity in textview is not working

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?

ArrayAdapter<Button> displays buttons and object reference behind it in ListView

this is my first "project" with android and I'm trying to make a single-activity app that displays a list of buttons using a listview. For some reason my ArrayAdapter displays the button correctly but behind it, it displays the object reference. So when I debug it using my phone, I see a button and right behind it its object reference.
My activity looks like this:
package com.example.madelenko.showcase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ArrayList<Button> projectList =
new ArrayList<Button>();
Button button1 = new Button(this);
Button button2 = new Button(this);
button1.setText("Spotify Streamer");
button2.setText("Scores App");
projectList.add(button1);
projectList.add(button2);
ArrayAdapter<Button> adapter =
new ArrayAdapter<Button>(this,R.layout.element_listview_layout,
R.id.list_item_string,projectList);
ListView listView = (ListView)findViewById(R.id.buttonListView);
listView.setAdapter(adapter);
}
#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);
}
}
My button element layout looks like this:
<?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">
<TextView
android:id="#+id/list_item_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:textSize="18sp"
android:textStyle="bold"
android:text=""/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/projectButton"
android:text="Hello world!"
android:layout_alignTop="#+id/list_item_string"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Finally, my listview looks like this:
<FrameLayout 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.madelenko.showcase.MainActivityFragment"
tools:showIn="#layout/activity_main">
<ListView
android:layout_width="wrap_content"
android:layout_height="401dp"
android:id="#+id/buttonListView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My nanodegree apps!"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal|top" />
</FrameLayout>
Please, be patient with this newbie!
ArrayAdapter<Button> is not a recommended pattern. The adapter is supposed to be adapting model objects, whether those are trivial (e.g., ArrayAdapter<String>) or more complex (e.g., ArrayAdapter<Invoice>).
So, in this case:
You are creating an ArrayList<Button>, but those buttons will never be shown on the screen
You are inflating a layout for the rows that contains a TextView and a Button; those Button widgets will be the ones that are shown
You are telling ArrayAdapter to bind the String representation of the Button (from a call to toString() from the ArrayList to the list_item_string TextView in the row, when you create your ArrayAdapter<Button> instance
Also note that it is rather unusual to see buttons in ListView rows, since the user can click on the ListView rows themselves.
If you want to have a ListView with two rows for your two strings, you can do something like this:
public class ListViewDemo extends ListActivity {
private static final String[] items={"Spotify Streamer", "Scores App"};
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
items));
}
}

How to create buttons that display images?

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.

Button not showing up on screen Android Application

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" />

How to implement word spacing in textview in android by coding at runtime

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:

Categories

Resources