I want to display and run a button below a list which is loaded using the tenantlistadapter class. My problem is that i dont know where to put the object.
my class is as follows
public class TenantListAdapter extends BaseAdapter {
private TenantList tenantList;
private Context _context;
private int selectedTenantPosition;
static int selectedid;
Button boutton_facebook;
Intent browserIntent;
public TenantListAdapter(Context context, TenantList array) {
this.tenantList = array;
this._context = context;
}
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
..
}
}
the view method display list view data only but i want the button to appear below the list. So putting the button inside the view is unsuitable because it displays only list data. is there another method to call the button outside the class ?
This is my xml file. i want the facebook button to appear below the list view
<?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:background="#ffffff"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3" >
<ImageView
android:id="#+id/nav_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY" />
<Button android:id="#+id/back_button"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:background="#android:color/transparent"/>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.7"
android:background="#drawable/home_background" >
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="355dp"
android:layout_margin="10dp"
android:cacheColorHint="#00000000" />
<Button
android:id="#+id/boutton_facebook"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:drawableLeft="#drawable/f_logo"
android:gravity="center_vertical|center_horizontal"
android:text="#string/event"
android:textSize="#dimen/facebook_button_text_size"
android:textStyle="bold"
android:typeface="serif" />
</LinearLayout>
</LinearLayout>
Just do these changes..Use android:layout_below="#+id/android:list in Button Widget
android:id="#+id/boutton_facebook"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:drawableLeft="#drawable/f_logo"
android:gravity="center_vertical|center_horizontal"
android:text="#string/event"
android:textSize="#dimen/facebook_button_text_size"
android:textStyle="bold"
android:typeface="serif"
android:layout_below="#+id/android:list" />
Related
I have a question regarding android layouts. I cannot achieve what I am looking for and I need some direction.
I am looking for a layout that will be inside a Scroll View layout. its hard to explain but a picture is worth thousand words.enter image description here
I want this inside a scroll view, can any help please?
If you do not want to add images dynamically than this is the layout you want Try this.
activity_main.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:id="#+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linMain"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/lin1"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="10dp">
<ImageView
android:id="#+id/imgView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#android:color/holo_purple" />
<ImageView
android:id="#+id/imgView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView1"
android:layout_marginTop="20dp"
android:background="#android:color/holo_red_light" />
<ImageView
android:id="#+id/imgView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView2"
android:layout_marginTop="20dp"
android:background="#android:color/holo_green_light" />
<ImageView
android:id="#+id/imgView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView3"
android:layout_marginTop="20dp"
android:background="#android:color/holo_blue_light" />
</RelativeLayout>
<LinearLayout
android:id="#+id/lin2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/linImages"></WebView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Here i am attaching screenshot of this layout UI.
Try this,I have made two layouts ..
1) activity_main that contains gridview and webview.
2)activity_gridview which contains imageview which will inflate in gridview.
I have added images dynamically to Gridview using BaseAdapter.
In MainActivity i have set that Adapter to GridView. Try this code once.
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fillViewport="false"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linImages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="100dp"
android:layout_height="match_parent"
android:numColumns="1"></GridView>
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/linImages"></WebView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
activity_gridview.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imgView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star2" />
<ImageView
android:id="#+id/imgView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star1" />
<ImageView
android:id="#+id/imgView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star3" />
<ImageView
android:id="#+id/imgView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star1" />
</LinearLayout>
CustomAdapter.java :
public class CustomAdapter extends BaseAdapter {
Context context;
int flags[];
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] flags) {
this.context = applicationContext;
this.flags = flags;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return flags.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.activity_gridview, null);
ImageView imgView1 = (ImageView) view.findViewById(R.id.imgView1);
ImageView imgView2 = (ImageView) view.findViewById(R.id.imgView2);
ImageView imgView3 = (ImageView) view.findViewById(R.id.imgView3);
ImageView imgView4 = (ImageView) view.findViewById(R.id.imgView4);
imgView1.setImageResource(flags[0]);
imgView2.setImageResource(flags[1]);
imgView3.setImageResource(flags[2]);
imgView4.setImageResource(flags[3]);
return view;
}
}
MainActivity.java :
public class MainActivity extends AppCompatActivity {
int images[] = {R.drawable.star2, R.drawable.star1, R.drawable.star3, R.drawable.star1};
GridView grid;
CustomAdapter customAdapter;
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView= (WebView) findViewById(R.id.webView);
grid = (GridView) findViewById(R.id.grid);
customAdapter = new CustomAdapter(getApplicationContext(), images);
grid.setAdapter(customAdapter);
}
}
i am a beginner in android programming.( i am using android studio for coding)
I am trying to design a dashboard for my android application taking guidance from the following link
and its working well, but i want to make it as per
Like This Image
i need 2 column layout with icon image, Title 1 and title 2 with a Background image.
can anyone help me.
thank you
you have to use customGridView using BaseAdapter. In customGridView show the ImageView with TextView for each list item.
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridViewCustom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</RelativeLayout>
grid_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
CustomGridViewMainActivity.java
public class CustomGridViewMainActivity extends Activity
{
GridView gridView;
GridViewCustomAdapter grisViewCustomeAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView=(GridView)findViewById(R.id.gridViewCustom);
// Create the Custom Adapter Object
grisViewCustomeAdapter = new GridViewCustomAdapter(this);
// Set the Adapter to GridView
gridView.setAdapter(grisViewCustomeAdapter);
// Handling touch/click Event on GridView Item
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
String selectedItem;
if(position%2==0)
selectedItem="Facebook";
else
selectedItem="Twitter";
Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();
}
});
}
}
then set the adapter with your customized view
GridViewCustomAdapter.java
public class GridViewCustomAdapter extends ArrayAdapter
{
Context context;
public GridViewCustomAdapter(Context context)
{
super(context, 0);
this.context=context;
}
public int getCount()
{
return 24;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
TextView textViewTitle = (TextView) row.findViewById(R.id.textView);
ImageView imageViewIte = (ImageView) row.findViewById(R.id.imageView);
if(position%2==0)
{
textViewTitle.setText("Facebook");
imageViewIte.setImageResource(R.drawable.facebook);
}
else
{
textViewTitle.setText("Twitter");
imageViewIte.setImageResource(R.drawable.twitter);
}
}
return row;
}
}
Output:
At last, I found the solution...
your XML File`
<?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="vertical"
android:background="#drawable/bg"
android:padding="16dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:layout_marginBottom="5dp"
android:textColor="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:layout_marginBottom="5dp"
android:textColor="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
`
then AndroidButtonWithIconAndText.java class
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class AndroidButtonWithIconAndText extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.android_button_with_icon_text);
}
}
and finally it may look like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
</LinearLayout>
In my layout I added two edit text by click on the add button and its added(works) and when I click on delete button it also delete the dynamic edit text.My question is how to get string value from it suppose I dynamically add two views and also delete when not needed.
here is my code..
public class PartDetails extends AppCompatActivity {
// Parent view for all rows and the add button.
private LinearLayout mContainerView;
// The "Add new" imageButton
private ImageButton mAddImageButton;
Button submit_part_details;
private View mExclusiveEmptyView;
EditText prequired,pnumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_part_details);
mContainerView = (LinearLayout) findViewById(R.id.parentView);
submit_part_details = (Button) findViewById(R.id.submit_part_details);
mAddImageButton = (ImageButton) findViewById(R.id.add_et_parts);
prequired= (EditText) findViewById(R.id.et_Prequired);
pnumber= (EditText) findViewById(R.id.et_Pnumber);
mAddImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inflateEditRow();
}
});
submit_part_details.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String parts=prequired.getText().toString();
String pnum =pnumber.getText().toString();
}
});
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// TODO: Handle screen rotation:
// encapsulate information in a parcelable object, and save it
// into the state bundle.
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// TODO: Handle screen rotation:
// restore the saved items and inflate each one with inflateEditRow;
}
// Helper for inflating a row
private void inflateEditRow() {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row_add_parts, null);
final EditText parts_required = (EditText) addView
.findViewById(R.id.et_parts);
final EditText parts_number = (EditText) addView
.findViewById(R.id.et_Pnumber);
final ImageButton deleteButton = (ImageButton) addView
.findViewById(R.id.delete_et_parts);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((LinearLayout) addView.getParent()).removeView(addView);
}
});
mContainerView.addView(addView);
}
}
this is parent XML layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#android:color/black"
android:layout_height="match_parent"
android:focusableInTouchMode="true">
<LinearLayout
android:id="#+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:layout_below="#+id/tv"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relative_layout1"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/parts_required"
android:layout_width="110dp"
android:layout_height="40dp"
android:text="Parts Required"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
android:background="#drawable/shape"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/parts_required"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<EditText
android:id="#+id/et_Prequired"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.8"
android:hint="text"
android:singleLine="true"
android:inputType="text"
android:textSize="20sp"
android:paddingLeft="20dp"
android:imeOptions="actionNext"
android:background="#drawable/shape_edit_text"
/>
<ImageButton
android:id="#+id/add_et_parts"
android:layout_width="0dp"
android:layout_height="40dp"
android:background="#null"
android:onClick="onAddNewClicked"
android:src="#android:drawable/ic_input_add"
android:layout_weight="0.2"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relative_layout2"
android:layout_marginTop="5dp"
android:layout_below="#+id/relative_layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/parts_number"
android:layout_width="110dp"
android:layout_height="40dp"
android:text="Parts Number"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
android:background="#drawable/shape"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/parts_number"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<EditText
android:id="#+id/et_Pnumber"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:hint="text"
android:singleLine="true"
android:inputType="text"
android:textSize="20sp"
android:paddingLeft="20dp"
android:imeOptions="actionNext"
android:background="#drawable/shape_edit_text"
/>
<!-- <ImageButton
android:id="#+id/add_et_parts_number"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#null"
android:src="#android:drawable/ic_input_add"
android:layout_weight="0.2"/>-->
</LinearLayout>
</RelativeLayout>
<Button
android:layout_marginTop="50dp"
android:id="#+id/submit_part_details"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#+id/snap"
android:textAllCaps="false"
android:textSize="20sp"
android:textColor="#fff"
android:background="#1c4648"
android:text="Submit All Details" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
this is Add Button XML layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#android:color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/relative_layout1"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/parts_required"
android:layout_width="110dp"
android:layout_height="40dp"
android:text="Parts Required"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
android:background="#drawable/shape"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/parts_required"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<EditText
android:id="#+id/et_parts"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.8"
android:singleLine="true"
android:inputType="text"
android:textSize="20sp"
android:hint="text"
android:paddingLeft="20dp"
android:imeOptions="actionNext"
android:background="#drawable/shape_edit_text"
/>
<ImageButton
android:id="#+id/delete_et_parts"
android:layout_width="0dp"
android:layout_height="40dp"
android:background="#null"
android:src="#android:drawable/ic_delete"
android:layout_weight="0.2"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relative_layout2"
android:layout_marginTop="5dp"
android:layout_below="#+id/relative_layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/parts_number"
android:layout_width="110dp"
android:layout_height="40dp"
android:text="Parts Number"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
android:background="#drawable/shape"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/parts_number"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<EditText
android:id="#+id/et_Pnumber"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:hint="text"
android:singleLine="true"
android:inputType="text"
android:textSize="20sp"
android:paddingLeft="20dp"
android:imeOptions="actionNext"
android:background="#drawable/shape_edit_text"
/>
<!-- <ImageButton
android:id="#+id/add_et_parts_number"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#null"
android:src="#android:drawable/ic_input_add"
android:layout_weight="0.2"/>-->
</LinearLayout>
</RelativeLayout>
</LinearLayout>
ArrayList<EditText> edtParts_required=new ArrayList<EditText>();
ArrayList<EditText> edtParts_number=new ArrayList<EditText>();
// Helper for inflating a row
private void inflateEditRow() {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row_add_parts, null);
final EditText parts_required = (EditText) addView
.findViewById(R.id.et_parts);
final EditText parts_number = (EditText) addView
.findViewById(R.id.et_Pnumber);
//Add EditText object to Collection
edtParts_required.add(parts_required);
edtParts_number.add(parts_required);
final ImageButton deleteButton = (ImageButton) addView
.findViewById(R.id.delete_et_parts);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((LinearLayout) addView.getParent()).removeView(addView);
}
});
mContainerView.addView(addView);
}
When you delete the inflated row remove the element from collection too
Loop through your Collection and get the text from dynamic EditText
for(int i=0;i<edtParts_required.size();i++){
String enterdText=edtParts_required.get(i).getText().toString();
}
you can do it in many ways . here are two of them:
make an ArrayList and whenever you add new edit text add it to this array too and when you delete remove it from here too. when you want to get their text put it in a for loop
BETTER WAY : you have a linear layout. get its child count and put it in for loop. something like this :
for (int i=0;i<mContainerView.getChildCount();i++){
View mView=mContainerView.getChildAt(i);
EditText myEditText=(EditText) mView.findViewById(R.id.et_parts);
String txt=myEditText.getText().toString();
EditText myEditText2=(EditText) mView.findViewById(R.id.et_Pnumber);
String txt2=myEditText2.getText().toString();
}
First of all, excuse me if it sounds duplicate. I have visited so many threads but couldn't find a suitable answer of my problem. I have tried ScrollingMovementMethod, android:scrollbar, wrap_content in the parent and many other things suggested in those threads but nothing worked for me. Feel free to edit the title as I couldn't find a better one.
Problem Description
I have a list view and each row of the listview has three controls 1. Image View (to Show the contact image) 2. TextView (to show the Contact name) 3. TextView (to show the status message of the contact (if available)).
What I have tried:-
1. XML layout ,
<?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" >
<ImageView
android:id="#+id/icon"
android:layout_width="22dp"
android:layout_height="50dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="15dp"
android:contentDescription="#string/strBuddyImage" >
</ImageView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="56dp" >
<TextView
android:id="#+id/uNameTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/customMsg"
android:minHeight="?android:attr/listPreferredItemHeight"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="#dimen/fontSizeListRowHeader" />
<TextView
android:id="#+id/customMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:maxLines="4"
android:textSize="#dimen/fontSizeListRowText"
android:textStyle="italic" />
</RelativeLayout>
</LinearLayout>
Adapter Which extends ArrayAdapter (which only overrides the getView method)
static class ViewHolder {
public ImageView imageView;
public TextView uNameTxtView;
public TextView custMsgTxtView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// Log.d("BuddyListAdapter", "Inside getView() " + position);
if ( rowView == null ) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.buddy_listview_row_layout, parent, false);
viewHolder = new ViewHolder();
viewHolder.uNameTxtView = (TextView) rowView.findViewById(R.id.uNameTxt);
viewHolder.imageView = (ImageView) rowView.findViewById(R.id.icon);
viewHolder.custMsgTxtView = (TextView) rowView.findViewById(R.id.customMsg);
rowView.setTag(viewHolder);
}
viewHolder = (ViewHolder) rowView.getTag();
synchronized (buddyList) {
buddy = buddyList.get(position);
}
viewHolder.uNameTxtView.setText(buddy.getDisplayName());
viewHolder.custMsgTxtView.setText(buddy.getCustomMessage());
// Change the icon for users who are offline
if ( buddy.getState() == 0 ) {
viewHolder.imageView.setImageResource(R.drawable.offline);
} else {
viewHolder.imageView.setImageResource(R.drawable.online);
}
// rowView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
return rowView;
}
What I get
Desired
I want to show all the rows of list view of same size and if someone has a bigger Status message (Which needs multiple lines) I would like to show the name of the person and first few lines (say 4) of the status message. No matter how big the status message is name should be always visible to the user.. How can I do that ?
Please point if any other mistake you find in the code.
One thing i noticed is you use a lot of hardcoded dimensions,is that really necesary? using wrap_content would be better practice on most layouts,that way you let android handle it.
As far as your question is concerned you can do two things:
1)Your two EditTexts are inside a relative layout,and if customMsg is 4 lines long,it will get scrolled ,and you'll loose your uNameTxt so you should limit customMsg more,to something like two lines,or less. OR if you are ok with loosing some parts of the code you could adapt your layout this way
<TextView
android:id="#+id/uNameTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout_alignParentTop="true"
android:minHeight="?android:attr/listPreferredItemHeight"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="#dimen/fontSizeListRowHeader" />
<TextView
android:id="#+id/customMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/uNameTxt"
android:maxLines="4"
android:textSize="#dimen/fontSizeListRowText"
android:textStyle="italic" />
it should scroll down no more and you should see Name and a part of customMsg
2)Edit your layout to something more efficient like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="56dp">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="15dp"
android:contentDescription="sasd" >
</ImageView>
<TextView
android:id="#+id/uNameTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:singleLine="true"
android:layout_toRightOf="#+id/icon"
android:textSize="#dimen/fontSizeListRowHeader"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="#+id/customMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/uNameTxt"
android:maxLines="2"
android:layout_toRightOf="#+id/icon"
android:textSize="#dimen/fontSizeListRowText"
android:textStyle="italic" />
</RelativeLayout>
I agree with sokie on the use of wrap_content. I tend to use LinearLayout a lot since it provides a consistent layout on various display sizes. One way of implementation is as follows. By replacing your inner RelativeLayout to LinearLayout, your text view containing the name will always be displayed and the message text view will display the maximum possibles lines (or can be limited by specifying the maxLines) with the available remaining/space. If you would like to display the whole status message, then you can achieve this by changing the parent LinearLayout height parameter to wrap_content.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="56dp"
android:minHeight="?android:attr/listPreferredItemHeight">
<ImageView
android:id="#+id/icon"
android:layout_width="22dp"
android:layout_height="50dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="15dp" >
</ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/uNameTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="#+id/customMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="4"
android:textStyle="italic"
/>
</LinearLayout>
</LinearLayout>
Okay, so I decided to take the Google classes for learning droid devving. I decided instead of the simple input box and button I would make multiple ones, and have each display a set color. The issue is it is jamming all input boxes on one line, rather than their own separate lines. This is what I have in the activity_color.xml
<?xml version="1.0" encoding="utf-8"?>
<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="horizontal" >
<EditText android:id="#+id/edit_message_blue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_blue" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_blue"
android:onClick="sendMessageBlue" />
<EditText android:id="#+id/edit_message_orange"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_orange" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_orange"
android:onClick="sendMessageOrange" />
</LinearLayout>
This is the main java class
package com.example.color.texts;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class ColorTexts extends Activity {
public final static String EXTRA_MESSAGE = "com.example.colortexts.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_texts);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_color_texts, menu);
return true;
}
/** Called when the user clicks the Send button for Blue */
public void sendMessageBlue(View view) {
Intent intent = new Intent (this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message_blue);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
/** Called when the user clicks the Send button for Red */
public void sendMessageRed(View view) {
Intent intent = new Intent (this, DisplayMessageActivityRed.class);
EditText editText = (EditText) findViewById(R.id.edit_message_red);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
/** Called when the user clicks the Send button for Orange */
public void sendMessageOrange(View view) {
Intent intent = new Intent (this, DisplayMessageActivityOrange.class);
EditText editText = (EditText) findViewById(R.id.edit_message_orange);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Now I guess the question is how to either tell it to start a new line, like android:layout_next="1" or something (I KNOW THAT DOESN'T EXIST), or would I have to add to the public class? Such as make another layout file and reference to that? I highly doubt the latter would work.
EDIT: Per the instructions laid out, I think this is how I was supposed to do it, but it only know shows the first line :(
<?xml version="1.0" encoding="utf-8"?>
<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" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText android:id="#+id/edit_message_blue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_blue" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_blue"
android:onClick="sendMessageBlue" />
</LinearLayout>
<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="horizontal" >
<EditText android:id="#+id/edit_message_red"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_red" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_red"
android:onClick="sendMessageRed" />
</LinearLayout>
<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="horizontal" >
<EditText android:id="#+id/edit_message_orange"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_orange" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_orange"
android:onClick="sendMessageOrange" />
</LinearLayout>
</LinearLayout>
Your parent layout is set to be android:orientation="horizontal". This needs to be set to android:orientation="vertical".
EDIT: Here is the reference in the Android docs:
http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:orientation