hello friends i am new android, i need your help.
my problem is i am adding textview array in linear layout Dynamic
way. but when it's reach near device screen width its not wrap
contenting .
like this way :
here is my xml file code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative_read_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill_horizontal" >
<RelativeLayout
android:id="#+id/bar_alphabet"
android:layout_width="match_parent"
android:layout_height="#dimen/single_gridviewHeight"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/bar_alphabet"
android:paddingTop="7dp" >
<LinearLayout
android:id="#+id/linelay_bar_alphabet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relative_read_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/toolbar"
android:layout_below="#+id/bar_alphabet"
android:background="#drawable/background_reading9"
android:gravity="left" >
<LinearLayout
android:id="#+id/linelay_wordIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/toolbar"
android:layout_below="#+id/bar_alphabet"
android:layout_marginLeft="180dp"
android:layout_marginTop="280dp"
android:background="#android:color/darker_gray"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/toolbar"
android:paddingLeft="15dp"
android:paddingRight="15dp" >
<ImageButton
android:id="#+id/imgbttn_help"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#android:color/transparent"
android:onClick="onclick"
android:scaleType="fitXY"
android:src="#drawable/help1" />
</RelativeLayout>
<ImageView
android:id="#+id/img_help"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="onclick"
android:scaleType="fitXY"
android:visibility="gone" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
code for adding textview in linear layout
public static ArrayList<TextView> sentence(String[] arr) {
if (linelay_wordIn.getChildCount() > 0)
linelay_wordIn.removeAllViews();
if (allTextView != null) {
allTextView.remove(txt);
allTextView.clear();
System.out.println("hello remove all textview here");
} else {
System.out.println("hello all textview array is null here");
}
String str1 = "";
for (int i = 0; i < arr.length; i++) {
str1 = str1 + arr[i].toString();
System.out.println(" senctence separte in word " + arr[i]
+ " words" + arr.length);
}
/* listview for getting textview */
System.out.println("sentence " + str1.toString() + "str1 length :: "
+ str1.length());
WindowManager wm = (WindowManager) contextG
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int maxWidth = display.getWidth() - 20;
txt = new TextView[arr.length];
for (int j = 0; j < arr.length; j++) {
txt[j] = new TextView(contextG);
txt[j].setId(j);
// txt[j].setTag(sent_audio1[j]);
txt[j].setBackgroundResource(Color.TRANSPARENT);
txt[j].setTextSize(60);
txt[j].setTypeface(
Typeface.createFromAsset(contextG.getAssets(), "TIMES.TTF"),
Typeface.BOLD);
// if (arr.length >= 5) {
//
// } else {
txt[j].setText(arr[j]);
// }
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
lp.setMargins(2, 2, 2, 2);
txt[j].setLayoutParams(lp);
txt[j].setTextColor(Color.BLACK);
txt[j].setClickable(true);
txt[j].setDuplicateParentStateEnabled(true);
txt[j].setFocusableInTouchMode(true);
txt[j].setFocusable(true);
txt[j].setOnTouchListener(myListener);
// System.out.println("txt[j]" + j + "id " + txt[j].getId());
allTextView.add(txt[j]); /* add textview into arraylist */
// System.out.println("id" + allTextView.get(j).getId() +
// "text "
// + allTextView.get(j).getText().toString());
// System.out.println("x" + allTextView.get(j).getX() + "y "
// + allTextView.get(j).getY());
// if (linelay_wordIn.getChildCount() > 5) {
// txt[j].setText(arr[j] + "\n");
// }
linelay_wordIn.addView(txt[j], lp);
// linelay_wordIn.getChildAt(j).getX();
// System.out.println("y " +
// linelay_wordIn.getChildAt(j).getX());
}
return allTextView;
}
i dnot know how to solve this problem very quick way or easy way.
if anybody can suggest better way that will helpful to me.
Thanks in advance.
Read this post! some others also faced the same problem.Link
but there is one little solution i can suggest you is adding TextView.setMaxLines() property
but this is also much limited, Anyways try this
Related
Thanks in advance and here is my problem. I have a database and i have to show its content whenever the "SHOW DATA" button is clicked (on the same activity (layout)). so when the user clicks again that button i want to remove the views in the layout so that the content of the database is not shown twice. I've have used removeAllView() and removeAllViewsInLayout() but it seems it's not working (for sure i'm not using correctly this method).
Here is the java code :
public void onClickShowData(){
LinearLayout ll = (LinearLayout) findViewById(R.id.cardViewContainer);
ll.removeAllViews();
showAll.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Cursor cursor = myDB.getAllData();
if (cursor.getCount() == 0) {
//Toast.makeText(MainActivity.this, "There is no data", Toast.LENGTH_SHORT).show();
showData("Error", "Nothing found");
return;
}
StringBuffer tmp = new StringBuffer();
LinearLayout ll = (LinearLayout)findViewById(R.id.cardViewContainer);
Context context = getApplicationContext();
LinearLayout ll2 = new LinearLayout(context);
ll2.setOrientation(LinearLayout.VERTICAL);
ll2.setLayoutParams(new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
TextView tv = new TextView(context);
CardView cv = new CardView(context);
cv.setId(R.id.cardView);
while (cursor.moveToNext()){
tmp.append("name : " + cursor.getString(0) + "\n" +
"calory : " + cursor.getString(1) + "\n" +
"protein : " + cursor.getString(2) + "\n" +
"lipid : " + cursor.getString(3) + "\n" +
"clucid: " + cursor.getString(4));
ll.addView(cv);
cv.addView(ll2);
tv.setText(tmp.toString());
ll2.addView(tv);
}
}
}
);
}
and this is the layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_database"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="gafood.mobiledeviceproject.arianchitgar.foodmakerwithga.databaseActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/scrollViewLL">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/Name"
android:hint="Food name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:layout_below="#+id/Name"
android:layout_centerHorizontal="true"
android:id="#+id/Calory"
android:hint="Calory"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:layout_below="#+id/Calory"
android:layout_alignRight="#+id/Calory"
android:layout_alignEnd="#+id/Calory"
android:id="#+id/protein"
android:hint="Protein" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:layout_below="#+id/protein"
android:layout_centerHorizontal="true"
android:id="#+id/lipid"
android:hint="Lipid" />
<Button
android:text="Add food"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lipid"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="22dp"
android:id="#+id/addFood" />
<Button
android:text="Update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addFood"
android:layout_centerHorizontal="true"
android:id="#+id/updateFood" />
<Button
android:text="Delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/updateFood"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/delete" />
<Button
android:text="Food DB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/addFood"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/showAll" />
<Button
android:text="search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/updateFood"
android:layout_alignLeft="#+id/updateFood"
android:layout_alignStart="#+id/updateFood"
android:id="#+id/search" />
<Button
android:text="Advanced"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/delete"
android:layout_alignLeft="#+id/delete"
android:layout_alignStart="#+id/delete"
android:id="#+id/advancedSearch" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardViewContainer"></LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
enter code here
When the app starts
When i click 3 times on show DB
You have to remove
LinearLayout ll = (LinearLayout) findViewById(R.id.cardViewContainer);
This line from inside onClickShowData() method and, add
LinearLayout ll;
To the top of your activity class.
then in onCreate(Bundle savedInstanceState) method add,
ll = (LinearLayout) findViewById(R.id.cardViewContainer);
In your current code, you are adding views to an ll and calling ll.removeAllViews() in a different object.
the final code should be something like,
public class YourActivityName extends Activity {
LinearLayout ll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = (LinearLayout) findViewById(R.id.cardViewContainer);
// .... other code //
}
public void onClickShowData(){
ll.removeAllViews();
showAll.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Cursor cursor = myDB.getAllData();
if (cursor.getCount() == 0) {
//Toast.makeText(MainActivity.this, "There is no data", Toast.LENGTH_SHORT).show();
showData("Error", "Nothing found");
return;
}
StringBuffer tmp = new StringBuffer();
Context context = getApplicationContext();
LinearLayout ll2 = new LinearLayout(context);
ll2.setOrientation(LinearLayout.VERTICAL);
ll2.setLayoutParams(new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
TextView tv = new TextView(context);
CardView cv = new CardView(context);
cv.setId(R.id.cardView);
while (cursor.moveToNext()){
tmp.append("name : " + cursor.getString(0) + "\n" +
"calory : " + cursor.getString(1) + "\n" +
"protein : " + cursor.getString(2) + "\n" +
"lipid : " + cursor.getString(3) + "\n" +
"clucid: " + cursor.getString(4));
ll.addView(cv);
cv.addView(ll2);
tv.setText(tmp.toString());
ll2.addView(tv);
}
}
}
);
}
}
You are setting your onClick() listener with showAll.setOnClickListener but the code that gets executed does not include ll.removeAllViews(). Change your click listener to something like the following to remove views when the button is clicked.
showAll.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) findViewById(R.id.cardViewContainer);
ll.removeAllViews();
...etc.
I have this chat application but because it only has one textview that being appended I don't know how to make it more presentable via designs I learnt the code from the internet and it is the easiest way I can do it can you help me guys?
Here is the code.
private String chat_msg,chat_user_name;
private void append_chat_conversation(DataSnapshot dataSnapshot) {
Iterator i = dataSnapshot.getChildren().iterator();
while (i.hasNext()){
chat_msg = (String) ((DataSnapshot)i.next()).getValue();
chat_user_name = (String) ((DataSnapshot)i.next()).getValue();
chat_conversation.setGravity(Gravity.BOTTOM);
if(chat_user_name.contains(pref.getString("username","").toString())){
chat_conversation.append(chat_user_name + " : " + chat_msg + " \n");
}else {
chat_conversation.append(chat_user_name + " : " + chat_msg + " \n");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/btn_send"
android:src="#drawable/send"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/msg_input"
android:layout_toLeftOf="#+id/btn_send"
android:layout_toStartOf="#+id/btn_send" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="15sp"
android:textColor="#000000"
android:scrollbars = "vertical"
android:id="#+id/textView"
android:layout_above="#+id/msg_input" />
</RelativeLayout>
Thank you sirs!!!
Try to add the TextView dynamically for each message.
To add the TextView dynamically, check this sample code
//Get the parent layout
RealtiveLayout relativeLayout = (RealtiveLayout)findViewById(R.id.parent_layout);
//New TextView
TextView textView = new TextView(this);
//Layout Params
textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
textView.setText("Your text");
//Add to the parent
relativelayout.addView(textView1);
I'm trying to build a table that will let me list results from a search. I want the two columns to be different widths (e.g. 30% and 70%). I have got this working for the column titles:-
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView1"
android:layout_marginTop="10dip"
android:padding="10dp"
android:shrinkColumns="*"
android:stretchColumns="*"
android:textColor="#color/white" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<TextView
android:id="#+id/TextViewTitle1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Date"
android:background="#drawable/border"
android:textColor="#color/bluish" />
<TextView
android:id="#+id/TextViewTitle2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:text="Task"
android:background="#drawable/border"
android:textColor="#color/bluish" />
</TableRow>
</TableLayout>
I then want to add rows to the table in my program where the columns have the same width as the title row, but my code generates rows where the columns are split 50%-50% on width:-
private void BuildTable(int rows, int cols) {
// outer for loop
for (int i = 1; i <= rows; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
for (int j = 1; j <= cols; j++) {
TextView tv1 = new TextView(this);
tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv1.setBackgroundResource(R.drawable.border);
tv1.setPadding(5, 5, 5, 5);
tv1.setText("R " + i + ", C" + j);
row.addView(tv1);
}
table_layout.addView(row);
}
}
I'd appreciate any help on getting the column width to the same as the title.
private void addRow(Data data){
TableRow row = (TableRow)View.inflate(getApplicationContext(), your table row , null);
//find your textviews and set data
(TextView)row.findViewById(R.id.TextViewTitlei1);
}
}
they will be distributed according to your required weight.
Well I've worked out what I needed to do this. Firstly set up a layout file, which I called table_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tr_row"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:weightSum="1" >
<TextView
android:id="#+id/TextViewTitlei1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text=" "
android:background="#drawable/border"
android:textColor="#color/bluish" />
<TextView
android:id="#+id/TextViewTitlei2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:text=" "
android:background="#drawable/border"
android:textColor="#color/bluish" />
</TableRow>
Then this is a simplified version of the code to make column widths 30% and 70%
private void addTableRow(String resIdTitle, String strValue){
int yy=4;
for (int i = 1; i <= yy; i++) {
System.out.println("Step 16");
LayoutInflater inflater = getLayoutInflater();
TableRow tr = (TableRow)inflater.inflate(R.layout.table_row, table_layout, false);
System.out.println("Step 17");
// Add First Column
TextView tvTitle = (TextView)tr.findViewById(R.id.TextViewTitlei1);
tvTitle.setText(resIdTitle+ " "+i);
System.out.println("Step 18");
// Add the 3rd Column
TextView tvValue = (TextView)tr.findViewById(R.id.TextViewTitlei2);
tvValue.setText(strValue+ " "+i);
System.out.println("Step 19");
table_layout.addView(tr);
}
}
That appears to work.
I am working on an Android mapping app that allows a user to type a search parameter into an EditText box, and then the closest destinations matching the query are plotted with pins on the MapView.
Clicking on a pin will give info about the location, but I would like to provide more information to the user up front. I thought a SlidingDrawer could populate and open up to show the user the names of the destinations/the distances away.
Having spent a lot of time on this, having done much reading, and having made little progress, I am now asking the community for assistance.
The problem I'm having is that I can't figure out how to populate the contents of the SlidingDrawer within the main activity. I am able to launch a new activity, but that takes my MapView off the screen. I need to be able to see the map as well as the SlidingDrawer. And I can't figure out how to populate the SlidingDrawer without it being set up as an Activity.
(And I should say also that it doesn't necessarily need to be a SlidingDrawer that contains the list of destinations. I could modify my layout to make a small (max 3 items) list and take away some of the map's real estate if that is easier, but I'm not sure how to do that, either.)
Here is the code that calls my SlidingDrawer:
//call SlidingDrawer
Intent drawerIntent = new Intent(this, SlidingDrawerFinal.class);
drawerIntent.putExtra("lat", lat);
drawerIntent.putExtra("lon", lon);
int numberOfTargetsForList = numberOfLoops;
drawerIntent.putExtra("numberOfTargetsForList", numberOfTargetsForList);
for(int i = 0; i < target.length; i++){
drawerIntent.putExtra("target" + Integer.toString(i), target[i]);
}
this.startActivity(drawerIntent);
This is the code in my SlidingDrawer class (extends Activity) that fills the list inside the drawer:
View inflatedDrawerLayout = getLayoutInflater().inflate(R.layout.drawer_main, null);
int width = getWindow().getAttributes().width;
int height = 300;
LayoutParams params = new LayoutParams(width, height);
getWindow().addContentView(inflatedDrawerLayout, params);
// add some data
ArrayList<MyData> myDataList = new ArrayList<MyData>();
myData = new MyData[numberOfTargets];
for(int i = 0; i < numberOfTargets; i++){
String lineTwo = "";
if(target[i].getRating().equals("Not Yet Rated")){
lineTwo = " " + target[i].getRating() + " " +
Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles";
}
else{
lineTwo = " rating: " + target[i].getRating() + "/5 stars " +
Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles";
}
String lineOne = (Integer.toString(i + 1) + ": " + target[i].getName() + ", " + target[i].getVicinity()) + "\n" + lineTwo;
myData[i] = new MyData(lineOne, i);
myDataList.add(myData[i]);
}
mListView = (ListView) findViewById(R.id.listview_);
mListView.setAdapter(new MyListAdapter(this, R.layout.drawer_row, myDataList));
drawer = (SlidingDrawer) findViewById(R.id.drawer);
handleButton = (Button) findViewById(R.id.handle);
drawer.animateOpen();
drawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
public void onDrawerOpened() {
handleButton.setBackgroundResource(R.drawable.handle_down_white);
}
});
My layout files:
main.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 xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip">
<EditText
android:id="#+id/geocode_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/placeName"
android:inputType="text"
android:lines="1" />
<Button
android:id="#+id/geocode_button"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/goLabel" />
</LinearLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<view android:id="#+id/mv"
class="com.google.android.maps.MapView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:clickable="true"
android:apiKey="my_API_Key"/>
</LinearLayout>
<include layout="#layout/drawer_main"/>
</FrameLayout>
</LinearLayout>
and, finally, drawer_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer"
android:handle="#+id/handle"
android:content="#+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#null"
android:layout_gravity="bottom">
<LinearLayout
android:id="#id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold|italic"
android:background="#android:color/white"
android:textColor="#android:color/black"
android:text="#string/top_search_results" >
</TextView>
<View
android:background="#android:drawable/divider_horizontal_bright"
android:layout_width="fill_parent"
android:layout_height="2dp" >
</View>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/listview_"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:divider="#android:color/transparent"
android:dividerHeight="2dp" >
</ListView>
</LinearLayout>
<Button
android:id="#id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/handle_down_white">
</Button>
</SlidingDrawer>
</FrameLayout>
Sorry that this is so lengthy. Any assistance will be greatly appreciated.
You can use Popup Window.
View popupView = layoutInflater.inflate(R.layout.popup_window, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageButton btnCancel = (ImageButton) popupView
.findViewById(R.id.btnCancel);
popupWindow.showAtLocation(LayoutView, Gravity.BOTTOM
| Gravity.CENTER_HORIZONTAL, (LayoutView.getWidth() - popupView
.getWidth()) / 2, LayoutView.getHeight()
- popupWindow.getHeight() - 10);
popupWindow.setAnimationStyle(Animation.RELATIVE_TO_SELF);
popupWindow.setFocusable(true);
popupWindow.update();
The solution I opted for is programmatically a bit less than ideal, but the layout works well. Since I knew I wouldn't have more than 3 results, I just changed my layout to include (up to) 3 small TextViews under my search bar and (sadly) got rid of the SlidingDrawer altogether. When the results come back, I populate as many TextViews as necessary, each with an onClickListener. It won't scale well, and everything is hard-coded; but it works. I may try to improve upon this solution as I learn more about Android.
new Layout:
<?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 xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip">
<EditText
android:id="#+id/geocode_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/placeName"
android:inputType="text"
android:lines="1" />
<Button
android:id="#+id/geocode_button"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/goLabel" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13dip"
android:textStyle="bold|italic"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:id="#+id/search_results_header" >
</TextView>
<View android:id="#+id/bar_over_one"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DADADA" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11dip"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:id="#+id/line_one" >
</TextView>
<View android:id="#+id/bar_over_two"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DADADA" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11dip"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:id="#+id/line_two" >
</TextView>
<View android:id="#+id/bar_over_three"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DADADA" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11dip"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:id="#+id/line_three" >
</TextView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<view android:id="#+id/mv"
class="com.google.android.maps.MapView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:clickable="true"
android:apiKey="0fmGmyFrFDUrPGrwXR_scZZxVx6CkSdK-sys-Qw"/>
</LinearLayout>
</LinearLayout>
new code:
private void populateList(int numberOfTargetsForList){
TextView header = (TextView) findViewById(R.id.search_results_header);
header.setText("Top Search Results");
int i = 0;
//first line in list
barOverOne.setVisibility(View.VISIBLE);
String ratingAndDistance = "";
if(target[i].getRating().equals("Not Yet Rated")){
ratingAndDistance = " " + target[i].getRating() + " " +
Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles";
}
else{
ratingAndDistance = " rating: " + target[i].getRating() + "/5 stars " +
Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles";
}
String detailsForLineOne = (Integer.toString(i + 1) + ": " + target[i].getName() + ", " + target[i].getVicinity()) + "\n" + ratingAndDistance;
TextView lineOne = (TextView) findViewById(R.id.line_one);
lineOne.setText(detailsForLineOne);
lineOne.setOnClickListener(this);
i++;
...(similar blocks handle cases of i = 1 and i =2)
Finally, in my onClick() method:
case R.id.line_one:{
String locForURL = "origin=" + Double.toString(lat/1000000.0) +
"," + Double.toString(lon/1000000.0) + "&destination=" +
Double.toString(target[0].getLat()/1000000.0) + "," +
Double.toString(target[0].getLon()/1000000.0);
Intent intent = new Intent(this, ParsingXMLDirectionsWithListView.class);
intent.putExtra("dirTitle", target[0].getName()+ " (" +
Double.toString((Math.round(target[0].getDistance()*100.0))/100.0) + " miles)");
intent.putExtra("locForURL", locForURL);
this.startActivity(intent);
break;
}//line_one
...(similar blocks for lines two and three)
Not perfect, but oh well. At least looks pretty good from the user's end, and I was never going to meet my deadline by continuing down the SlidingDrawer route. Maybe when I have some more free time I'll see if I can figure that out....
Hi a have RelativeLayout and want display 6 buttons like a "TableLayout", i calc size buttons and tried display on Activity. Only last button appear.
I trying do Activity with 6 buttons (buttonsperscreen = 6) when call method.
Can you help me?
The code is:
private void doLayoutWithButtons(int buttonsperscreen) {
// if total butons is % 2 == 0
if (buttonsperscreen % 2 == 0) {
// get display dimensions
DisplayMetrics d = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(d);
int w = d.widthPixels;
int h = d.heightPixels;
// calc size of buttons
int widthButton = w / 2; // only two columns of buttons.
int heightButton = h / ((buttonsperscreen) / 2); // sample 100px /
// (6/2) = 3
int posx = 0;
int posy = 0;
for (int i = 1; i <= buttonsperscreen; i++) {
Button newButton = new Button(this);
newButton.setId(100 + i + 1); // ID of zero will not work
newButton.setText("Botão: " + i);
// atribuindo o tamanho do botão.
newButton.setHeight(heightButton);
newButton.setWidth(widthButton);
newButton.setId(1000 + i);
// positioning buttons...
RelativeLayout layout1 = new RelativeLayout(this);
// set layout with size of button
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
widthButton, heightButton);
params.leftMargin = posx;
params.topMargin = posy;
newButton.setLayoutParams(params);
layout1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout1.addView(newButton);
setContentView(layout1);
// to calc positions x,y for each button for next iteration
if (posx + widthButton < w) {
posx = posx + widthButton;
} else {
posx = 0;
posy = posy + heightButton;
}
}
}
}
Thanks
Mateus
If you're trying to display buttons evenly spaced across a screen, you don't need to do this in code. You can specify things like this in a layout.
Something like this will give 6 buttons equal widths displayed horizontally.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_height="wrap_content" android:id="#+id/button1"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
<Button android:layout_height="wrap_content" android:id="#+id/button2"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_height="wrap_content" android:id="#+id/button3"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
<Button android:layout_height="wrap_content" android:id="#+id/button4"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_height="wrap_content" android:id="#+id/button5"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
<Button android:layout_height="wrap_content" android:id="#+id/button6"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
As long as you keep the layout_width to be 0dp, and set the weight to be 1, Android will automatically set the widths to be equal, no matter what number of buttons you have.