my question is like putting a ScrollView HorizontalScrollView and a table containing TextView Array.
This is my main.java:
public class tabla extends Activity {
SitesList sitesList = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabla);
TableLayout layout =(TableLayout)findViewById(R.id.tabla);
TableRow tr[] = new TableRow[80];
TextView variacion[];
TextView nemotecnico[];
TextView precio[];
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL("http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=75");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;
/** Assign textview array lenght by arraylist size */
nemotecnico = new TextView[sitesList.getNemotecnico().size()];
variacion = new TextView[sitesList.getVariacion().size()];
precio = new TextView[sitesList.getPrecio().size()];
/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getRegistro().size(); i++) {
variacion[i] = new TextView(this);
variacion[i].setText(" " +sitesList.getVariacion().get(i));
nemotecnico[i] = new TextView(this);
nemotecnico[i].setText(" " +sitesList.getNemotecnico().get(i));
precio[i] = new TextView(this);
precio[i].setText(" " + sitesList.getPrecio().get(i));
Pattern pattern = Pattern.compile("^([a-z: ]*)?+(\\+?[0-9]+([,\\.][0-9]*)?)$");
Matcher matcher = pattern.matcher(sitesList.getVariacion().get(i));
if (!matcher.matches())
variacion[i].setTextColor(Color.parseColor("#ff0000"));
else
variacion[i].setTextColor(Color.parseColor("#008000"));
}
for (int i = 0; i < 80; i++) {
tr[i] = new TableRow(this);
tr[i].addView(nemotecnico[i], new TableRow.LayoutParams(1));
tr[i].addView(precio[i], new TableRow.LayoutParams(2));
tr[i].addView(variacion[i], new TableRow.LayoutParams(3));
layout.addView(tr[i], new TableLayout.LayoutParams());
}
/** Set the layout view to display */
setContentView(layout);
}
}
As you see in my code:
TableLayout layout =(TableLayout)findViewById(R.id.tabla);
TableRow tr[] = new TableRow[80];
TextView variacion[];
TextView nemotecnico[];
TextView precio[];
And show the TextView in TableRow
for (int i = 0; i < 80; i++) {
tr[i] = new TableRow(this);
tr[i].addView(nemotecnico[i], new TableRow.LayoutParams(1));
tr[i].addView(precio[i], new TableRow.LayoutParams(2));
tr[i].addView(variacion[i], new TableRow.LayoutParams(3));
layout.addView(tr[i], new TableLayout.LayoutParams());
}
/** Set the layout view to display */
setContentView(layout);
But i can't put HorizontalScrollView and ScrollView because my Xml is as shown below.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/tabla"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/fondo" >
</TableLayout>
Much appreciate your help, thanks!.
I don't understand why you cannot wrap your entire table in both vertical and horizontal scroll views or wrap the individual cells (whichever you prefer). The first option looks like this:
<ScrollView ...>
<HorizontalScrollView ...>
<TableLayout ...>
</HorizontalScrollView>
</ScrollView>
However this only allows the user to scroll horizontally or vertically for each gesture, not diagonally. But this question covers creating a two dimensional ScrollView: Scrollview vertical and horizontal in android.
Related
I am trying to create a 3x4 grid of buttons using the following code:
public class Grid extends AppCompatActivity {
LinearLayout myLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_grid);
myLayout = (LinearLayout) findViewById(R.id.content_grid);
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(this);
LayoutParams rowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
row.setLayoutParams(rowParams);
for (int j = 0; j < 4; j++) {
final Button btn = new Button(this);
LayoutParams btnParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(btnParams);
btn.setText(" " + (j + 1 + (i * 4)));
btn.setId(j + 1 + (i * 4));
row.addView(btn);
}
myLayout.addView(row);
}
setContentView(myLayout);;
}
}
This code works perfectly fine and creates the grid as I want it, however if I change the line
myLayout = (LinearLayout) findViewById(R.id.content_grid);
to
myLayout = new LinearLayout(this);
Then only the first row of the grid is produced, not the 2nd and 3rd. I want to know why this is happening?
Whether the line
setContentView(R.id.content_grid);
is there or not does not seem make a difference in the second case.
In addition my content_grid.xml file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_grid"
</LinearLayout>
By default LinearLayout set orientation horizontal. Try set orientation vertical to myLayout by one LayoutParams.
I am creating various textviews inside a linear layout (lay). I want that when the textview reach out of the screen then that textview automatically jump on the next line instead of out of the screen. I am a beginner in android and not getting how to achieve it.
Here is my code:
for (int i = 0; i < string.size(); i++) {
final TextView txt = new TextView(con);
txt.setText(string.get(i));
lay.addView(txt);
}
Try This
TextView txt = new TextView(context);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
private void populateViews(LinearLayout linearLayout, ArrayList<TextView> views, View extraView)
{
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
float mWidth = displaymetrics.widthPixels;
extraView.measure(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.removeAllViews();
int maxWidth = (int) mWidth - extraView.getMeasuredWidth() - 10;
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params;
LinearLayout newLL = new LinearLayout(mContext);
newLL.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
newLL.setGravity(Gravity.LEFT);
newLL.setOrientation(LinearLayout.HORIZONTAL);
int widthSoFar = 0;
for (int i = 0; i < views.size(); i++)
{
LinearLayout LL = new LinearLayout(mContext);
LL.setOrientation(LinearLayout.HORIZONTAL);
LL.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
LL.setLayoutParams(new ListView.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
views.get(i).measure(0, 0);
params = new LinearLayout.LayoutParams(views.get(i).getMeasuredWidth(), LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 2, 10, 2);
LL.addView(views.get(i), params);
LL.measure(0, 0);
widthSoFar += views.get(i).getMeasuredWidth();
if (widthSoFar >= maxWidth)
{
linearLayout.addView(newLL);
newLL = new LinearLayout(mContext);
newLL.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
newLL.setOrientation(LinearLayout.HORIZONTAL);
newLL.setGravity(Gravity.LEFT);
params = new LinearLayout.LayoutParams(LL.getMeasuredWidth(), LL.getMeasuredHeight());
newLL.addView(LL, params);
widthSoFar = LL.getMeasuredWidth();
}
else
{
newLL.addView(LL);
}
}
linearLayout.addView(newLL);
}
This method should be able to help you. I had made this method long back, so with a few modifications you should be able to use it. The first parameter is a LinearLayout which will be the base vertical linear layout which will act as the primary container for the views. The second parameter are the actual views you have to add. The third parameter in my case was an Image which I was using like a placeholder.
You can add like this also.
for (int i = 0; i < string.size(); i++) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View) inflater.inflate(
R.layout.Layout_Design, null);
TextView txt = (TextView) view.findViewById(R.id.some_text);
txt.setText("for runtime change");
layout.addView(view);
// layout is linear layout in my case.
}
Layout_Design:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="80dp" >
<RelativeLayout
android:layout_width="80dp"
android:layout_height="80dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:paddingTop="5dp" >
<TextView
android:id="#+id/some_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SOme Text" />
</RelativeLayout>
</FrameLayout>
I'm working on an android app. In one of the activities, I have a list of services and I want the user to rate each service. The services are in a TableLayout (each service in its own row). At the end of the row, I have a Rate button. When the user click on it, a Dialog pops up with ratingBar, EditView and two buttons at the bottom. My problem is that when the keyboard pops up on the button, and the user writes few lines in the EditText box, the keyword hides the buttons. Therefore, the user can't press the buttons, the user needs to minimize the keyboard in order to see the buttons.
Now, I saw few questions about the topic, but all of them had xml solutions. In my case, the dialog is all code, I don't use xml layout at this case. (I'm not sure it's the right way, but it works for my needs)
Here is a picture
This is the code of the dialog. This is what happen when clicking on the rate button.
Any ideas?
e.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
boolean same = false;
TableRow row = new TableRow(QAListActivity.this);
RatingBar ratingBar = new RatingBar(QAListActivity.this);;
Dialog rankDialog = new Dialog(QAListActivity.this);
rankDialog.setContentView(R.layout.qa_rate);
rankDialog.setCancelable(true);
rankDialog.setCanceledOnTouchOutside(true);
TableLayout table1 = new TableLayout(QAListActivity.this);
ArrayList<RatingBar> ratingBars = new ArrayList<RatingBar>();
EditText comment = new EditText(QAListActivity.this);
for(int z = 0; z < tour.QAList.size(); z++){
if(tour.QAList.get(z).getServiceType().equalsIgnoreCase(tour.voucherList.get(position).getServiceType())){
rankDialog.setTitle("Rate " + tour.voucherList.get(position).getDescription());
same = true;
row = new TableRow(QAListActivity.this);
TextView text = new TextView(QAListActivity.this);
ratingBar = new RatingBar(QAListActivity.this);
ratingBars.add(ratingBar);
text.setText(tour.QAList.get(z).getQualityDesc());
text.setTextSize(20);
text.setHeight(40);
text.setGravity(Gravity.CENTER_VERTICAL);
row.addView(text);
row.addView(ratingBar);
row.setGravity(Gravity.CENTER);
table1.addView(row,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tour.voucherList.get(position).voucher_rating.add(tour.QAList.get(z));
}
}
if(same){
String type = tour.voucherList.get(position).getServiceType();
if (type.equalsIgnoreCase("CNV")){
String[] qa_bus = {"Bus Make","Bus Year","Number of Seats","Bus Company","Bathroom on Bus?","Driver's Name",
"Driver speaks English","Helpfulness of driver (on/off bus)","Were there Gate1 signs on the bus?"};
TableLayout table_bus = new TableLayout(QAListActivity.this);
for(int count = 0; count < qa_bus.length; count++){
TableRow row_make = new TableRow(QAListActivity.this);
EditText make = new EditText(QAListActivity.this);
make.setHint(qa_bus[count]);
make.setWidth(table.getWidth()/2+50);
row_make.addView(make);
row_make.setGravity(Gravity.CENTER);
table_bus.addView(row_make, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));
}
table1.addView(table_bus);
}
else{
TableLayout table_comment = new TableLayout(QAListActivity.this);
TableRow row_comment = new TableRow(QAListActivity.this);
comment = new EditText(QAListActivity.this);
comment.setHint("Remarks");
comment.setWidth(table.getWidth()/2+50);
row_comment.addView(comment);
row_comment.setGravity(Gravity.CENTER);
table_comment.addView(row_comment, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));
table1.addView(table_comment);
}
TableLayout table_buttons = new TableLayout(QAListActivity.this);
TableRow save_row = new TableRow(QAListActivity.this);
Button save = new Button(QAListActivity.this);
save.setText("Save");
save_row.addView(save);
save_row.setGravity(Gravity.CENTER);
final Dialog rankDialogTemp = rankDialog;
final ArrayList<RatingBar> ratingBarsTemp = ratingBars;
final EditText remarksTemp = comment;
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int xx = 0; xx < ratingBarsTemp.size(); xx++){
Log.d("ratingBar", "" + ratingBarsTemp.get(xx).getRating());
tour.voucherList.get(position).voucher_rating.get(xx).setRating(ratingBarsTemp.get(xx).getRating());
}
tour.voucherList.get(position).setQAcompleted(true);
tour.voucherList.get(position).setRemarks(remarksTemp.getText().toString());
rowTemp.setBackgroundColor(Color.GREEN);
getInfo.saveOfflineData(QAListActivity.this, new Gson().toJson(TourListActivity.TourList), DateFormat.getDateTimeInstance().format(new Date()));
rankDialogTemp.dismiss();
}
});
Button back = new Button(QAListActivity.this);
back.setText("Back");
save_row.addView(back);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
rankDialogTemp.dismiss();
}
});
table_buttons.addView(save_row);
table1.addView(table_buttons);
}
rankDialog.addContentView(table1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//now that the dialog is set up, it's time to show it
rankDialog.show();
}
});
My qa_rate.xml code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/qa_rate"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/rating"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:stretchColumns="*"
android:paddingRight="5dp"
android:orientation="vertical" >
</TableLayout >
</ScrollView>
Maybe play with the windowSoftInputMode
http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
Or maybe put your buttons in a scroll view so the user can scroll down to it.
Updated code snipped as asked:
int MovieNum=children.size();
String[] MovieName=new String[MovieNum];
String[] MovieCover=new String[MovieNum];
RadioGroup rg = new RadioGroup (this);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton rb[]= new RadioButton[children.size()];
//LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
//LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
//layout.addView(rg, p);
for (int i = 0; i < children.size(); i++)
{
Element movieAtt = (Element)doc.getRootElement().getChild("movies").getChildren("movie").get(i);
MovieName[i]=movieAtt.getAttributeValue( "Title" );
MovieCover[i]=movieAtt.getAttributeValue( "cover" );
ShowTime[i]=movieAtt.getAttributeValue( "showtime" );
TweetText+=" I will see "+movieAtt.getAttributeValue("Title");
System.out.println(TweetText);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.rowlayout, R.id.label, MovieName);
//setListAdapter(adapter);
//String item = (String) getListAdapter().getItem(position);
rb[i] = new RadioButton(this);
rb[i].setText(movieAtt.getAttributeValue("Title"));
rg.addView(rb[i]);
//Calling this func to get Images
//LoadImageFromWebOperations(movieAtt.getAttributeValue( "cover" ));
}
}
}
catch (MalformedURLException e1)
{
e1.printStackTrace();
System.out.println("ErrorThrowedIs: "+e1);
Toast.makeText(getBaseContext(), e1.toString(),5);
}
}
public static Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}
catch (Exception e)
{
return null;
}
}
//Updated code ends here
I have to display 3 things in my list View. One is going to be a Radio button corresponding to every row. I can not use Radio group as i am not aware of the list length which is dynamically generated. So i can not set it as Radio group.
I wish to know how can i do the same.
Also what i am doing is, i want to get an image along in the same list for every row which is created from the path i get at runtime.
So during my program, i would get these values :
for (int i = 0; i < mvLen; i++)
{
Element movieAtt = (Element)doc.getRootElement().getChild("movies").getChildren("movie").get(i);
MovieName[i]=movieAtt.getAttributeValue( "Title" );
MovieCover[i]=movieAtt.getAttributeValue( "cover" );
ShowTime[i]=movieAtt.getAttributeValue( "showtime" );
Now clearly this is an string array of Movie
Names in MovieName and array of strings of URL for the cover of that movie. I wish to display all those images in front of the Movie Names. My list adapter and corrosponding XML file are listed as below. Can anyone suggest changes/hints for the same . I have been reading http://www.vogella.de/articles/AndroidListView/article.html as well. But could not figure out much to meet my requirements.
So. Create RadioGroup in your class RadioGroup radioGroup = new RadioGroup(this); and simply add radiobuttons to it when you filling content radioGroup.addView(radioButton);
Summary:
RadioGroup radioGroup = new RadioGroup(this);
//Cycle begin
RadioButton rButton = (RadioButton)findViewById(R.id.my_radiobutton);
// OR
RadioButton radioButton = new RadioButton(this);
radioButton .setText(R.string.radio_group_snack);
radioButton .setId(R.id.snack);
radioGroup.addView(rButton);
radioGroup.addView(radioButton);
//Cycle end
Example of custom adapter and getview method(it's 100% work code):
public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String[] users = getUsers(); //it returns list of users in String array
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.main, parent, false);
TextView label=(TextView)row.findViewById(R.id.label); //TextView decalred in my layout xml file
label.setText(users[position]);
ImageView icon=(ImageView)row.findViewById(R.id.icon); //ImageView decalred in my layout xml file
byte[] bb = getAvatar(users[position]); //some method where I get image for user. It not interesting for us so I cut it from here...
if(bb != null && bb.length != 0){
icon.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length)); //SET IMAGE FOR ROW
icon.setVisibility(View.VISIBLE); //SET VISIBILITY OF IMAGE
}
//RIGHT HERE YOU CAN MANIPULATE WITH YOUR RADIOBUTTONS. FOR EXAMPLE:
RadioButton rButton = (RadioButton)findViewById(R.id.my_radio_button);
radioGroup.addView(rButton); //WHERE radioGroup IS RADIOGROUP YOU INITIALIZED BEFORE;
return row;
}
}
}
And this is my onCreate method:
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
db.open();
String[] users = db.getUsersList();
ListView listView = getListView();
this.setListAdapter(new MyCustomAdapter(this, R.layout.main, users));
db.close();
}
For each row in the list get the radioButton and add it to the RadioGroup.
You might have to create all the radioButton's upfront and store them in a list.Set them in the ContentView depending on the position
I want to scroll my TextView to make visible a specific position in the text. How can I do that? I tried bringPointIntoView (int offset) but without success.
Source code:
public class TextScrollActivity extends Activity {
public void onCreate (final Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
final int position = 500;
final TextView textView = new TextView (this);
final ScrollView scrollView = new ScrollView (this);
scrollView.addView (textView);
Button button = new Button (this);
button.setText ("Scroll to " + position);
LinearLayout layout = new LinearLayout (this);
layout.setOrientation (LinearLayout.VERTICAL);
layout.addView (scrollView,
new LayoutParams (LayoutParams.FILL_PARENT, 200));
layout.addView (button, new LayoutParams (LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
StringBuilder builder = new StringBuilder ();
for (int i = 0; i < 1000; i++)
builder.append (String.format ("[ %05d ] ", i));
textView.setText (builder);
setContentView (layout);
button.setOnClickListener (new OnClickListener () {
public void onClick (View v) {
System.out.println (textView.bringPointIntoView (position * 10));
// scrollView.scrollTo (0, position * 10); // no
}
});
}
}
For those who have the same problem, I finally made my own implementation of bringPointIntoView:
public static void bringPointIntoView (TextView textView,
ScrollView scrollView, int offset)
{
int line = textView.getLayout ().getLineForOffset (offset);
int y = (int) ((line + 0.5) * textView.getLineHeight ());
scrollView.smoothScrollTo (0, y - scrollView.getHeight () / 2);
}
Don't hesitate if you have a better solution.
Does adding a movement method to the text view solve the problem?
textView.setMovementMethod(new ScrollingMovementMethod());
Just FYI for anyone else with the same problem, on a listView with large listItems, the overloaded bringPointIntoView can be passed a ListView instead of a ScrollView and use the ScrollTo method instead of smoothScrollTo.