There is long string. I am dividing long string into words and setting TextView for each word.
(You can ask why I need this function? When user clicks on textview(word), application shows the meaning of the word)
...
TextView tv = new TextView(this);
tv.setTextSize(24);
tv.setText(word);
ll.addView(tv);
...
My LinearLayout:
<LinearLayout
android:id="#+id/llReader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical" >
</LinearLayout>
If I put LinearLayout's orientation vertical, it is putting each TextView in new line.
For example:
word1
word2
word3
If I put it in horizontal orientation, it is putting all words only in one line:
word 1, word 2, word 3
In result word4,word5, word6 is not visible.
How to add TextViews programmatically in this way?
You should add Horizontal LinearLayout in your Vertical LinearLayout:
<LinearLayout
android:id="#+id/verticalLinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/horizontalLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
In your activity,
private LinearLayout mReaderLayout;
protected void onCreate(Bundle savedInstanceState) {
mReaderLayout = (LinearLayout) findViewById(R.layout.llReader);
}
private void addText(String text) {
TextView textView = new TextView(this);
textView.setText(text);
mReaderLayout.addView(textView);
}
You can use gridview with adapter.
<GridView
android:id="#+id/gridview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="50dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>
and in onCreate:
String[] strings= "Long string".split(" ");
gridView = (GridView) findViewById(R.id.gridview1);
// Create adapter to set value for grid view
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, strings);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) v).getText() , Toast.LENGTH_SHORT).show();
}
});
You can add use a RelativeLayout right away or you can add a two buttons in a linear, vertical Layout and then add that into a horizontal layout. Then add the next vertical linear layout with the other two buttons into the horizontal layout.
Related
I need to add TextViews in Horizontal ScrollView and on scroll I have to check which textview is in the middle of the center if three textView are showing on the screen? Note: I have added horizontal scrollView and TextViews.Here is my code.
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/catagorybar"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
</HorizontalScrollView>
Suggest you use Gallery - A view that shows items in a center-locked, horizontally scrolling list. and set your yourGallery:
yourGallery.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
//get focused/center view position here
});
Here is one sample available, but in the gallery adapter, in getView, you should inflate your custom view instead of just ImageView.
You can get if the TextView partially/totally visible or not using the way described here:
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (text.getLocalVisibleRect(scrollBounds)) {
// Any portion of the TextView, even a single pixel, is within the visible window
} else {
// NONE of the TextView is within the visible window
}
So, you have to get the HorizontalScrollView children and loop through it, then see what are the visible children and from that you can get the centered child:
List<Integer> centered_textViews = new ArrayList<Integer>();
TextView text;
HorizontalScrollView scrollView = findViewById(R.id.your_hsv);
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
for (int i = 0; i < scrollView.getChildCount(); i++) {
text = (TextView) scrollView.getChildAt(i);
if (text.getLocalVisibleRect(scrollBounds))
centered_textViews.add(i);
}
int centered_textview_index = (int) (centered_textViews.size() / 2);
TextView centered_text = (TextView) (scrollView
.getChildAt(centered_textViews.get(centered_textview_index)));
I am trying to fire a event when a user click the delete icon in the list , But I could not able to do it.I am using a simple cursor adapter with setviewbinder to display items in the list.In the setviewBinder method I set the textview layout clicklistener to be null that's why, view is not clicked when a user click in the textview.When a user is swipe in the list immediately delete icon is appeared.I need to fire a click event in the deleteIcon.Currently, when a user is clicked the delete icon, the whole view is clickable.I need to achieve when a user is clicked the delete icon, need to fired the event, only in the delete icon.
public boolean setViewValue(final View view, final Cursor data, int columnIndex) {
LinearLayout layoutTaskView=(LinearLayout)view.findViewById(R.id.layoutTaskView),
layoutTaskDelete = (LinearLayout) view.findViewById(R.id.LayoutDeleteTask);
boolean crossed = Boolean.valueOf(data.getString(columnIndex));
if(crossed){
icon.setImageState(new int[] { android.R.attr.state_checked }, true);
text1.setPaintFlags(text1.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
text2.setPaintFlags(text2.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
layoutTaskView.setOnClickListener(null);
}
}
Layout Xml File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/layoutTaskView"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:padding="15dp" >
<TextView
android:id="#+id/taskTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/taskName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:paddingLeft="10dp"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/LayoutDeleteTask"
android:clickable="false">
<ImageView
android:id="#android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:padding="15dp"
android:src="#drawable/checkmark" />
</LinearLayout>
</LinearLayout>
ListView xml
<com.myexample.CrossView
android:id="#+id/crossview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</com.myexample.CrossView>
Adapter Code
String[] from = new String []
{
DatabaseHelper.COLUMN_CATEGORY,DatabaseHelper.COLUMN_TASKNAME,
DatabaseHelper.COLUMN_CROSSED};
int[] to = new int[] {R.id.taskTime,R.id.taskName, android.R.id.content};
adapter = new SimpleCursorAdapter(context, R.layout.layout_list_item, data, from, to, 0);
adapter.setViewBinder(new CrossBinder());
lv.setAdapter(adapter);
public void onItemClick(AdapterView<?> parent, View v, final int position, final long id) {
ImageView icon = (ImageView)v.findViewById(android.R.id.icon);
icon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(v.getId() == android.R.id.icon){
}
}
}
You need to use android:descendantFocusability = "afterDescendants" parameter to root layout of list item. In your case, the root LinearLayout.
For more information check this link.
EDIT : remove android:clickable="false" from your LinearLayout containing ImageView. Put android:clickable="true" in ImageView. Moreover, you can use ImageButton instead ImageView.
P.S. I have suggested to use android:descendantFocusability in main parent LinearLayout.
Create custom adapter like that answer Custom adapter for a list of items that have multiple child items?
This answer give tutorial link to create custom dapters for listview.
I created an app where clicking a button I create a TextView inside a LinearLayout(hosted inside a ScrollView).
When I click the button the TextViews are displayed starting from the top like this example:
First Image
http://imgur.com/8fhtYZs (Sorry I don't have reputation to display it in the post so I inserted the link)
But my task is to create the textviews at the bottom of my LinearLayout and make the TextViews already created scroll up the new one. (Sorry for my bad english) I show an example to be more comprehensible.
Second Image
http://imgur.com/RLHooaH
This is my code:
public class Messaggi2 extends ActionBarActivity implements OnClickListener {
LinearLayout mLayout;
ScrollView scroll;
Button invia;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messaggi2);
mLayout = (LinearLayout) findViewById(R.id.LinearScrollLayout);
scroll = (ScrollView) findViewById(R.id.scrollView2);
invia = (Button) findViewById(R.id.Invia);
invia.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
mLayout.addView(createNewTextView("Message"));
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
textView.setBackgroundResource(R.drawable.balloon_broadcast_incoming_pressed);
return textView;
}
}
And this is my Layout.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">
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="fill_parent"
android:layout_height="640dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<LinearLayout
android:id="#+id/LinearScrollLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/Messaggio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/balloon_broadcast_incoming_pressed"
android:text="#string/messaggi" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="70dp"
android:background="#drawable/linear_layout_bg" >
<EditText
android:id="#+id/Scrivi"
android:layout_width="440dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="#string/scriviMessaggio" />
<Button
android:id="#+id/Invia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/send_btn" />
</LinearLayout>
</LinearLayout>
There is no such thing as LinearLayout.BOTTOM, nor can we add rule for LinearLayout.
But the good news is to insert into LinearLayout, one could decide the sequence.. Just use the addView function with 3 arguments as below
mLayout.addView(view, index, param);
The index determined the order.
You need to set your linear layout's height to the height of your ScrollView so instead of using wrap_content cause that is wrapping your linearlayout change it to match_parent
example:
<LinearLayout
android:id="#+id/LinearScrollLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
When you want to add the button at the bottom of the linearlayout just add a rule to your layout params.
example:
private TextView createNewTextView(String text) {
LinearLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) txt1.getLayoutParams();
layoutParams.addRule(LinearLayout.BOTTOM, 1);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
textView.setBackgroundResource(R.drawable.balloon_broadcast_incoming_pressed);
return textView;
}
I can't manage to add a textview when picking an element of an AutoCompleteTextView. Can you find the problem in this code?
actv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = new TextView(view.getContext());
tv.setText("something");
Log.d("Test","Test");//Works!!
layout=(LinearLayout) findViewById(R.id.container);
layout.addView(tv);
}
});
Here is my activity layout:
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/global">
<LinearLayout
android:orientation="vertical"
android:layout_width="382dp"
android:layout_height="518dp"
android:id="#+id/container"
android:focusableInTouchMode="false">
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView_aff_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cli_id"
android:hint="Choisissez un prospect"
android:ems="10"
android:text="">
</AutoCompleteTextView>
</LinearLayout>
You're not setting any layout params to your TextView, so basically you're not telling it how it should be displayed. Try something like this:
final TextView tv = new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
...
layout.addView(tv);
I want to make ListView in android like this
this is possible in iPhone ,in iPhone they used to call it Circle view
It also possible using xml.jst u need to inflate rowLayout then add "Padding" to its view.
The below code execute properly
Please try the below code..
public class MainActivity extends ListActivity {
/** Called when the activity is first created. */
ListView lv;
int [] arr={20,40,60,80,80,60,40,20};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lv=getListView();
CustomAdapter adapter=new CustomAdapter(this, arr);
lv.setAdapter(adapter);
}
write below getview method in your customAdapter. change the array as per your requirement
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(context);
LinearLayout llInside = new LinearLayout(context);
ll.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
llInside.setLayoutParams(new ListView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(context);
tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv.setText("this is textbview");
tv.setTextColor(Color.RED);
tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
llInside.addView(tv);
//the padding is set here dynamically
int rem = position % 8;
Log.d("" + rem, "" + rem);
llInside.setPadding(img[rem], 0, 0,0 );
ll.addView(llInside);
return ll;
}
Output
If it doesn't need to be too dynamic you could use a scroll view, which contains a LinearLayout, and have textViews inside it with different padding on the left which would stagger them like this, but it would take a while to do and wouldn't be very dynamic in terms of length. I can't think of any other way to do this in android though.
Something like this, and after a few start decrementing the padding to get the zig zag
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="40dp"
android:text="TextView" />
</LinearLayout>
</ScrollView>