TextView moves one line down when text changes programmatically - android

This is a simple shell app that will display number of files in data/app folder when button is clicked the textviews are aligned as they should be, but when the button is clicked the textview that displays the number of files is moved down one line. Why?
Here is the code and the main.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="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="32dp"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:text="#string/user_apps" />
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView2"
android:text="0" />
</RelativeLayout>
code:
package rs.test.rootapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class RootAppActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String command[]={"su", "-c","ls /data/app | wc -l"};
Shell shell = new Shell();
String text = shell.sendShellCommand(command);
setNewTextInTextView(text);
}
});
}
public void setNewTextInTextView(String text){
//TextView tv= new TextView (this);
//tv.setText(text);
//setContentView(tv);
TextView tv =(TextView) findViewById(R.id.tv);
tv.setText(text);
}
}
Screenshots:

set the text in textview after done the trimming operation on it,may be this will work, try it
tv.setText(text.trim());

With looking very quickly it seems like the return from the command is coming with the text.. Try and replace the new line in text before setting the text in the TextView.

Related

Using EditText in ViewSwitcher doesn't enable it to be editable at first touch

I am using ViewSwitcher to switch between EditText and TextView. When I click the TextView for the first time, it changes to EditText and then I have to click EditText again so that the keyboard appears and I to be able to edit the text.
The behavior I want is to be able to edit the text from the first click on the TextView. I don't want the user to click twice each time they want to edit the text.
Is there any way to do that ?
Here is a sample of the code to make it more clear:
XML code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Trial">
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/user_name_switcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<TextView
android:id="#+id/username_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="12dp"
android:paddingTop="8dp"
android:text="User Name"
android:textColor="#android:color/black"
android:textSize="18sp" />
<EditText
android:id="#+id/username_et"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="User Name"
android:inputType="textPersonName" />
</ViewSwitcher>
<Button
android:id="#+id/save_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="24dp"
android:layout_marginTop="24dp"
android:padding="12dp"
android:text="save" />
</LinearLayout>
Java code
package com.example.test
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ViewSwitcher;
public class Trial extends AppCompatActivity {
TextView userNameTV;
EditText userNameET;
Button saveBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trial);
userNameTV = findViewById(R.id.username_tv);
userNameET = findViewById(R.id.username_et);
saveBtn = findViewById(R.id.save_btn);
saveBtn.setEnabled(false);
userNameTV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textViewClicked();
userNameET.setText(userNameTV.getText().toString());
}
});
saveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ViewSwitcher switcher = findViewById(R.id.user_name_switcher);
switcher.showNext();
saveBtn.setEnabled(false);
}
});
}
public void textViewClicked() {
ViewSwitcher switcher = findViewById(R.id.user_name_switcher);
switcher.showNext();
saveBtn.setEnabled(true);
}
}

Android TextView doesn't show affected text

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

Android Development Button

I am learning to make android apps and I have a problem with my hello world programs buttons.
Here's my code:
package com.Norwood.helloandroid; import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android.");
setContentView(tv);
}
public void onclick01(View View)
{
Toast.makeText(this, "Will change in 1.2", Toast.LENGTH_SHORT).show();
}
public void onclick02(View View)
{
Toast.makeText(this, "Will change in 1.2", Toast.LENGTH_SHORT).show();
}
}
And my XML file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
android:textSize="60sp" />
<Button
android:id="#+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:minHeight="92sp"
android:onClick="onclick01"
android:text="#string/ChangeCoulour"
android:textSize="22sp" />
<Button
android:id="#+id/Button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:minHeight="92sp"
android:onClick="onclick02"
android:text="#string/Change_Text_Colour"
android:textSize="22sp" />
</RelativeLayout>
But when I run the app both on the emulator and on my android the buttons don't show up.
And they show up in the graphical layout of the XML file.
Thanks
Collin N.
try:
setContentView(R.layout.yourXml)
You cannot do it your way because you must set your view to your xml first. have setContentview on the line after onCreate
TextView tv = new TextView(this);
tv.setText("Hello, Android.");
setContentView(tv);//wrong
reply to comment:
Like this:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_XML_FILE_NAME);
TextView tv = new TextView(this);
tv.setText("Hello, Android.");
}

creating a simple layout on android

I'm very new with android development. I'm trying to develop an application, I decided to try out the code below but I keep getting errors when I try to open it on the emulator. please can anyone tell me what I'm doing wrong?
package hajara.android.MyRecipes;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MyRecipesActivity extends Activity {
Button btn;
TextView t1, t2;
EditText e;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
t1 = (TextView)findViewById(R.id.text1);
t2 = (TextView)findViewById(R.id.text2);
e = (EditText)findViewById(R.id.edit1);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener((OnClickListener) this);
}
}
My main.xml file is
<?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" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter a string:"
/>
<EditText android:id="#+id/edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:editable="true"
android:singleLine="true"
/>
<Button android:id="#+id/button1"
android:text="OK"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
Your onClick listener is done incorrectly. I find it easier to simply create a method such as:
public void buttonOnClick(View v) {
// Do something
}
and within your XML layout file (ie. main.xml) invoke the onClick attribute:
<Button
...
android:onClick="buttonOnClick"
...
/>
I think you haven't Declared button Properly.
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Your Code Goes here
}
});
And Hope you have Declared your Activity in Manifest File.

