If i am adding multiple text views in android it gets hide i need to break when screen finished
As an example my code is something like this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout5"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="last checking text"/>
</LinearLayout>
and the result seems to be
I need the text to break down when screen finished down
and break to another line.
this is only the example code i am doing this thing programmatically, so please dont answer of adding more linear layout.
For flexible layout you can go with FlexboxLayout and details you can get from Android Developers Blog and for Open source FlexboxLayout dependency you can reach to Github.
If you want your textView appearing with multiple line with a scrollable properties, then no need to use multiple textView.
Check the bellow solution:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"`enter code here`
android:background="#000000"
android:orientation="vertical" >
<TextView
android:id="#+id/textView_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#40000000"
android:maxLines="100"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:scrollbarThumbVertical="#android:color/transparent"
android:scrollbars="vertical"
android:textColor="#color/white"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
And bellow code on your Activity:
TextView txtView = (TextView) findViewById(R.id.TextView_id);
txtView.setText("your text here");
txtView.setMovementMethod(new ScrollingMovementMethod());
If You want to create TextView programmatically and make Listener for each .
Here is the Code . First remove all textview in layout.
TextView tv[] = new TextView[subCategory.length];
for (int i = 0; i < subCategory.length; i++) {
tv[i] = new TextView(this);
tv[i].setText(subCategory[i]);
tv[i].setId(i);
layout5.addView(tv[i]);
tv[i].setOnClickListener(onclicklistener);
}
And For Listner
OnClickListener onclicklistener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == tv[0]){
//do whatever you want....
}
}
};
If you want to use number of textviews at run time, You can try using GridView as
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
And Gridview item as TextView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text"
/>
You can perform action in onItemClickListener() //based on the position or textview text, choose action using switch case or something else.
GridView gridView = (GridView) findViewById(R.id.grid_view);
gridView.setAdapter(yourCustomAdapterObject);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//here view is your textview
switch (position) {
case 0: //action
break;
case 1://action
break;
....
}
}
});
Related
I am making a gridlayout which contains multiple textviews. The xml code for the same is:
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alignmentMode="alignMargins"
android:background="#android:color/white"
android:columnCount="1"
android:columnOrderPreserved="false"
android:rowCount="1">
<LinearLayout
android:id="#+id/row1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/white"
android:weightSum="5">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="#android:color/black"
android:textSize="32sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="#android:color/black"
android:textSize="32sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="#android:color/black"
android:textSize="32sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="#android:color/black"
android:textSize="32sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="#android:color/black"
android:textSize="32sp" />
</LinearLayout>
</GridLayout>
As you can see in this, for each of the textviews I have defined the onClick method. The click is handled like:
public void onClick(View v) {
if (v instanceof TextView && isInitiatePhase) {
((TextView) v).setText(String.valueOf(++count));
if (count == 5) {
isInitiatePhase = false;
}
} else if (v instanceof TextView) {
// now we have to start cutting/setting the color to red
((TextView) v).setTextColor(Color.RED);
}
}
The initiation is handled fine, now after the initiation phase when I am clicking the TextViews I want to know which one is clicked. Is there a way to find something like an index of the TextView in the onClick() method?
Does LinearLayout provide that functionality, if the TextView does not?
Also, is my approach correct here? Or is there another Layout that I should use for the same?
Instead on adding onClick, we can add onClickListener in each child view inside linearlayout of gridlayout. Something like this,
//Add gridlayout id in xml
GridLayout girdLayout = (GridLayout) findViewById(R.id.gridlayout_id);
final LinearLayout ll = gridLayout.findViewById(R.id.row1);
int childCount = ll.getChildCount();
for (final int i= 0; i < childCount; i++){
TextView textView = (TextView) ll.getChildAt(i);
textView.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
// Do your job, i current index of child
}
});
}
I make a project and ı stuck in there.
In my design papers like that
And I did't put the arrow as seem so I can do it just Text side.
Like that
How can I put the Arrows ?
My codes are
String[] maddeler = new String[] { "YÖNETİM KURULU BAŞKANI FİKRİ ÖZTÜRK'ÜN MESAJI", "GENEL MÜDÜR CÜNEYT AĞCA İLE RÖPORTAJ", "21. YILINDA OPET",
"MİSYONUMUZ VİZYONUMUZ DEĞERLERİMİZ"};
ArrayList<String> maddelerListe = new ArrayList<String>();
maddelerListe.addAll( Arrays.asList(maddeler) );
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, maddelerListe);
mainListView.setAdapter( listAdapter );
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position)
{
case 0:
Intent baskanlar =new Intent(getBaseContext(),BaskanlarSplash.class);
startActivity(baskanlar);
break;
case 1:
Intent baskanlar2=new Intent(getBaseContext(),BaskanlarSplash2.class);
startActivity(baskanlar2);
break;
case 2:
Toast.makeText(getBaseContext(),"içerik bekleniyor",Toast.LENGTH_LONG).show();
break;
case 3:
Toast.makeText(getBaseContext(),"içerik bekleniyor",Toast.LENGTH_LONG).show();
break;
}
}
});
**My simplerow.xml **
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#color/homeBac"
>
</TextView>
This is my Listview Normaly
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/mainListView">
</ListView>
If you can help my I'm be so greteful :)
change the row to
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#color/colorAccent"
android:textSize="16sp"
android:layout_weight="1"></TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_dialog_info" />
</LinearLayout>
and then change the adapter declaration to
ArrayAdapter<String> listAdapter =
new ArrayAdapter<String>(this, R.layout.simplerow,R.id.rowTextView, maddelerListe);
Make the base layout of your simplerow a RelativeLayout and set the values kind of like this. You can make the arrow a drawable ImageView if you'd prefer. May have to change settings but this should work.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#color/homeBac" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#color/homeBac"
android:text=">" />
</RelativeLayout>
Change your row layout like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_your_arrow"/>
</LinearLayout>
I am new to Android so I've been trying to tweak around for the past couple days but am stuck at this part. For my app, I'm trying to input a Button "Add More" which inflates a view to help incorporate user input but I'm running into issues. I looked around and I used his source code to help with my issue but I am running into the problem that my inflated xml layout is overlapping my previous set-up layout.
This is the layout that I have setup for my new_class_container file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:background="#drawable/gradient_gray">
<!-- Class Name -->
<TextView
android:id="#+id/register_class_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/write_class_name"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"/>
<EditText
android:id="#+id/register_class_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/register_class_textview"
android:layout_alignBaseline="#+id/register_class_textview"
android:inputType="text"
android:hint="#string/example_name"
android:singleLine="true" >
</EditText>
<!-- Assignment Type and Grade Weight -->
<TextView
android:id="#+id/register_assign_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/register_class_textview"
android:layout_alignLeft="#+id/register_class_textview"
android:text="#string/write_assignment_type"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<EditText
android:id="#+id/register_assign_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/register_assign_textview"
android:layout_alignLeft="#+id/register_assign_textview"
android:gravity="center_horizontal"
android:hint="#string/example_assign"
android:singleLine="true">
</EditText>
<!-- Save Button -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="bottom" >
<Button
android:id="#+id/button_save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/save_class" />
</RelativeLayout>
<Button
android:id="#+id/btnAddNewItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/register_assign_edit"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:gravity="center_horizontal"
android:onClick="onAddNewClicked"
android:paddingLeft="5dp"
android:text="#string/more_assign"
android:textColor="#FFFFFF" />
<EditText
android:id="#+id/register_weight_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/btnAddNewItem"
android:layout_alignLeft="#+id/register_weight_textview"
android:ems="10"
android:hint="#string/example_weight"
android:inputType="number"
android:singleLine="true" >
</EditText>
<TextView
android:id="#+id/register_weight_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/register_assign_textview"
android:layout_alignBottom="#+id/register_assign_textview"
android:layout_alignParentRight="true"
android:text="#string/write_assign_percent"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
This is how the layout for the code above looks like and I want to be able to press the "Add More" Button and it inflates to "create a new row", per say, of the TextViews of Assignment Type and Grade Weight and EditTexts of the respective TextViews. But when I run the app and press add more, the new set of TextView + EditText pop up right on top of what I have rather than underneath. The following is my other xml file new_class
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/register_assign_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="#string/write_assignment_type"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="#+id/register_weight_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="#string/write_assign_percent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="30dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/register_assign_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="#string/example_assign"
android:singleLine="true">
</EditText>
<EditText
android:id="#+id/register_weight_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/example_weight"
android:layout_marginLeft="30dp"
android:gravity="center_horizontal"
android:inputType="number"
android:singleLine="true">
</EditText>
</LinearLayout>
</LinearLayout>
And this is the Java Code that I have
public class NewClass extends Activity{
private RelativeLayout mContainerView;
private Button mAddButton;
private View mExclusiveEmptyView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_class_container);
mContainerView = (RelativeLayout) findViewById(R.id.parentView);
mAddButton = (Button) findViewById(R.id.btnAddNewItem);
}
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
//TODO: Handle Screen Rotation
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// TODO: Handle screen rotation:
// restore the saved items and inflate each one with inflateEditRow;
}
// onClick handler for the "Add new" button;
public void onAddNewClicked(View v) {
// Inflate a new row and hide the button self.
inflateEditRow(null);
v.setVisibility(View.GONE);
}
// onClick handler for the "X" button of each row
public void onDeleteClicked(View v) {
// remove the row by calling the getParent on button
mContainerView.removeView((View) v.getParent());
}
private void inflateEditRow(String name) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.new_class, null);
//final ImageButton deleteButton = (ImageButton) rowView
// .findViewById(R.id.buttonDelete);
final EditText editText = (EditText) rowView
.findViewById(R.id.register_assign_edit);
if (name != null && !name.isEmpty()) {
editText.setText(name);
} else {
mExclusiveEmptyView = rowView;
//deleteButton.setVisibility(View.INVISIBLE);
}
// A TextWatcher to control the visibility of the "Add new" button and
// handle the exclusive empty view.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if (s.toString().isEmpty()) {
mAddButton.setVisibility(View.GONE);
//deleteButton.setVisibility(View.INVISIBLE);
if (mExclusiveEmptyView != null
&& mExclusiveEmptyView != rowView) {
mContainerView.removeView(mExclusiveEmptyView);
}
mExclusiveEmptyView = rowView;
} else {
if (mExclusiveEmptyView == rowView) {
mExclusiveEmptyView = null;
}
mAddButton.setVisibility(View.VISIBLE);
// deleteButton.setVisibility(View.VISIBLE);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
// Inflate at the end of all rows but before the "Add new" button
mContainerView.addView(rowView, mContainerView.getChildCount() - 1);
}
}
What can I do to prevent it from overlapping and showing up underneath? And how will I be able to move the button each time it's 'updated'? Because if I move the button to the new_class xml file then it won't show up originally for me to add more views.
I tried implementing the following code and what it did was enabled me to properly get a dynamic view added the first time but each time after it was overlapping the added view.
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.register_assign_edit);
rowView.setLayoutParams(p);
This will work with having two layout files: main_layout and layout_to_include.
main_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="#+id/sv1" >
<LinearLayout
android:id="#+id/llAddingContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<include layout="#layout/layout_to_include"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/llButtonHolder"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="#+id/sv1"
android:gravity="center_horizontal"
android:layout_above="#+id/button2" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Add More" />
</LinearLayout>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textAlignment="center"
android:text="Save Class" />
</RelativeLayout>
layout_to_include.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class Name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Ex: CS 301" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_below="#+id/linearLayout1" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Assignment Type"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ex: Homework" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginRight="10dp"
android:layout_weight="1" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grade Weight(%)"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ex: 10" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
To use these layouts in your code:
....
setContentView(R.layout.main_layout);
....
llAddingContainer = (LinearLayout) findViewById(R.id.llAddingContainer);
// Clicking on this button will add another view from 'layout_to_include' to the ScrollView
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
View view = getLayoutInflater().inflate(R.layout.layout_to_include, null);
// Find the elements in this view and set them up
EditText et1 = (EditText) view.findViewById(R.id.editText1)
....
....
....
// Add the view to the LinearLayout inside ScrollView
llAddingContainer.addView(view);
});
I'm not sure, but you can try follow code to move any view. I recommend to use RelativeLayout like your main layout.
RelativeLayout.LayoutParams bParams1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
bParams1.leftMargin = (int) leftMrgn;
bParams1.topMargin = (int) topMgrn;
bParams1.height = (int) bHeight;
bParams1.width = (int) bWidth;
anyView.setLayoutParams(bParams1);
RelativeLayouts act like a FrameLayout if you don't give them any LayoutParams (constraints like below, above, center, etc.).
You can either construct the appropriate RelativeLayout.LayoutParams, or better yet, create an empty vertical LinearLayout in your first layout that will contain the rows. Then, just add the rows to the LinearLayout instead of the RelativeLayout.
How can I handle in my activity layout long text from which I don´t know how long it is?
I have this text in a textview and when it is too long, it overlays my button.
Are you talking about a Textview or Button? If its a Textview ,You can use a marque textview like below.It will scroll automatically so that all the text will be shown
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="marquee"
android:lines="1"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:text="Your Looooooooooooooooooooooooooooooooooooooooong text!"
android:textColor="#000"
android:textSize="15dp" />
and in your code,
textview.setSelected(true);
textview.setEllipsize(TruncateAt.MARQUEE);
textview.setHorizontallyScrolling(true);
textview.setSingleLine(true);
textview.setLines(1);
If you want an expandable Textview, do something like this.It have a button to expand/collapse textview.
In 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"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="sdfsdfsdfsfasfdsfgdfgdfsgsdfgsdfgsdfgsdgsdgdsgdsgsdfgdsgdsfgsdfgsdgsdfgsdfgdsfgsdgdsgfsdgfsdgsdfgdsgdsfgdsfgdsfgsdghjghjghjghjghjfg"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout1"
android:text="Button" />
</RelativeLayout>
and in code,
boolean checkflag = true;// declare it as public
final RelativeLayout rl=(RelativeLayout)findViewById(R.id.relativeLayout1);
Button b=(Button)findViewById(R.id.button1);
final TextView text=(TextView)findViewById(R.id.textView1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(checkflag==true)
{
text.setSingleLine(true);
checkflag=false;
}
else
{
text.setSingleLine(false);
checkflag=true;
}
}
});
This is my main.xml:`
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:addStatesFromChildren="false"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/header"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_width="fill_parent"
android:layout_alignParentTop="true">
<TableRow>
<TextView
android:id="#+id/leftHeader"
android:textColor="#android:color/black"
android:layout_margin="1dp"
android:gravity="center_horizontal"
android:layout_weight="1"
android:background="#ffcccccc"
android:text="textview1"
android:layout_width="30dip" />
<TextView
android:id="#+id/rightHeader"
android:textColor="#android:color/black"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_margin="1dp"
android:background="#android:color/white"
android:text="textview2"
android:layout_width="70dip" />
</TableRow>
</TableLayout>
<ScrollView
android:id="#+id/table_scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:layout_below="#+id/header"
android:layout_above="#+id/buttonTable">
<TableLayout
android:id="#+id/maintable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<TableRow
android:id="#+id/maintableRow">
<TextView
android:id="#+id/maintableLeft"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginRight="1dp"
android:layout_marginLeft="1dp"
android:text="textview1"
android:layout_width="30dip" />
<TextView
android:id="#+id/maintableRight"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:text="textview2"
android:layout_width="70dip" />
</TableRow>
</TableLayout>
</ScrollView>
<TableLayout
android:id="#+id/buttonTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_alignParentBottom="true">
<TableRow>
<Button
android:id="#+id/button_ResetData"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Reset Data"
android:layout_gravity="center"
android:layout_weight="1" />
<Button
android:id="#+id/button_Refresh"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Refresh"
android:gravity="center"
android:layout_weight="1" />
</TableRow>
</TableLayout>
`
After I press one of the buttons its text alignment goes to the left, how can I make it stay in center?
something similar happens with the first table (header) (textview1, textview2) but here it also reduses the textsize the amount of the text in the view...
EDIT: I managed to find the code that causes the change but I don't know how to fix this.
Maybe some one can explain me why does this happen and how to fix it?
private void fillTableView(List<BatteryInfo> list) {
TextView leftHeader = (TextView) findViewById(R.id.leftHeader);
TextView rightHeader = (TextView) findViewById(R.id.rightHeader);
LayoutParams leftHeaderLayoutParams = leftHeader.getLayoutParams();
LayoutParams rightHeaderLayoutParams = rightHeader.getLayoutParams();
TableLayout contentTable = (TableLayout) findViewById(R.id.maintable);
contentTable.removeAllViews();
for (int i = list.size() - 1; i >= 0; i--) {
TextView tv1 = new TextView(this);
tv1.setTextColor(Color.BLACK);
tv1.setGravity(Gravity.CENTER);
tv1.setBackgroundColor(Color.LTGRAY);
tv1.setLayoutParams(leftHeaderLayoutParams);
tv1.setText(String.valueOf(list.get(i).getLevel()));
TextView tv2 = new TextView(this);
tv2.setTextColor(Color.BLACK);
tv2.setGravity(Gravity.CENTER);
tv2.setBackgroundColor(Color.WHITE);
tv2.setLayoutParams(rightHeaderLayoutParams);
tv2.setText(list.get(i).getDurationTimeInString());
TableRow tblRow = new TableRow(this);
tblRow.addView(tv1);
tblRow.addView(tv2);
contentTable.addView(tblRow);
}
}
After I use removeAllViews or any addView function the layout of the top most table changes as I explained before. Why does this happen?
You can fix this by changing the android:layout_width="30dip" and android:layout_width="70dip" in leftHeader and rightHeader to android:layout_width="fill_parent". I have to admit that I'm not entirely certain why that is, though.
Instead of clearing and re-filling a TableLayout every time, though, you might want to consider using a ListAdapter. It's much more suited to what you're doing (and will probably perform better, too).
UPDATE NUMBER 2:
Notice the change in the main.xml and the change in the onCreate method. This will make your header static while letting your listview scroll, and changing from TableLayout to LinearLayout for your buttons will fix the alignment issue.
Here's how to do this with a custom adapter instead of TableLayout (which isn't really meant for dynamic data like this):
Change you main.xml to this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:addStatesFromChildren="false" android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:id="#+id/header">
<TextView android:id="#+id/leftHeader" android:textColor="#android:color/black"
android:layout_margin="1dp" android:gravity="center_horizontal"
android:layout_weight="1" android:background="#ffcccccc" android:text="textview1"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="#+id/rightHeader" android:textColor="#android:color/black"
android:gravity="center_horizontal" android:layout_weight="1"
android:layout_margin="1dp" android:background="#android:color/white"
android:text="textview2" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:id="#+id/buttonTable" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_alignParentBottom="true" >
<Button android:id="#+id/button_ResetData"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="Reset Data" android:layout_gravity="center"
android:layout_weight="1" />
<Button android:id="#+id/button_Refresh"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="Refresh" android:gravity="center"
android:layout_weight="1" />
</LinearLayout>
<ListView android:id="#+id/listView" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"
android:fadingEdge="none"
android:layout_below="#id/header" android:layout_above="#id/buttonTable">
</ListView>
Add XML layout files for your rows (row.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="#+id/maintableLeft"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_marginRight="1dp" android:layout_marginLeft="1dp"
android:gravity="center_horizontal" android:background="#ffcccccc"
android:textColor="#android:color/black" android:layout_width="fill_parent" />
<TextView android:id="#+id/maintableRight"
android:layout_height="wrap_content" android:background="#ffcccccc"
android:textColor="#android:color/black" android:layout_weight="1"
android:layout_marginLeft="1dp" android:layout_marginRight="1dp"
android:gravity="center_horizontal" android:layout_width="fill_parent" />
</LinearLayout>
Now everything is in place to create a custom class from BaseAdapter to display your information (and make your layout work better):
class CustomAdapter extends BaseAdapter {
private List<BatteryInfo> info;
private LayoutInflater inflater;
public CustomAdapter(Context context, List<BatteryInfo> x) {
this.info = x;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return info.size();
}
public Object getItem(int position) {
return info.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
v = inflater.inflate(R.layout.row, parent, false);
BatteryInfo b = (BatteryInfo) getItem(position);
((TextView) v.findViewById(R.id.maintableLeft)).setText(b
.getDurationTimeInString());
((TextView) v.findViewById(R.id.maintableRight)).setText(b
.getLevel());
return v;
}
}
Modify your onCreate method to inflate the header, add it, and then bind your data to your ListView using the new CustomAdapter (I've created a fake BatteryInfo class for this example, you'll need to modify things slightly to get this working):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView lv = (ListView) this.findViewById(R.id.listView);
final Context context = this;
Button refresh = (Button) this.findViewById(R.id.button_Refresh);
refresh.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
List<BatteryInfo> x = new ArrayList<BatteryInfo>();
x.add(new BatteryInfo());
x.add(new BatteryInfo());
x.add(new BatteryInfo());
x.add(new BatteryInfo());
lv.setAdapter(new CustomAdapter(context, x));
}
});
}