Android TextView doesn't show affected text - android

Ok so... I've been trying to affect text to TextView (because it's meant to show text, right?)
But for some unknown to me reason it refuses to print whatever I put into it.
My application has two activities, the MainAcivity, or a welcoming screen, per se, and a GameActivity (the project is a Domino game).
I create my previously written java objects at the beginning of the GameActivity class, trying then to show player's name within a TextView.
Player player1 = new Player("Hulk");
creates a player with name Hulk, as you can imagine. I have a method within Player.java to return player's name:
player1.getName()
returns string "Hulk"
I then try to set my TextView's text to hulk, first off by creating a handle to it with:
TextView p1v = (TextView) findViewById(R.textviews.p1view);
then doing the following:
p1v.setText("Player 1 : " + player1.toString());
the p1view is defined as follows within game.xml layout:
<LinearLayout
...
<LinearLayout
...
<TextView
android:id="#+textviews/p1view"
android:text="Board"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
I tried p1view.textAppend, tried using android:editable="true". Nothing seems to work.
Funnily enough though, when I try doing the same thing, within MainActivity, it works, as so:
public class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
with the TextView defined within main.xml as follows:
<TextView
android:id="#+textviews/mainview_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Following are the full codes of the files, just to avoid answers like: Give us the full code D:
GameActivity.java:
package domino.asd;
import LI260.*; import android.app.Activity; import android.os.Bundle;
import android.widget.TextView;
public class GameActivity extends Activity {
private Player player1 = new Player("HULK");
private Player player2 = new Player("CPU");
private Pioche bag = new Pioche();
private Plateau board = new Plateau();
private Game game1 = new Game(bag, board, player1, player2);
TextView p1name = null;
TextView p2name = null;
/**
* Called when the activity is first created.
*
* #param savedInstanceState
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
TextView tv = (TextView) findViewById(R.textviews.p1view);
tv.setText(player1.getName());
} }
game.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="10"
>
<!-- Played layout -->
<LinearLayout
android:id="#+drawable/gameview_Scores"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_gravity="top"
android:layout_weight="2"
android:layout_marginBottom="10px"
android:orientation="horizontal"
android:background="#ff0000"
android:gravity="center"
>
<Button
android:id="#+buttons/exitButton"
android:text="Exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
/>
<Button
android:id="#+buttons/drawButton"
android:text="Draw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+buttons/scoresButton"
android:text="Show scores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- Board layout -->
<LinearLayout
android:id="#+textviews/gameview_Table"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_gravity="center"
android:layout_weight="6"
android:layout_marginBottom="10px"
android:background="#ffff00"
>
<TextView
android:id="#+textviews/p1view"
android:text="Board"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- At hand layout -->
<LinearLayout
android:id="#+drawable/gameview_Playable"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="2"
android:layout_gravity="bottom"
android:background="#ff00ff"
>
<TextView
android:id="#+textviews/gameview_AtHandText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
MainActivity.java:
package domino.asd;
import LI260.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Player p1 = new Player("Hulk");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
}
public void btnClick_Name(View view) {
if (view.getId() == R.buttons.B_EnterName) {
EditText playersname = (EditText) findViewById(R.string.playerNameInput);
Toast.makeText(this, "Your name: " + playersname.getText().toString(), Toast.LENGTH_SHORT).show();
}
setContentView(R.layout.game);
}
}
and lastly main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<EditText
android:id="#+string/playerNameInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="1"
/>
<TextView
android:id="#+textviews/mainview_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+buttons/B_EnterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Save name and start game."
android:onClick="btnClick_Name"
/>
</LinearLayout>

When you are first loading the activity, you are calling this code:
setContentView(R.layout.main);
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
Yet when the user click's the button you are only calling this code:
setContentView(R.layout.game);
Because you have reset the main content view the child view mainview_player has been destroyed and replaced with a new one when inflated from R.layout.game.
You would need to call this code again to find the new view in the game layout and populate it with your player name:
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
You would be better to look into how activities relate to one another and start a new activity for R.layout.game

Related

TextView gets overlaped(instead of Overwriting)in a Preference Screen

I am trying to build a preference screen which consists of a Layout with some TextViews and EditText Preference.
When I use textView.setText(inputString) in the Activity, it does not overwrite the existing TextView, instead it creates a new copy of TextView with inputString as text and it overlaps with the existing TextView. Is there any way that the setText overwrites the existing TextView itself.
This is the XML Layout with the TextView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#color/blue"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"
></ListView>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:textColor="#color/pseudoBlack"
android:layout_height="wrap_content"
android:layout_marginStart="59dp"
android:layout_marginTop="100dp"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:text="TextView"
android:textColor="#color/pseudoBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView5"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_toEndOf="#+id/textView2"
android:layout_marginStart="100dp" />
<TextView
android:text="TextView"
android:textColor="#color/pseudoBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="43dp"
android:id="#+id/textView11"
android:layout_below="#+id/textView2"
android:layout_toEndOf="#+id/textView2"
android:layout_marginStart="21dp" />
</RelativeLayout>
And this is the Preference Screen I am using.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
>
<Preference android:layout="#layout/inner" android:key="innerkey"
/>
<Preference android:key="mskey"
android:title="show"
/>
<EditTextPreference android:key="editpref"
android:title="Add"
/>
</PreferenceScreen>
This is the Activity Class in which I am trying to do setText().
package com.example.asus.layoutcheck;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.widget.TextView;
public class SecondaryActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inner);
TextView txt1 = (TextView)findViewById(R.id.textView2);
TextView txt2 = (TextView)findViewById(R.id.textView5);
TextView txt3 = (TextView)findViewById(R.id.textView11);
txt1.setText("First Text");
txt2.setText("Second Text");
txt3.setText("Third Text");
final String message = "This is the message" ;
addPreferencesFromResource(R.xml.pref);
final Preference msg = findPreference("mskey");
msg.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
msg.setSummary(message);
return true;
}
});
Preference edit = findPreference("editpref");
}
}
I have added the screenshot of the result.
How can I overcome this Issue?Thanks in advance
replace setContentView(R.layout.inner); with addPreferencesFromResource(//your preferenceScreen);
I've encountered this similar issue, and it happens with other view as well like a TextureView inside a FrameLayout. I resized my TextureView smaller and I can still see the previous TextureView in the back flickering.
What made it work is funny -> I just put a black background color (probably any color) to my layout (LinearLayout) and it is working fine now.
I added: android:background="#000000"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical"
android:visibility="visible"
tools:context=".CameraActivity">
It seems the background is not cleared, if it doesn't know what color to clear it with.

Android - HorizontalScrollView will not scroll all the way

This is bugging me for a while now, Im using HorizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT) which scrolls my ScrollView to the right, but not all the way, it's takes the scrollbar 99% percent to the right, I can manually scroll the rest of the way but for some reason it does not do it when i call the fullScroll() API.
Here's a sample code.... If you push the button a few times, the TextView will expand and you'll see the problem.
Activity:
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
HorizontalScrollView hsv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
hsv = (HorizontalScrollView)findViewById(R.id.horizontalScrollView1);
}
public void btnOnClick(View v) {
tv.append("a");
hsv.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="push the button" />
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnOnClick"
android:text="Button" />
</LinearLayout>
I updated your code. Replace your code with below one. Which perfectly moves HorizontalScrollView to right of the screen.
Replace Your MainActivity.java with below one:
public class AndroidHTMLActivity extends Activity{
TextView tv;
HorizontalScrollView hsv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
}
public void btnOnClick(View v){
tv.append("a");
System.out.println("tv.getWidth()="+tv.getWidth());
hsv = (HorizontalScrollView)findViewById(R.id.horizontalScrollView1);
ViewTreeObserver vto = hsv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
#Override
public void onGlobalLayout() {
hsv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
hsv.scrollTo(tv.getWidth(), 0);
}
});
}
}
And replace youe Xml file with below one:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/ll_parent_layout">
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="push the button" />
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnOnClick"
android:text="Button" />
</LinearLayout>
This code fulfile your condition which you wants.

how to assign a string value associated with an image to a textwidget when image is clicked

package com.me.trial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class TrialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setClickable(true);
OnClickListener l;
}
private void hasBeenClicked(<method invoked when the user has clicked>){
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="56dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="64dp" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitEnd"
android:tag="Employee name"
android:src="#drawable/img18" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
I need the application to pass some string value associated to it to be passed into the text as soon as the user clicks on the image.How do i use the click Listeners in android to achieve the following task.
It is not necessary to fill in the code snippet that i have given.Any new ideas regarding the design are most welcome.
U can set text as Tag to the Image so that u can get that text onClick event. Tag can be set either in XML or dynamically.
public class TrialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setTag("YOur text");
img.setClickable(true);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String urText= arg0.getTag().toString();
EditText edt = (EditText)findViewById(R.id.editText1);
edt.setText(urText);
}
});
}
}
This code demonstrates how to achieve your requirement dynamically.

Android auto focus change

i want to have a set of buttons and the focus on buttons should change periodically first i want to try it with 2 buttons.this is my main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/number_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.72"
android:focusable="true"
android:gravity="center"
android:text="#string/number"
android:textSize="10pt" android:clickable="true"
android:focusableInTouchMode="true"/>
<Button
android:text="#string/contact"
android:textSize="10pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/contact_button"
android:layout_weight="1" android:layout_gravity="center"
android:focusable="true" android:gravity="center"
android:focusableInTouchMode="true"/>
</LinearLayout>
in the source code i have used FOCUS_LEFT,RIGHT and all other options ,but i am getting a run time exception. the orientation is set as vertical in linear layout.Please help me to sort out this problem, if anybody know how to set the timer option as well post it here
here is the code
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class HelloandroidActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Object o=null;
// o.toString();
// TextView tv = new TextView(this);
// tv.setText("Hello Andriod");
setContentView(R.layout.main);
Button mybtn= (Button)findViewById(R.id.number_button);
mybtn.requestFocus();
try
{
Button mybtn2= (Button)mybtn.focusSearch(View.FOCUS_DOWN);
mybtn2.requestFocus();
}
catch(Exception e)
{
Log.e("focus change","focus failed",e);
}
}
}
The problem is that you is finding a control in mybtn. Inside your activity you should call focusSearch in Activity context. Try this:
Button mybtn2 = (Button) focusSearch(View.FOCUS_DOWN);
if (mybtn2 != null)
mybtn2.requestFocus();

Android sliding menu to change content on page can this be done?

The main page has a horizontalScrollView across the top and a number of TextView text fields within it. I would like to be able to scroll and click on any on the TextView text and it change the RelativeLayout to match the selection made in the HorizontalScrollView.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<HorizontalScrollView android:id="#+id/horizontalScrollView1" android:layout_height="wrap_content" android:scrollbars="none" android:layout_width="wrap_content">
<LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent" android:id="#+id/linearLayout1">
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTOne" android:text="tOne"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTTwo" android:text="tTwo"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTThree" android:text="tThree"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTFour" android:text="tFour"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTFive" android:text="tFive"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTSix" android:text="tSix"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTSeven" android:text="tSeven"></TextView>
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/relativeLayout1"></RelativeLayout>
</LinearLayout>
I have a second layout page called tone which would be linked to the menu option tOne in the HorizontalScrollView.
tone.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is tOne"></TextView>
</LinearLayout>
The main activity will flag if one of the first 4 options are selected by writing to the LogCat. Is it possible to display the tone.xml within the relativelayout if the user clicks the tOne option from the HorizontalScrollView? Then if the user selects tTwo display ttwo.xml in the RelativeLayoutView etc?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SlideMenu extends Activity implements OnClickListener {
/** Called when the activity is first created. */
TextView tOne, tTwo, tThree, tFour, tFive, tSix, tSeven, tEight, tNine, tTen;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout vSpace = (RelativeLayout)findViewById(R.id.relativeLayout1);
tOne = (TextView)findViewById(R.id.EditTOne);
tTwo = (TextView)findViewById(R.id.EditTTwo);
tThree = (TextView)findViewById(R.id.EditTThree);
tFour = (TextView)findViewById(R.id.EditTFour);
tFive = (TextView)findViewById(R.id.EditTFive);
tSix = (TextView)findViewById(R.id.EditTSix);
tSeven = (TextView)findViewById(R.id.EditTSeven);
tOne.setOnClickListener(this);
tTwo.setOnClickListener(this);
tThree.setOnClickListener(this);
tFour.setOnClickListener(this);
tFive.setOnClickListener(this);
tSix.setOnClickListener(this);
tSeven.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
//--------------
case R.id.EditTOne:
System.out.println("Text One pressed");
break;
case R.id.EditTTwo:
System.out.println("Text Two pressed");
break;
case R.id.EditTThree:
System.out.println("Text Three pressed");
break;
case R.id.EditTFour:
System.out.println("Text Four pressed");
break;
}
}
}
I believe it is fairly simple. I added
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.tone, vSpace);
after you print "Text One Pressed". Be sure to make vSpace a field first.
To do this with all of them, call
vSpace.removeAllViews()
before you inflate each view.

Categories

Resources