Despite much fiddling, I am unable to make my TextView visible.
I've set up a string which is supposed to contain the text to be displayed in the TextView, but the text is never seen in the Graphical Layout. Even if I use "android:text="text", and change the size, appearance, etc, nothing changes.
My Java code:
public class MainClass extends Activity {
float goldCount = 0.0f;
ImageView minionClick;
TextView textGoldCount;
String textTotal;
#Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
minionClick = (ImageView) findViewById(R.id.minioncentreid);
textGoldCount = (TextView) findViewById(R.id.textviewtop);
textTotal = goldCount + " Gold";
textGoldCount.setText(textTotal);`
My XML code:
`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayoutid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/mainbackground"
>
<TextView
android:id="#+id/textviewtop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="25">
</TextView>
<ImageView
android:id="#+id/minioncentreid"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="50"
android:contentDescription="#+string/desc"
android:src="#drawable/minioncentrethree"
/>
<TextView
android:id="#+id/textviewbottom"
android:layout_weight="25"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
</TextView>
`
The TextView at the bottom is there to that the image in the middle is centred. And I just deleted it and checked, but it did nothing to solve the problem.
Not sure, but maybe it's related to the values in weight and height attributes. You must to declare the height to 0 as follows:
<TextView
android:id="#+id/textviewtop"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" > // 25 and 50 can be replaced by 1 and 2
<ImageView
android:id="#+id/minioncentreid"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="2"
android:contentDescription="#+string/desc"
android:src="#drawable/minioncentrethree" />
<TextView
android:id="#+id/textviewbottom"
android:layout_weight="1"
android:layout_height="0dip"
android:layout_width="wrap_content" />
Let me know if this works.
I'll also add to Filo's answer that you should use android:weightSum attribute in your LinearLayout which should be 100 (25 + 50 + 25) according to your layout.
Related
I am creating a simple tasklist app, and I want to programmatically add a TextView to an embedded LinearLayout. However, it is not showing up for some reason.
I try to add the TextView in MainActivity (irrelevant code removed).
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
processExtraData();
}
private void processExtraData(){
Integer hours = 4;
Integer minutes = 35;
Integer seconds = 0;
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linlayout);
TextView txt1 = new TextView(this);
final float scale = this.getResources().getDisplayMetrics().density;
int pixels = (int) (64 * scale + 0.5f);
ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(0, pixels, 1f);
txt1.setLayoutParams(params); txt1.setText(hours.toString()+":"+minutes.toString()+":"+seconds.toString());
linearLayout.addView(txt1);
}
The embedded LinearLayout is declared in activity_main:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Super Reminder App!"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:orientation="horizontal">
<EditText
android:id="#+id/taskAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.5"
android:hint="New Task" />
<Button
android:id="#+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="Add Task" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="12.5"
android:orientation="horizontal">
<ListView
android:id="#+id/tasks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.5"/>
<LinearLayout
android:id="#+id/linlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"/>
</LinearLayout>
<Button
android:id="#+id/clearButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Clear All" />
</LinearLayout>
I have already tested and made sure that onNewIntent and processExtraData are called. No errors are thrown either. Could this be a GUI issue? Any help would be greatly appreciated.
I have a LinearLayout set to a static height of 100dp. I then have a textView inside it on which I set height to WRAP_CONTENT. The problem is the textView is causing the LinearLayout to expand fully to accommodate the textView when the height of the textView is greater than 100dp
I set it up this way because I want to be able to show and hide the full text when the user clicks a button. I thought setting the linearlayout's height to some minimum and then updating it to WRAP_CONTENT when clicked would work, but when I try to limit the height, the LinearLayout ignores it. I tried to set clipChildren to true but that didn't help. Code is below. I am using Android Annotations, but I don't think that's related to the problem.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp"
android:fitsSystemWindows="true"
android:background="#color/light_grey">
<LinearLayout
android:orientation="vertical"
android:clipChildren="true"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And the Java code:
#EActivity(R.layout.activity_test)
public class TestActivity extends AppCompatActivity {
#ViewById(R.id.text_view)
TextView textView;
String input;
public static final String INPUT_STRING_KEY = "inputStringKeyForParser";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.input = this.getIntent().getExtras().getString(INPUT_STRING_KEY);
}
#AfterViews
public void showText() {
textView.setText(input);
}
}
Does anyone know how to get the TextView to stop causing the linearlayout to expand? Thank you!
Try
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="100dp" />
Ok. I think that you can't apply height property this way because your layout is the child of ScrollView. Put your TextView inside another LinearLayout like this:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp"
android:fitsSystemWindows="true"
android:background="#color/light_grey">
<LinearLayout
android:orientation="vertical"
android:clipChildren="true"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
I'm trying to make the content of a textview to automatically scroll to the bottom at the most recent text appended. I tried many ways from many post but seems there's no way to make it work. I trie also different layout_wheight and width parameters and so on. The silly thing is that I already made it in the past and it worked, but now using the same set up it is not.
Here's my latest xml code:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.5"
android:weightSum="2">
<ScrollView
android:id="#+id/askScv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical">
<TextView
android:id="#+id/askTxv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:maxLines="99999"
android:scrollbars="vertical"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</ScrollView>
<ScrollView
android:id="#+id/answerScv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical">
<TextView
android:id="#+id/answerTxv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:maxLines="99999"
android:scrollbars="vertical"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</ScrollView>
</TableRow>
and the Java one:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mQuestionEdt = (EditText) findViewById(R.id.questionEdt);
mAnswerTxv = (TextView) findViewById(R.id.answerTxv);
mAnswerTxv.setMovementMethod(new ScrollingMovementMethod());
mAnswerScv=(ScrollView)findViewById(R.id.answerScv);
mAnswerScv.fullScroll(View.FOCUS_DOWN);
mAskTxv=(TextView) findViewById(R.id.askTxv);
mAskTxv.setMovementMethod(new ScrollingMovementMethod());
mAskScv=(ScrollView)findViewById(R.id.askScv);
mAskScv.fullScroll(View.FOCUS_DOWN);
mAskBtn = (ImageButton) findViewById(R.id.askBtn);
mAskBtn.setOnClickListener(this);
mStartBtn = (ImageButton) findViewById(R.id.startBtn);
mStartBtn.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Bundle result;
if (view.equals(mAskBtn)) {
result=mAnsweringMachine.answer(mQuestionEdt.getText().toString());
mAskTxv.append("\n" + mQuestionEdt.getText().toString());
mAskScv.smoothScrollTo(0,mAskTxv.getBottom());
mAnswerTxv.append("\n" + result.getString(AnsweringMachine.DIALOGUE_RESULT));
mAnswerScv.smoothScrollTo(0,mAnswerTxv.getBottom());
} else if (view.equals(mStartBtn)) {
result = mAnsweringMachine.runStartingPrompt();
mAnswerTxv.append(result.getString(AnsweringMachine.DIALOGUE_RESULT));
//mAnswerScv.fullScroll(View.FOCUS_DOWN);
}
}
And here's my old working code, xml:
<ScrollView
android:id="#+id/terminalScv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/terminalTxv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge" />
</ScrollView>
and Java one:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_terminal);
terminalTxv=(TextView) findViewById(R.id.terminalTxv);
terminalScv=(ScrollView) findViewById(R.id.terminalScv);
terminalScv.fullScroll(View.FOCUS_DOWN);
}
#Override
protected void onBtDataReceived(int[] data) {
super.onBtDataReceived(data);
String out=String.valueOf(terminalTxv.getLineCount())+" - ";
for (int x=0;x<data.length-1;x++){
out+=data[x];
out+=" ";
}
out+="\n";
terminalTxv.append(out);
terminalScv.smoothScrollTo(0, terminalTxv.getBottom());
}
Hope somebody can make me understand this enigma....
Thanks!
In the XML for the textView element I would try to add:
android:gravity="bottom"
Also are you sure you need to specify android:scrollbars="vertical" in both scrollview and textview? In addition, I noticed the difference between old working version is that it doesn't uses setMovementMethod() - personally I would use this method without scrollview, but since you already have scroollview I don't see a reason why would you need it...
I am trying to make a simple Checkbook app, whose MainActivity stores a list of transactions. I would like a TextView at the top and bottom of the screen that show the account balance and an option to add a new transaction, respectively. I would like a list of transactions in between that scroll. I was able to implement a ListView and add a header and footer view, but if the transaction list exceeds the size of the screen the headers and footers can scroll off screen.
Is there any way to position a ListView within the linear layout, or freeze the headers/footers to stay on the screen?
Here is my XML file so far:
<TextView
android:id="#+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/add_transaction_string">
</TextView>
And here is my onCreate, which has no syntax errors but I am unable to click the footerview to add a transaction:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbook);
ListView list = (ListView) findViewById(R.id.transaction_list_view);
// Create a new Adapter
mAdapter = new TransactionAdapter(list.getContext());
// Inflate footerView and headerView
LayoutInflater inflater = getLayoutInflater();
TextView headerView = (TextView) inflater.inflate(R.layout.header_view, null);
TextView footerView = (TextView) inflater.inflate(R.layout.footer_view, null);
// Set listener for footerView
footerView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent transactionIntent = new Intent(CheckbookActivity.this, AddTransactionActivity.class);
startActivityForResult(transactionIntent, ADD_TRANSACTION_REQUEST);
}
});
list.setAdapter(mAdapter);
}
use the below code. This will satisfy your requirement. I tried this and working for me.
Relative layout with below,above attributes. Relativelayout is better than Linear layout with weight method.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:text="ListView Heading" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="ListView Footer" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView1"
android:layout_above="#id/textView2"
></ListView>
The UI will like this
Try this way, hope this will help you to solve your problem.
Instead of using header/footer just put as below code in your XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/header_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_transaction_string">
</TextView>
</LinearLayout>
Yes, you can do it with weightsum and layout_weight in linearlayout and also you can create this type of view using RelativeLayout.
1) In LinearLayout just add weightsum="1" to your linearlayout and add layout_weight="0.2" to each of your header and footer and add layout_weight="0.6" to your listview.
2) In relativeLayout add alignParentTop to your header and alignParentBottom to your footer and set listview to layout_below="#+id/header" and layout_above="2+id/footer"
I found a possible solution for your problem from a similiar post. Hope this helps you.
For what you are trying to accomplish to freeze the header/footer. It will be easier to use a relative layout to position the header/footer then have your listview in the middle
<RelativeLayout ...>
<TextView
android:id="#+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header_view"
android:layout_above="#+id/footer_view">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="#string/add_transaction_string">
</TextView>
</RelativeLayout>
You can use a LinearLayout for this task. But I don't recommend it as it's a bit "hacky".
Get all the elements in a array: Example:- (weatherArray)
Loop through all the elements :-
Example:-
mainLayout = ((LinearLayout)refreshObj.get("mainLayout"));
mainLayout.removeAllViews();
for (int i = 0; i < weatherArray.length(); i++) {
View childView = getLayoutInflater().inflate(R.layout.weather_row4_item, mainLayout,false);
TextView todayTempStatus = (TextView) childView.findViewById(R.id.todayTempStatus);
todayTempStatus.setText("");
}
This is an example without using listview, which we will populate lienarlayout using child view.
pretty easy question here, but I am not a java superuser quite yet.
I am using a ViewFlipper to provide a number of images, and a sliding-drawer to contain text specific to each image. I would like to have the text of the sliding drawer change to a specific string dependent on which child view is currently displayed.
Here is the XML:
<?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:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev" />
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" />
</LinearLayout>
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="#+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/image1"></ImageView>
<ImageView android:id="#+id/imageView2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/image2"></ImageView>
</ViewFlipper>
</LinearLayout>
here is the java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flipper = (ViewFlipper)findViewById(R.id.flipper);
next = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
imageview1 = (ImageView)findViewById(R.id.imageView1);
imageview2 = (ImageView)findViewById(R.id.imageView2);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == next) {
flipper.setInAnimation(inFromRightAnimation());
flipper.setOutAnimation(outToLeftAnimation());
flipper.showNext();
}
if (v == previous) {
flipper.setInAnimation(inFromLeftAnimation());
flipper.setOutAnimation(outToRightAnimation());
flipper.showPrevious();
}
}
I'm sure the answer is really obvious, but that's why I need your help....THANKS!
When you call showNext() and showPrevious(), update your TextView to match.