Button click event issure, layout,and parsing issue?

There are three problems I am facing:
When I click(+)button, the edit boxes are going underneath the button, whereas i want them to be displayed above.
currently displaying:
Want like this:
<edit text1> <edit text2>
<edit text3> <edit text4>
<edit text5> <edit text6>
<edit text7> <edit text8>
<button> <button2>
As you can see, I tried parsing the edit text value which i got from the xml, into the docalc() function, and displaying the value in textbox. but its not working out. showing me nothing.
can I parse values in dostuff, if yes how will i inter-relate those in docalc?
Thanks in advance...
Java Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PlusbuttonActivity extends Activity
implements OnClickListener {
TextView tt;
TextView j;
EditText amount1;
EditText amount2;
double x=0;
double y=0;
double a=0;
double z=0;
double b=0;
Button btnButton;
/** Called when the activity is first created. */
private LinearLayout root;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// modified
amount1=(EditText)findViewById(R.id.edittext1);
amount2=(EditText)findViewById(R.id.edittext2);
// modified
View btnButton =(Button) findViewById(R.id.button_next);
Button mButton = (Button) findViewById(R.id.button);
mButton.setGravity(Gravity.CENTER);
tt=(TextView)findViewById(R.id.tt);
j=(TextView)findViewById(R.id.j);
root = (LinearLayout) findViewById(R.id.linearLayout);
mButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
View view = doStuff();
addViewToRoot(view);
break;
case R.id.button_next:
View view1 = doCalc();
addViewToRoot(view1);
break;
}
}
private View doCalc() {
// TODO Auto-generated method stub
x=Double.parseDouble(amount1.getText().toString());
y=Double.parseDouble(amount2.getText().toString());
z=(x*703);
tt.setText(Double.toString(z));
return tt;
}
private View doStuff() {
EditText t = new EditText(PlusbuttonActivity.this);
t.setGravity(Gravity.LEFT);
t.setWidth(250);
EditText a = new EditText(PlusbuttonActivity.this);
a.setGravity(Gravity.RIGHT);
a.setWidth(250);
LinearLayout l = new LinearLayout(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// t.setBackgroundColor(0xffCECECE);
a.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.addView(t);
l.addView(a);
return l;
}
private void addViewToRoot(View v){
root.addView(v);
}
}
xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
android:text="Units"
android:id="#+id/Units"
/>
<TextView android:layout_height="wrap_content"
android:gravity="right"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="Grades"
android:id="#+id/j"></TextView>
</LinearLayout>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="250px"
android:layout_height="wrap_content"
android:id="#+id/edittext1">
</EditText>
<EditText
android:layout_height="wrap_content"
android:id="#+id/edittext2" android:layout_width="150dp">
</EditText>
</LinearLayout>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="250px"
android:layout_height="wrap_content"
android:id="#+id/edittext3">
</EditText>
<EditText
android:layout_height="wrap_content"
android:id="#+id/edittext4" android:layout_width="150dp">
</EditText>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button android:layout_height="wrap_content"
android:gravity="center" android:id="#+id/button" android:text="+" android:layout_width="wrap_content"></Button>
<Button
android:id="#+id/button_next"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:text="CALCULATE"
>
</Button>
</RelativeLayout>
<TextView android:text="TextView"
android:id="#+id/tt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
First, set your LinearLayout's id to something other than main. Like root. Main is the name of the xml file containing your layout, not the id of the LinearLayout item.
Second, Instead of using this in the constructor for the EditText, use PlusbuttonActivity.this. So change the line you use to create the EditText to this:
EditText t = new EditText(PlusbuttonActivity.this);
You have to do this because when you're in the onClick method your technically in the OnClickListener class, and this refers to the OnClickListener object. By using PlusbuttonActivity.this you're clarifying that you mean the PlusbuttonActivity object that you're currently in, not the OnClickListener.
Third, to address the other issue, you can't reference root from you anonymous OnClickListener class. Instead of calling root.addView(t), you can extract that out to a method. Putting it all together we get this:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class PlusbuttonActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout root;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButton = (Button) findViewById(R.id.button1);
root = (LinearLayout) findViewById(R.id.root);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText t = new EditText(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addViewToRoot(t);
}
});
}
private void addViewToRoot(View v){
root.addView(v);
}
}
Change the id of LinearLayout in main.xml, use different name.
Save & Clean project.
Fix like #Kurtis Nusbaum mentioned above!

Categories

Resources